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 <src/common/utils.h>
31 #include <common/common.h>
34 int lttng_opt_quiet
= 1;
35 int lttng_opt_verbose
= 3;
38 struct valid_test_input
{
49 struct symlink_test_input
{
51 char *expected_result
;
54 /* Valid test cases */
55 static struct valid_test_input valid_tests_inputs
[] = {
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]);
124 char errmsg
[ERRSIZE
];
125 static void printerr(char *msg
)
127 fprintf(stderr
, "test_utils_expand_path: error: %s\n", msg
);
130 int prepare_valid_results(void)
133 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
134 *pprev_path
= NULL
, *empty
= NULL
;
137 /* Prepare the relative paths */
138 cur_path
= realpath(".", NULL
);
139 prev_path
= realpath("..", NULL
);
140 pprev_path
= realpath("../..", NULL
);
142 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
143 printerr("strdup out of memory");
148 /* allocate memory for the expected results */
149 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
150 if (!valid_tests_expected_results
) {
151 printerr("out of memory");
155 for (i
= 0; i
< num_valid_tests
; i
++) {
156 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
157 if (valid_tests_expected_results
[i
] == NULL
) {
158 printerr("malloc expected results");
163 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
165 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
166 relative
= prev_path
;
167 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
168 relative
= pprev_path
;
173 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
174 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
186 int free_valid_results(void)
190 for (i
= 0; i
< num_valid_tests
; i
++) {
191 free(valid_tests_expected_results
[i
]);
194 free(valid_tests_expected_results
);
199 int prepare_symlink_tree(void)
202 char tmppath
[PATH_MAX
];
204 /* Create the temporary directory */
205 if (mkdtemp(tree_origin
) == NULL
) {
210 /* Create the directories of the test tree */
211 for (i
= 0; i
< num_tree_dirs
; i
++) {
212 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
214 if (mkdir(tmppath
, 0755) != 0) {
215 snprintf(errmsg
, ERRSIZE
, "mkdir %s", tmppath
);
221 /* Create the symlinks of the test tree */
222 for (i
= 0; i
< num_tree_symlinks
; i
++) {
223 snprintf(tmppath
, PATH_MAX
, "%s/%s",
224 tree_origin
, tree_symlinks
[i
].orig
);
226 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
227 snprintf(errmsg
, ERRSIZE
, "symlink %s to %s",
228 tmppath
, tree_symlinks
[i
].dest
);
240 int free_symlink_tree(void)
243 char tmppath
[PATH_MAX
];
245 /* Remove the symlinks from the test tree */
246 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
247 snprintf(tmppath
, PATH_MAX
, "%s/%s",
248 tree_origin
, tree_symlinks
[i
].orig
);
250 if (unlink(tmppath
) != 0) {
251 snprintf(errmsg
, ERRSIZE
, "unlink %s", tmppath
);
257 /* Remove the directories from the test tree */
258 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
259 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
261 if (rmdir(tmppath
) != 0) {
262 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tmppath
);
268 /* Remove the temporary directory */
269 if (rmdir(tree_origin
) != 0) {
270 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tree_origin
);
281 static void test_utils_expand_path(void)
284 char name
[100], tmppath
[PATH_MAX
];
287 /* Test valid cases */
288 for (i
= 0; i
< num_valid_tests
; i
++) {
289 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
291 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
293 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
298 /* Test symlink tree cases */
299 int treelen
= strlen(tree_origin
) + 1;
300 for (i
= 0; i
< num_symlink_tests
; i
++) {
301 sprintf(name
, "symlink tree test case: [tmppath/]%s",
302 symlink_tests_inputs
[i
].input
);
304 snprintf(tmppath
, PATH_MAX
, "%s/%s",
305 tree_origin
, symlink_tests_inputs
[i
].input
);
306 result
= utils_expand_path(tmppath
);
307 ok(result
!= NULL
&& strcmp(result
+ treelen
,
308 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
313 /* Test invalid cases */
314 for (i
= 0; i
< num_invalid_tests
; i
++) {
315 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
317 result
= utils_expand_path(invalid_tests_inputs
[i
]);
318 if (result
!= NULL
) {
321 ok(result
== NULL
, name
);
325 int main(int argc
, char **argv
)
327 if (prepare_symlink_tree() != 0) {
331 if (prepare_valid_results() != 0) {
335 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
337 diag("utils_expand_path tests");
339 test_utils_expand_path();
341 free_valid_results();
344 return exit_status();
347 free_valid_results();