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