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