summaryrefslogtreecommitdiffstats
path: root/src/lib-fs/test-fs-metawrap.c
blob: 161f1c5eeb92d314906011cdb8422e3fa00fb68e (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
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "istream.h"
#include "ostream.h"
#include "fs-test.h"
#include "test-common.h"

static const struct fs_settings fs_set;

static void test_fs_metawrap_stat(void)
{
	struct fs *fs;
	struct fs_file *file;
	struct test_fs_file *test_file;
	struct istream *input;
	struct stat st;
	const char *error;
	unsigned int i;

	test_begin("fs metawrap stat");

	if (fs_init("metawrap", "test", &fs_set, &fs, &error) < 0)
		i_fatal("fs_init() failed: %s", error);

	for (i = 0; i < 2; i++) {
		file = fs_file_init(fs, "foo", FS_OPEN_MODE_READONLY);

		test_file = test_fs_file_get(fs, "foo");
		str_append(test_file->contents, "key:value\n\n12345678901234567890");

		if (i == 0) {
			input = fs_read_stream(file, 2);
			test_istream_set_max_buffer_size(test_file->input, 2);
		} else {
			input = NULL;
		}

		test_assert_idx(fs_stat(file, &st) == 0 && st.st_size == 20, i);

		i_stream_unref(&input);
		fs_file_deinit(&file);
	}
	fs_deinit(&fs);
	test_end();
}

static void test_fs_metawrap_async(void)
{
	test_fs_async("metawrap", FS_PROPERTY_METADATA, "metawrap", "test");
	test_fs_async("metawrap passthrough", 0, "metawrap", "test");
	test_fs_async("double-metawrap", FS_PROPERTY_METADATA, "metawrap", "metawrap:test");
}

static void test_fs_metawrap_write_empty(void)
{
	struct fs *fs;
	struct stat st;
	const char *error;

	test_begin("fs metawrap write empty file");
	if (fs_init("metawrap", "test", &fs_set, &fs, &error) < 0)
		i_fatal("fs_init() failed: %s", error);
	struct fs_file *file = fs_file_init(fs, "foo", FS_OPEN_MODE_REPLACE);
	struct ostream *output = fs_write_stream(file);
	test_assert(fs_write_stream_finish(file, &output) > 0);
	test_assert(fs_stat(file, &st) == 0 && st.st_size == 0);
	fs_file_deinit(&file);
	fs_deinit(&fs);
	test_end();
}

static void test_fs_metawrap_write_fname_rename(void)
{
	struct fs *fs;
	const char *error;

	test_begin("fs metawrap write fname rename");
	if (fs_init("metawrap", "test", &fs_set, &fs, &error) < 0)
		i_fatal("fs_init() failed: %s", error);
	struct fs_file *file = fs_file_init(fs, "foo", FS_OPEN_MODE_REPLACE);
	struct ostream *output = fs_write_stream(file);
	o_stream_nsend_str(output, "test");
	fs_set_metadata(file, FS_METADATA_WRITE_FNAME, "renamed");
	test_assert(fs_write_stream_finish(file, &output) > 0);
	test_assert(strcmp(fs_file_path(file), "renamed") == 0);
	fs_file_deinit(&file);
	fs_deinit(&fs);
	test_end();
}

int main(void)
{
	static void (*const test_functions[])(void) = {
		test_fs_metawrap_stat,
		test_fs_metawrap_async,
		test_fs_metawrap_write_empty,
		test_fs_metawrap_write_fname_rename,
		NULL
	};
	return test_run(test_functions);
}