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