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