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>
32 int lttng_opt_quiet
= 1;
33 int lttng_opt_verbose
= 3;
36 struct valid_test_input
{
47 struct symlink_test_input
{
49 char *expected_result
;
52 /* Valid test cases */
53 static struct valid_test_input valid_tests_inputs
[] = {
54 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
55 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
56 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
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/c/e" },
60 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
61 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
62 { "./a/b/c/d/../../../../e", ".", "/e" },
63 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
66 { "../../", "../..", "/" },
67 { "../..", "../..", "" },
72 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
73 { "/a/b/c/d/../../../../../e", "", "/e" },
77 char **valid_tests_expected_results
;
78 static const int num_valid_tests
=
79 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
81 /* Symlinks test cases */
82 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
84 static const char * const tree_dirs
[] = {
90 static const int num_tree_dirs
=
91 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
93 static struct tree_symlink tree_symlinks
[] = {
99 { "a/b/c/g", "../../../" },
101 static const int num_tree_symlinks
=
102 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
104 static struct symlink_test_input symlink_tests_inputs
[] = {
105 { "a/g/../l/.", "a/b/l" },
106 { "a/g/../l/./", "a/b/l/" },
107 { "a/g/../l/..", "a/b" },
108 { "a/g/../l/../", "a/b/" },
111 static const int num_symlink_tests
=
112 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
114 /* Invalid test cases */
115 static char *invalid_tests_inputs
[] = {
118 static const int num_invalid_tests
=
119 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
122 char errmsg
[ERRSIZE
];
123 static void printerr(char *msg
)
125 fprintf(stderr
, "test_utils_expand_path: error: %s\n", msg
);
128 int prepare_valid_results()
131 char *relative
, *cur_path
, *prev_path
, *pprev_path
, *empty
;
133 /* Prepare the relative paths */
134 cur_path
= realpath(".", NULL
);
135 prev_path
= realpath("..", NULL
);
136 pprev_path
= realpath("../..", NULL
);
139 /* allocate memory for the expected results */
140 valid_tests_expected_results
= malloc(sizeof(char *) * num_valid_tests
);
141 for (i
= 0; i
< num_valid_tests
; i
++) {
142 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
143 if (valid_tests_expected_results
[i
] == NULL
) {
144 printerr("malloc expected results");
149 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
151 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
152 relative
= prev_path
;
153 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
154 relative
= pprev_path
;
159 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
160 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
171 int free_valid_results()
175 for (i
= 0; i
< num_valid_tests
; i
++) {
176 free(valid_tests_expected_results
[i
]);
179 free(valid_tests_expected_results
);
184 int prepare_symlink_tree()
187 char tmppath
[PATH_MAX
];
189 /* Create the temporary directory */
190 if (mkdtemp(tree_origin
) == NULL
) {
195 /* Create the directories of the test tree */
196 for (i
= 0; i
< num_tree_dirs
; i
++) {
197 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
199 if (mkdir(tmppath
, 0755) != 0) {
200 snprintf(errmsg
, ERRSIZE
, "mkdir %s", tmppath
);
206 /* Create the symlinks of the test tree */
207 for (i
= 0; i
< num_tree_symlinks
; i
++) {
208 snprintf(tmppath
, PATH_MAX
, "%s/%s",
209 tree_origin
, tree_symlinks
[i
].orig
);
211 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
212 snprintf(errmsg
, ERRSIZE
, "symlink %s to %s",
213 tmppath
, tree_symlinks
[i
].dest
);
225 int free_symlink_tree()
228 char tmppath
[PATH_MAX
];
230 /* Remove the symlinks from the test tree */
231 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
232 snprintf(tmppath
, PATH_MAX
, "%s/%s",
233 tree_origin
, tree_symlinks
[i
].orig
);
235 if (unlink(tmppath
) != 0) {
236 snprintf(errmsg
, ERRSIZE
, "unlink %s", tmppath
);
242 /* Remove the directories from the test tree */
243 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
244 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
246 if (rmdir(tmppath
) != 0) {
247 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tmppath
);
253 /* Remove the temporary directory */
254 if (rmdir(tree_origin
) != 0) {
255 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tree_origin
);
266 static void test_utils_expand_path(void)
269 char name
[100], tmppath
[PATH_MAX
];
272 /* Test valid cases */
273 for (i
= 0; i
< num_valid_tests
; i
++) {
274 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
276 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
278 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
283 /* Test symlink tree cases */
284 int treelen
= strlen(tree_origin
) + 1;
285 for (i
= 0; i
< num_symlink_tests
; i
++) {
286 sprintf(name
, "symlink tree test case: [tmppath/]%s",
287 symlink_tests_inputs
[i
].input
);
289 snprintf(tmppath
, PATH_MAX
, "%s/%s",
290 tree_origin
, symlink_tests_inputs
[i
].input
);
291 result
= utils_expand_path(tmppath
);
292 ok(result
!= NULL
&& strcmp(result
+ treelen
,
293 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
298 /* Test invalid cases */
299 for (i
= 0; i
< num_invalid_tests
; i
++) {
300 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
302 result
= utils_expand_path(invalid_tests_inputs
[i
]);
303 if (result
!= NULL
) {
306 ok(result
== NULL
, name
);
310 int main(int argc
, char **argv
)
312 if (prepare_symlink_tree() != 0) {
316 if (prepare_valid_results() != 0) {
320 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
322 diag("utils_expand_path tests");
324 test_utils_expand_path();
326 free_valid_results();
329 return exit_status();
332 free_valid_results();