test-mail-storage.c revision e3fd3e1a6aac7cba78b147bcdd06eedb34039f3d
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "lib.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "ioloop.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "mkdir-parents.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "unlink-directory.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "hex-binary.h"
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen#include "randgen.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "test-common.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "master-service.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "mail-storage-service.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "mail-storage-private.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic void test_init_storage(struct mail_storage *storage_r)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen{
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen i_zero(storage_r);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen storage_r->user = t_new(struct mail_user, 1);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen storage_r->user->event = event_create(NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen}
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic void test_deinit_storage(struct mail_storage *storage)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen{
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_clear_error(storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if (array_is_created(&storage->error_stack)) {
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen mail_storage_clear_error(storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen i_assert(array_count(&storage->error_stack) == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen array_free(&storage->error_stack);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen }
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen event_unref(&storage->user->event);
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen}
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstruct test_mail_storage_ctx {
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen pool_t pool;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen struct mail_storage_service_ctx *storage_service;
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen struct mail_user *user;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen struct mail_storage_service_user *service_user;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen struct ioloop *ioloop;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen const char *mail_home;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen};
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstatic void test_mail_storage_errors(void)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen{
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct mail_storage storage;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen enum mail_error mail_error;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const char *errstr;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen test_begin("mail storage errors");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_init_storage(&storage);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* try a regular error */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PERM, "error1");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "error1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(!storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* set the error to itself */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_get_last_error(&storage, &mail_error));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "error1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
46b823ac3bce2c0f9f0fc73911e48d3a77b04fbeTimo Sirainen test_assert(!storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* clear the error - asking for it afterwards is a bug */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_clear_error(&storage);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(!storage.last_error_is_internal);
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen /* set internal error in preparation for the next test */
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen test_expect_error_string("critical0");
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen mail_storage_set_critical(&storage, "critical0");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_expect_no_more_errors();
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical0") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* internal error without specifying what it is. this needs to clear
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen the previous internal error. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_internal_error(&storage);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_internal_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(!storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* proper internal error */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_expect_error_string("critical1");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_critical(&storage, "critical1");
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen test_expect_no_more_errors();
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* use it in the following internal error */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_expect_error_string("critical2: critical1");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_critical(&storage, "critical2: %s",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_get_last_internal_error(&storage, &mail_error));
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_expect_no_more_errors();
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical2: critical1") == 0);
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(storage.last_error_is_internal);
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen /* use the previous non-internal error as part of the internal error */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_error_string("critical3: "MAIL_ERRSTR_CRITICAL_MSG);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_critical(&storage, "critical3: %s",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_get_last_error(&storage, &mail_error));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_expect_no_more_errors();
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen errstr = mail_storage_get_last_internal_error(&storage, &mail_error);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strncmp(errstr, "critical3: ", 11) == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(errstr+11, MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(storage.last_error_is_internal);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* clear the error again and check that all is as expected */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_clear_error(&storage);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(!storage.last_error_is_internal);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* use internal error as a regular error (although that really
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen shouldn't be done) */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_error_string("critical4");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_critical(&storage, "critical4");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS,
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_get_last_internal_error(&storage, &mail_error));
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_no_more_errors();
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "critical4") == 0);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical4") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen test_assert(!storage.last_error_is_internal);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_deinit_storage(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_end();
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen}
6adf683655750bcb809275cd65dc75fd12214198Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenstatic void test_mail_storage_last_error_push_pop(void)
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen{
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen struct mail_storage storage;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen enum mail_error mail_error;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_begin("mail_storage_last_error_push/pop()");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_init_storage(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* regular error 1 */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PERM, "regular error 1");
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_last_error_push(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 1 */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_expect_error_string("critical error 1");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_critical(&storage, "critical error 1");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_no_more_errors();
6adf683655750bcb809275cd65dc75fd12214198Timo Sirainen mail_storage_last_error_push(&storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 2 */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS, "regular error 2");
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_last_error_push(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 2 */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_error_string("critical error 2");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_critical(&storage, "critical error 2");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_expect_no_more_errors();
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_last_error_push(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* -- clear all errors -- */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_clear_error(&storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 2 pop */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_last_error_pop(&storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 2") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(storage.last_error_is_internal);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 2 pop */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_last_error_pop(&storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 2") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 2") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(!storage.last_error_is_internal);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* critical error 1 pop */
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen mail_storage_last_error_pop(&storage);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 1") == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(storage.last_error_is_internal);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 1 pop */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_last_error_pop(&storage);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 1") == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 1") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(!storage.last_error_is_internal);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_deinit_storage(&storage);
e20f5920759215f328ce12dcca071b4e7dda3d48Timo Sirainen test_end();
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen}
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenstatic void test_mail_init(struct test_mail_storage_ctx *ctx)
46631c1d903c409444b1b1c4a1d41a033c09ee37Timo Sirainen{
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen const char *error;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen char path_buf[4096];
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen unsigned char rand[4];
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen ctx->pool = pool_allocfree_create("test pool");
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen if (getcwd(path_buf, sizeof(path_buf)) == NULL)
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen i_fatal("getcwd() failed: %m");
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen random_fill(rand, sizeof(rand));
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen ctx->mail_home = p_strdup_printf(ctx->pool, "%s/.test-dir%s/", path_buf,
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen binary_to_hex(rand, sizeof(rand)));
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0 &&
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen errno != ENOENT)
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen i_warning("unlink_directory(%s) failed: %s", ctx->mail_home, error);
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen ctx->ioloop = io_loop_create();
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen ctx->storage_service = mail_storage_service_init(master_service, NULL,
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS |
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT |
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS);
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen}
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainenstatic void test_mail_deinit(struct test_mail_storage_ctx *ctx)
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen{
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen const char *error;
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen mail_storage_service_deinit(&ctx->storage_service);
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if (chdir(ctx->mail_home) < 0)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen i_fatal("chdir(%s) failed: %m", ctx->mail_home);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen if (chdir("..") < 0)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen i_fatal("chdir(..) failed: %m");
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen &error) < 0)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen i_error("unlink_directory(%s) failed: %s", ctx->mail_home, error);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen io_loop_destroy(&ctx->ioloop);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen pool_unref(&ctx->pool);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen i_zero(ctx);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen}
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic int test_mail_init_user(const char *user, const char *driver,
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen const char *driver_opts, const char *sep,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const char *const *extra_input,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct test_mail_storage_ctx *ctx)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen{
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const char *error, *home;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen ARRAY_TYPE(const_string) opts;
da574ef2db96f258d24bc4c89a77833036d13a95Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen home = t_strdup_printf("%s%s", ctx->mail_home, user);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen const char *const default_input[] = {
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen t_strdup_printf("mail=%s:~/%s", driver, driver_opts),
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen "postmaster_address=postmaster@localhost",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen "namespace=inbox",
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen "namespace/inbox/prefix=",
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen "namespace/inbox/inbox=yes",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen t_strdup_printf("namespace/inbox/separator=%s", sep),
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen t_strdup_printf("home=%s/%s", home, user),
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen };
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen i_assert(mkdir_parents(home, S_IRWXU)==0 || errno == EEXIST);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen t_array_init(&opts, 20);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen array_append(&opts, default_input, N_ELEMENTS(default_input));
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen if (extra_input != NULL)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen while(*extra_input != NULL)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen array_append(&opts, extra_input++, 1);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen array_append_zero(&opts);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct mail_storage_service_input input = {
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen .userdb_fields = array_idx(&opts, 0),
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen .username = user,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen .no_userdb_lookup = TRUE,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen .debug = FALSE,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen };
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen if (mail_storage_service_lookup_next(ctx->storage_service, &input,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen &ctx->service_user, &ctx->user,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen &error) < 0) {
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen i_error("mail_storage_service_lookup_next(%s) failed: %s",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen user, error);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen return -1;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen }
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen return 0;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen}
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen#define test_mail_init_maildir_user(user) test_mail_init_user(user,"maildir","",NULL)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainenstatic void test_mail_deinit_user(struct test_mail_storage_ctx *ctx)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen{
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen mail_user_unref(&ctx->user);
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen mail_storage_service_user_unref(&ctx->service_user);
d28179fd78550a58be44dcb1e3e830ab7d33172dTimo Sirainen}
d28179fd78550a58be44dcb1e3e830ab7d33172dTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint main(int argc, char **argv)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen{
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen int ret;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen void (*const tests[])(void) = {
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_mail_storage_errors,
b3f4c31f1533e25380f49f77d5bb1251bf43db2aTimo Sirainen test_mail_storage_last_error_push_pop,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen NULL
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen };
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen master_service = master_service_init("test-mail-storage",
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen MASTER_SERVICE_FLAG_STANDALONE |
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS |
c096257fbdaf4b9fcf8eb97aae94afdbb4e71ed4Timo Sirainen MASTER_SERVICE_FLAG_NO_SSL_INIT |
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen &argc, &argv, "");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen ret = test_run(tests);
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen master_service_deinit(&master_service);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen return ret;
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen}
c096257fbdaf4b9fcf8eb97aae94afdbb4e71ed4Timo Sirainen