changeset 9457:4dca587d5b75 HEAD

Added libstorage-test.a for unit testing lib-storage users.
author Timo Sirainen <tss@iki.fi>
date Thu, 04 Jun 2009 20:20:49 -0400
parents 63f4a2825bbe
children 19fc2147cc10
files src/lib-storage/Makefile.am src/lib-storage/test-mail-storage.c src/lib-storage/test-mail.c src/lib-storage/test-mailbox.c
diffstat 4 files changed, 647 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/Makefile.am	Thu Jun 04 20:14:52 2009 -0400
+++ b/src/lib-storage/Makefile.am	Thu Jun 04 20:20:49 2009 -0400
@@ -1,6 +1,7 @@
 SUBDIRS = list index register
 
 noinst_LTLIBRARIES = libstorage.la libstorage_service.la
+noinst_LIBRARIES = libstorage_test.a
 
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
@@ -65,6 +66,11 @@
 libdovecot_storage_la_DEPENDENCIES = $(shlibs)
 libdovecot_storage_la_LDFLAGS = -export-dynamic
 
+libstorage_test_a_SOURCES = \
+	test-mail-storage.c \
+	test-mailbox.c \
+	test-mail.c
+
 if INSTALL_HEADERS
   pkginc_libdir=$(pkgincludedir)
   pkginc_lib_HEADERS = $(headers)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/test-mail-storage.c	Thu Jun 04 20:20:49 2009 -0400
@@ -0,0 +1,73 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "mail-storage-private.h"
+#include "test-mail-storage.h"
+
+extern struct mail_storage test_storage;
+struct mail_index_module_register mail_index_module_register = { 0 };
+
+static struct mail_storage *test_storage_alloc(void)
+{
+	struct mail_storage *storage;
+	pool_t pool;
+
+	pool = pool_alloconly_create("test mail storage", 1024);
+	storage = p_new(pool, struct mail_storage, 1);
+	*storage = test_storage;
+	storage->pool = pool;
+	return storage;
+}
+
+static void
+test_storage_get_list_settings(const struct mail_namespace *ns ATTR_UNUSED,
+			      struct mailbox_list_settings *set)
+{
+	if (set->layout == NULL)
+		set->layout = "test";
+	if (set->subscription_fname == NULL)
+		set->subscription_fname = "subscriptions";
+}
+
+static int
+test_mailbox_create(struct mail_storage *storage,
+		    struct mailbox_list *list ATTR_UNUSED,
+		    const char *name ATTR_UNUSED,
+		    bool directory ATTR_UNUSED)
+{
+	mail_storage_set_error(storage, MAIL_ERROR_NOTPOSSIBLE,
+			       "Test mailbox creation isn't supported");
+	return -1;
+}
+
+struct mail_storage test_storage = {
+	MEMBER(name) "test",
+	MEMBER(class_flags) 0,
+
+	{
+		NULL,
+		NULL,
+		NULL,
+		test_storage_alloc,
+		NULL,
+		NULL,
+		NULL,
+		test_storage_get_list_settings,
+		NULL,
+		test_mailbox_open,
+		test_mailbox_create,
+		NULL
+	}
+};
+
+struct mail_storage *test_mail_storage_create(void)
+{
+	struct mail_storage *storage;
+
+	storage = test_storage_alloc();
+	storage->refcount = 1;
+	storage->storage_class = &test_storage;
+	p_array_init(&storage->module_contexts, storage->pool, 5);
+	return storage;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/test-mail.c	Thu Jun 04 20:20:49 2009 -0400
@@ -0,0 +1,227 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "mail-storage-private.h"
+#include "test-mail-storage.h"
+
+extern struct mail_vfuncs test_mail_vfuncs;
+
+struct mail *
+test_mailbox_mail_alloc(struct mailbox_transaction_context *t,
+			enum mail_fetch_field wanted_fields ATTR_UNUSED,
+			struct mailbox_header_lookup_ctx *wanted_headers ATTR_UNUSED)
+{
+	struct mail_private *mail;
+	pool_t pool;
+
+	pool = pool_alloconly_create("test mail", 1024);
+	mail = p_new(pool, struct mail_private, 1);
+	mail->mail.box = t->box;
+	mail->mail.transaction = t;
+	mail->v = test_mail_vfuncs;
+	mail->pool = pool;
+	p_array_init(&mail->module_contexts, pool, 5);
+	return &mail->mail;
+}
+
+static void test_mail_free(struct mail *mail)
+{
+	struct mail_private *pmail = (struct mail_private *)mail;
+
+	pool_unref(&pmail->pool);
+}
+
+static void test_mail_set_seq(struct mail *mail, uint32_t seq)
+{
+	mail->seq = seq;
+	mail->uid = seq;
+
+	mail->expunged = TRUE;
+	mail->has_nuls = FALSE;
+	mail->has_no_nuls = FALSE;
+}
+
+static bool test_mail_set_uid(struct mail *mail, uint32_t uid)
+{
+	test_mail_set_seq(mail, uid);
+	return TRUE;
+}
+
+static void test_mail_set_uid_cache_updates(struct mail *mail ATTR_UNUSED,
+					    bool set ATTR_UNUSED)
+{
+}
+
+static enum mail_flags test_mail_get_flags(struct mail *mail ATTR_UNUSED)
+{
+	return 0;
+}
+
+static const char *const *
+test_mail_get_keywords(struct mail *mail ATTR_UNUSED)
+{
+	return t_new(const char *, 1);
+}
+
+static const ARRAY_TYPE(keyword_indexes) *
+test_mail_get_keyword_indexes(struct mail *mail ATTR_UNUSED)
+{
+	ARRAY_TYPE(keyword_indexes) *kw_indexes;
+
+	kw_indexes = t_new(ARRAY_TYPE(keyword_indexes), 1);
+	t_array_init(kw_indexes, 1);
+	(void)array_append_space(kw_indexes);
+	return kw_indexes;
+}
+
+static uint64_t test_mail_get_modseq(struct mail *mail ATTR_UNUSED)
+{
+	return 0;
+}
+
+static int
+test_mail_get_parts(struct mail *mail ATTR_UNUSED,
+		    const struct message_part **parts_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_date(struct mail *mail ATTR_UNUSED,
+		   time_t *date_r ATTR_UNUSED, int *timezone_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_received_date(struct mail *mail ATTR_UNUSED,
+			    time_t *date_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_save_date(struct mail *mail ATTR_UNUSED,
+			time_t *date_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_test_mail_size(struct mail *mail ATTR_UNUSED,
+			     uoff_t *size_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_physical_size(struct mail *mail ATTR_UNUSED,
+			    uoff_t *size_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_first_header(struct mail *mail ATTR_UNUSED,
+			   const char *field ATTR_UNUSED,
+			   bool decode_to_utf8 ATTR_UNUSED,
+			   const char **value_r)
+{
+	*value_r = NULL;
+	return 0;
+}
+
+static int
+test_mail_get_headers(struct mail *mail ATTR_UNUSED,
+		      const char *field ATTR_UNUSED,
+		      bool decode_to_utf8 ATTR_UNUSED,
+		      const char *const **value_r)
+{
+	*value_r = NULL;
+	return 0;
+}
+
+static int
+test_mail_get_header_stream(struct mail *mail ATTR_UNUSED,
+			    struct mailbox_header_lookup_ctx *headers ATTR_UNUSED,
+			    struct istream **stream_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_stream(struct mail *mail ATTR_UNUSED,
+		     struct message_size *hdr_size ATTR_UNUSED,
+		     struct message_size *body_size ATTR_UNUSED,
+		     struct istream **stream_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mail_get_special(struct mail *mail ATTR_UNUSED,
+		      enum mail_fetch_field field ATTR_UNUSED,
+		      const char **value_r ATTR_UNUSED)
+{
+	return -1;
+}
+
+static void
+test_mail_update_flags(struct mail *mail ATTR_UNUSED,
+		       enum modify_type modify_type ATTR_UNUSED,
+		       enum mail_flags flags ATTR_UNUSED)
+{
+}
+
+static void
+test_mail_update_keywords(struct mail *mail ATTR_UNUSED,
+			  enum modify_type modify_type ATTR_UNUSED,
+			  struct mail_keywords *keywords ATTR_UNUSED)
+{
+}
+
+static void test_mail_expunge(struct mail *mail ATTR_UNUSED)
+{
+}
+
+static void
+test_mail_set_cache_corrupted(struct mail *mail ATTR_UNUSED,
+			      enum mail_fetch_field field ATTR_UNUSED)
+{
+}
+
+static struct index_mail *
+test_mail_get_index_mail(struct mail *mail ATTR_UNUSED)
+{
+	return NULL;
+}
+
+struct mail_vfuncs test_mail_vfuncs = {
+	NULL,
+	test_mail_free,
+	test_mail_set_seq,
+	test_mail_set_uid,
+	test_mail_set_uid_cache_updates,
+
+	test_mail_get_flags,
+	test_mail_get_keywords,
+	test_mail_get_keyword_indexes,
+	test_mail_get_modseq,
+	test_mail_get_parts,
+	test_mail_get_date,
+	test_mail_get_received_date,
+	test_mail_get_save_date,
+	test_mail_get_test_mail_size,
+	test_mail_get_physical_size,
+	test_mail_get_first_header,
+	test_mail_get_headers,
+	test_mail_get_header_stream,
+	test_mail_get_stream,
+	test_mail_get_special,
+	test_mail_update_flags,
+	test_mail_update_keywords,
+	test_mail_expunge,
+	test_mail_set_cache_corrupted,
+	test_mail_get_index_mail
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/test-mailbox.c	Thu Jun 04 20:20:49 2009 -0400
@@ -0,0 +1,341 @@
+/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "mail-storage-private.h"
+#include "test-mail-storage.h"
+
+#define TEST_UID_VALIDITY 1
+
+static bool test_mailbox_is_readonly(struct mailbox *box ATTR_UNUSED)
+{
+	return FALSE;
+}
+
+static bool test_mailbox_allow_new_keywords(struct mailbox *box ATTR_UNUSED)
+{
+	return TRUE;
+}
+
+static int test_mailbox_enable(struct mailbox *box,
+			       enum mailbox_feature features)
+{
+	box->enabled_features |= features;
+	return 0;
+}
+
+static int test_mailbox_close(struct mailbox *box ATTR_UNUSED)
+{
+	return 0;
+}
+
+static void test_mailbox_get_status(struct mailbox *box ATTR_UNUSED,
+				    enum mailbox_status_items items ATTR_UNUSED,
+				    struct mailbox_status *status_r)
+{
+	memset(status_r, 0, sizeof(*status_r));
+	status_r->uidvalidity = TEST_UID_VALIDITY;
+	status_r->uidnext = 1;
+}
+
+static struct mailbox_sync_context *
+test_mailbox_sync_init(struct mailbox *box,
+		       enum mailbox_sync_flags flags ATTR_UNUSED)
+{
+	struct mailbox_sync_context *ctx;
+
+	ctx = i_new(struct mailbox_sync_context, 1);
+	ctx->box = box;
+	return ctx;
+}
+
+static bool
+test_mailbox_sync_next(struct mailbox_sync_context *ctx ATTR_UNUSED,
+		       struct mailbox_sync_rec *sync_rec_r ATTR_UNUSED)
+{
+	return FALSE;
+}
+
+static int
+test_mailbox_sync_deinit(struct mailbox_sync_context *ctx,
+			 enum mailbox_status_items status_items,
+			 struct mailbox_status *status_r)
+{
+	test_mailbox_get_status(ctx->box, status_items, status_r);
+	i_free(ctx);
+	return 0;
+}
+
+static void test_mailbox_notify_changes(struct mailbox *box ATTR_UNUSED)
+{
+}
+
+static struct mailbox_transaction_context *
+test_mailbox_transaction_begin(struct mailbox *box,
+			       enum mailbox_transaction_flags flags)
+{
+	struct mailbox_transaction_context *ctx;
+
+	ctx = i_new(struct mailbox_transaction_context, 1);
+	ctx->box = box;
+	ctx->flags = flags;
+	i_array_init(&ctx->module_contexts, 5);
+	return ctx;
+}
+
+static void
+test_mailbox_transaction_rollback(struct mailbox_transaction_context *t)
+{
+	array_free(&t->module_contexts);
+	i_free(t);
+}
+
+static int
+test_mailbox_transaction_commit(struct mailbox_transaction_context *t,
+				uint32_t *uid_validity_r,
+				uint32_t *first_saved_uid_r,
+				uint32_t *last_saved_uid_r)
+{
+	*uid_validity_r = TEST_UID_VALIDITY;
+	*first_saved_uid_r = *last_saved_uid_r = 0;
+	test_mailbox_transaction_rollback(t);
+	return 0;
+}
+
+static void
+test_mailbox_transaction_set_max_modseq(struct mailbox_transaction_context *t ATTR_UNUSED,
+					uint64_t max_modseq ATTR_UNUSED,
+					ARRAY_TYPE(seq_range) *seqs ATTR_UNUSED)
+{
+}
+
+static int
+test_mailbox_keywords_create(struct mailbox *box ATTR_UNUSED,
+			     const char *const keywords[] ATTR_UNUSED,
+			     struct mail_keywords **keywords_r,
+			     bool skip_invalid ATTR_UNUSED)
+{
+	*keywords_r = i_new(struct mail_keywords, 1);
+	return 0;
+}
+
+static struct mail_keywords *
+test_mailbox_keywords_create_from_indexes(struct mailbox *box ATTR_UNUSED,
+					  const ARRAY_TYPE(keyword_indexes) *idx ATTR_UNUSED)
+{
+	return i_new(struct mail_keywords, 1);
+}
+
+static void test_mailbox_keywords_free(struct mail_keywords *keywords)
+{
+	i_free(keywords);
+}
+
+static bool
+test_mailbox_keyword_is_valid(struct mailbox *box ATTR_UNUSED,
+			      const char *keyword ATTR_UNUSED,
+			      const char **error_r ATTR_UNUSED)
+{
+	return TRUE;
+}
+
+static void
+test_mailbox_get_seq_range(struct mailbox *box ATTR_UNUSED,
+			   uint32_t uid1, uint32_t uid2,
+			   uint32_t *seq1_r, uint32_t *seq2_r)
+{
+	*seq1_r = uid1;
+	*seq2_r = uid2;
+}
+
+static void
+test_mailbox_get_uid_range(struct mailbox *box ATTR_UNUSED,
+			   const ARRAY_TYPE(seq_range) *seqs,
+			   ARRAY_TYPE(seq_range) *uids)
+{
+	array_append_array(uids, seqs);
+}
+
+static bool
+test_mailbox_get_expunged_uids(struct mailbox *box ATTR_UNUSED,
+			       uint64_t modseq ATTR_UNUSED,
+			       const ARRAY_TYPE(seq_range) *uids ATTR_UNUSED,
+			       ARRAY_TYPE(seq_range) *expunged_uids ATTR_UNUSED)
+{
+	return FALSE;
+}
+
+static struct mailbox_header_lookup_ctx *
+test_mailbox_header_lookup_init(struct mailbox *box,
+				const char *const headers[])
+{
+	struct mailbox_header_lookup_ctx *ctx;
+
+	ctx = i_new(struct mailbox_header_lookup_ctx, 1);
+	ctx->box = box;
+	ctx->headers = headers; /* now exactly right, but .. */
+	return ctx;
+}
+
+static void
+test_mailbox_header_lookup_deinit(struct mailbox_header_lookup_ctx *ctx)
+{
+	i_free(ctx);
+}
+
+static struct mail_search_context *
+test_mailbox_search_init(struct mailbox_transaction_context *t,
+			 struct mail_search_args *args,
+			 const enum mail_sort_type *sort_program ATTR_UNUSED)
+{
+	struct mail_search_context *ctx;
+
+	ctx = i_new(struct mail_search_context, 1);
+	ctx->transaction = t;
+	ctx->args = args;
+
+	i_array_init(&ctx->results, 5);
+	i_array_init(&ctx->module_contexts, 5);
+	return ctx;
+}
+
+static int test_mailbox_search_deinit(struct mail_search_context *ctx)
+{
+	array_free(&ctx->results);
+	array_free(&ctx->module_contexts);
+	i_free(ctx);
+	return 0;
+}
+
+static int
+test_mailbox_search_next_nonblock(struct mail_search_context *ctx ATTR_UNUSED,
+				  struct mail *mail ATTR_UNUSED,
+				  bool *tryagain_r)
+{
+	*tryagain_r = FALSE;
+	return 0;
+}
+
+static bool
+test_mailbox_search_next_update_seq(struct mail_search_context *ctx ATTR_UNUSED)
+{
+	return FALSE;
+}
+
+static struct mail_save_context *
+test_mailbox_save_alloc(struct mailbox_transaction_context *t)
+{
+	struct mail_save_context *ctx;
+
+	ctx = i_new(struct mail_save_context, 1);
+	ctx->transaction = t;
+	return ctx;
+}
+
+static int
+test_mailbox_save_begin(struct mail_save_context *ctx ATTR_UNUSED,
+			struct istream *input ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mailbox_save_continue(struct mail_save_context *ctx ATTR_UNUSED)
+{
+	return -1;
+}
+
+static int
+test_mailbox_save_finish(struct mail_save_context *ctx ATTR_UNUSED)
+{
+	return -1;
+}
+
+static void
+test_mailbox_save_cancel(struct mail_save_context *ctx ATTR_UNUSED)
+{
+}
+
+static int
+test_mailbox_copy(struct mail_save_context *ctx ATTR_UNUSED,
+		  struct mail *mail ATTR_UNUSED)
+{
+	return -1;
+}
+
+static bool test_mailbox_is_inconsistent(struct mailbox *box ATTR_UNUSED)
+{
+	return FALSE;
+}
+
+struct mailbox test_mailbox = {
+	MEMBER(name) NULL,
+	MEMBER(storage) NULL,
+	MEMBER(list) NULL,
+
+	{
+		test_mailbox_is_readonly,
+		test_mailbox_allow_new_keywords,
+		test_mailbox_enable,
+		test_mailbox_close,
+		test_mailbox_get_status,
+		NULL,
+		NULL,
+		test_mailbox_sync_init,
+		test_mailbox_sync_next,
+		test_mailbox_sync_deinit,
+		NULL,
+		test_mailbox_notify_changes,
+		test_mailbox_transaction_begin,
+		test_mailbox_transaction_commit,
+		test_mailbox_transaction_rollback,
+		test_mailbox_transaction_set_max_modseq,
+		test_mailbox_keywords_create,
+		test_mailbox_keywords_create_from_indexes,
+		test_mailbox_keywords_free,
+		test_mailbox_keyword_is_valid,
+		test_mailbox_get_seq_range,
+		test_mailbox_get_uid_range,
+		test_mailbox_get_expunged_uids,
+		NULL,
+		NULL,
+		NULL,
+		test_mailbox_mail_alloc,
+		test_mailbox_header_lookup_init,
+		test_mailbox_header_lookup_deinit,
+		test_mailbox_search_init,
+		test_mailbox_search_deinit,
+		test_mailbox_search_next_nonblock,
+		test_mailbox_search_next_update_seq,
+		test_mailbox_save_alloc,
+		test_mailbox_save_begin,
+		test_mailbox_save_continue,
+		test_mailbox_save_finish,
+		test_mailbox_save_cancel,
+		test_mailbox_copy,
+		test_mailbox_is_inconsistent
+	}
+};
+
+struct mailbox *
+test_mailbox_open(struct mail_storage *storage, struct mailbox_list *list,
+		  const char *name, struct istream *input ATTR_UNUSED,
+		  enum mailbox_open_flags flags)
+{
+	struct mailbox *box;
+	pool_t pool;
+
+	pool = pool_alloconly_create("test mailbox", 1024);
+	box = p_new(pool, struct mailbox, 1);
+	*box = test_mailbox;
+	box->name = p_strdup(pool, name);
+	box->storage = storage;
+	box->list = list;
+
+	box->pool = pool;
+	box->open_flags = flags;
+
+	p_array_init(&box->search_results, pool, 16);
+	p_array_init(&box->module_contexts, pool, 5);
+	return box;
+}