2 * Copyright (C) 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/utils.hpp>
19 #include <common/path.hpp>
20 #include <common/common.hpp>
23 int lttng_opt_quiet
= 1;
24 int lttng_opt_verbose
= 3;
28 struct valid_test_input
{
30 const char *relative_part
;
31 const char *absolute_part
;
39 struct symlink_test_input
{
41 const char *expected_result
;
44 /* Valid test cases */
45 struct valid_test_input valid_tests_inputs
[] = {
46 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
47 { "/a//b//c/d/e", "", "/a/b/c/d/e" },
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" },
59 { "../../", "../..", "/" },
60 { "../..", "../..", "" },
65 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
66 { "/a/b/c/d/../../../../../e", "", "/e" },
70 char **valid_tests_expected_results
;
71 const int num_valid_tests
=
72 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
74 /* Symlinks test cases */
75 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
77 const char * const tree_dirs
[] = {
83 const int num_tree_dirs
=
84 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
86 struct tree_symlink tree_symlinks
[] = {
92 { "a/b/c/g", "../../../" },
94 const int num_tree_symlinks
=
95 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
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/" },
104 const int num_symlink_tests
=
105 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
107 /* Invalid test cases */
108 char *invalid_tests_inputs
[] = {
111 const int num_invalid_tests
=
112 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
115 #define PRINT_ERR(fmt, args...) \
116 fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args)
118 static int prepare_valid_results(void)
121 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
122 *pprev_path
= NULL
, *empty
= NULL
;
125 /* Prepare the relative paths */
126 cur_path
= realpath(".", NULL
);
127 prev_path
= realpath("..", NULL
);
128 pprev_path
= realpath("../..", NULL
);
130 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
131 PRINT_ERR("strdup out of memory");
136 /* allocate memory for the expected results */
137 valid_tests_expected_results
= calloc
<char *>(num_valid_tests
);
138 if (!valid_tests_expected_results
) {
139 PRINT_ERR("out of memory");
143 for (i
= 0; i
< num_valid_tests
; i
++) {
144 valid_tests_expected_results
[i
] = calloc
<char>(PATH_MAX
);
145 if (valid_tests_expected_results
[i
] == NULL
) {
146 PRINT_ERR("malloc expected results");
151 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
153 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
154 relative
= prev_path
;
155 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
156 relative
= pprev_path
;
161 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
162 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
174 static int free_valid_results(void)
178 for (i
= 0; i
< num_valid_tests
; i
++) {
179 free(valid_tests_expected_results
[i
]);
182 free(valid_tests_expected_results
);
187 static int prepare_symlink_tree(void)
190 char tmppath
[PATH_MAX
] = {};
192 /* Create the temporary directory */
193 if (mkdtemp(tree_origin
) == NULL
) {
194 PRINT_ERR("failed to mkdtemp");
198 /* Create the directories of the test tree */
199 for (i
= 0; i
< num_tree_dirs
; i
++) {
200 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
,
203 if (mkdir(tmppath
, 0755) != 0) {
204 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
209 /* Create the symlinks of the test tree */
210 for (i
= 0; i
< num_tree_symlinks
; i
++) {
211 snprintf(tmppath
, sizeof(tmppath
), "%s/%s",
212 tree_origin
, tree_symlinks
[i
].orig
);
214 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
215 PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath
,
216 tree_symlinks
[i
].dest
);
227 static int free_symlink_tree(void)
230 char tmppath
[PATH_MAX
];
232 /* Remove the symlinks from the test tree */
233 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
234 snprintf(tmppath
, PATH_MAX
, "%s/%s",
235 tree_origin
, tree_symlinks
[i
].orig
);
237 if (unlink(tmppath
) != 0) {
238 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
243 /* Remove the directories from the test tree */
244 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
245 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
247 if (rmdir(tmppath
) != 0) {
248 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
253 /* Remove the temporary directory */
254 if (rmdir(tree_origin
) != 0) {
255 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
265 static void test_utils_expand_path(void)
268 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
271 /* Test valid cases */
272 for (i
= 0; i
< num_valid_tests
; i
++) {
273 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
275 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
277 strcmp(result
, valid_tests_expected_results
[i
]) == 0, "%s", name
);
283 * Get the realpath for the tree_origin since it can itself be a
286 result
= realpath(tree_origin
, real_tree_origin
);
288 fail("realpath failed.");
292 /* Test symlink tree cases */
293 treelen
= strlen(real_tree_origin
) + 1;
294 for (i
= 0; i
< num_symlink_tests
; i
++) {
297 sprintf(name
, "symlink tree test case: [tmppath/]%s",
298 symlink_tests_inputs
[i
].input
);
300 ret
= snprintf(tmppath
, PATH_MAX
, "%s/%s",
302 symlink_tests_inputs
[i
].input
);
303 if (ret
== -1 || ret
>= PATH_MAX
) {
304 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
306 symlink_tests_inputs
[i
].input
);
310 result
= utils_expand_path(tmppath
);
311 ok(result
!= NULL
&& strcmp(result
+ treelen
,
312 symlink_tests_inputs
[i
].expected_result
) == 0, "%s", name
);
317 /* Test invalid cases */
318 for (i
= 0; i
< num_invalid_tests
; i
++) {
319 const char *test_input
= invalid_tests_inputs
[i
];
321 sprintf(name
, "invalid test case: %s", test_input
?
322 test_input
: "NULL");
324 result
= utils_expand_path(test_input
);
325 if (result
!= NULL
) {
328 ok(result
== NULL
, "%s", name
);
334 if (prepare_symlink_tree() != 0) {
338 if (prepare_valid_results() != 0) {
342 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
344 diag("utils_expand_path tests");
346 test_utils_expand_path();
348 free_valid_results();
351 return exit_status();
354 free_valid_results();