-
Notifications
You must be signed in to change notification settings - Fork 17
/
test-linux.c
174 lines (136 loc) · 4.25 KB
/
test-linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* @file test-linux.c
* @brief cfgpath.h test code for the Linux platform.
*
* Copyright (C) 2013 Adam Nielsen <malvineous@shikadi.net>
*
* This code is placed in the public domain. You are free to use it for any
* purpose. If you add new platform support, please contribute a patch!
*/
#include <string.h>
#include <stdio.h>
#ifndef __linux__
#define __linux__
#endif
#undef WIN32
#define getenv test_getenv
#define mkdir test_mkdir
#include "cfgpath.h"
int test_env_xdg_valid; /* Does $XDG_CONFIG_HOME exist? */
int test_env_home_valid; /* Does $HOME exist? */
char getenv_buffer[256];
char *test_getenv(const char *var)
{
if (test_env_xdg_valid && (strcmp(var, "XDG_CONFIG_HOME") == 0)) {
strcpy(getenv_buffer, "/home/test/.config");
return getenv_buffer;
}
if (test_env_home_valid && (strcmp(var, "HOME") == 0)) {
strcpy(getenv_buffer, "/home/test");
return getenv_buffer;
}
return NULL;
}
int test_mkdir(const char *path, mode_t mode)
{
return 0;
}
#define TOSTRING_X(x) #x
#define TOSTRING(x) TOSTRING_X(x)
#define RUN_TEST(result, msg) \
TEST_FUNC(buffer, sizeof(buffer), "test-linux"); \
CHECK_RESULT(result, msg)
#define CHECK_RESULT(result, msg) \
if (strcmp(buffer, result) != 0) { \
printf("FAIL: %s:%d " TOSTRING(TEST_FUNC) "() returned the wrong value.\n" \
"Expected: %s\nGot: %s\n", __FILE__, __LINE__, result, buffer); \
return 1; \
} else { \
printf("PASS: " TOSTRING(TEST_FUNC) "() " msg "\n"); \
}
int main(int argc, char *argv[])
{
char buffer[256];
/*
* get_user_config_file()
*/
#define TEST_RESULT "/home/test/.config/test-linux.conf"
#define TEST_FUNC get_user_config_file
test_env_xdg_valid = 1;
test_env_home_valid = 1;
TEST_FUNC(buffer, 5, "test-linux");
CHECK_RESULT("", "returns empty string when buffer is too small.");
test_env_xdg_valid = 1;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $HOME and not $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 0;
RUN_TEST("", "returns empty string when $XDG_CONFIG_HOME and $HOME are absent.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_config_folder()
*/
#define TEST_RESULT "/home/test/.config/test-linux/"
#define TEST_FUNC get_user_config_folder
test_env_xdg_valid = 1;
test_env_home_valid = 1;
TEST_FUNC(buffer, 5, "test-linux");
CHECK_RESULT("", "returns empty string when buffer is too small.");
test_env_xdg_valid = 1;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $HOME and not $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 0;
RUN_TEST("", "returns empty string when $XDG_CONFIG_HOME and $HOME are absent.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_data_folder()
*/
#define TEST_RESULT "/home/test/.local/share/test-linux/"
#define TEST_FUNC get_user_data_folder
test_env_xdg_valid = 1;
test_env_home_valid = 1;
TEST_FUNC(buffer, 5, "test-linux");
CHECK_RESULT("", "returns empty string when buffer is too small.");
test_env_xdg_valid = 1;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $HOME and not $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 0;
RUN_TEST("", "returns empty string when $XDG_CONFIG_HOME and $HOME are absent.");
#undef TEST_FUNC
#undef TEST_RESULT
/*
* get_user_cache_folder()
*/
#define TEST_RESULT "/home/test/.cache/test-linux/"
#define TEST_FUNC get_user_cache_folder
test_env_xdg_valid = 1;
test_env_home_valid = 1;
TEST_FUNC(buffer, 5, "test-linux");
CHECK_RESULT("", "returns empty string when buffer is too small.");
test_env_xdg_valid = 1;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 1;
RUN_TEST(TEST_RESULT, "works with $HOME and not $XDG_CONFIG_HOME.");
test_env_xdg_valid = 0;
test_env_home_valid = 0;
RUN_TEST("", "returns empty string when $XDG_CONFIG_HOME and $HOME are absent.");
#undef TEST_FUNC
#undef TEST_RESULT
printf("All tests passed for platform: Linux.\n");
return 0;
}