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