X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=tests%2Funit%2Ftest_utils_expand_path.cpp;h=428acad2285191827a65232ce6bf1bc6e7e94bf7;hp=3cb1a81604c585ae12fe898ebbb0109deffb8586;hb=HEAD;hpb=c9e313bc594f40a86eed237dce222c0fc99c957f diff --git a/tests/unit/test_utils_expand_path.cpp b/tests/unit/test_utils_expand_path.cpp index 3cb1a8160..8809448ca 100644 --- a/tests/unit/test_utils_expand_path.cpp +++ b/tests/unit/test_utils_expand_path.cpp @@ -5,25 +5,24 @@ * */ -#include +#include +#include +#include + +#include #include #include -#include - +#include #include #include - #include -#include -#include -#include - /* For error.h */ int lttng_opt_quiet = 1; int lttng_opt_verbose = 3; int lttng_opt_mi; +namespace { struct valid_test_input { const char *input; const char *relative_part; @@ -41,89 +40,77 @@ struct symlink_test_input { }; /* 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/..", "", "/" }, +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]); +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[] = { +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", "../../../" }, +const int num_tree_dirs = sizeof(tree_dirs) / sizeof(tree_dirs[0]); + +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]); +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/", "" }, + { "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]); +const int num_symlink_tests = sizeof(symlink_tests_inputs) / sizeof(symlink_tests_inputs[0]); /* Invalid test cases */ -static char *invalid_tests_inputs[] = { - NULL, +char *invalid_tests_inputs[] = { + nullptr, }; -static const int num_invalid_tests = - sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); +const int num_invalid_tests = sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]); +} /* namespace */ -#define PRINT_ERR(fmt, args...) \ - fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args) +#define PRINT_ERR(fmt, args...) fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ##args) -static int prepare_valid_results(void) +static int prepare_valid_results() { int i; - char *relative, *cur_path = NULL, *prev_path = NULL, - *pprev_path = NULL, *empty = NULL; + char *relative, *cur_path = nullptr, *prev_path = nullptr, *pprev_path = nullptr, + *empty = nullptr; int ret = 0; /* Prepare the relative paths */ - cur_path = realpath(".", NULL); - prev_path = realpath("..", NULL); - pprev_path = realpath("../..", NULL); + cur_path = realpath(".", nullptr); + prev_path = realpath("..", nullptr); + pprev_path = realpath("../..", nullptr); empty = strdup(""); if (!cur_path || !prev_path || !pprev_path || !empty) { PRINT_ERR("strdup out of memory"); @@ -132,15 +119,15 @@ static int prepare_valid_results(void) } /* allocate memory for the expected results */ - valid_tests_expected_results = (char **) zmalloc(sizeof(char *) * num_valid_tests); + valid_tests_expected_results = calloc(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] = (char *) malloc(PATH_MAX); - if (valid_tests_expected_results[i] == NULL) { + valid_tests_expected_results[i] = calloc(PATH_MAX); + if (valid_tests_expected_results[i] == nullptr) { PRINT_ERR("malloc expected results"); ret = -1; goto end; @@ -156,8 +143,11 @@ static int prepare_valid_results(void) relative = empty; } - snprintf(valid_tests_expected_results[i], PATH_MAX, - "%s%s", relative, valid_tests_inputs[i].absolute_part); + snprintf(valid_tests_expected_results[i], + PATH_MAX, + "%s%s", + relative, + valid_tests_inputs[i].absolute_part); } end: @@ -169,7 +159,7 @@ end: return ret; } -static int free_valid_results(void) +static int free_valid_results() { int i; @@ -182,21 +172,20 @@ static int free_valid_results(void) return 0; } -static int prepare_symlink_tree(void) +static int prepare_symlink_tree() { int i; char tmppath[PATH_MAX] = {}; /* Create the temporary directory */ - if (mkdtemp(tree_origin) == NULL) { + if (mkdtemp(tree_origin) == nullptr) { 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]); + snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin, tree_dirs[i]); if (mkdir(tmppath, 0755) != 0) { PRINT_ERR("mkdir failed with path \"%s\"", tmppath); @@ -206,12 +195,12 @@ static int prepare_symlink_tree(void) /* 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); + 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); + PRINT_ERR("failed to symlink \"%s\" to \"%s\"", + tmppath, + tree_symlinks[i].dest); goto error; } } @@ -222,15 +211,14 @@ error: return 1; } -static int free_symlink_tree(void) +static int free_symlink_tree() { 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); + 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); @@ -260,7 +248,7 @@ error: return 1; } -static void test_utils_expand_path(void) +static void test_utils_expand_path() { char *result; char name[100], tmppath[PATH_MAX], real_tree_origin[PATH_MAX]; @@ -271,8 +259,9 @@ static void test_utils_expand_path(void) 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, "%s", name); + ok(result != nullptr && strcmp(result, valid_tests_expected_results[i]) == 0, + "%s", + name); free(result); } @@ -292,22 +281,27 @@ static void test_utils_expand_path(void) for (i = 0; i < num_symlink_tests; i++) { int ret; - sprintf(name, "symlink tree test case: [tmppath/]%s", - symlink_tests_inputs[i].input); + 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); + 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); + real_tree_origin, + symlink_tests_inputs[i].input); fail("%s", name); continue; } result = utils_expand_path(tmppath); - ok(result != NULL && strcmp(result + treelen, - symlink_tests_inputs[i].expected_result) == 0, "%s", name); + ok(result != nullptr && + strcmp(result + treelen, symlink_tests_inputs[i].expected_result) == 0, + "%s", + name); free(result); } @@ -316,18 +310,17 @@ static void test_utils_expand_path(void) 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"); + sprintf(name, "invalid test case: %s", test_input ? test_input : "NULL"); result = utils_expand_path(test_input); - if (result != NULL) { + if (result != nullptr) { free(result); } - ok(result == NULL, "%s", name); + ok(result == nullptr, "%s", name); } } -int main(void) +int main() { if (prepare_symlink_tree() != 0) { goto error_mkdir;