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;
35 struct valid_test_input
{
46 struct symlink_test_input
{
48 char *expected_result
;
51 /* Valid test cases */
52 static struct valid_test_input valid_tests_inputs
[] = {
53 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
54 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
55 { "../a/b/c/d/../e", "..", "/a/b/c/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/c/e" },
59 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
60 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
61 { "./a/b/c/d/../../../../e", ".", "/e" },
62 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
65 { "../../", "../..", "/" },
66 { "../..", "../..", "" },
71 { "/../a/b/c/d/e", "", "/a/b/c/d/e" },
72 { "/a/b/c/d/../../../../../e", "", "/e" },
76 char **valid_tests_expected_results
;
77 static const int num_valid_tests
=
78 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
80 /* Symlinks test cases */
81 char tree_origin
[] = "/tmp/test_utils_expand_path.XXXXXX";
83 static const char * const tree_dirs
[] = {
89 static const int num_tree_dirs
=
90 sizeof(tree_dirs
) / sizeof(tree_dirs
[0]);
92 static struct tree_symlink tree_symlinks
[] = {
98 { "a/b/c/g", "../../../" },
100 static const int num_tree_symlinks
=
101 sizeof(tree_symlinks
) / sizeof(tree_symlinks
[0]);
103 static struct symlink_test_input symlink_tests_inputs
[] = {
104 { "a/g/../l/.", "a/b/l" },
105 { "a/g/../l/./", "a/b/l/" },
106 { "a/g/../l/..", "a/b" },
107 { "a/g/../l/../", "a/b/" },
110 static const int num_symlink_tests
=
111 sizeof(symlink_tests_inputs
) / sizeof(symlink_tests_inputs
[0]);
113 /* Invalid test cases */
114 static char *invalid_tests_inputs
[] = {
117 static const int num_invalid_tests
=
118 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
121 char errmsg
[ERRSIZE
];
122 static void printerr(char *msg
)
124 fprintf(stderr
, "test_utils_expand_path: error: %s\n", msg
);
127 int prepare_valid_results()
130 char *relative
, *cur_path
, *prev_path
, *pprev_path
, *empty
;
132 /* Prepare the relative paths */
133 cur_path
= realpath(".", NULL
);
134 prev_path
= realpath("..", NULL
);
135 pprev_path
= realpath("../..", NULL
);
138 /* allocate memory for the expected results */
139 valid_tests_expected_results
= malloc(sizeof(char *) * num_valid_tests
);
140 for (i
= 0; i
< num_valid_tests
; i
++) {
141 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
142 if (valid_tests_expected_results
[i
] == NULL
) {
143 printerr("malloc expected results");
148 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
150 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
151 relative
= prev_path
;
152 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
153 relative
= pprev_path
;
158 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
159 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
170 int free_valid_results()
174 for (i
= 0; i
< num_valid_tests
; i
++) {
175 free(valid_tests_expected_results
[i
]);
178 free(valid_tests_expected_results
);
183 int prepare_symlink_tree()
186 char tmppath
[PATH_MAX
];
188 /* Create the temporary directory */
189 if (mkdtemp(tree_origin
) == NULL
) {
194 /* Create the directories of the test tree */
195 for (i
= 0; i
< num_tree_dirs
; i
++) {
196 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
198 if (mkdir(tmppath
, 0755) != 0) {
199 snprintf(errmsg
, ERRSIZE
, "mkdir %s", tmppath
);
205 /* Create the symlinks of the test tree */
206 for (i
= 0; i
< num_tree_symlinks
; i
++) {
207 snprintf(tmppath
, PATH_MAX
, "%s/%s",
208 tree_origin
, tree_symlinks
[i
].orig
);
210 if (symlink(tree_symlinks
[i
].dest
, tmppath
) != 0) {
211 snprintf(errmsg
, ERRSIZE
, "symlink %s to %s",
212 tmppath
, tree_symlinks
[i
].dest
);
224 int free_symlink_tree()
227 char tmppath
[PATH_MAX
];
229 /* Remove the symlinks from the test tree */
230 for (i
= num_tree_symlinks
- 1; i
> -1; i
--) {
231 snprintf(tmppath
, PATH_MAX
, "%s/%s",
232 tree_origin
, tree_symlinks
[i
].orig
);
234 if (unlink(tmppath
) != 0) {
235 snprintf(errmsg
, ERRSIZE
, "unlink %s", tmppath
);
241 /* Remove the directories from the test tree */
242 for (i
= num_tree_dirs
- 1; i
> -1; i
--) {
243 snprintf(tmppath
, PATH_MAX
, "%s/%s", tree_origin
, tree_dirs
[i
]);
245 if (rmdir(tmppath
) != 0) {
246 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tmppath
);
252 /* Remove the temporary directory */
253 if (rmdir(tree_origin
) != 0) {
254 snprintf(errmsg
, ERRSIZE
, "rmdir %s", tree_origin
);
265 static void test_utils_expand_path(void)
268 char name
[100], tmppath
[PATH_MAX
];
271 /* Test valid cases */
272 for (i
= 0; i
< num_valid_tests
; i
++) {
273 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
275 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
277 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
282 /* Test symlink tree cases */
283 int treelen
= strlen(tree_origin
) + 1;
284 for (i
= 0; i
< num_symlink_tests
; i
++) {
285 sprintf(name
, "symlink tree test case: [tmppath/]%s",
286 symlink_tests_inputs
[i
].input
);
288 snprintf(tmppath
, PATH_MAX
, "%s/%s",
289 tree_origin
, symlink_tests_inputs
[i
].input
);
290 result
= utils_expand_path(tmppath
);
291 ok(result
!= NULL
&& strcmp(result
+ treelen
,
292 symlink_tests_inputs
[i
].expected_result
) == 0, name
);
297 /* Test invalid cases */
298 for (i
= 0; i
< num_invalid_tests
; i
++) {
299 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
301 result
= utils_expand_path(invalid_tests_inputs
[i
]);
302 if (result
!= NULL
) {
305 ok(result
== NULL
, name
);
309 int main(int argc
, char **argv
)
311 if (prepare_symlink_tree() != 0) {
315 if (prepare_valid_results() != 0) {
319 plan_tests(num_valid_tests
+ num_invalid_tests
+ num_symlink_tests
);
321 diag("utils_expand_path tests");
323 test_utils_expand_path();
325 free_valid_results();
328 return exit_status();
331 free_valid_results();