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/path.h>
21 #include <common/common.h>
24 int lttng_opt_quiet
= 1;
25 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 static 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 static 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 static const char * const tree_dirs
[] = {
83 static const int num_tree_dirs
=
84 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
86 static struct tree_symlink tree_symlinks
[] = {
92 { "a/b/c/g", "../../../" },
94 static 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 static const int num_symlink_tests
=
105 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
107 /* Invalid test cases */
108 static char *invalid_tests_inputs
[] = {
111 static const int num_invalid_tests
=
112 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
114 #define PRINT_ERR(fmt, args...) \
115 fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args)
117 static int prepare_valid_results(void)
120 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
121 *pprev_path
= NULL
, *empty
= NULL
;
124 /* Prepare the relative paths */
125 cur_path
= realpath(".", NULL
);
126 prev_path
= realpath("..", NULL
);
127 pprev_path
= realpath("../..", NULL
);
129 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
130 PRINT_ERR("strdup out of memory");
135 /* allocate memory for the expected results */
136 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
137 if (!valid_tests_expected_results
) {
138 PRINT_ERR("out of memory");
142 for (i
= 0; i
< num_valid_tests
; i
++) {
143 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
144 if (valid_tests_expected_results
[i
] == NULL
) {
145 PRINT_ERR("malloc expected results");
150 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
152 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
153 relative
= prev_path
;
154 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
155 relative
= pprev_path
;
160 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
161 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
173 static int free_valid_results(void)
177 for (i
= 0; i
< num_valid_tests
; i
++) {
178 free(valid_tests_expected_results
[i
]);
181 free(valid_tests_expected_results
);
186 static int prepare_symlink_tree(void)
189 char tmppath
[PATH_MAX
] = {};
191 /* Create the temporary directory */
192 if (mkdtemp(tree_origin
) == NULL
) {
193 PRINT_ERR("failed to mkdtemp");
197 /* Create the directories of the test tree */
198 for (i
= 0; i
< num_tree_dirs
; i
++) {
199 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
,
202 if (mkdir(tmppath
, 0755) != 0) {
203 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
208 /* Create the symlinks of the test tree */
209 for (i
= 0; i
< num_tree_symlinks
; i
++) {
210 snprintf(tmppath
, sizeof(tmppath
), "%s/%s",
211 tree_origin
, tree_symlinks
[i
].orig
);
213 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
214 PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath
,
215 tree_symlinks
[i
].dest
);
226 static int free_symlink_tree(void)
229 char tmppath
[PATH_MAX
];
231 /* Remove the symlinks from the test tree */
232 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
233 snprintf(tmppath
, PATH_MAX
, "%s/%s",
234 tree_origin
, tree_symlinks
[i
].orig
);
236 if (unlink(tmppath
) != 0) {
237 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
242 /* Remove the directories from the test tree */
243 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
244 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
246 if (rmdir(tmppath
) != 0) {
247 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
252 /* Remove the temporary directory */
253 if (rmdir(tree_origin
) != 0) {
254 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
264 static void test_utils_expand_path(void)
267 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
270 /* Test valid cases */
271 for (i
= 0; i
< num_valid_tests
; i
++) {
272 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
274 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
276 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
282 * Get the realpath for the tree_origin since it can itself be a
285 result
= realpath(tree_origin
, real_tree_origin
);
287 fail("realpath failed.");
291 /* Test symlink tree cases */
292 treelen
= strlen(real_tree_origin
) + 1;
293 for (i
= 0; i
< num_symlink_tests
; i
++) {
296 sprintf(name
, "symlink tree test case: [tmppath/]%s",
297 symlink_tests_inputs
[i
].input
);
299 ret
= snprintf(tmppath
, PATH_MAX
, "%s/%s",
301 symlink_tests_inputs
[i
].input
);
302 if (ret
== -1 || ret
>= PATH_MAX
) {
303 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
305 symlink_tests_inputs
[i
].input
);
309 result
= utils_expand_path(tmppath
);
310 ok(result
!= NULL
&& strcmp(result
+ treelen
,
311 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
316 /* Test invalid cases */
317 for (i
= 0; i
< num_invalid_tests
; i
++) {
318 const char *test_input
= invalid_tests_inputs
[i
];
320 sprintf(name
, "invalid test case: %s", test_input
?
321 test_input
: "NULL");
323 result
= utils_expand_path(test_input
);
324 if (result
!= NULL
) {
327 ok(result
== NULL
, name
);
331 int main(int argc
, char **argv
)
333 if (prepare_symlink_tree() != 0) {
337 if (prepare_valid_results() != 0) {
341 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
343 diag("utils_expand_path tests");
345 test_utils_expand_path();
347 free_valid_results();
350 return exit_status();
353 free_valid_results();