2 * Copyright (C) 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/common.hpp>
9 #include <common/path.hpp>
10 #include <common/utils.hpp>
17 #include <sys/types.h>
21 int lttng_opt_quiet
= 1;
22 int lttng_opt_verbose
= 3;
26 struct valid_test_input
{
28 const char *relative_part
;
29 const char *absolute_part
;
37 struct symlink_test_input
{
39 const char *expected_result
;
42 /* Valid test cases */
43 struct valid_test_input valid_tests_inputs
[] = {
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" },
57 { "../../", "../..", "/" },
58 { "../..", "../..", "" },
63 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
64 { "/a/b/c/d/../../../../../e", "", "/e" },
68 char **valid_tests_expected_results
;
69 const int num_valid_tests
= sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
71 /* Symlinks test cases */
72 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
74 const char *const tree_dirs
[] = {
80 const int num_tree_dirs
= sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
82 struct tree_symlink tree_symlinks
[] = {
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", "../../../" },
86 const int num_tree_symlinks
= sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
88 static struct symlink_test_input symlink_tests_inputs
[] = {
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/", "" },
92 const int num_symlink_tests
= sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
94 /* Invalid test cases */
95 char *invalid_tests_inputs
[] = {
98 const int num_invalid_tests
= sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
101 #define PRINT_ERR(fmt, args...) fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ##args)
103 static int prepare_valid_results(void)
106 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
, *pprev_path
= NULL
, *empty
= NULL
;
109 /* Prepare the relative paths */
110 cur_path
= realpath(".", NULL
);
111 prev_path
= realpath("..", NULL
);
112 pprev_path
= realpath("../..", NULL
);
114 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
115 PRINT_ERR("strdup out of memory");
120 /* allocate memory for the expected results */
121 valid_tests_expected_results
= calloc
<char *>(num_valid_tests
);
122 if (!valid_tests_expected_results
) {
123 PRINT_ERR("out of memory");
127 for (i
= 0; i
< num_valid_tests
; i
++) {
128 valid_tests_expected_results
[i
] = calloc
<char>(PATH_MAX
);
129 if (valid_tests_expected_results
[i
] == NULL
) {
130 PRINT_ERR("malloc expected results");
135 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
137 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
138 relative
= prev_path
;
139 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
140 relative
= pprev_path
;
145 snprintf(valid_tests_expected_results
[i
],
149 valid_tests_inputs
[i
].absolute_part
);
161 static int free_valid_results(void)
165 for (i
= 0; i
< num_valid_tests
; i
++) {
166 free(valid_tests_expected_results
[i
]);
169 free(valid_tests_expected_results
);
174 static int prepare_symlink_tree(void)
177 char tmppath
[PATH_MAX
] = {};
179 /* Create the temporary directory */
180 if (mkdtemp(tree_origin
) == NULL
) {
181 PRINT_ERR("failed to mkdtemp");
185 /* Create the directories of the test tree */
186 for (i
= 0; i
< num_tree_dirs
; i
++) {
187 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
, tree_dirs
[i
]);
189 if (mkdir(tmppath
, 0755) != 0) {
190 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
195 /* Create the symlinks of the test tree */
196 for (i
= 0; i
< num_tree_symlinks
; i
++) {
197 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
, tree_symlinks
[i
].orig
);
199 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
200 PRINT_ERR("failed to symlink \"%s\" to \"%s\"",
202 tree_symlinks
[i
].dest
);
213 static int free_symlink_tree(void)
216 char tmppath
[PATH_MAX
];
218 /* Remove the symlinks from the test tree */
219 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
220 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_symlinks
[i
].orig
);
222 if (unlink(tmppath
) != 0) {
223 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
228 /* Remove the directories from the test tree */
229 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
230 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
232 if (rmdir(tmppath
) != 0) {
233 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
238 /* Remove the temporary directory */
239 if (rmdir(tree_origin
) != 0) {
240 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
250 static void test_utils_expand_path(void)
253 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
256 /* Test valid cases */
257 for (i
= 0; i
< num_valid_tests
; i
++) {
258 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
260 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
261 ok(result
!= NULL
&& strcmp(result
, valid_tests_expected_results
[i
]) == 0,
269 * Get the realpath for the tree_origin since it can itself be a
272 result
= realpath(tree_origin
, real_tree_origin
);
274 fail("realpath failed.");
278 /* Test symlink tree cases */
279 treelen
= strlen(real_tree_origin
) + 1;
280 for (i
= 0; i
< num_symlink_tests
; i
++) {
284 "symlink tree test case: [tmppath/]%s",
285 symlink_tests_inputs
[i
].input
);
287 ret
= snprintf(tmppath
,
291 symlink_tests_inputs
[i
].input
);
292 if (ret
== -1 || ret
>= PATH_MAX
) {
293 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
295 symlink_tests_inputs
[i
].input
);
299 result
= utils_expand_path(tmppath
);
301 strcmp(result
+ treelen
, symlink_tests_inputs
[i
].expected_result
) == 0,
308 /* Test invalid cases */
309 for (i
= 0; i
< num_invalid_tests
; i
++) {
310 const char *test_input
= invalid_tests_inputs
[i
];
312 sprintf(name
, "invalid test case: %s", test_input
? test_input
: "NULL");
314 result
= utils_expand_path(test_input
);
315 if (result
!= NULL
) {
318 ok(result
== NULL
, "%s", name
);
324 if (prepare_symlink_tree() != 0) {
328 if (prepare_valid_results() != 0) {
332 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
334 diag("utils_expand_path tests");
336 test_utils_expand_path();
338 free_valid_results();
341 return exit_status();
344 free_valid_results();