2 * Copyright (C) - 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
29 #include <common/utils.h>
30 #include <common/common.h>
33 int lttng_opt_quiet
= 1;
34 int lttng_opt_verbose
= 3;
37 struct valid_test_input
{
48 struct symlink_test_input
{
50 char *expected_result
;
53 /* Valid test cases */
54 static struct valid_test_input valid_tests_inputs
[] = {
55 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
56 { "/a//b//c/d/e", "", "/a/b/c/d/e" },
57 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
58 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
59 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
60 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
61 { "./a/b/../c/d/../e", ".", "/a/c/e" },
62 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
63 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
64 { "./a/b/c/d/../../../../e", ".", "/e" },
65 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
68 { "../../", "../..", "/" },
69 { "../..", "../..", "" },
74 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
75 { "/a/b/c/d/../../../../../e", "", "/e" },
79 char **valid_tests_expected_results
;
80 static const int num_valid_tests
=
81 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
83 /* Symlinks test cases */
84 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
86 static const char * const tree_dirs
[] = {
92 static const int num_tree_dirs
=
93 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
95 static struct tree_symlink tree_symlinks
[] = {
101 { "a/b/c/g", "../../../" },
103 static const int num_tree_symlinks
=
104 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
106 static struct symlink_test_input symlink_tests_inputs
[] = {
107 { "a/g/../l/.", "a/b/l" },
108 { "a/g/../l/./", "a/b/l/" },
109 { "a/g/../l/..", "a/b" },
110 { "a/g/../l/../", "a/b/" },
113 static const int num_symlink_tests
=
114 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
116 /* Invalid test cases */
117 static char *invalid_tests_inputs
[] = {
120 static const int num_invalid_tests
=
121 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
123 #define PRINT_ERR(fmt, args...) \
124 fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args)
126 int prepare_valid_results(void)
129 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
130 *pprev_path
= NULL
, *empty
= NULL
;
133 /* Prepare the relative paths */
134 cur_path
= realpath(".", NULL
);
135 prev_path
= realpath("..", NULL
);
136 pprev_path
= realpath("../..", NULL
);
138 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
139 PRINT_ERR("strdup out of memory");
144 /* allocate memory for the expected results */
145 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
146 if (!valid_tests_expected_results
) {
147 PRINT_ERR("out of memory");
151 for (i
= 0; i
< num_valid_tests
; i
++) {
152 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
153 if (valid_tests_expected_results
[i
] == NULL
) {
154 PRINT_ERR("malloc expected results");
159 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
161 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
162 relative
= prev_path
;
163 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
164 relative
= pprev_path
;
169 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
170 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
182 int free_valid_results(void)
186 for (i
= 0; i
< num_valid_tests
; i
++) {
187 free(valid_tests_expected_results
[i
]);
190 free(valid_tests_expected_results
);
195 int prepare_symlink_tree(void)
198 char tmppath
[PATH_MAX
] = {};
200 /* Create the temporary directory */
201 if (mkdtemp(tree_origin
) == NULL
) {
202 PRINT_ERR("failed to mkdtemp");
206 /* Create the directories of the test tree */
207 for (i
= 0; i
< num_tree_dirs
; i
++) {
208 snprintf(tmppath
, sizeof(tmppath
), "%s/%s", tree_origin
,
211 if (mkdir(tmppath
, 0755) != 0) {
212 PRINT_ERR("mkdir failed with path \"%s\"", tmppath
);
217 /* Create the symlinks of the test tree */
218 for (i
= 0; i
< num_tree_symlinks
; i
++) {
219 snprintf(tmppath
, sizeof(tmppath
), "%s/%s",
220 tree_origin
, tree_symlinks
[i
].orig
);
222 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
223 PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath
,
224 tree_symlinks
[i
].dest
);
235 int free_symlink_tree(void)
238 char tmppath
[PATH_MAX
];
240 /* Remove the symlinks from the test tree */
241 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
242 snprintf(tmppath
, PATH_MAX
, "%s/%s",
243 tree_origin
, tree_symlinks
[i
].orig
);
245 if (unlink(tmppath
) != 0) {
246 PRINT_ERR("failed to unlink \"%s\"", tmppath
);
251 /* Remove the directories from the test tree */
252 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
253 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
255 if (rmdir(tmppath
) != 0) {
256 PRINT_ERR("failed to rmdir \"%s\"", tmppath
);
261 /* Remove the temporary directory */
262 if (rmdir(tree_origin
) != 0) {
263 PRINT_ERR("failed to rmdir \"%s\"", tree_origin
);
273 static void test_utils_expand_path(void)
276 char name
[100], tmppath
[PATH_MAX
], real_tree_origin
[PATH_MAX
];
279 /* Test valid cases */
280 for (i
= 0; i
< num_valid_tests
; i
++) {
281 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
283 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
285 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
291 * Get the realpath for the tree_origin since it can itself be a
294 result
= realpath(tree_origin
, real_tree_origin
);
296 fail("realpath failed.");
300 /* Test symlink tree cases */
301 treelen
= strlen(real_tree_origin
) + 1;
302 for (i
= 0; i
< num_symlink_tests
; i
++) {
305 sprintf(name
, "symlink tree test case: [tmppath/]%s",
306 symlink_tests_inputs
[i
].input
);
308 ret
= snprintf(tmppath
, PATH_MAX
, "%s/%s",
310 symlink_tests_inputs
[i
].input
);
311 if (ret
== -1 || ret
>= PATH_MAX
) {
312 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
314 symlink_tests_inputs
[i
].input
);
318 result
= utils_expand_path(tmppath
);
319 ok(result
!= NULL
&& strcmp(result
+ treelen
,
320 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
325 /* Test invalid cases */
326 for (i
= 0; i
< num_invalid_tests
; i
++) {
327 const char *test_input
= invalid_tests_inputs
[i
];
329 sprintf(name
, "invalid test case: %s", test_input
?
330 test_input
: "NULL");
332 result
= utils_expand_path(test_input
);
333 if (result
!= NULL
) {
336 ok(result
== NULL
, name
);
340 int main(int argc
, char **argv
)
342 if (prepare_symlink_tree() != 0) {
346 if (prepare_valid_results() != 0) {
350 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
352 diag("utils_expand_path tests");
354 test_utils_expand_path();
356 free_valid_results();
359 return exit_status();
362 free_valid_results();