bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#include "lib.h"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#include "str.h"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#include "istream.h"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#include "fs-test.h"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen#include "test-common.h"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainenstatic const struct fs_settings fs_set;
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainenstatic void test_fs_metawrap_stat(void)
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen{
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen struct fs *fs;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen struct fs_file *file;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen struct test_fs_file *test_file;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen struct istream *input;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen struct stat st;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen const char *error;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen unsigned int i;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen test_begin("fs metawrap stat");
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen if (fs_init("metawrap", "test", &fs_set, &fs, &error) < 0)
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen i_fatal("fs_init() failed: %s", error);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen for (i = 0; i < 2; i++) {
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen file = fs_file_init(fs, "foo", FS_OPEN_MODE_READONLY);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen test_file = test_fs_file_get(fs, "foo");
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen str_append(test_file->contents, "key:value\n\n12345678901234567890");
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen if (i == 0) {
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen input = fs_read_stream(file, 2);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen test_istream_set_max_buffer_size(test_file->input, 2);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen } else {
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen input = NULL;
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen test_assert_idx(fs_stat(file, &st) == 0 && st.st_size == 20, i);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
204ee6ed414f5e4eeb6f6c10763b55daf56f11acJosef 'Jeff' Sipek i_stream_unref(&input);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen fs_file_deinit(&file);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen fs_deinit(&fs);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen test_end();
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen}
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainenstatic void test_fs_metawrap_async(void)
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen{
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen test_fs_async("metawrap", FS_PROPERTY_METADATA, "metawrap", "test");
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen test_fs_async("metawrap passthrough", 0, "metawrap", "test");
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen test_fs_async("double-metawrap", FS_PROPERTY_METADATA, "metawrap", "metawrap:test");
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen}
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainenstatic void test_fs_metawrap_write_empty(void)
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen{
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen struct fs *fs;
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen const char *error;
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen test_begin("fs metawrap write empty file");
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen if (fs_init("metawrap", "test", &fs_set, &fs, &error) < 0)
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen i_fatal("fs_init() failed: %s", error);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen struct fs_file *file = fs_file_init(fs, "foo", FS_OPEN_MODE_REPLACE);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen struct ostream *output = fs_write_stream(file);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen test_assert(fs_write_stream_finish(file, &output) > 0);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen fs_file_deinit(&file);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen fs_deinit(&fs);
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen test_end();
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen}
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainenint main(void)
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen test_fs_metawrap_stat,
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen test_fs_metawrap_async,
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen test_fs_metawrap_write_empty,
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen NULL
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen };
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen return test_run(test_functions);
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen}