From 796beea86bcdc92e4ba2f4865414a951b1717e5c Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 30 May 2017 10:28:43 +0300 Subject: [PATCH] lib: test-path-util unit test - small fixes and cleanups 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 | 55 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/lib/test-path-util.c b/src/lib/test-path-util.c index 9e7f274e7e..9c736224db 100644 --- a/src/lib/test-path-util.c +++ b/src/lib/test-path-util.c @@ -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 #include -#include +#include -#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(); } -- 2.47.3