test-fs-posix.c revision 78717e55d8c4b6528d1afe70505a19e4fcc0a56f
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody/* Copyright (c) 2016-2017 Dovecot authors, see the included COPYING file */
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "lib.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "str.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "fs-api.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "safe-mkdir.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "safe-mkstemp.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "test-common.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include "unlink-directory.h"
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include <sys/stat.h>
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody#include <unistd.h>
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmodystatic void test_fs_posix(void)
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody{
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody const char testdir[] = ".test-fs-posix";
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody const char *unlink_err;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody if (unlink_directory(testdir, UNLINK_DIRECTORY_FLAG_RMDIR, &unlink_err) < 0 &&
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody errno != ENOENT) {
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody i_error("Couldn't prepare test directory (%s): %s", testdir, unlink_err);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody goto error_no_testdir;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody }
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody if (safe_mkdir(testdir, 0700, (uid_t)-1, (gid_t)-1) != 1) {
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody /* Something just raced us to create this directory, bail. */
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody goto error_no_testdir;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody }
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody int ret;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody const char *error;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct fs *fs;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct fs_settings fs_set;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix filesystem");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody i_zero(&fs_set);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody ret = fs_init("posix", t_strdup_printf("prefix=%s/", testdir), &fs_set, &fs, &error);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_out_quiet("fs_init() failed", ret >= 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody if (ret < 0) {
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody goto error_no_fs;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody }
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct fs *ref = fs;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_ref(ref);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_unref(&ref);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(ref == NULL);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs != NULL);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_get_parent(fs) == NULL);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert_strcmp(fs_get_driver(fs), "posix");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct fs_file *file;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody char buf[10];
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody ssize_t count;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix bad file read");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody file = fs_file_init(fs, "fail_1", FS_OPEN_MODE_READONLY);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_exists(file) == 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody count = fs_read(file, buf, 1);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(count == -1 && errno == ENOENT);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_file_deinit(&file);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix good file write");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody file = fs_file_init(fs, "good1", FS_OPEN_MODE_REPLACE);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(file != NULL);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_exists(file) == 0); /* file not created until data is written */
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_write(file, "X", 1) == 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_exists(file) == 1);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_file_deinit(&file);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix good file read");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody file = fs_file_init(fs, "good1", FS_OPEN_MODE_READONLY);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_exists(file) == 1);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody errno = 0;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody count = fs_read(file, buf, 2);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(count == 1 && errno == 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_file_deinit(&file);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct fs_iter *iter = fs_iter_init(fs, "/", 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody const char *filename;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix iterator");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody filename = fs_iter_next(iter);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert_strcmp(filename, "good1");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_iter_next(iter) == NULL);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_iter_deinit(&iter);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody struct stat st;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_begin("test-fs-posix file stat and delete");
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody file = fs_file_init(fs, "good1", FS_OPEN_MODE_READONLY);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_stat(file, &st) == 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(st.st_size == 1);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_assert(fs_delete(file) == 0);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_file_deinit(&file);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_end();
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody fs_deinit(&fs);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmodyerror_no_fs:
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody if (unlink_directory(testdir, UNLINK_DIRECTORY_FLAG_RMDIR, &unlink_err) != 0)
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody i_error("Couldn't clean up test directory (%s): %s", testdir, unlink_err);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmodyerror_no_testdir:
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody return;
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody}
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmodyint main(void)
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody{
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody static void (*const test_functions[])(void) = {
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody test_fs_posix,
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody NULL
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody };
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody return test_run(test_functions);
78717e55d8c4b6528d1afe70505a19e4fcc0a56fPhil Carmody}