Commit | Line | Data |
---|---|---|
cc7f9e36 RB |
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 | ||
02bf969d RB |
24 | #include <sys/stat.h> |
25 | #include <sys/types.h> | |
26 | ||
cc7f9e36 RB |
27 | #include <tap/tap.h> |
28 | ||
29 | #include <src/common/utils.h> | |
30 | ||
ad7c9c18 | 31 | /* For error.h */ |
cc7f9e36 RB |
32 | int lttng_opt_quiet = 1; |
33 | int lttng_opt_verbose = 3; | |
34 | ||
35 | struct valid_test_input { | |
36 | char *input; | |
37 | char *relative_part; | |
38 | char *absolute_part; | |
39 | }; | |
40 | ||
02bf969d RB |
41 | struct tree_symlink { |
42 | char *orig; | |
43 | char *dest; | |
44 | }; | |
45 | ||
46 | struct symlink_test_input { | |
47 | char *input; | |
48 | char *expected_result; | |
49 | }; | |
50 | ||
cc7f9e36 RB |
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" }, | |
63 | { "a/", ".", "/a/" }, | |
64 | { "a", ".", "/a" }, | |
65 | { "../../", "../..", "/" }, | |
66 | { "../..", "../..", "" }, | |
67 | { "../", "..", "/" }, | |
68 | { "..", "..", "" }, | |
69 | { "./", ".", "/" }, | |
70 | { ".", ".", "" }, | |
ab5ff479 RB |
71 | { "/../a/b/c/d/e", "", "/a/b/c/d/e" }, |
72 | { "/a/b/c/d/../../../../../e", "", "/e" }, | |
9a506343 RB |
73 | { "/..", "", "/" }, |
74 | { "/a/..", "", "/" }, | |
cc7f9e36 RB |
75 | }; |
76 | char **valid_tests_expected_results; | |
77 | static const int num_valid_tests = | |
78 | sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
79 | ||
02bf969d RB |
80 | /* Symlinks test cases */ |
81 | char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX"; | |
82 | ||
83 | static const char * const tree_dirs[] = { | |
84 | "a", | |
85 | "a/b", | |
86 | "a/b/c", | |
87 | "a/e", | |
88 | }; | |
89 | static const int num_tree_dirs = | |
90 | sizeof(tree_dirs) / sizeof(tree_dirs[0]); | |
91 | ||
92 | static struct tree_symlink tree_symlinks[] = { | |
93 | { "a/d", "b/c/" }, | |
94 | { "a/g", "d/" }, | |
95 | { "a/b/f", "../e/" }, | |
96 | { "a/b/h", "../g/" }, | |
97 | { "a/b/k", "c/g/" }, | |
98 | { "a/b/c/g", "../../../" }, | |
99 | }; | |
100 | static const int num_tree_symlinks = | |
101 | sizeof(tree_symlinks) / sizeof(tree_symlinks[0]); | |
102 | ||
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/" }, | |
108 | { "a/b/h/g/", "" }, | |
109 | }; | |
110 | static const int num_symlink_tests = | |
111 | sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]); | |
112 | ||
cc7f9e36 RB |
113 | /* Invalid test cases */ |
114 | static char *invalid_tests_inputs[] = { | |
115 | NULL, | |
cc7f9e36 RB |
116 | }; |
117 | static const int num_invalid_tests = | |
118 | sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); | |
119 | ||
02bf969d RB |
120 | #define ERRSIZE 100 |
121 | char errmsg[ERRSIZE]; | |
122 | static void printerr(char *msg) | |
123 | { | |
124 | fprintf(stderr, "test_utils_expand_path: error: %s\n", msg); | |
125 | } | |
126 | ||
cc7f9e36 RB |
127 | int prepare_valid_results() |
128 | { | |
129 | int i; | |
130 | char *relative, *cur_path, *prev_path, *pprev_path, *empty; | |
131 | ||
132 | /* Prepare the relative paths */ | |
133 | cur_path = realpath(".", NULL); | |
134 | prev_path = realpath("..", NULL); | |
135 | pprev_path = realpath("../..", NULL); | |
136 | empty = strdup(""); | |
137 | ||
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) { | |
02bf969d | 143 | printerr("malloc expected results"); |
13b42522 | 144 | free(empty); |
cc7f9e36 RB |
145 | return 1; |
146 | } | |
147 | ||
148 | if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) { | |
149 | relative = cur_path; | |
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; | |
154 | } else { | |
155 | relative = empty; | |
156 | } | |
157 | ||
158 | snprintf(valid_tests_expected_results[i], PATH_MAX, | |
159 | "%s%s", relative, valid_tests_inputs[i].absolute_part); | |
160 | } | |
161 | ||
162 | free(cur_path); | |
163 | free(prev_path); | |
164 | free(pprev_path); | |
165 | free(empty); | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | int free_valid_results() | |
171 | { | |
172 | int i; | |
173 | ||
174 | for (i = 0; i < num_valid_tests; i++) { | |
175 | free(valid_tests_expected_results[i]); | |
176 | } | |
177 | ||
178 | free(valid_tests_expected_results); | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
02bf969d RB |
183 | int prepare_symlink_tree() |
184 | { | |
185 | int i; | |
186 | char tmppath[PATH_MAX]; | |
187 | ||
188 | /* Create the temporary directory */ | |
189 | if (mkdtemp(tree_origin) == NULL) { | |
190 | printerr("mkdtemp"); | |
191 | goto error; | |
192 | } | |
193 | ||
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]); | |
197 | ||
198 | if (mkdir(tmppath, 0755) != 0) { | |
199 | snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath); | |
200 | printerr(errmsg); | |
201 | goto error; | |
202 | } | |
203 | } | |
204 | ||
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); | |
209 | ||
210 | if (symlink(tree_symlinks[i].dest, tmppath) != 0) { | |
211 | snprintf(errmsg, ERRSIZE, "symlink %s to %s", | |
212 | tmppath, tree_symlinks[i].dest); | |
213 | printerr(errmsg); | |
214 | goto error; | |
215 | } | |
216 | } | |
217 | ||
218 | return 0; | |
219 | ||
220 | error: | |
221 | return 1; | |
222 | } | |
223 | ||
224 | int free_symlink_tree() | |
225 | { | |
226 | int i; | |
227 | char tmppath[PATH_MAX]; | |
228 | ||
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); | |
233 | ||
234 | if (unlink(tmppath) != 0) { | |
235 | snprintf(errmsg, ERRSIZE, "unlink %s", tmppath); | |
236 | printerr(errmsg); | |
237 | goto error; | |
238 | } | |
239 | } | |
240 | ||
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]); | |
244 | ||
245 | if (rmdir(tmppath) != 0) { | |
246 | snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath); | |
247 | printerr(errmsg); | |
248 | goto error; | |
249 | } | |
250 | } | |
251 | ||
252 | /* Remove the temporary directory */ | |
253 | if (rmdir(tree_origin) != 0) { | |
254 | snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin); | |
255 | printerr(errmsg); | |
256 | goto error; | |
257 | } | |
258 | ||
259 | return 0; | |
260 | ||
261 | error: | |
262 | return 1; | |
263 | } | |
264 | ||
cc7f9e36 RB |
265 | static void test_utils_expand_path(void) |
266 | { | |
267 | char *result; | |
02bf969d | 268 | char name[100], tmppath[PATH_MAX]; |
cc7f9e36 RB |
269 | int i; |
270 | ||
271 | /* Test valid cases */ | |
272 | for (i = 0; i < num_valid_tests; i++) { | |
cc7f9e36 RB |
273 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
274 | ||
275 | result = utils_expand_path(valid_tests_inputs[i].input); | |
ba526563 RB |
276 | ok(result != NULL && |
277 | strcmp(result, valid_tests_expected_results[i]) == 0, name); | |
cc7f9e36 RB |
278 | |
279 | free(result); | |
280 | } | |
281 | ||
02bf969d RB |
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); | |
287 | ||
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); | |
293 | ||
294 | free(result); | |
295 | } | |
296 | ||
cc7f9e36 RB |
297 | /* Test invalid cases */ |
298 | for (i = 0; i < num_invalid_tests; i++) { | |
cc7f9e36 RB |
299 | sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]); |
300 | ||
301 | result = utils_expand_path(invalid_tests_inputs[i]); | |
302 | if (result != NULL) { | |
303 | free(result); | |
304 | } | |
305 | ok(result == NULL, name); | |
306 | } | |
307 | } | |
308 | ||
309 | int main(int argc, char **argv) | |
310 | { | |
02bf969d RB |
311 | if (prepare_symlink_tree() != 0) { |
312 | goto error_mkdir; | |
313 | } | |
314 | ||
cc7f9e36 | 315 | if (prepare_valid_results() != 0) { |
02bf969d | 316 | goto error_malloc; |
cc7f9e36 RB |
317 | } |
318 | ||
02bf969d | 319 | plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests); |
cc7f9e36 RB |
320 | |
321 | diag("utils_expand_path tests"); | |
322 | ||
323 | test_utils_expand_path(); | |
324 | ||
325 | free_valid_results(); | |
02bf969d RB |
326 | free_symlink_tree(); |
327 | ||
cc7f9e36 | 328 | return exit_status(); |
02bf969d RB |
329 | |
330 | error_malloc: | |
331 | free_valid_results(); | |
332 | ||
333 | error_mkdir: | |
334 | free_symlink_tree(); | |
335 | ||
336 | return 1; | |
cc7f9e36 | 337 | } |