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