Commit | Line | Data |
---|---|---|
cc7f9e36 | 1 | /* |
9d16b343 | 2 | * Copyright (C) 2013 Raphaël Beamonte <raphael.beamonte@gmail.com> |
cc7f9e36 | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
cc7f9e36 | 5 | * |
cc7f9e36 RB |
6 | */ |
7 | ||
8 | #include <assert.h> | |
9 | #include <string.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <limits.h> | |
13 | ||
02bf969d RB |
14 | #include <sys/stat.h> |
15 | #include <sys/types.h> | |
16 | ||
cc7f9e36 RB |
17 | #include <tap/tap.h> |
18 | ||
343af227 | 19 | #include <common/utils.h> |
8098bf0a MD |
20 | #include <common/common.h> |
21 | ||
ad7c9c18 | 22 | /* For error.h */ |
cc7f9e36 RB |
23 | int lttng_opt_quiet = 1; |
24 | int lttng_opt_verbose = 3; | |
c7e35b03 | 25 | int lttng_opt_mi; |
cc7f9e36 RB |
26 | |
27 | struct valid_test_input { | |
28 | char *input; | |
29 | char *relative_part; | |
30 | char *absolute_part; | |
31 | }; | |
32 | ||
02bf969d RB |
33 | struct tree_symlink { |
34 | char *orig; | |
35 | char *dest; | |
36 | }; | |
37 | ||
38 | struct symlink_test_input { | |
39 | char *input; | |
40 | char *expected_result; | |
41 | }; | |
42 | ||
cc7f9e36 RB |
43 | /* Valid test cases */ |
44 | static struct valid_test_input valid_tests_inputs[] = { | |
45 | { "/a/b/c/d/e", "", "/a/b/c/d/e" }, | |
4b223a67 | 46 | { "/a//b//c/d/e", "", "/a/b/c/d/e" }, |
cc7f9e36 RB |
47 | { "./a/b/c/d/e", ".", "/a/b/c/d/e" }, |
48 | { "../a/b/c/d/../e", "..", "/a/b/c/e" }, | |
49 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, | |
50 | { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" }, | |
51 | { "./a/b/../c/d/../e", ".", "/a/c/e" }, | |
52 | { "../a/b/../../c/./d/./e", "..", "/c/d/e" }, | |
53 | { "../../a/b/../c/d/../../e", "../..", "/a/e" }, | |
54 | { "./a/b/c/d/../../../../e", ".", "/e" }, | |
55 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, | |
56 | { "a/", ".", "/a/" }, | |
57 | { "a", ".", "/a" }, | |
58 | { "../../", "../..", "/" }, | |
59 | { "../..", "../..", "" }, | |
60 | { "../", "..", "/" }, | |
61 | { "..", "..", "" }, | |
62 | { "./", ".", "/" }, | |
63 | { ".", ".", "" }, | |
ab5ff479 RB |
64 | { "/../a/b/c/d/e", "", "/a/b/c/d/e" }, |
65 | { "/a/b/c/d/../../../../../e", "", "/e" }, | |
9a506343 RB |
66 | { "/..", "", "/" }, |
67 | { "/a/..", "", "/" }, | |
cc7f9e36 RB |
68 | }; |
69 | char **valid_tests_expected_results; | |
70 | static const int num_valid_tests = | |
71 | sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
72 | ||
02bf969d RB |
73 | /* Symlinks test cases */ |
74 | char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX"; | |
75 | ||
76 | static const char * const tree_dirs[] = { | |
77 | "a", | |
78 | "a/b", | |
79 | "a/b/c", | |
80 | "a/e", | |
81 | }; | |
82 | static const int num_tree_dirs = | |
83 | sizeof(tree_dirs) / sizeof(tree_dirs[0]); | |
84 | ||
85 | static struct tree_symlink tree_symlinks[] = { | |
86 | { "a/d", "b/c/" }, | |
87 | { "a/g", "d/" }, | |
88 | { "a/b/f", "../e/" }, | |
89 | { "a/b/h", "../g/" }, | |
90 | { "a/b/k", "c/g/" }, | |
91 | { "a/b/c/g", "../../../" }, | |
92 | }; | |
93 | static const int num_tree_symlinks = | |
94 | sizeof(tree_symlinks) / sizeof(tree_symlinks[0]); | |
95 | ||
96 | static struct symlink_test_input symlink_tests_inputs[] = { | |
97 | { "a/g/../l/.", "a/b/l" }, | |
98 | { "a/g/../l/./", "a/b/l/" }, | |
99 | { "a/g/../l/..", "a/b" }, | |
100 | { "a/g/../l/../", "a/b/" }, | |
101 | { "a/b/h/g/", "" }, | |
102 | }; | |
103 | static const int num_symlink_tests = | |
104 | sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]); | |
105 | ||
cc7f9e36 RB |
106 | /* Invalid test cases */ |
107 | static char *invalid_tests_inputs[] = { | |
108 | NULL, | |
cc7f9e36 RB |
109 | }; |
110 | static const int num_invalid_tests = | |
111 | sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); | |
112 | ||
b35d936e JG |
113 | #define PRINT_ERR(fmt, args...) \ |
114 | fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args) | |
02bf969d | 115 | |
8dfbc0b4 | 116 | static int prepare_valid_results(void) |
cc7f9e36 RB |
117 | { |
118 | int i; | |
8098bf0a MD |
119 | char *relative, *cur_path = NULL, *prev_path = NULL, |
120 | *pprev_path = NULL, *empty = NULL; | |
121 | int ret = 0; | |
cc7f9e36 RB |
122 | |
123 | /* Prepare the relative paths */ | |
124 | cur_path = realpath(".", NULL); | |
125 | prev_path = realpath("..", NULL); | |
126 | pprev_path = realpath("../..", NULL); | |
127 | empty = strdup(""); | |
8098bf0a | 128 | if (!cur_path || !prev_path || !pprev_path || !empty) { |
b35d936e | 129 | PRINT_ERR("strdup out of memory"); |
8098bf0a MD |
130 | ret = -1; |
131 | goto end; | |
132 | } | |
cc7f9e36 RB |
133 | |
134 | /* allocate memory for the expected results */ | |
8098bf0a MD |
135 | valid_tests_expected_results = zmalloc(sizeof(char *) * num_valid_tests); |
136 | if (!valid_tests_expected_results) { | |
b35d936e | 137 | PRINT_ERR("out of memory"); |
8098bf0a MD |
138 | ret = -1; |
139 | goto end; | |
140 | } | |
cc7f9e36 RB |
141 | for (i = 0; i < num_valid_tests; i++) { |
142 | valid_tests_expected_results[i] = malloc(PATH_MAX); | |
143 | if (valid_tests_expected_results[i] == NULL) { | |
b35d936e | 144 | PRINT_ERR("malloc expected results"); |
8098bf0a MD |
145 | ret = -1; |
146 | goto end; | |
cc7f9e36 RB |
147 | } |
148 | ||
149 | if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) { | |
150 | relative = cur_path; | |
151 | } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) { | |
152 | relative = prev_path; | |
153 | } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) { | |
154 | relative = pprev_path; | |
155 | } else { | |
156 | relative = empty; | |
157 | } | |
158 | ||
159 | snprintf(valid_tests_expected_results[i], PATH_MAX, | |
160 | "%s%s", relative, valid_tests_inputs[i].absolute_part); | |
161 | } | |
162 | ||
8098bf0a | 163 | end: |
cc7f9e36 RB |
164 | free(cur_path); |
165 | free(prev_path); | |
166 | free(pprev_path); | |
167 | free(empty); | |
168 | ||
8098bf0a | 169 | return ret; |
cc7f9e36 RB |
170 | } |
171 | ||
8dfbc0b4 | 172 | static int free_valid_results(void) |
cc7f9e36 RB |
173 | { |
174 | int i; | |
175 | ||
176 | for (i = 0; i < num_valid_tests; i++) { | |
177 | free(valid_tests_expected_results[i]); | |
178 | } | |
179 | ||
180 | free(valid_tests_expected_results); | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
8dfbc0b4 | 185 | static int prepare_symlink_tree(void) |
02bf969d RB |
186 | { |
187 | int i; | |
b35d936e | 188 | char tmppath[PATH_MAX] = {}; |
02bf969d RB |
189 | |
190 | /* Create the temporary directory */ | |
191 | if (mkdtemp(tree_origin) == NULL) { | |
b35d936e | 192 | PRINT_ERR("failed to mkdtemp"); |
02bf969d RB |
193 | goto error; |
194 | } | |
195 | ||
196 | /* Create the directories of the test tree */ | |
197 | for (i = 0; i < num_tree_dirs; i++) { | |
b35d936e JG |
198 | snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, |
199 | tree_dirs[i]); | |
02bf969d RB |
200 | |
201 | if (mkdir(tmppath, 0755) != 0) { | |
b35d936e | 202 | PRINT_ERR("mkdir failed with path \"%s\"", tmppath); |
02bf969d RB |
203 | goto error; |
204 | } | |
205 | } | |
206 | ||
207 | /* Create the symlinks of the test tree */ | |
208 | for (i = 0; i < num_tree_symlinks; i++) { | |
b35d936e | 209 | snprintf(tmppath, sizeof(tmppath), "%s/%s", |
02bf969d RB |
210 | tree_origin, tree_symlinks[i].orig); |
211 | ||
212 | if (symlink(tree_symlinks[i].dest, tmppath) != 0) { | |
b35d936e JG |
213 | PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath, |
214 | tree_symlinks[i].dest); | |
02bf969d RB |
215 | goto error; |
216 | } | |
217 | } | |
218 | ||
219 | return 0; | |
220 | ||
221 | error: | |
222 | return 1; | |
223 | } | |
224 | ||
8dfbc0b4 | 225 | static int free_symlink_tree(void) |
02bf969d RB |
226 | { |
227 | int i; | |
228 | char tmppath[PATH_MAX]; | |
229 | ||
230 | /* Remove the symlinks from the test tree */ | |
231 | for (i = num_tree_symlinks - 1; i > -1; i--) { | |
232 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
233 | tree_origin, tree_symlinks[i].orig); | |
234 | ||
235 | if (unlink(tmppath) != 0) { | |
b35d936e | 236 | PRINT_ERR("failed to unlink \"%s\"", tmppath); |
02bf969d RB |
237 | goto error; |
238 | } | |
239 | } | |
240 | ||
241 | /* Remove the directories from the test tree */ | |
242 | for (i = num_tree_dirs - 1; i > -1; i--) { | |
243 | snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]); | |
244 | ||
245 | if (rmdir(tmppath) != 0) { | |
b35d936e | 246 | PRINT_ERR("failed to rmdir \"%s\"", tmppath); |
02bf969d RB |
247 | goto error; |
248 | } | |
249 | } | |
250 | ||
251 | /* Remove the temporary directory */ | |
252 | if (rmdir(tree_origin) != 0) { | |
b35d936e | 253 | PRINT_ERR("failed to rmdir \"%s\"", tree_origin); |
02bf969d RB |
254 | goto error; |
255 | } | |
256 | ||
257 | return 0; | |
258 | ||
259 | error: | |
260 | return 1; | |
261 | } | |
262 | ||
cc7f9e36 RB |
263 | static void test_utils_expand_path(void) |
264 | { | |
265 | char *result; | |
f66e964a JR |
266 | char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX]; |
267 | int i, treelen; | |
cc7f9e36 RB |
268 | |
269 | /* Test valid cases */ | |
270 | for (i = 0; i < num_valid_tests; i++) { | |
cc7f9e36 RB |
271 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
272 | ||
273 | result = utils_expand_path(valid_tests_inputs[i].input); | |
ba526563 RB |
274 | ok(result != NULL && |
275 | strcmp(result, valid_tests_expected_results[i]) == 0, name); | |
cc7f9e36 RB |
276 | |
277 | free(result); | |
278 | } | |
279 | ||
f66e964a JR |
280 | /* |
281 | * Get the realpath for the tree_origin since it can itself be a | |
282 | * symlink. | |
283 | */ | |
284 | result = realpath(tree_origin, real_tree_origin); | |
285 | if (!result) { | |
286 | fail("realpath failed."); | |
287 | return; | |
288 | } | |
289 | ||
02bf969d | 290 | /* Test symlink tree cases */ |
f66e964a | 291 | treelen = strlen(real_tree_origin) + 1; |
02bf969d | 292 | for (i = 0; i < num_symlink_tests; i++) { |
b35d936e JG |
293 | int ret; |
294 | ||
02bf969d RB |
295 | sprintf(name, "symlink tree test case: [tmppath/]%s", |
296 | symlink_tests_inputs[i].input); | |
297 | ||
b35d936e JG |
298 | ret = snprintf(tmppath, PATH_MAX, "%s/%s", |
299 | real_tree_origin, | |
300 | symlink_tests_inputs[i].input); | |
301 | if (ret == -1 || ret >= PATH_MAX) { | |
302 | PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"", | |
303 | real_tree_origin, | |
304 | symlink_tests_inputs[i].input); | |
305 | fail(name); | |
306 | continue; | |
307 | } | |
02bf969d RB |
308 | result = utils_expand_path(tmppath); |
309 | ok(result != NULL && strcmp(result + treelen, | |
310 | symlink_tests_inputs[i].expected_result) == 0, name); | |
311 | ||
312 | free(result); | |
313 | } | |
314 | ||
cc7f9e36 RB |
315 | /* Test invalid cases */ |
316 | for (i = 0; i < num_invalid_tests; i++) { | |
8082d471 | 317 | const char *test_input = invalid_tests_inputs[i]; |
cc7f9e36 | 318 | |
8082d471 JG |
319 | sprintf(name, "invalid test case: %s", test_input ? |
320 | test_input : "NULL"); | |
321 | ||
322 | result = utils_expand_path(test_input); | |
cc7f9e36 RB |
323 | if (result != NULL) { |
324 | free(result); | |
325 | } | |
326 | ok(result == NULL, name); | |
327 | } | |
328 | } | |
329 | ||
330 | int main(int argc, char **argv) | |
331 | { | |
02bf969d RB |
332 | if (prepare_symlink_tree() != 0) { |
333 | goto error_mkdir; | |
334 | } | |
335 | ||
cc7f9e36 | 336 | if (prepare_valid_results() != 0) { |
02bf969d | 337 | goto error_malloc; |
cc7f9e36 RB |
338 | } |
339 | ||
02bf969d | 340 | plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests); |
cc7f9e36 RB |
341 | |
342 | diag("utils_expand_path tests"); | |
343 | ||
344 | test_utils_expand_path(); | |
345 | ||
346 | free_valid_results(); | |
02bf969d RB |
347 | free_symlink_tree(); |
348 | ||
cc7f9e36 | 349 | return exit_status(); |
02bf969d RB |
350 | |
351 | error_malloc: | |
352 | free_valid_results(); | |
353 | ||
354 | error_mkdir: | |
355 | free_symlink_tree(); | |
356 | ||
357 | return 1; | |
cc7f9e36 | 358 | } |