Run clang-format on the whole tree
[lttng-tools.git] / tests / unit / test_utils_expand_path.cpp
CommitLineData
cc7f9e36 1/*
9d16b343 2 * Copyright (C) 2013 Raphaël Beamonte <raphael.beamonte@gmail.com>
cc7f9e36 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
cc7f9e36 5 *
cc7f9e36
RB
6 */
7
28ab034a
JG
8#include <common/common.hpp>
9#include <common/path.hpp>
10#include <common/utils.hpp>
11
12#include <limits.h>
cc7f9e36
RB
13#include <stdio.h>
14#include <stdlib.h>
28ab034a 15#include <string.h>
02bf969d
RB
16#include <sys/stat.h>
17#include <sys/types.h>
e30c79d2
MJ
18#include <tap/tap.h>
19
ad7c9c18 20/* For error.h */
cc7f9e36
RB
21int lttng_opt_quiet = 1;
22int lttng_opt_verbose = 3;
c7e35b03 23int lttng_opt_mi;
cc7f9e36 24
f1494934 25namespace {
cc7f9e36 26struct valid_test_input {
b53d4e59
SM
27 const char *input;
28 const char *relative_part;
29 const char *absolute_part;
cc7f9e36
RB
30};
31
02bf969d 32struct tree_symlink {
b53d4e59
SM
33 const char *orig;
34 const char *dest;
02bf969d
RB
35};
36
37struct symlink_test_input {
b53d4e59
SM
38 const char *input;
39 const char *expected_result;
02bf969d
RB
40};
41
cc7f9e36 42/* Valid test cases */
f1494934 43struct valid_test_input valid_tests_inputs[] = {
28ab034a
JG
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/..", "", "/" },
cc7f9e36
RB
67};
68char **valid_tests_expected_results;
28ab034a 69const int num_valid_tests = sizeof(valid_tests_inputs) / sizeof(valid_tests_inputs[0]);
cc7f9e36 70
02bf969d
RB
71/* Symlinks test cases */
72char tree_origin[] = "/tmp/test_utils_expand_path.XXXXXX";
73
28ab034a 74const char *const tree_dirs[] = {
02bf969d
RB
75 "a",
76 "a/b",
77 "a/b/c",
78 "a/e",
79};
28ab034a 80const int num_tree_dirs = sizeof(tree_dirs) / sizeof(tree_dirs[0]);
02bf969d 81
f1494934 82struct tree_symlink tree_symlinks[] = {
28ab034a
JG
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", "../../../" },
02bf969d 85};
28ab034a 86const int num_tree_symlinks = sizeof(tree_symlinks) / sizeof(tree_symlinks[0]);
02bf969d
RB
87
88static struct symlink_test_input symlink_tests_inputs[] = {
28ab034a
JG
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/", "" },
02bf969d 91};
28ab034a 92const int num_symlink_tests = sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]);
02bf969d 93
cc7f9e36 94/* Invalid test cases */
f1494934 95char *invalid_tests_inputs[] = {
cc7f9e36 96 NULL,
cc7f9e36 97};
28ab034a 98const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
f1494934 99} /* namespace */
cc7f9e36 100
28ab034a 101#define PRINT_ERR(fmt, args...) fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ##args)
02bf969d 102
8dfbc0b4 103static int prepare_valid_results(void)
cc7f9e36
RB
104{
105 int i;
28ab034a 106 char *relative, *cur_path = NULL, *prev_path = NULL, *pprev_path = NULL, *empty = NULL;
8098bf0a 107 int ret = 0;
cc7f9e36
RB
108
109 /* Prepare the relative paths */
110 cur_path = realpath(".", NULL);
111 prev_path = realpath("..", NULL);
112 pprev_path = realpath("../..", NULL);
113 empty = strdup("");
8098bf0a 114 if (!cur_path || !prev_path || !pprev_path || !empty) {
b35d936e 115 PRINT_ERR("strdup out of memory");
8098bf0a
MD
116 ret = -1;
117 goto end;
118 }
cc7f9e36
RB
119
120 /* allocate memory for the expected results */
64803277 121 valid_tests_expected_results = calloc<char *>(num_valid_tests);
8098bf0a 122 if (!valid_tests_expected_results) {
b35d936e 123 PRINT_ERR("out of memory");
8098bf0a
MD
124 ret = -1;
125 goto end;
126 }
cc7f9e36 127 for (i = 0; i < num_valid_tests; i++) {
64803277 128 valid_tests_expected_results[i] = calloc<char>(PATH_MAX);
cc7f9e36 129 if (valid_tests_expected_results[i] == NULL) {
b35d936e 130 PRINT_ERR("malloc expected results");
8098bf0a
MD
131 ret = -1;
132 goto end;
cc7f9e36
RB
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
28ab034a
JG
145 snprintf(valid_tests_expected_results[i],
146 PATH_MAX,
147 "%s%s",
148 relative,
149 valid_tests_inputs[i].absolute_part);
cc7f9e36
RB
150 }
151
8098bf0a 152end:
cc7f9e36
RB
153 free(cur_path);
154 free(prev_path);
155 free(pprev_path);
156 free(empty);
157
8098bf0a 158 return ret;
cc7f9e36
RB
159}
160
8dfbc0b4 161static int free_valid_results(void)
cc7f9e36
RB
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
8dfbc0b4 174static int prepare_symlink_tree(void)
02bf969d
RB
175{
176 int i;
b35d936e 177 char tmppath[PATH_MAX] = {};
02bf969d
RB
178
179 /* Create the temporary directory */
180 if (mkdtemp(tree_origin) == NULL) {
b35d936e 181 PRINT_ERR("failed to mkdtemp");
02bf969d
RB
182 goto error;
183 }
184
185 /* Create the directories of the test tree */
186 for (i = 0; i < num_tree_dirs; i++) {
28ab034a 187 snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, tree_dirs[i]);
02bf969d
RB
188
189 if (mkdir(tmppath, 0755) != 0) {
b35d936e 190 PRINT_ERR("mkdir failed with path \"%s\"", tmppath);
02bf969d
RB
191 goto error;
192 }
193 }
194
195 /* Create the symlinks of the test tree */
196 for (i = 0; i < num_tree_symlinks; i++) {
28ab034a 197 snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, tree_symlinks[i].orig);
02bf969d
RB
198
199 if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
28ab034a
JG
200 PRINT_ERR("failed to symlink \"%s\" to \"%s\"",
201 tmppath,
202 tree_symlinks[i].dest);
02bf969d
RB
203 goto error;
204 }
205 }
206
207 return 0;
208
209error:
210 return 1;
211}
212
8dfbc0b4 213static int free_symlink_tree(void)
02bf969d
RB
214{
215 int i;
216 char tmppath[PATH_MAX];
217
218 /* Remove the symlinks from the test tree */
28ab034a
JG
219 for (i = num_tree_symlinks - 1; i > -1; i--) {
220 snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_symlinks[i].orig);
02bf969d
RB
221
222 if (unlink(tmppath) != 0) {
b35d936e 223 PRINT_ERR("failed to unlink \"%s\"", tmppath);
02bf969d
RB
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) {
b35d936e 233 PRINT_ERR("failed to rmdir \"%s\"", tmppath);
02bf969d
RB
234 goto error;
235 }
236 }
237
238 /* Remove the temporary directory */
239 if (rmdir(tree_origin) != 0) {
b35d936e 240 PRINT_ERR("failed to rmdir \"%s\"", tree_origin);
02bf969d
RB
241 goto error;
242 }
243
244 return 0;
245
246error:
247 return 1;
248}
249
cc7f9e36
RB
250static void test_utils_expand_path(void)
251{
252 char *result;
f66e964a
JR
253 char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX];
254 int i, treelen;
cc7f9e36
RB
255
256 /* Test valid cases */
257 for (i = 0; i < num_valid_tests; i++) {
cc7f9e36
RB
258 sprintf(name, "valid test case: %s", valid_tests_inputs[i].input);
259
260 result = utils_expand_path(valid_tests_inputs[i].input);
28ab034a
JG
261 ok(result != NULL && strcmp(result, valid_tests_expected_results[i]) == 0,
262 "%s",
263 name);
cc7f9e36
RB
264
265 free(result);
266 }
267
f66e964a
JR
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
02bf969d 278 /* Test symlink tree cases */
f66e964a 279 treelen = strlen(real_tree_origin) + 1;
02bf969d 280 for (i = 0; i < num_symlink_tests; i++) {
b35d936e
JG
281 int ret;
282
28ab034a
JG
283 sprintf(name,
284 "symlink tree test case: [tmppath/]%s",
285 symlink_tests_inputs[i].input);
02bf969d 286
28ab034a
JG
287 ret = snprintf(tmppath,
288 PATH_MAX,
289 "%s/%s",
290 real_tree_origin,
291 symlink_tests_inputs[i].input);
b35d936e
JG
292 if (ret == -1 || ret >= PATH_MAX) {
293 PRINT_ERR("truncation occurred while concatenating paths \"%s\" and \"%s\"",
28ab034a
JG
294 real_tree_origin,
295 symlink_tests_inputs[i].input);
9f4a25d3 296 fail("%s", name);
b35d936e
JG
297 continue;
298 }
02bf969d 299 result = utils_expand_path(tmppath);
28ab034a
JG
300 ok(result != NULL &&
301 strcmp(result + treelen, symlink_tests_inputs[i].expected_result) == 0,
302 "%s",
303 name);
02bf969d
RB
304
305 free(result);
306 }
307
cc7f9e36
RB
308 /* Test invalid cases */
309 for (i = 0; i < num_invalid_tests; i++) {
8082d471 310 const char *test_input = invalid_tests_inputs[i];
cc7f9e36 311
28ab034a 312 sprintf(name, "invalid test case: %s", test_input ? test_input : "NULL");
8082d471
JG
313
314 result = utils_expand_path(test_input);
cc7f9e36
RB
315 if (result != NULL) {
316 free(result);
317 }
9f4a25d3 318 ok(result == NULL, "%s", name);
cc7f9e36
RB
319 }
320}
321
f46376a1 322int main(void)
cc7f9e36 323{
02bf969d
RB
324 if (prepare_symlink_tree() != 0) {
325 goto error_mkdir;
326 }
327
cc7f9e36 328 if (prepare_valid_results() != 0) {
02bf969d 329 goto error_malloc;
cc7f9e36
RB
330 }
331
02bf969d 332 plan_tests(num_valid_tests + num_invalid_tests + num_symlink_tests);
cc7f9e36
RB
333
334 diag("utils_expand_path tests");
335
336 test_utils_expand_path();
337
338 free_valid_results();
02bf969d
RB
339 free_symlink_tree();
340
cc7f9e36 341 return exit_status();
02bf969d
RB
342
343error_malloc:
344 free_valid_results();
345
346error_mkdir:
347 free_symlink_tree();
348
349 return 1;
cc7f9e36 350}
This page took 0.063655 seconds and 4 git commands to generate.