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