summaryrefslogtreecommitdiffstats
path: root/src/lib-fs/test-fs-posix.c
blob: d6fea11e735b9ff1e37f4d3d76847d59352fa9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "ostream.h"
#include "fs-api.h"
#include "safe-mkdir.h"
#include "safe-mkstemp.h"
#include "test-common.h"
#include "unlink-directory.h"
#include <sys/stat.h>
#include <unistd.h>

static void test_fs_posix(void)
{
	const char testdir[] = ".test-fs-posix";
	const char *unlink_err;

	if (unlink_directory(testdir, UNLINK_DIRECTORY_FLAG_RMDIR, &unlink_err) < 0) {
		i_error("Couldn't prepare test directory (%s): %s", testdir, unlink_err);
		goto error_no_testdir;
	}
	if (safe_mkdir(testdir, 0700, (uid_t)-1, (gid_t)-1) != 1) {
		/* Something just raced us to create this directory, bail. */
		goto error_no_testdir;
	}

	int ret;
	const char *error;
	struct fs *fs;
	struct fs_settings fs_set;

	test_begin("test-fs-posix filesystem");
	i_zero(&fs_set);
	ret = fs_init("posix", t_strdup_printf("prefix=%s/", testdir), &fs_set, &fs, &error);
	test_out_quiet("fs_init() failed", ret >= 0);
	if (ret < 0) {
		test_end();
		goto error_no_fs;
	}

	struct fs *ref = fs;
	fs_ref(ref);
	fs_unref(&ref);
	test_assert(ref == NULL);
	test_assert(fs != NULL);

	test_assert(fs_get_parent(fs) == NULL);
	test_assert_strcmp(fs_get_driver(fs), "posix");
	test_end();

	struct fs_file *file;
	char buf[10];
	ssize_t count;
	test_begin("test-fs-posix bad file read");
	file = fs_file_init(fs, "fail_1", FS_OPEN_MODE_READONLY);
	test_assert(fs_exists(file) == 0);
	count = fs_read(file, buf, 1);
	test_assert(count == -1 && errno == ENOENT &&
		    strstr(fs_file_last_error(file), "No such file or directory") != NULL);
	fs_file_deinit(&file);
	test_end();

	test_begin("test-fs-posix good file write");
	file = fs_file_init(fs, "good1", FS_OPEN_MODE_REPLACE);
	test_assert(file != NULL);
	test_assert(fs_exists(file) == 0); /* file not created until data is written */
	test_assert(fs_write(file, "X", 1) == 0);
	test_assert(fs_exists(file) == 1);
	fs_file_deinit(&file);
	test_end();

	test_begin("test-fs-posix good file read");
	file = fs_file_init(fs, "good1", FS_OPEN_MODE_READONLY);
	test_assert(fs_exists(file) == 1);
	errno = 0;
	count = fs_read(file, buf, 2);
	test_assert(count == 1 && errno == 0);
	fs_file_deinit(&file);
	test_end();

	struct fs_iter *iter = fs_iter_init(fs, "/", 0);
	const char *filename;
	test_begin("test-fs-posix iterator");
	filename = fs_iter_next(iter);
	test_assert_strcmp(filename, "good1");
	test_assert(fs_iter_next(iter) == NULL);
	fs_iter_deinit(&iter, &error);
	test_end();

	struct stat st;
	test_begin("test-fs-posix file stat and delete");
	file = fs_file_init(fs, "good1", FS_OPEN_MODE_READONLY);
	test_assert(fs_stat(file, &st) == 0);
	test_assert(st.st_size == 1);
	test_assert(fs_delete(file) == 0);
	fs_file_deinit(&file);
	test_end();

	test_begin("test-fs-posix file write fname rename");
	file = fs_file_init(fs, "subdir/badfname", FS_OPEN_MODE_REPLACE);
	struct ostream *output = fs_write_stream(file);
	o_stream_nsend_str(output, "hello");
	fs_set_metadata(file, FS_METADATA_WRITE_FNAME, "subdir/rename1");
	test_assert(fs_write_stream_finish(file, &output) == 1);
	test_assert(strcmp(fs_file_path(file), "subdir/rename1") == 0);
	fs_file_deinit(&file);
	file = fs_file_init(fs, "subdir/rename1", FS_OPEN_MODE_READONLY);
	test_assert(fs_stat(file, &st) == 0);
	test_assert(st.st_size == 5);
	fs_file_deinit(&file);
	test_end();

	test_begin("test-fs-posix file copy fname rename");
	struct fs_file *src = fs_file_init(fs, "subdir/rename1", FS_OPEN_MODE_READONLY);
	struct fs_file *dest = fs_file_init(fs, "subdir/badfname", FS_OPEN_MODE_REPLACE);
	fs_set_metadata(dest, FS_METADATA_WRITE_FNAME, "subdir/rename2");
	test_assert(fs_copy(src, dest) == 0);
	test_assert(strcmp(fs_file_path(dest), "subdir/rename2") == 0);
	fs_file_deinit(&src);
	fs_file_deinit(&dest);
	file = fs_file_init(fs, "subdir/rename2", FS_OPEN_MODE_READONLY);
	test_assert(fs_stat(file, &st) == 0);
	test_assert(st.st_size == 5);
	fs_file_deinit(&file);
	test_end();

	fs_deinit(&fs);

error_no_fs:
	if (unlink_directory(testdir, UNLINK_DIRECTORY_FLAG_RMDIR, &unlink_err) < 0)
		i_error("Couldn't clean up test directory (%s): %s", testdir, unlink_err);
error_no_testdir:
	return;
}

int main(void)
{
	static void (*const test_functions[])(void) = {
		test_fs_posix,
		NULL
	};
	return test_run(test_functions);
}