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 <src/common/utils.h>
29 int lttng_opt_quiet
= 1;
30 int lttng_opt_verbose
= 3;
32 struct valid_test_input
{
38 /* Valid test cases */
39 static struct valid_test_input valid_tests_inputs
[] = {
40 { "/a/b/c/d/e", "", "/a/b/c/d/e" },
41 { "./a/b/c/d/e", ".", "/a/b/c/d/e" },
42 { "../a/b/c/d/../e", "..", "/a/b/c/e" },
43 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
44 { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" },
45 { "./a/b/../c/d/../e", ".", "/a/c/e" },
46 { "../a/b/../../c/./d/./e", "..", "/c/d/e" },
47 { "../../a/b/../c/d/../../e", "../..", "/a/e" },
48 { "./a/b/c/d/../../../../e", ".", "/e" },
49 { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" },
52 { "../../", "../..", "/" },
53 { "../..", "../..", "" },
59 char **valid_tests_expected_results
;
60 static const int num_valid_tests
=
61 sizeof(valid_tests_inputs
) / sizeof(valid_tests_inputs
[0]);
63 /* Invalid test cases */
64 static char *invalid_tests_inputs
[] = {
67 "/a/b/c/d/../../../../../e",
69 static const int num_invalid_tests
=
70 sizeof(invalid_tests_inputs
) / sizeof(invalid_tests_inputs
[0]);
72 int prepare_valid_results()
75 char *relative
, *cur_path
, *prev_path
, *pprev_path
, *empty
;
77 /* Prepare the relative paths */
78 cur_path
= realpath(".", NULL
);
79 prev_path
= realpath("..", NULL
);
80 pprev_path
= realpath("../..", NULL
);
83 /* allocate memory for the expected results */
84 valid_tests_expected_results
= malloc(sizeof(char *) * num_valid_tests
);
85 for (i
= 0; i
< num_valid_tests
; i
++) {
86 valid_tests_expected_results
[i
] = malloc(PATH_MAX
);
87 if (valid_tests_expected_results
[i
] == NULL
) {
88 fprintf(stderr
, "malloc expected results");
92 if (strcmp(valid_tests_inputs
[i
].relative_part
, ".") == 0) {
94 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "..") == 0) {
96 } else if (strcmp(valid_tests_inputs
[i
].relative_part
, "../..") == 0) {
97 relative
= pprev_path
;
102 snprintf(valid_tests_expected_results
[i
], PATH_MAX
,
103 "%s%s", relative
, valid_tests_inputs
[i
].absolute_part
);
114 int free_valid_results()
118 for (i
= 0; i
< num_valid_tests
; i
++) {
119 free(valid_tests_expected_results
[i
]);
122 free(valid_tests_expected_results
);
127 static void test_utils_expand_path(void)
132 /* Test valid cases */
133 for (i
= 0; i
< num_valid_tests
; i
++) {
135 sprintf(name
, "valid test case: %s", valid_tests_inputs
[i
].input
);
137 result
= utils_expand_path(valid_tests_inputs
[i
].input
);
139 strcmp(result
, valid_tests_expected_results
[i
]) == 0, name
);
144 /* Test invalid cases */
145 for (i
= 0; i
< num_invalid_tests
; i
++) {
147 sprintf(name
, "invalid test case: %s", invalid_tests_inputs
[i
]);
149 result
= utils_expand_path(invalid_tests_inputs
[i
]);
150 if (result
!= NULL
) {
153 ok(result
== NULL
, name
);
157 int main(int argc
, char **argv
)
159 if (prepare_valid_results() != 0) {
163 plan_tests(num_valid_tests
+ num_invalid_tests
);
165 diag("utils_expand_path tests");
167 test_utils_expand_path();
169 free_valid_results();
170 return exit_status();