Add Unit test to poll compatibility layer
[lttng-tools.git] / tests / unit / test_utils_expand_path.c
index 6b809b88d99b0d724b1ed579ecdef03d2834519f..d047c207dac0885afcbc0d9544bfbd1dd4c7b798 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"    },
@@ -70,6 +73,8 @@ static struct valid_test_input valid_tests_inputs[] = {
        { ".",                          ".",            ""              },
        { "/../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 =
@@ -122,24 +127,37 @@ static void printerr(char *msg)
        fprintf(stderr, "test_utils_expand_path: error: %s\n", msg);
 }
 
-int prepare_valid_results()
+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) {
+               printerr("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) {
+               printerr("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;
+                       ret = -1;
+                       goto end;
                }
 
                if (strcmp(valid_tests_inputs[i].relative_part, ".") == 0) {
@@ -156,15 +174,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()
+int free_valid_results(void)
 {
        int i;
 
@@ -177,7 +196,7 @@ int free_valid_results()
        return 0;
 }
 
-int prepare_symlink_tree()
+int prepare_symlink_tree(void)
 {
        int i;
        char tmppath[PATH_MAX];
@@ -218,7 +237,7 @@ error:
        return 1;
 }
 
-int free_symlink_tree()
+int free_symlink_tree(void)
 {
        int i;
        char tmppath[PATH_MAX];
@@ -262,8 +281,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++) {
@@ -276,14 +295,24 @@ 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++) {
                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);
+                               real_tree_origin, symlink_tests_inputs[i].input);
                result = utils_expand_path(tmppath);
                ok(result != NULL && strcmp(result + treelen,
                                        symlink_tests_inputs[i].expected_result) == 0, name);
@@ -293,9 +322,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.025492 seconds and 4 git commands to generate.