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 | ||
87e96404 | 29 | #include <common/utils.h> |
8098bf0a MD |
30 | #include <common/common.h> |
31 | ||
ad7c9c18 | 32 | /* For error.h */ |
cc7f9e36 RB |
33 | int lttng_opt_quiet = 1; |
34 | int lttng_opt_verbose = 3; | |
c7e35b03 | 35 | int lttng_opt_mi; |
cc7f9e36 RB |
36 | |
37 | struct valid_test_input { | |
38 | char *input; | |
39 | char *relative_part; | |
40 | char *absolute_part; | |
41 | }; | |
42 | ||
02bf969d RB |
43 | struct tree_symlink { |
44 | char *orig; | |
45 | char *dest; | |
46 | }; | |
47 | ||
48 | struct symlink_test_input { | |
49 | char *input; | |
50 | char *expected_result; | |
51 | }; | |
52 | ||
cc7f9e36 RB |
53 | /* Valid test cases */ |
54 | static struct valid_test_input valid_tests_inputs[] = { | |
55 | { "/a/b/c/d/e", "", "/a/b/c/d/e" }, | |
56 | { "./a/b/c/d/e", ".", "/a/b/c/d/e" }, | |
57 | { "../a/b/c/d/../e", "..", "/a/b/c/e" }, | |
58 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, | |
59 | { "../../a/b/c/d/e", "../..", "/a/b/c/d/e" }, | |
60 | { "./a/b/../c/d/../e", ".", "/a/c/e" }, | |
61 | { "../a/b/../../c/./d/./e", "..", "/c/d/e" }, | |
62 | { "../../a/b/../c/d/../../e", "../..", "/a/e" }, | |
63 | { "./a/b/c/d/../../../../e", ".", "/e" }, | |
64 | { ".././a/b/c/d/./e", "..", "/a/b/c/d/e" }, | |
65 | { "a/", ".", "/a/" }, | |
66 | { "a", ".", "/a" }, | |
67 | { "../../", "../..", "/" }, | |
68 | { "../..", "../..", "" }, | |
69 | { "../", "..", "/" }, | |
70 | { "..", "..", "" }, | |
71 | { "./", ".", "/" }, | |
72 | { ".", ".", "" }, | |
ab5ff479 RB |
73 | { "/../a/b/c/d/e", "", "/a/b/c/d/e" }, |
74 | { "/a/b/c/d/../../../../../e", "", "/e" }, | |
9a506343 RB |
75 | { "/..", "", "/" }, |
76 | { "/a/..", "", "/" }, | |
cc7f9e36 RB |
77 | }; |
78 | char **valid_tests_expected_results; | |
79 | static const int num_valid_tests = | |
80 | sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]); | |
81 | ||
02bf969d RB |
82 | /* Symlinks test cases */ |
83 | char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX"; | |
84 | ||
85 | static const char * const tree_dirs[] = { | |
86 | "a", | |
87 | "a/b", | |
88 | "a/b/c", | |
89 | "a/e", | |
90 | }; | |
91 | static const int num_tree_dirs = | |
92 | sizeof(tree_dirs) / sizeof(tree_dirs[0]); | |
93 | ||
94 | static struct tree_symlink tree_symlinks[] = { | |
95 | { "a/d", "b/c/" }, | |
96 | { "a/g", "d/" }, | |
97 | { "a/b/f", "../e/" }, | |
98 | { "a/b/h", "../g/" }, | |
99 | { "a/b/k", "c/g/" }, | |
100 | { "a/b/c/g", "../../../" }, | |
101 | }; | |
102 | static const int num_tree_symlinks = | |
103 | sizeof(tree_symlinks) / sizeof(tree_symlinks[0]); | |
104 | ||
105 | static struct symlink_test_input symlink_tests_inputs[] = { | |
106 | { "a/g/../l/.", "a/b/l" }, | |
107 | { "a/g/../l/./", "a/b/l/" }, | |
108 | { "a/g/../l/..", "a/b" }, | |
109 | { "a/g/../l/../", "a/b/" }, | |
110 | { "a/b/h/g/", "" }, | |
111 | }; | |
112 | static const int num_symlink_tests = | |
113 | sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]); | |
114 | ||
cc7f9e36 RB |
115 | /* Invalid test cases */ |
116 | static char *invalid_tests_inputs[] = { | |
117 | NULL, | |
cc7f9e36 RB |
118 | }; |
119 | static const int num_invalid_tests = | |
120 | sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); | |
121 | ||
02bf969d RB |
122 | #define ERRSIZE 100 |
123 | char errmsg[ERRSIZE]; | |
124 | static void printerr(char *msg) | |
125 | { | |
126 | fprintf(stderr, "test_utils_expand_path: error: %s\n", msg); | |
127 | } | |
128 | ||
1661351d | 129 | int prepare_valid_results(void) |
cc7f9e36 RB |
130 | { |
131 | int i; | |
8098bf0a MD |
132 | char *relative, *cur_path = NULL, *prev_path = NULL, |
133 | *pprev_path = NULL, *empty = NULL; | |
134 | int ret = 0; | |
cc7f9e36 RB |
135 | |
136 | /* Prepare the relative paths */ | |
137 | cur_path = realpath(".", NULL); | |
138 | prev_path = realpath("..", NULL); | |
139 | pprev_path = realpath("../..", NULL); | |
140 | empty = strdup(""); | |
8098bf0a MD |
141 | if (!cur_path || !prev_path || !pprev_path || !empty) { |
142 | printerr("strdup out of memory"); | |
143 | ret = -1; | |
144 | goto end; | |
145 | } | |
cc7f9e36 RB |
146 | |
147 | /* allocate memory for the expected results */ | |
8098bf0a MD |
148 | valid_tests_expected_results = zmalloc(sizeof(char *) * num_valid_tests); |
149 | if (!valid_tests_expected_results) { | |
150 | printerr("out of memory"); | |
151 | ret = -1; | |
152 | goto end; | |
153 | } | |
cc7f9e36 RB |
154 | for (i = 0; i < num_valid_tests; i++) { |
155 | valid_tests_expected_results[i] = malloc(PATH_MAX); | |
156 | if (valid_tests_expected_results[i] == NULL) { | |
02bf969d | 157 | printerr("malloc expected results"); |
8098bf0a MD |
158 | ret = -1; |
159 | goto end; | |
cc7f9e36 RB |
160 | } |
161 | ||
162 | if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) { | |
163 | relative = cur_path; | |
164 | } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) { | |
165 | relative = prev_path; | |
166 | } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) { | |
167 | relative = pprev_path; | |
168 | } else { | |
169 | relative = empty; | |
170 | } | |
171 | ||
172 | snprintf(valid_tests_expected_results[i], PATH_MAX, | |
173 | "%s%s", relative, valid_tests_inputs[i].absolute_part); | |
174 | } | |
175 | ||
8098bf0a | 176 | end: |
cc7f9e36 RB |
177 | free(cur_path); |
178 | free(prev_path); | |
179 | free(pprev_path); | |
180 | free(empty); | |
181 | ||
8098bf0a | 182 | return ret; |
cc7f9e36 RB |
183 | } |
184 | ||
1661351d | 185 | int free_valid_results(void) |
cc7f9e36 RB |
186 | { |
187 | int i; | |
188 | ||
189 | for (i = 0; i < num_valid_tests; i++) { | |
190 | free(valid_tests_expected_results[i]); | |
191 | } | |
192 | ||
193 | free(valid_tests_expected_results); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
1661351d | 198 | int prepare_symlink_tree(void) |
02bf969d RB |
199 | { |
200 | int i; | |
201 | char tmppath[PATH_MAX]; | |
202 | ||
203 | /* Create the temporary directory */ | |
204 | if (mkdtemp(tree_origin) == NULL) { | |
205 | printerr("mkdtemp"); | |
206 | goto error; | |
207 | } | |
208 | ||
209 | /* Create the directories of the test tree */ | |
210 | for (i = 0; i < num_tree_dirs; i++) { | |
211 | snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]); | |
212 | ||
213 | if (mkdir(tmppath, 0755) != 0) { | |
214 | snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath); | |
215 | printerr(errmsg); | |
216 | goto error; | |
217 | } | |
218 | } | |
219 | ||
220 | /* Create the symlinks of the test tree */ | |
221 | for (i = 0; i < num_tree_symlinks; i++) { | |
222 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
223 | tree_origin, tree_symlinks[i].orig); | |
224 | ||
225 | if (symlink(tree_symlinks[i].dest, tmppath) != 0) { | |
226 | snprintf(errmsg, ERRSIZE, "symlink %s to %s", | |
227 | tmppath, tree_symlinks[i].dest); | |
228 | printerr(errmsg); | |
229 | goto error; | |
230 | } | |
231 | } | |
232 | ||
233 | return 0; | |
234 | ||
235 | error: | |
236 | return 1; | |
237 | } | |
238 | ||
1661351d | 239 | int free_symlink_tree(void) |
02bf969d RB |
240 | { |
241 | int i; | |
242 | char tmppath[PATH_MAX]; | |
243 | ||
244 | /* Remove the symlinks from the test tree */ | |
245 | for (i = num_tree_symlinks - 1; i > -1; i--) { | |
246 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
247 | tree_origin, tree_symlinks[i].orig); | |
248 | ||
249 | if (unlink(tmppath) != 0) { | |
250 | snprintf(errmsg, ERRSIZE, "unlink %s", tmppath); | |
251 | printerr(errmsg); | |
252 | goto error; | |
253 | } | |
254 | } | |
255 | ||
256 | /* Remove the directories from the test tree */ | |
257 | for (i = num_tree_dirs - 1; i > -1; i--) { | |
258 | snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]); | |
259 | ||
260 | if (rmdir(tmppath) != 0) { | |
261 | snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath); | |
262 | printerr(errmsg); | |
263 | goto error; | |
264 | } | |
265 | } | |
266 | ||
267 | /* Remove the temporary directory */ | |
268 | if (rmdir(tree_origin) != 0) { | |
269 | snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin); | |
270 | printerr(errmsg); | |
271 | goto error; | |
272 | } | |
273 | ||
274 | return 0; | |
275 | ||
276 | error: | |
277 | return 1; | |
278 | } | |
279 | ||
cc7f9e36 RB |
280 | static void test_utils_expand_path(void) |
281 | { | |
282 | char *result; | |
02bf969d | 283 | char name[100], tmppath[PATH_MAX]; |
cc7f9e36 RB |
284 | int i; |
285 | ||
286 | /* Test valid cases */ | |
287 | for (i = 0; i < num_valid_tests; i++) { | |
cc7f9e36 RB |
288 | sprintf(name, "valid test case: %s", valid_tests_inputs[i].input); |
289 | ||
290 | result = utils_expand_path(valid_tests_inputs[i].input); | |
ba526563 RB |
291 | ok(result != NULL && |
292 | strcmp(result, valid_tests_expected_results[i]) == 0, name); | |
cc7f9e36 RB |
293 | |
294 | free(result); | |
295 | } | |
296 | ||
02bf969d RB |
297 | /* Test symlink tree cases */ |
298 | int treelen = strlen(tree_origin) + 1; | |
299 | for (i = 0; i < num_symlink_tests; i++) { | |
300 | sprintf(name, "symlink tree test case: [tmppath/]%s", | |
301 | symlink_tests_inputs[i].input); | |
302 | ||
303 | snprintf(tmppath, PATH_MAX, "%s/%s", | |
304 | tree_origin, symlink_tests_inputs[i].input); | |
305 | result = utils_expand_path(tmppath); | |
306 | ok(result != NULL && strcmp(result + treelen, | |
307 | symlink_tests_inputs[i].expected_result) == 0, name); | |
308 | ||
309 | free(result); | |
310 | } | |
311 | ||
cc7f9e36 RB |
312 | /* Test invalid cases */ |
313 | for (i = 0; i < num_invalid_tests; i++) { | |
55295456 | 314 | const char *test_input = invalid_tests_inputs[i]; |
cc7f9e36 | 315 | |
55295456 JG |
316 | sprintf(name, "invalid test case: %s", test_input ? |
317 | test_input : "NULL"); | |
318 | ||
319 | result = utils_expand_path(test_input); | |
cc7f9e36 RB |
320 | if (result != NULL) { |
321 | free(result); | |
322 | } | |
323 | ok(result == NULL, name); | |
324 | } | |
325 | } | |
326 | ||
327 | int main(int argc, char **argv) | |
328 | { | |
02bf969d RB |
329 | if (prepare_symlink_tree() != 0) { |
330 | goto error_mkdir; | |
331 | } | |
332 | ||
cc7f9e36 | 333 | if (prepare_valid_results() != 0) { |
02bf969d | 334 | goto error_malloc; |
cc7f9e36 RB |
335 | } |
336 | ||
02bf969d | 337 | plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests); |
cc7f9e36 RB |
338 | |
339 | diag("utils_expand_path tests"); | |
340 | ||
341 | test_utils_expand_path(); | |
342 | ||
343 | free_valid_results(); | |
02bf969d RB |
344 | free_symlink_tree(); |
345 | ||
cc7f9e36 | 346 | return exit_status(); |
02bf969d RB |
347 | |
348 | error_malloc: | |
349 | free_valid_results(); | |
350 | ||
351 | error_mkdir: | |
352 | free_symlink_tree(); | |
353 | ||
354 | return 1; | |
cc7f9e36 | 355 | } |