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.
26 #include <sys/types.h>
30 #include <src/common/utils.h>
32 #include <common/common.h>
35 int lttng_opt_quiet
= 1;
36 int lttng_opt_verbose
= 3;
39 struct valid_test_input
{
50 struct symlink_test_input
{
52 char *expected_result
;
55 /* Valid test cases */
56 static struct valid_test_input valid_tests_inputs
[] = {
57 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
58 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
59 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
60 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
61 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
62 { "./a/b/../c/d/../e", ".", "/a/c/e" },
63 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
64 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
65 { "./a/b/c/d/../../../../e", ".", "/e" },
66 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
69 { "../../", "../..", "/" },
70 { "../..", "../..", "" },
75 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
76 { "/a/b/c/d/../../../../../e", "", "/e" },
80 char **valid_tests_expected_results
;
81 static const int num_valid_tests
=
82 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
84 /* Symlinks test cases */
85 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
87 static const char * const tree_dirs
[] = {
93 static const int num_tree_dirs
=
94 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
96 static struct tree_symlink tree_symlinks
[] = {
100 { "a/b/h", "../g/" },
102 { "a/b/c/g", "../../../" },
104 static const int num_tree_symlinks
=
105 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
107 static struct symlink_test_input symlink_tests_inputs
[] = {
108 { "a/g/../l/.", "a/b/l" },
109 { "a/g/../l/./", "a/b/l/" },
110 { "a/g/../l/..", "a/b" },
111 { "a/g/../l/../", "a/b/" },
114 static const int num_symlink_tests
=
115 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
117 /* Invalid test cases */
118 static char *invalid_tests_inputs
[] = {
121 static const int num_invalid_tests
=
122 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
125 char errmsg
[ERRSIZE
];
126 static void printerr(char *msg
)
128 fprintf(stderr
, "test_utils_expand_path: error: %s\n", msg
);
131 int prepare_valid_results(void)
134 char *relative
, *cur_path
= NULL
, *prev_path
= NULL
,
135 *pprev_path
= NULL
, *empty
= NULL
;
138 /* Prepare the relative paths */
139 cur_path
= realpath(".", NULL
);
140 prev_path
= realpath("..", NULL
);
141 pprev_path
= realpath("../..", NULL
);
143 if (!cur_path
|| !prev_path
|| !pprev_path
|| !empty
) {
144 printerr("strdup out of memory");
149 /* allocate memory for the expected results */
150 valid_tests_expected_results
= zmalloc(sizeof(char *) * num_valid_tests
);
151 if (!valid_tests_expected_results
) {
152 printerr("out of memory");
156 for (i
= 0; i
< num_valid_tests
; i
++) {
157 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
158 if (valid_tests_expected_results
[i
] == NULL
) {
159 printerr("malloc expected results");
164 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
166 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
167 relative
= prev_path
;
168 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
169 relative
= pprev_path
;
174 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
175 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
187 int free_valid_results(void)
191 for (i
= 0; i
< num_valid_tests
; i
++) {
192 free(valid_tests_expected_results
[i
]);
195 free(valid_tests_expected_results
);
200 int prepare_symlink_tree(void)
203 char tmppath
[PATH_MAX
];
205 /* Create the temporary directory */
206 if (mkdtemp(tree_origin
) == NULL
) {
211 /* Create the directories of the test tree */
212 for (i
= 0; i
< num_tree_dirs
; i
++) {
213 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
215 if (mkdir(tmppath
, 0755) != 0) {
216 snprintf(errmsg
, ERRSIZE
, "mkdir %s", tmppath
);
222 /* Create the symlinks of the test tree */
223 for (i
= 0; i
< num_tree_symlinks
; i
++) {
224 snprintf(tmppath
, PATH_MAX
, "%s/%s",
225 tree_origin
, tree_symlinks
[i
].orig
);
227 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
228 snprintf(errmsg
, ERRSIZE
, "symlink %s to %s",
229 tmppath
, tree_symlinks
[i
].dest
);
241 int free_symlink_tree(void)
244 char tmppath
[PATH_MAX
];
246 /* Remove the symlinks from the test tree */
247 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
248 snprintf(tmppath
, PATH_MAX
, "%s/%s",
249 tree_origin
, tree_symlinks
[i
].orig
);
251 if (unlink(tmppath
) != 0) {
252 snprintf(errmsg
, ERRSIZE
, "unlink %s", tmppath
);
258 /* Remove the directories from the test tree */
259 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
260 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
262 if (rmdir(tmppath
) != 0) {
263 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tmppath
);
269 /* Remove the temporary directory */
270 if (rmdir(tree_origin
) != 0) {
271 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tree_origin
);
282 static void test_utils_expand_path(void)
285 char name
[100], tmppath
[PATH_MAX
];
288 /* Test valid cases */
289 for (i
= 0; i
< num_valid_tests
; i
++) {
290 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
292 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
294 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
299 /* Test symlink tree cases */
300 int treelen
= strlen(tree_origin
) + 1;
301 for (i
= 0; i
< num_symlink_tests
; i
++) {
302 sprintf(name
, "symlink tree test case: [tmppath/]%s",
303 symlink_tests_inputs
[i
].input
);
305 snprintf(tmppath
, PATH_MAX
, "%s/%s",
306 tree_origin
, symlink_tests_inputs
[i
].input
);
307 result
= utils_expand_path(tmppath
);
308 ok(result
!= NULL
&& strcmp(result
+ treelen
,
309 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
314 /* Test invalid cases */
315 for (i
= 0; i
< num_invalid_tests
; i
++) {
316 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
318 result
= utils_expand_path(invalid_tests_inputs
[i
]);
319 if (result
!= NULL
) {
322 ok(result
== NULL
, name
);
326 int main(int argc
, char **argv
)
328 if (prepare_symlink_tree() != 0) {
332 if (prepare_valid_results() != 0) {
336 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
338 diag("utils_expand_path tests");
340 test_utils_expand_path();
342 free_valid_results();
345 return exit_status();
348 free_valid_results();