Tests: Add test_utils_expand_path to unit tests
[lttng-tools.git] / tests / unit / test_utils_expand_path.c
1 /*
2 * Copyright (C) - 2013 Raphaƫl Beamonte <raphael.beamonte@gmail.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #include <assert.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <limits.h>
23
24 #include <tap/tap.h>
25
26 #include <src/common/utils.h>
27
28 /* For lttngerr.h */
29 int lttng_opt_quiet = 1;
30 int lttng_opt_verbose = 3;
31
32 struct valid_test_input {
33 char *input;
34 char *relative_part;
35 char *absolute_part;
36 };
37
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" },
50 { "a/", ".", "/a/" },
51 { "a", ".", "/a" },
52 { "../../", "../..", "/" },
53 { "../..", "../..", "" },
54 { "../", "..", "/" },
55 { "..", "..", "" },
56 { "./", ".", "/" },
57 { ".", ".", "" },
58 };
59 char **valid_tests_expected_results;
60 static const int num_valid_tests =
61 sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
62
63 /* Invalid test cases */
64 static char *invalid_tests_inputs[] = {
65 NULL,
66 "/../a/b/c/d/e",
67 "/a/b/c/d/../../../../../e",
68 };
69 static const int num_invalid_tests =
70 sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
71
72 int prepare_valid_results()
73 {
74 int i;
75 char *relative, *cur_path, *prev_path, *pprev_path, *empty;
76
77 /* Prepare the relative paths */
78 cur_path = realpath(".", NULL);
79 prev_path = realpath("..", NULL);
80 pprev_path = realpath("../..", NULL);
81 empty = strdup("");
82
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");
89 return 1;
90 }
91
92 if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
93 relative = cur_path;
94 } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) {
95 relative = prev_path;
96 } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) {
97 relative = pprev_path;
98 } else {
99 relative = empty;
100 }
101
102 snprintf(valid_tests_expected_results[i], PATH_MAX,
103 "%s%s", relative, valid_tests_inputs[i].absolute_part);
104 }
105
106 free(cur_path);
107 free(prev_path);
108 free(pprev_path);
109 free(empty);
110
111 return 0;
112 }
113
114 int free_valid_results()
115 {
116 int i;
117
118 for (i = 0; i < num_valid_tests; i++) {
119 free(valid_tests_expected_results[i]);
120 }
121
122 free(valid_tests_expected_results);
123
124 return 0;
125 }
126
127 static void test_utils_expand_path(void)
128 {
129 char *result;
130 int i;
131
132 /* Test valid cases */
133 for (i = 0; i < num_valid_tests; i++) {
134 char name[100];
135 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
136
137 result = utils_expand_path(valid_tests_inputs[i].input);
138 ok(strcmp(result, valid_tests_expected_results[i]) == 0, name);
139
140 free(result);
141 }
142
143 /* Test invalid cases */
144 for (i = 0; i < num_invalid_tests; i++) {
145 char name[100];
146 sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
147
148 result = utils_expand_path(invalid_tests_inputs[i]);
149 if (result != NULL) {
150 free(result);
151 }
152 ok(result == NULL, name);
153 }
154 }
155
156 int main(int argc, char **argv)
157 {
158 if (prepare_valid_results() != 0) {
159 return 1;
160 }
161
162 plan_tests(num_valid_tests + num_invalid_tests);
163
164 diag("utils_expand_path tests");
165
166 test_utils_expand_path();
167
168 free_valid_results();
169 return exit_status();
170 }
This page took 0.052938 seconds and 5 git commands to generate.