]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-path-util unit test - small fixes and cleanups
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Tue, 30 May 2017 07:28:43 +0000 (10:28 +0300)
committerGitLab <gitlab@git.dovecot.net>
Sun, 4 Jun 2017 16:29:49 +0000 (19:29 +0300)
Don't use /tmp, especially because on OSX it's a symlink to /private/tmp/,
which causes these tests to fail.

src/lib/test-path-util.c

index 9e7f274e7e962b40252bdb1a629cbdb5352901bf..9c736224dba4c6afb9ba4cf08cf46872a758fa95 100644 (file)
@@ -1,13 +1,16 @@
 /* Copyright (c) 2016 Dovecot authors, see the included COPYING file */
 
+#include "test-lib.h"
+#include "path-util.h"
+#include "unlink-directory.h"
+
 #include <unistd.h>
 #include <stdlib.h>
-#include <libgen.h>
+#include <sys/stat.h>
 
-#include "test-lib.h"
-#include "path-util.h"
+#define TEMP_DIRNAME ".test-path-util"
 
-static char tmpdir[64];
+static const char *tmpdir;
 static const char *cwd;
 static const char *link1;
 static const char *link2;
@@ -68,19 +71,19 @@ static void test_nonexistent_path(void) {
 }
 
 static void test_relative_dotdot() {
-       const char *rel_path = t_strconcat("../", basename(tmpdir), NULL);
+       const char *rel_path = "../"TEMP_DIRNAME;
        const char *npath = NULL, *error = NULL;
        test_assert(t_normpath_to(rel_path, tmpdir, &npath, &error) == 0);
        test_assert_strcmp(npath, tmpdir);
 
        test_assert(t_normpath_to("..", tmpdir, &npath, &error) == 0);
-       test_assert_strcmp(npath, "/tmp");
+       test_assert_strcmp(npath, cwd);
 
        test_assert(t_normpath_to("../", tmpdir, &npath, &error) == 0);
-       test_assert_strcmp(npath, "/tmp");
+       test_assert_strcmp(npath, cwd);
 
        test_assert(t_normpath_to("../.", tmpdir, &npath, &error) == 0);
-       test_assert_strcmp(npath, "/tmp");
+       test_assert_strcmp(npath, cwd);
 }
 
 static void test_link1() {
@@ -106,39 +109,37 @@ static void test_abspath_vs_normpath() {
        test_assert_strcmp(norm, "/bin");
 }
 
+static void test_cleanup(void)
+{
+       const char *error;
+
+       if (unlink_directory(tmpdir, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0)
+               i_error("unlink_directory() failed: %s", error);
+}
+
 static void test_init(void) {
        const char *error;
        test_assert(t_get_working_dir(&cwd, &error) == 0);
-       strcpy(tmpdir, "/tmp/tmpdir.XXXXXX");
-       if (mkdtemp(tmpdir) == NULL) {
-               i_fatal("mkdtemp: %m");
+       tmpdir = t_strconcat(cwd, "/"TEMP_DIRNAME, NULL);
+
+       test_cleanup();
+       if (mkdir(tmpdir, 0700) < 0) {
+               i_fatal("mkdir: %m");
        }
 
        link1 = t_strconcat(tmpdir, "/link1", NULL);
        if (symlink(tmpdir, link1) < 0) {
-               i_fatal("symlink: %m");
+               i_fatal("symlink(%s, %s) failed: %m", tmpdir, link1);
        }
 
        /* link2 and link3 point to each other to create a loop */
        link2 = t_strconcat(tmpdir, "/link2", NULL);
        link3 = t_strconcat(tmpdir, "/link3", NULL);
        if (symlink(link3, link2) < 0) {
-               i_fatal("symlink: %m");
+               i_fatal("symlink(%s, %s) failed: %m", link3, link2);
        }
        if (symlink(link2, link3) < 0) {
-               i_fatal("symlink: %m");
-       }
-}
-
-static void test_deinit(void) {
-       if (unlink(link1) < 0) {
-               i_fatal("unlink: %m");
-       } if (unlink(link2) < 0) {
-               i_fatal("unlink: %m");
-       } if (unlink(link3) < 0) {
-               i_fatal("unlink: %m");
-       } if (rmdir(tmpdir) < 0) {
-               i_fatal("rmdir: %m");
+               i_fatal("symlink(%s, %s) failed: %m", link2, link3);
        }
 }
 
@@ -154,6 +155,6 @@ void test_path_util(void) {
        test_link1();
        test_link_loop();
        test_abspath_vs_normpath();
-       test_deinit();
+       test_cleanup();
        test_end();
 }