1 #include <test.h>
2
3 #include <files_names.h>
4 #include <file_lib.h>
5
test_first_file_separator(void)6 static void test_first_file_separator(void)
7 {
8 const char *out;
9
10 const char *in = "/tmp/myfile";
11 out = FirstFileSeparator(in);
12 assert_true(out == in);
13
14 in = "tmp/myfile";
15 out = FirstFileSeparator(in);
16 assert_true(out == in + 3);
17
18 in = "c:/tmp/myfile";
19 out = FirstFileSeparator(in);
20 assert_true(out == in + 2);
21
22 in = "\\\\my\\windows\\share";
23 out = FirstFileSeparator(in);
24 assert_true(out == in + 1);
25
26 }
27
test_get_parent_directory_copy(void)28 static void test_get_parent_directory_copy(void)
29 {
30 char *out;
31
32 #ifndef _WIN32
33
34 /* unix, will fail on windows because of IsFileSep */
35
36 out = GetParentDirectoryCopy("/some/path/here");
37 assert_string_equal(out, "/some/path");
38 free(out);
39
40 out = GetParentDirectoryCopy("/some/path/here/dir/");
41 assert_string_equal(out, "/some/path/here/dir");
42 free(out);
43
44 out = GetParentDirectoryCopy("/some/path/here/dir/.");
45 assert_string_equal(out, "/some/path/here/dir");
46 free(out);
47
48 out = GetParentDirectoryCopy("/some");
49 assert_string_equal(out, "/");
50 free(out);
51
52 #else /* _WIN32 */
53
54 /* windows, will fail on unix because of IsFileSep */
55
56 out = GetParentDirectoryCopy("c:\\some\\path with space\\here and now");
57 assert_string_equal(out, "c:\\some\\path with space");
58 free(out);
59
60 out = GetParentDirectoryCopy("c:\\some");
61 assert_string_equal(out, "c:\\");
62 free(out);
63
64 out = GetParentDirectoryCopy("\\\\some\\path");
65 assert_string_equal(out, "\\\\some");
66 free(out);
67
68 out = GetParentDirectoryCopy("\\\\some");
69 assert_string_equal(out, "\\\\");
70 free(out);
71
72 #endif /* _WIN32 */
73 }
74
test_delete_redundant_slashes(void)75 static void test_delete_redundant_slashes(void)
76 {
77 {
78 char str[] = "///a//b////c/";
79 DeleteRedundantSlashes(str);
80 assert_string_equal(str, "/a/b/c/");
81 }
82
83 {
84 char str[] = "a//b////c/";
85 DeleteRedundantSlashes(str);
86 assert_string_equal(str, "a/b/c/");
87 }
88
89 {
90 char str[] = "/a/b/c/";
91 DeleteRedundantSlashes(str);
92 assert_string_equal(str, "/a/b/c/");
93 }
94
95 {
96 char str[] = "a///b////c///";
97 DeleteRedundantSlashes(str);
98 assert_string_equal(str, "a/b/c/");
99 }
100
101 {
102 char str[] = "a///b////c";
103 DeleteRedundantSlashes(str);
104 assert_string_equal(str, "a/b/c");
105 }
106
107 {
108 char str[] = "a///b/c";
109 DeleteRedundantSlashes(str);
110 assert_string_equal(str, "a/b/c");
111 }
112
113 {
114 char str[] = "alpha///beta/charlie///zeta";
115 DeleteRedundantSlashes(str);
116 assert_string_equal(str, "alpha/beta/charlie/zeta");
117 }
118
119 {
120 char str[] = "////alpha///beta/charlie///zeta///";
121 DeleteRedundantSlashes(str);
122 assert_string_equal(str, "/alpha/beta/charlie/zeta/");
123 }
124
125 {
126 char str[] = "/a";
127 DeleteRedundantSlashes(str);
128 assert_string_equal(str, "/a");
129 }
130
131 {
132 char str[] = "/alpha";
133 DeleteRedundantSlashes(str);
134 assert_string_equal(str, "/alpha");
135 }
136 }
137
test_join_paths(void)138 static void test_join_paths(void)
139 {
140 char joined[PATH_MAX] = { 0 };
141
142 #ifndef _WIN32
143 /* unix, will fail on windows because of different file separator */
144
145 strlcat(joined, "/tmp", PATH_MAX);
146 JoinPaths(joined, PATH_MAX, "test");
147 assert_string_equal(joined, "/tmp/test");
148
149 JoinPaths(joined, PATH_MAX, "/test2");
150 assert_string_equal(joined, "/tmp/test/test2");
151
152 strlcat(joined, "/", PATH_MAX);
153 JoinPaths(joined, PATH_MAX, "test3");
154 assert_string_equal(joined, "/tmp/test/test2/test3");
155
156 strlcat(joined, "/", PATH_MAX);
157 JoinPaths(joined, PATH_MAX, "/test4");
158 assert_string_equal(joined, "/tmp/test/test2/test3/test4");
159
160 memset(joined, 0, PATH_MAX);
161 JoinPaths(joined, PATH_MAX, "test5");
162 assert_string_equal(joined, "test5");
163
164 memset(joined, 0, PATH_MAX);
165 JoinPaths(joined, PATH_MAX, "/test6");
166 assert_string_equal(joined, "/test6");
167
168 memset(joined, 0, PATH_MAX);
169 strlcat(joined, "test6", PATH_MAX);
170 JoinPaths(joined, PATH_MAX, "test7");
171 assert_string_equal(joined, "test6/test7");
172
173 #else /* _WIN32 */
174 /* windows, will fail on unix because of different file separator */
175
176 strlcat(joined, "C:\\tmp", PATH_MAX);
177 JoinPaths(joined, PATH_MAX, "test");
178 assert_string_equal(joined, "C:\\tmp\\test");
179
180 JoinPaths(joined, PATH_MAX, "\\test2");
181 assert_string_equal(joined, "C:\\tmp\\test\\test2");
182
183 strlcat(joined, "\\", PATH_MAX);
184 JoinPaths(joined, PATH_MAX, "test3");
185 assert_string_equal(joined, "C:\\tmp\\test\\test2\\test3");
186
187 strlcat(joined, "\\", PATH_MAX);
188 JoinPaths(joined, PATH_MAX, "\\test4");
189 assert_string_equal(joined, "C:\\tmp\\test\\test2\\test3\\test4");
190
191 memset(joined, 0, PATH_MAX);
192 JoinPaths(joined, PATH_MAX, "test5");
193 assert_string_equal(joined, "test5");
194
195 memset(joined, 0, PATH_MAX);
196 JoinPaths(joined, PATH_MAX, "C:\\test6");
197 assert_string_equal(joined, "C:\\test6");
198
199 memset(joined, 0, PATH_MAX);
200 strlcat(joined, "test6", PATH_MAX);
201 JoinPaths(joined, PATH_MAX, "test7");
202 assert_string_equal(joined, "test6\\test7");
203 #endif
204 }
205
test_get_absolute_path(void)206 static void test_get_absolute_path(void)
207 {
208 char *abs_path = NULL;
209 char expected[PATH_MAX] = { 0 };
210 char orig[PATH_MAX] = { 0 };
211
212 #ifndef _WIN32
213 /* unix, will fail on windows because of different file separator */
214
215 abs_path = GetAbsolutePath("/tmp/test");
216 assert_string_equal(abs_path, "/tmp/test");
217 free(abs_path);
218
219 abs_path = GetAbsolutePath("/tmp/test/../test2");
220 assert_string_equal(abs_path, "/tmp/test2");
221 free(abs_path);
222
223 getcwd(expected, PATH_MAX);
224 abs_path = GetAbsolutePath("test/test2");
225 assert_true(IsAbsoluteFileName(abs_path));
226 strlcat(expected, "/test/test2", PATH_MAX);
227 assert_string_equal(abs_path, expected);
228 free(abs_path);
229 memset(expected, 0, PATH_MAX);
230
231 getcwd(expected, PATH_MAX);
232 abs_path = GetAbsolutePath("./test");
233 assert_true(IsAbsoluteFileName(abs_path));
234 strlcat(expected, "/test", PATH_MAX);
235 assert_string_equal(abs_path, expected);
236 free(abs_path);
237 memset(expected, 0, PATH_MAX);
238
239 getcwd(expected, PATH_MAX);
240 abs_path = GetAbsolutePath("test/../test2");
241 assert_true(IsAbsoluteFileName(abs_path));
242 strlcat(expected, "/test2", PATH_MAX);
243 assert_string_equal(abs_path, expected);
244 free(abs_path);
245 memset(expected, 0, PATH_MAX);
246
247 getcwd(expected, PATH_MAX);
248 strlcat(orig, expected, PATH_MAX);
249 chdir("..");
250 ChopLastNode(expected);
251 abs_path = GetAbsolutePath("test/test2");
252 assert_true(IsAbsoluteFileName(abs_path));
253 strlcat(expected, "/test/test2", PATH_MAX);
254 assert_string_equal(abs_path, expected);
255 free(abs_path);
256 memset(expected, 0, PATH_MAX);
257 chdir(orig);
258 memset(orig, 0, PATH_MAX);
259
260 #else /* _WIN32 */
261 /* windows, will fail on unix because of different file separator */
262
263 abs_path = GetAbsolutePath("C:\\tmp\\test");
264 assert_string_equal(abs_path, "C:\\tmp\\test");
265 free(abs_path);
266
267 abs_path = GetAbsolutePath("C:\\tmp\\test\\..\\test2");
268 assert_string_equal(abs_path, "C:\\tmp\\test2");
269 free(abs_path);
270
271 getcwd(expected, PATH_MAX);
272 abs_path = GetAbsolutePath("test\\test2");
273 assert_true(IsAbsoluteFileName(abs_path));
274 strlcat(expected, "\\test\\test2", PATH_MAX);
275 assert_string_equal(abs_path, expected);
276 free(abs_path);
277 memset(expected, 0, PATH_MAX);
278
279 getcwd(expected, PATH_MAX);
280 abs_path = GetAbsolutePath(".\\test");
281 assert_true(IsAbsoluteFileName(abs_path));
282 strlcat(expected, "\\test", PATH_MAX);
283 assert_string_equal(abs_path, expected);
284 free(abs_path);
285 memset(expected, 0, PATH_MAX);
286
287 getcwd(expected, PATH_MAX);
288 abs_path = GetAbsolutePath("test\\..\\test2");
289 assert_true(IsAbsoluteFileName(abs_path));
290 strlcat(expected, "\\test2", PATH_MAX);
291 assert_string_equal(abs_path, expected);
292 free(abs_path);
293 memset(expected, 0, PATH_MAX);
294
295 getcwd(expected, PATH_MAX);
296 strlcat(orig, expected, PATH_MAX);
297 chdir("..");
298 ChopLastNode(expected);
299 abs_path = GetAbsolutePath("test\\test2");
300 assert_true(IsAbsoluteFileName(abs_path));
301 strlcat(expected, "\\test\\test2", PATH_MAX);
302 assert_string_equal(abs_path, expected);
303 free(abs_path);
304 memset(expected, 0, PATH_MAX);
305 chdir(orig);
306 memset(orig, 0, PATH_MAX);
307
308 #endif
309 }
310
main()311 int main()
312 {
313 PRINT_TEST_BANNER();
314 const UnitTest tests[] =
315 {
316 unit_test(test_first_file_separator),
317 unit_test(test_get_parent_directory_copy),
318 unit_test(test_delete_redundant_slashes),
319 unit_test(test_join_paths),
320 unit_test(test_get_absolute_path)
321 };
322
323 return run_tests(tests);
324 }
325