/* 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;
}
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() {
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);
}
}
test_link1();
test_link_loop();
test_abspath_vs_normpath();
- test_deinit();
+ test_cleanup();
test_end();
}