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/e" },
58 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
59 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
60 { "./a/b/../c/d/../e", ".", "/a/c/e" },
61 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
62 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
63 { "./a/b/c/d/../../../../e", ".", "/e" },
64 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
67 { "../../", "../..", "/" },
68 { "../..", "../..", "" },
73 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
74 { "/a/b/c/d/../../../../../e", "", "/e" },
78 char **valid_tests_expected_results
;
79 static const int num_valid_tests
=
80 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
82 /* Symlinks test cases */
83 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
85 static const char * const tree_dirs
[] = {
91 static const int num_tree_dirs
=
92 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
94 static struct tree_symlink tree_symlinks
[] = {
100 { "a/b/c/g", "../../../" },
102 static const int num_tree_symlinks
=
103 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
105 static struct symlink_test_input symlink_tests_inputs
[] = {
106 { "a/g/../l/.", "a/b/l" },
107 { "a/g/../l/./", "a/b/l/" },
108 { "a/g/../l/..", "a/b" },
109 { "a/g/../l/../", "a/b/" },
112 static const int num_symlink_tests
=
113 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
115 /* Invalid test cases */
116 static char *invalid_tests_inputs
[] = {
119 static const int num_invalid_tests
=
120 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
123 char errmsg
[ERRSIZE
];
124 static void printerr(char *msg
)
126 fprintf(stderr
, "test_utils_expand_path: error: %s\n", msg
);
129 int prepare_valid_results(void)
132 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
133 *pprev_path
= NULL
, *empty
= NULL
;
136 /* Prepare the relative paths */
137 cur_path
= realpath(".", NULL
);
138 prev_path
= realpath("..", NULL
);
139 pprev_path
= realpath("../..", NULL
);
141 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
142 printerr("strdup out of memory");
147 /* allocate memory for the expected results */
148 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
149 if (!valid_tests_expected_results
) {
150 printerr("out of memory");
154 for (i
= 0; i
< num_valid_tests
; i
++) {
155 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
156 if (valid_tests_expected_results
[i
] == NULL
) {
157 printerr("malloc expected results");
162 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
164 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
165 relative
= prev_path
;
166 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
167 relative
= pprev_path
;
172 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
173 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
185 int free_valid_results(void)
189 for (i
= 0; i
< num_valid_tests
; i
++) {
190 free(valid_tests_expected_results
[i
]);
193 free(valid_tests_expected_results
);
198 int prepare_symlink_tree(void)
201 char tmppath
[PATH_MAX
];
203 /* Create the temporary directory */
204 if (mkdtemp(tree_origin
) == NULL
) {
209 /* Create the directories of the test tree */
210 for (i
= 0; i
< num_tree_dirs
; i
++) {
211 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
213 if (mkdir(tmppath
, 0755) != 0) {
214 snprintf(errmsg
, ERRSIZE
, "mkdir %s", tmppath
);
220 /* Create the symlinks of the test tree */
221 for (i
= 0; i
< num_tree_symlinks
; i
++) {
222 snprintf(tmppath
, PATH_MAX
, "%s/%s",
223 tree_origin
, tree_symlinks
[i
].orig
);
225 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
226 snprintf(errmsg
, ERRSIZE
, "symlink %s to %s",
227 tmppath
, tree_symlinks
[i
].dest
);
239 int free_symlink_tree(void)
242 char tmppath
[PATH_MAX
];
244 /* Remove the symlinks from the test tree */
245 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
246 snprintf(tmppath
, PATH_MAX
, "%s/%s",
247 tree_origin
, tree_symlinks
[i
].orig
);
249 if (unlink(tmppath
) != 0) {
250 snprintf(errmsg
, ERRSIZE
, "unlink %s", tmppath
);
256 /* Remove the directories from the test tree */
257 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
258 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
260 if (rmdir(tmppath
) != 0) {
261 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tmppath
);
267 /* Remove the temporary directory */
268 if (rmdir(tree_origin
) != 0) {
269 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tree_origin
);
280 static void test_utils_expand_path(void)
283 char name
[100], tmppath
[PATH_MAX
];
286 /* Test valid cases */
287 for (i
= 0; i
< num_valid_tests
; i
++) {
288 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
290 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
292 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
297 /* Test symlink tree cases */
298 int treelen
= strlen(tree_origin
) + 1;
299 for (i
= 0; i
< num_symlink_tests
; i
++) {
300 sprintf(name
, "symlink tree test case: [tmppath/]%s",
301 symlink_tests_inputs
[i
].input
);
303 snprintf(tmppath
, PATH_MAX
, "%s/%s",
304 tree_origin
, symlink_tests_inputs
[i
].input
);
305 result
= utils_expand_path(tmppath
);
306 ok(result
!= NULL
&& strcmp(result
+ treelen
,
307 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
312 /* Test invalid cases */
313 for (i
= 0; i
< num_invalid_tests
; i
++) {
314 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
316 result
= utils_expand_path(invalid_tests_inputs
[i
]);
317 if (result
!= NULL
) {
320 ok(result
== NULL
, name
);
324 int main(int argc
, char **argv
)
326 if (prepare_symlink_tree() != 0) {
330 if (prepare_valid_results() != 0) {
334 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
336 diag("utils_expand_path tests");
338 test_utils_expand_path();
340 free_valid_results();
343 return exit_status();
346 free_valid_results();