tests: make functions static in test_utils_expand_path.c
[lttng-tools.git] / tests / unit / test_utils_expand_path.c
index 65582ff805cee08ab8c1dedeb1bba48df966f54a..e0cd366132b8184b7f4e8e362507cdd3f31660ac 100644 (file)
 
 #include <tap/tap.h>
 
-#include <src/common/utils.h>
+#include <common/utils.h>
+#include <common/common.h>
 
-/* For lttngerr.h */
+/* For error.h */
 int lttng_opt_quiet = 1;
 int lttng_opt_verbose = 3;
+int lttng_opt_mi;
 
 struct valid_test_input {
        char *input;
@@ -51,6 +53,7 @@ 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"    },
@@ -117,31 +120,40 @@ static char *invalid_tests_inputs[] = {
 static const int num_invalid_tests =
                sizeof(invalid_tests_inputs) / sizeof(invalid_tests_inputs[0]);
 
-#define ERRSIZE 100
-char errmsg[ERRSIZE];
-static void printerr(char *msg)
-{
-       fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
-}
+#define PRINT_ERR(fmt, args...)                                                \
+       fprintf(stderr, "test_utils_expand_path: error: " fmt "\n", ## args)
 
-int prepare_valid_results()
+static int prepare_valid_results(void)
 {
        int i;
-       char *relative, *cur_path, *prev_path, *pprev_path, *empty;
+       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 = malloc(sizeof(char *) * num_valid_tests);
+       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) {
-                       printerr("malloc expected results");
-                       return 1;
+                       PRINT_ERR("malloc expected results");
+                       ret = -1;
+                       goto end;
                }
 
                if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
@@ -158,15 +170,16 @@ int prepare_valid_results()
                                "%s%s", relative, valid_tests_inputs[i].absolute_part);
        }
 
+end:
        free(cur_path);
        free(prev_path);
        free(pprev_path);
        free(empty);
 
-       return 0;
+       return ret;
 }
 
-int free_valid_results()
+static int free_valid_results(void)
 {
        int i;
 
@@ -179,37 +192,36 @@ int free_valid_results()
        return 0;
 }
 
-int prepare_symlink_tree()
+static int prepare_symlink_tree(void)
 {
        int i;
-       char tmppath[PATH_MAX];
+       char tmppath[PATH_MAX] = {};
 
        /* Create the temporary directory */
        if (mkdtemp(tree_origin) == NULL) {
-               printerr("mkdtemp");
+               PRINT_ERR("failed to mkdtemp");
                goto error;
        }
 
        /* Create the directories of the test tree */
        for (i = 0; i < num_tree_dirs; i++) {
-               snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
+               snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin,
+                               tree_dirs[i]);
 
                if (mkdir(tmppath, 0755) != 0) {
-                       snprintf(errmsg, ERRSIZE, "mkdir %s", tmppath);
-                       printerr(errmsg);
+                       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, PATH_MAX, "%s/%s",
+               snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                tree_origin, tree_symlinks[i].orig);
 
                if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
-                       snprintf(errmsg, ERRSIZE, "symlink %s to %s",
-                                       tmppath, tree_symlinks[i].dest);
-                       printerr(errmsg);
+                       PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath,
+                                       tree_symlinks[i].dest);
                        goto error;
                }
        }
@@ -220,7 +232,7 @@ error:
        return 1;
 }
 
-int free_symlink_tree()
+static int free_symlink_tree(void)
 {
        int i;
        char tmppath[PATH_MAX];
@@ -231,8 +243,7 @@ int free_symlink_tree()
                                tree_origin, tree_symlinks[i].orig);
 
                if (unlink(tmppath) != 0) {
-                       snprintf(errmsg, ERRSIZE, "unlink %s", tmppath);
-                       printerr(errmsg);
+                       PRINT_ERR("failed to unlink \"%s\"", tmppath);
                        goto error;
                }
        }
@@ -242,16 +253,14 @@ int free_symlink_tree()
                snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
 
                if (rmdir(tmppath) != 0) {
-                       snprintf(errmsg, ERRSIZE, "rmdir %s", tmppath);
-                       printerr(errmsg);
+                       PRINT_ERR("failed to rmdir \"%s\"", tmppath);
                        goto error;
                }
        }
 
        /* Remove the temporary directory */
        if (rmdir(tree_origin) != 0) {
-               snprintf(errmsg, ERRSIZE, "rmdir %s", tree_origin);
-               printerr(errmsg);
+               PRINT_ERR("failed to rmdir \"%s\"", tree_origin);
                goto error;
        }
 
@@ -264,8 +273,8 @@ error:
 static void test_utils_expand_path(void)
 {
        char *result;
-       char name[100], tmppath[PATH_MAX];
-       int i;
+       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++) {
@@ -278,14 +287,34 @@ static void test_utils_expand_path(void)
                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 */
-       int treelen = strlen(tree_origin) + 1;
+       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);
 
-               snprintf(tmppath, PATH_MAX, "%s/%s",
-                               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);
+                       fail(name);
+                       continue;
+               }
                result = utils_expand_path(tmppath);
                ok(result != NULL && strcmp(result + treelen,
                                        symlink_tests_inputs[i].expected_result) == 0, name);
@@ -295,9 +324,12 @@ static void test_utils_expand_path(void)
 
        /* Test invalid cases */
        for (i = 0; i < num_invalid_tests; i++) {
-               sprintf(name, "invalid test case: %s", invalid_tests_inputs[i]);
+               const char *test_input = invalid_tests_inputs[i];
+
+               sprintf(name, "invalid test case: %s", test_input ?
+                               test_input : "NULL");
 
-               result = utils_expand_path(invalid_tests_inputs[i]);
+               result = utils_expand_path(test_input);
                if (result != NULL) {
                        free(result);
                }
This page took 0.026229 seconds and 4 git commands to generate.