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 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()
106 char *relative
, *cur_path
= nullptr, *prev_path
= nullptr, *pprev_path
= nullptr,
110 /* Prepare the relative paths */
111 cur_path
= realpath(".", nullptr);
112 prev_path
= realpath("..", nullptr);
113 pprev_path
= realpath("../..", nullptr);
115 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
116 PRINT_ERR("strdup out of memory");
121 /* allocate memory for the expected results */
122 valid_tests_expected_results
= calloc
<char *>(num_valid_tests
);
123 if (!valid_tests_expected_results
) {
124 PRINT_ERR("out of memory");
128 for (i
= 0; i
< num_valid_tests
; i
++) {
129 valid_tests_expected_results
[i
] = calloc
<char>(PATH_MAX
);
130 if (valid_tests_expected_results
[i
] == nullptr) {
131 PRINT_ERR("malloc expected results");
136 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
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
;
146 snprintf(valid_tests_expected_results
[i
],
150 valid_tests_inputs
[i
].absolute_part
);
162 static int free_valid_results()
166 for (i
= 0; i
< num_valid_tests
; i
++) {
167 free(valid_tests_expected_results
[i
]);
170 free(valid_tests_expected_results
);
175 static int prepare_symlink_tree()
178 char tmppath
[PATH_MAX
] = {};
180 /* Create the temporary directory */
181 if (mkdtemp(tree_origin
) == nullptr) {
182 PRINT_ERR("failed to mkdtemp");
186 /* Create the directories of the test tree */
187 for (i
= 0; i
< num_tree_dirs
; i
++) {
188 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
, tree_dirs
[i
]);
190 if (mkdir(tmppath
, 0755) != 0) {
191 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
196 /* Create the symlinks of the test tree */
197 for (i
= 0; i
< num_tree_symlinks
; i
++) {
198 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
, tree_symlinks
[i
].orig
);
200 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
201 PRINT_ERR("failed to symlink \"%s\" to \"%s\"",
203 tree_symlinks
[i
].dest
);
214 static int free_symlink_tree()
217 char tmppath
[PATH_MAX
];
219 /* Remove the symlinks from the test tree */
220 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
221 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_symlinks
[i
].orig
);
223 if (unlink(tmppath
) != 0) {
224 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
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
]);
233 if (rmdir(tmppath
) != 0) {
234 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
239 /* Remove the temporary directory */
240 if (rmdir(tree_origin
) != 0) {
241 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
251 static void test_utils_expand_path()
254 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
257 /* Test valid cases */
258 for (i
= 0; i
< num_valid_tests
; i
++) {
259 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
261 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
262 ok(result
!= nullptr && strcmp(result
, valid_tests_expected_results
[i
]) == 0,
270 * Get the realpath for the tree_origin since it can itself be a
273 result
= realpath(tree_origin
, real_tree_origin
);
275 fail("realpath failed.");
279 /* Test symlink tree cases */
280 treelen
= strlen(real_tree_origin
) + 1;
281 for (i
= 0; i
< num_symlink_tests
; i
++) {
285 "symlink tree test case: [tmppath/]%s",
286 symlink_tests_inputs
[i
].input
);
288 ret
= snprintf(tmppath
,
292 symlink_tests_inputs
[i
].input
);
293 if (ret
== -1 || ret
>= PATH_MAX
) {
294 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
296 symlink_tests_inputs
[i
].input
);
300 result
= utils_expand_path(tmppath
);
301 ok(result
!= nullptr &&
302 strcmp(result
+ treelen
, symlink_tests_inputs
[i
].expected_result
) == 0,
309 /* Test invalid cases */
310 for (i
= 0; i
< num_invalid_tests
; i
++) {
311 const char *test_input
= invalid_tests_inputs
[i
];
313 sprintf(name
, "invalid test case: %s", test_input
? test_input
: "NULL");
315 result
= utils_expand_path(test_input
);
316 if (result
!= nullptr) {
319 ok(result
== nullptr, "%s", name
);
325 if (prepare_symlink_tree() != 0) {
329 if (prepare_valid_results() != 0) {
333 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
335 diag("utils_expand_path tests");
337 test_utils_expand_path();
339 free_valid_results();
342 return exit_status();
345 free_valid_results();