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