Fix: syscall event rule: emission sites not compared in is_equal
[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 nullptr,
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()
104 {
105 int i;
106 char *relative, *cur_path = nullptr, *prev_path = nullptr, *pprev_path = nullptr,
107 *empty = nullptr;
108 int ret = 0;
109
110 /* Prepare the relative paths */
111 cur_path = realpath(".", nullptr);
112 prev_path = realpath("..", nullptr);
113 pprev_path = realpath("../..", nullptr);
114 empty = strdup("");
115 if (!cur_path || !prev_path || !pprev_path || !empty) {
116 PRINT_ERR("strdup out of memory");
117 ret = -1;
118 goto end;
119 }
120
121 /* allocate memory for the expected results */
122 valid_tests_expected_results = calloc<char *>(num_valid_tests);
123 if (!valid_tests_expected_results) {
124 PRINT_ERR("out of memory");
125 ret = -1;
126 goto end;
127 }
128 for (i = 0; i < num_valid_tests; i++) {
129 valid_tests_expected_results[i] = calloc<char>(PATH_MAX);
130 if (valid_tests_expected_results[i] == nullptr) {
131 PRINT_ERR("malloc expected results");
132 ret = -1;
133 goto end;
134 }
135
136 if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
137 relative = cur_path;
138 } else if (strcmp(valid_tests_inputs[i].relative_part, "..") == 0) {
139 relative = prev_path;
140 } else if (strcmp(valid_tests_inputs[i].relative_part, "../..") == 0) {
141 relative = pprev_path;
142 } else {
143 relative = empty;
144 }
145
146 snprintf(valid_tests_expected_results[i],
147 PATH_MAX,
148 "%s%s",
149 relative,
150 valid_tests_inputs[i].absolute_part);
151 }
152
153 end:
154 free(cur_path);
155 free(prev_path);
156 free(pprev_path);
157 free(empty);
158
159 return ret;
160 }
161
162 static int free_valid_results()
163 {
164 int i;
165
166 for (i = 0; i < num_valid_tests; i++) {
167 free(valid_tests_expected_results[i]);
168 }
169
170 free(valid_tests_expected_results);
171
172 return 0;
173 }
174
175 static int prepare_symlink_tree()
176 {
177 int i;
178 char tmppath[PATH_MAX] = {};
179
180 /* Create the temporary directory */
181 if (mkdtemp(tree_origin) == nullptr) {
182 PRINT_ERR("failed to mkdtemp");
183 goto error;
184 }
185
186 /* Create the directories of the test tree */
187 for (i = 0; i < num_tree_dirs; i++) {
188 snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, tree_dirs[i]);
189
190 if (mkdir(tmppath, 0755) != 0) {
191 PRINT_ERR("mkdir failed with path \"%s\"", tmppath);
192 goto error;
193 }
194 }
195
196 /* Create the symlinks of the test tree */
197 for (i = 0; i < num_tree_symlinks; i++) {
198 snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, tree_symlinks[i].orig);
199
200 if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
201 PRINT_ERR("failed to symlink \"%s\" to \"%s\"",
202 tmppath,
203 tree_symlinks[i].dest);
204 goto error;
205 }
206 }
207
208 return 0;
209
210 error:
211 return 1;
212 }
213
214 static int free_symlink_tree()
215 {
216 int i;
217 char tmppath[PATH_MAX];
218
219 /* Remove the symlinks from the test tree */
220 for (i = num_tree_symlinks - 1; i > -1; i--) {
221 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_symlinks[i].orig);
222
223 if (unlink(tmppath) != 0) {
224 PRINT_ERR("failed to unlink \"%s\"", tmppath);
225 goto error;
226 }
227 }
228
229 /* Remove the directories from the test tree */
230 for (i = num_tree_dirs - 1; i > -1; i--) {
231 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
232
233 if (rmdir(tmppath) != 0) {
234 PRINT_ERR("failed to rmdir \"%s\"", tmppath);
235 goto error;
236 }
237 }
238
239 /* Remove the temporary directory */
240 if (rmdir(tree_origin) != 0) {
241 PRINT_ERR("failed to rmdir \"%s\"", tree_origin);
242 goto error;
243 }
244
245 return 0;
246
247 error:
248 return 1;
249 }
250
251 static void test_utils_expand_path()
252 {
253 char *result;
254 char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX];
255 int i, treelen;
256
257 /* Test valid cases */
258 for (i = 0; i < num_valid_tests; i++) {
259 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
260
261 result = utils_expand_path(valid_tests_inputs[i].input);
262 ok(result != nullptr && strcmp(result, valid_tests_expected_results[i]) == 0,
263 "%s",
264 name);
265
266 free(result);
267 }
268
269 /*
270 * Get the realpath for the tree_origin since it can itself be a
271 * symlink.
272 */
273 result = realpath(tree_origin, real_tree_origin);
274 if (!result) {
275 fail("realpath failed.");
276 return;
277 }
278
279 /* Test symlink tree cases */
280 treelen = strlen(real_tree_origin) + 1;
281 for (i = 0; i < num_symlink_tests; i++) {
282 int ret;
283
284 sprintf(name,
285 "symlink tree test case: [tmppath/]%s",
286 symlink_tests_inputs[i].input);
287
288 ret = snprintf(tmppath,
289 PATH_MAX,
290 "%s/%s",
291 real_tree_origin,
292 symlink_tests_inputs[i].input);
293 if (ret == -1 || ret >= PATH_MAX) {
294 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
295 real_tree_origin,
296 symlink_tests_inputs[i].input);
297 fail("%s", name);
298 continue;
299 }
300 result = utils_expand_path(tmppath);
301 ok(result != nullptr &&
302 strcmp(result + treelen, symlink_tests_inputs[i].expected_result) == 0,
303 "%s",
304 name);
305
306 free(result);
307 }
308
309 /* Test invalid cases */
310 for (i = 0; i < num_invalid_tests; i++) {
311 const char *test_input = invalid_tests_inputs[i];
312
313 sprintf(name, "invalid test case: %s", test_input ? test_input : "NULL");
314
315 result = utils_expand_path(test_input);
316 if (result != nullptr) {
317 free(result);
318 }
319 ok(result == nullptr, "%s", name);
320 }
321 }
322
323 int main()
324 {
325 if (prepare_symlink_tree() != 0) {
326 goto error_mkdir;
327 }
328
329 if (prepare_valid_results() != 0) {
330 goto error_malloc;
331 }
332
333 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
334
335 diag("utils_expand_path tests");
336
337 test_utils_expand_path();
338
339 free_valid_results();
340 free_symlink_tree();
341
342 return exit_status();
343
344 error_malloc:
345 free_valid_results();
346
347 error_mkdir:
348 free_symlink_tree();
349
350 return 1;
351 }
This page took 0.038219 seconds and 5 git commands to generate.