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