2 * Copyright (C) 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
19 #include <common/utils.h>
20 #include <common/common.h>
23 int lttng_opt_quiet
= 1;
24 int lttng_opt_verbose
= 3;
27 struct valid_test_input
{
38 struct symlink_test_input
{
40 char *expected_result
;
43 /* Valid test cases */
44 static struct valid_test_input valid_tests_inputs
[] = {
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/d/e" },
48 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
49 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
50 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
51 { "./a/b/../c/d/../e", ".", "/a/c/e" },
52 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
53 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
54 { "./a/b/c/d/../../../../e", ".", "/e" },
55 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
58 { "../../", "../..", "/" },
59 { "../..", "../..", "" },
64 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
65 { "/a/b/c/d/../../../../../e", "", "/e" },
69 char **valid_tests_expected_results
;
70 static const int num_valid_tests
=
71 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
73 /* Symlinks test cases */
74 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
76 static const char * const tree_dirs
[] = {
82 static const int num_tree_dirs
=
83 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
85 static struct tree_symlink tree_symlinks
[] = {
91 { "a/b/c/g", "../../../" },
93 static const int num_tree_symlinks
=
94 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
96 static struct symlink_test_input symlink_tests_inputs
[] = {
97 { "a/g/../l/.", "a/b/l" },
98 { "a/g/../l/./", "a/b/l/" },
99 { "a/g/../l/..", "a/b" },
100 { "a/g/../l/../", "a/b/" },
103 static const int num_symlink_tests
=
104 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
106 /* Invalid test cases */
107 static char *invalid_tests_inputs
[] = {
110 static const int num_invalid_tests
=
111 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
113 #define PRINT_ERR(fmt, args...) \
114 fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args)
116 static int prepare_valid_results(void)
119 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
120 *pprev_path
= NULL
, *empty
= NULL
;
123 /* Prepare the relative paths */
124 cur_path
= realpath(".", NULL
);
125 prev_path
= realpath("..", NULL
);
126 pprev_path
= realpath("../..", NULL
);
128 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
129 PRINT_ERR("strdup out of memory");
134 /* allocate memory for the expected results */
135 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
136 if (!valid_tests_expected_results
) {
137 PRINT_ERR("out of memory");
141 for (i
= 0; i
< num_valid_tests
; i
++) {
142 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
143 if (valid_tests_expected_results
[i
] == NULL
) {
144 PRINT_ERR("malloc expected results");
149 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
151 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
152 relative
= prev_path
;
153 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
154 relative
= pprev_path
;
159 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
160 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
172 static int free_valid_results(void)
176 for (i
= 0; i
< num_valid_tests
; i
++) {
177 free(valid_tests_expected_results
[i
]);
180 free(valid_tests_expected_results
);
185 static int prepare_symlink_tree(void)
188 char tmppath
[PATH_MAX
] = {};
190 /* Create the temporary directory */
191 if (mkdtemp(tree_origin
) == NULL
) {
192 PRINT_ERR("failed to mkdtemp");
196 /* Create the directories of the test tree */
197 for (i
= 0; i
< num_tree_dirs
; i
++) {
198 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
,
201 if (mkdir(tmppath
, 0755) != 0) {
202 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
207 /* Create the symlinks of the test tree */
208 for (i
= 0; i
< num_tree_symlinks
; i
++) {
209 snprintf(tmppath
, sizeof(tmppath
), "%s/%s",
210 tree_origin
, tree_symlinks
[i
].orig
);
212 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
213 PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath
,
214 tree_symlinks
[i
].dest
);
225 static int free_symlink_tree(void)
228 char tmppath
[PATH_MAX
];
230 /* Remove the symlinks from the test tree */
231 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
232 snprintf(tmppath
, PATH_MAX
, "%s/%s",
233 tree_origin
, tree_symlinks
[i
].orig
);
235 if (unlink(tmppath
) != 0) {
236 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
241 /* Remove the directories from the test tree */
242 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
243 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
245 if (rmdir(tmppath
) != 0) {
246 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
251 /* Remove the temporary directory */
252 if (rmdir(tree_origin
) != 0) {
253 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
263 static void test_utils_expand_path(void)
266 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
269 /* Test valid cases */
270 for (i
= 0; i
< num_valid_tests
; i
++) {
271 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
273 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
275 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
281 * Get the realpath for the tree_origin since it can itself be a
284 result
= realpath(tree_origin
, real_tree_origin
);
286 fail("realpath failed.");
290 /* Test symlink tree cases */
291 treelen
= strlen(real_tree_origin
) + 1;
292 for (i
= 0; i
< num_symlink_tests
; i
++) {
295 sprintf(name
, "symlink tree test case: [tmppath/]%s",
296 symlink_tests_inputs
[i
].input
);
298 ret
= snprintf(tmppath
, PATH_MAX
, "%s/%s",
300 symlink_tests_inputs
[i
].input
);
301 if (ret
== -1 || ret
>= PATH_MAX
) {
302 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
304 symlink_tests_inputs
[i
].input
);
308 result
= utils_expand_path(tmppath
);
309 ok(result
!= NULL
&& strcmp(result
+ treelen
,
310 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
315 /* Test invalid cases */
316 for (i
= 0; i
< num_invalid_tests
; i
++) {
317 const char *test_input
= invalid_tests_inputs
[i
];
319 sprintf(name
, "invalid test case: %s", test_input
?
320 test_input
: "NULL");
322 result
= utils_expand_path(test_input
);
323 if (result
!= NULL
) {
326 ok(result
== NULL
, name
);
330 int main(int argc
, char **argv
)
332 if (prepare_symlink_tree() != 0) {
336 if (prepare_valid_results() != 0) {
340 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
342 diag("utils_expand_path tests");
344 test_utils_expand_path();
346 free_valid_results();
349 return exit_status();
352 free_valid_results();