bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen#include "lib.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "ioloop.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "mkdir-parents.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "unlink-directory.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "hex-binary.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "randgen.h"
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen#include "test-common.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "master-service.h"
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#include "mail-storage-service.h"
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen#include "mail-storage-private.h"
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainenstatic void test_init_storage(struct mail_storage *storage_r)
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen{
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen i_zero(storage_r);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen storage_r->user = t_new(struct mail_user, 1);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen storage_r->user->event = event_create(NULL);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen}
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainenstatic void test_deinit_storage(struct mail_storage *storage)
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen{
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen mail_storage_clear_error(storage);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen if (array_is_created(&storage->error_stack)) {
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen mail_storage_clear_error(storage);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen i_assert(array_count(&storage->error_stack) == 0);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen array_free(&storage->error_stack);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen }
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen event_unref(&storage->user->event);
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen}
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomistruct test_mail_storage_ctx {
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi pool_t pool;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct mail_storage_service_ctx *storage_service;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct mail_user *user;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct mail_storage_service_user *service_user;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct ioloop *ioloop;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *mail_home;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi};
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainenstatic void test_mail_storage_errors(void)
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen{
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen struct mail_storage storage;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen enum mail_error mail_error;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen const char *errstr;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_begin("mail storage errors");
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen test_init_storage(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* try a regular error */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PERM, "error1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "error1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* set the error to itself */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS,
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_get_last_error(&storage, &mail_error));
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "error1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* clear the error - asking for it afterwards is a bug */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_clear_error(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen /* set internal error in preparation for the next test */
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_expect_error_string("critical0");
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen mail_storage_set_critical(&storage, "critical0");
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_expect_no_more_errors();
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical0") == 0);
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen test_assert(storage.last_error_is_internal);
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen /* internal error without specifying what it is. this needs to clear
fe7a56906e8bdf6e42cc981b4a62e31bac4a30f4Timo Sirainen the previous internal error. */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_internal_error(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_internal_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* proper internal error */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* use it in the following internal error */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical2: critical1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical2: %s",
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_get_last_internal_error(&storage, &mail_error));
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical2: critical1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* use the previous non-internal error as part of the internal error */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical3: "MAIL_ERRSTR_CRITICAL_MSG);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical3: %s",
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_get_last_error(&storage, &mail_error));
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen errstr = mail_storage_get_last_internal_error(&storage, &mail_error);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strncmp(errstr, "critical3: ", 11) == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(errstr+11, MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* clear the error again and check that all is as expected */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_clear_error(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* use internal error as a regular error (although that really
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen shouldn't be done) */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical4");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical4");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS,
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_get_last_internal_error(&storage, &mail_error));
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "critical4") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical4") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen test_deinit_storage(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_end();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen}
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainenstatic void test_mail_storage_last_error_push_pop(void)
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen{
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen struct mail_storage storage;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen enum mail_error mail_error;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_begin("mail_storage_last_error_push/pop()");
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen test_init_storage(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* regular error 1 */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PERM, "regular error 1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_push(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* critical error 1 */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical error 1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical error 1");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_push(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* regular error 2 */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS, "regular error 2");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_push(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* critical error 2 */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_error_string("critical error 2");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_set_critical(&storage, "critical error 2");
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_expect_no_more_errors();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_push(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* -- clear all errors -- */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_clear_error(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* critical error 2 pop */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_pop(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 2") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* regular error 2 pop */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_pop(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 2") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 2") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PARAMS);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* critical error 1 pop */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_pop(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_TEMP);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen /* regular error 1 pop */
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen mail_storage_last_error_pop(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 1") == 0);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(mail_error == MAIL_ERROR_PERM);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_assert(!storage.last_error_is_internal);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
6610dc505a7053e6d52e79cc865e845f44901f4cTimo Sirainen test_deinit_storage(&storage);
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_end();
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen}
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomistatic void test_mail_init(struct test_mail_storage_ctx *ctx)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi{
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *error;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi char path_buf[4096];
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi unsigned char rand[4];
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ctx->pool = pool_allocfree_create("test pool");
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (getcwd(path_buf, sizeof(path_buf)) == NULL)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_fatal("getcwd() failed: %m");
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi random_fill(rand, sizeof(rand));
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ctx->mail_home = p_strdup_printf(ctx->pool, "%s/.test-dir%s/", path_buf,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi binary_to_hex(rand, sizeof(rand)));
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0 &&
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi errno != ENOENT)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_warning("unlink_directory(%s) failed: %s", ctx->mail_home, error);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ctx->ioloop = io_loop_create();
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ctx->storage_service = mail_storage_service_init(master_service, NULL,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS |
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_LOG_INIT |
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MAIL_STORAGE_SERVICE_FLAG_NO_PLUGINS);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi}
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomistatic void test_mail_deinit(struct test_mail_storage_ctx *ctx)
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen{
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *error;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi mail_storage_service_deinit(&ctx->storage_service);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (chdir(ctx->mail_home) < 0)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_fatal("chdir(%s) failed: %m", ctx->mail_home);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (chdir("..") < 0)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_fatal("chdir(..) failed: %m");
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi &error) < 0)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_error("unlink_directory(%s) failed: %s", ctx->mail_home, error);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi io_loop_destroy(&ctx->ioloop);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi pool_unref(&ctx->pool);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_zero(ctx);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi}
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomistatic int test_mail_init_user(const char *user, const char *driver,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *driver_opts, const char *sep,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *const *extra_input,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct test_mail_storage_ctx *ctx)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi{
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *error, *home;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ARRAY_TYPE(const_string) opts;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi home = t_strdup_printf("%s%s", ctx->mail_home, user);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi const char *const default_input[] = {
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi t_strdup_printf("mail=%s:~/%s", driver, driver_opts),
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi "postmaster_address=postmaster@localhost",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi "namespace=inbox",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi "namespace/inbox/prefix=",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi "namespace/inbox/inbox=yes",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi t_strdup_printf("namespace/inbox/separator=%s", sep),
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi t_strdup_printf("home=%s/%s", home, user),
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi };
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (unlink_directory(home, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_error("%s", error);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_assert(mkdir_parents(home, S_IRWXU)==0 || errno == EEXIST);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi t_array_init(&opts, 20);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi array_append(&opts, default_input, N_ELEMENTS(default_input));
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (extra_input != NULL)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi while(*extra_input != NULL)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi array_append(&opts, extra_input++, 1);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi array_append_zero(&opts);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi struct mail_storage_service_input input = {
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi .userdb_fields = array_idx(&opts, 0),
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi .username = user,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi .no_userdb_lookup = TRUE,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi .debug = FALSE,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi };
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi if (mail_storage_service_lookup_next(ctx->storage_service, &input,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi &ctx->service_user, &ctx->user,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi &error) < 0) {
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi i_error("mail_storage_service_lookup_next(%s) failed: %s",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi user, error);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi return -1;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi }
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi return 0;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi}
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi#define test_mail_init_maildir_user(user) test_mail_init_user(user,"maildir","",NULL)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomistatic void test_mail_deinit_user(struct test_mail_storage_ctx *ctx)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi{
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi mail_user_unref(&ctx->user);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi mail_storage_service_user_unref(&ctx->service_user);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi}
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistruct mailbox_verify_test_cases {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi char ns_sep;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi char list_sep;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *box;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi int ret;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi} test_cases[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', ".DUMPSTER", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "DUMPSTER", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "~DUMPSTER", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '.', "INBOX/INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', "INBOX/INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "INBOX/INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "INBOX/INBOX", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "/etc/passwd", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '.', "foo.bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "/foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "foo/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "foo//bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "/foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo//bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', ".foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo.", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo..bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', ".foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo.", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo..bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', ".", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi};
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomitest_mailbox_verify_name_one(struct mailbox_verify_test_cases *test_case,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mail_namespace *ns,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi size_t i)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mailbox *box;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi int ret;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi box = mailbox_alloc(ns->list, test_case->box, 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ret = mailbox_verify_name(box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#ifdef DEBUG
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret != test_case->ret) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("%c == %c %c == %c",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case->ns_sep, mail_namespace_get_sep(ns),
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case->list_sep, mailbox_list_get_hierarchy_sep(ns->list));
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *error = "should have failed";
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi error = mailbox_get_last_error(box, NULL);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("Failed test for mailbox %s: %s", test_case->box, error);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#endif
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_assert_idx(ret == test_case->ret, i);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi /* Cannot rename to INBOX */
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (strcmp(test_case->box, "INBOX") == 0) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ret = mailbox_create(box, NULL, FALSE);
c415d35f8f49a7a34ac42d9ebe4616f857459d65Timo Sirainen test_assert_idx(ret == 0, i);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_delete(box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi return;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mailbox *src = mailbox_alloc(ns->list, "RENAME", 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi enum mailbox_existence exists;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi /* check if the mailbox exists */
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ret = mailbox_exists(src, FALSE, &exists);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_assert_idx(ret == 0, i);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret != 0) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&src);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi return;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (exists == MAILBOX_EXISTENCE_NONE)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi (void)mailbox_create(src, NULL, FALSE);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ret = mailbox_rename(src, box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi #ifdef DEBUG
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret != test_case->ret) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("%c == %c %c == %c",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case->ns_sep, mail_namespace_get_sep(ns),
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case->list_sep, mailbox_list_get_hierarchy_sep(ns->list));
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *error = "should have failed";
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi error = mailbox_get_last_error(box, NULL);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("Failed test for mailbox %s: %s", test_case->box, error);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#endif
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_assert_idx(ret == test_case->ret, i);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_delete(box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&src);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomitest_mailbox_verify_name_continue(struct mailbox_verify_test_cases *test_cases,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi size_t ncases, struct test_mail_storage_ctx *ctx)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mail_namespace *ns =
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mail_namespace_find_inbox(ctx->user->namespaces);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi for(size_t i = 0; i < ncases; i++) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if ((test_cases[i].ns_sep != '\0' &&
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi (test_cases[i].ns_sep != mail_namespace_get_sep(ns))) ||
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi (test_cases[i].list_sep != '\0' &&
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_cases[i].list_sep != mailbox_list_get_hierarchy_sep(ns->list)))
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi continue;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_one(&test_cases[i], ns, i);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_verify_name_driver_slash(const char *driver,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *driver_opts,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct test_mail_storage_ctx *ctx)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *const ns2[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace=subspace",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace/subspace/separator=/",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace/subspace/prefix=SubSpace/",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi NULL
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi };
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (test_mail_init_user("testuser", driver, driver_opts, "/", ns2, ctx) < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi return;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_continue(test_cases, N_ELEMENTS(test_cases), ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit_user(ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_verify_name_driver_dot(const char *driver,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *driver_opts,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct test_mail_storage_ctx *ctx)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *const ns2[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace=subspace",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace/subspace/separator=.",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace/subspace/prefix=SubSpace.",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi NULL
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi };
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (test_mail_init_user("testuser", driver, driver_opts, ".", ns2, ctx) < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi return;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_continue(test_cases, N_ELEMENTS(test_cases), ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit_user(ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_verify_name(void)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *name;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *driver;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *opts;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi } test_cases[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mbox", "mbox", "" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mbox LAYOUT=FS", "mbox", ":LAYOUT=FS" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mbox LAYOUT=INDEX", "mbox", ":LAYOUT=INDEX" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "maildir LAYOUT=INDEX", "maildir", ":LAYOUT=INDEX" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "sdbox", "sdbox", "" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "sdbox LAYOUT=FS", "sdbox", ":LAYOUT=FS" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "sdbox LAYOUT=INDEX", "sdbox", ":LAYOUT=INDEX" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mdbox", "mdbox", "" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mdbox LAYOUT=FS", "mdbox", ":LAYOUT=FS" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { "mdbox LAYOUT=INDEX", "mdbox", ":LAYOUT=INDEX" },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi };
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct test_mail_storage_ctx ctx;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_zero(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_init(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi for(unsigned int i = 0; i < N_ELEMENTS(test_cases); i++) T_BEGIN {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin(t_strdup_printf("mailbox_verify_name (%s SEP=.)", test_cases[i].name));
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_driver_dot(test_cases[i].driver, test_cases[i].opts, &ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin(t_strdup_printf("mailbox_verify_name (%s SEP=/)", test_cases[i].name));
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_driver_slash(test_cases[i].driver, test_cases[i].opts, &ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi } T_END;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_list_maildir_continue(struct test_mail_storage_ctx *ctx)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mailbox_verify_test_cases test_cases[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', ".DUMPSTER", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "DUMPSTER", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "~DUMPSTER", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "INBOX/new", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "INBOX/cur", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "INBOX/tmp", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "/etc/passwd", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "SubSpace/new", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "SubSpace/cur", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "SubSpace/tmp", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "INBOX.new", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "INBOX.cur", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "INBOX.tmp", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "SubSpace.new", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "SubSpace.cur", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "SubSpace.tmp", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '.', "INBOX/INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', "INBOX/INBOX", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "INBOX/INBOX", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "INBOX/INBOX", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '.', "foo.bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '/', "foo.bar", 0 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "/foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "foo/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "foo//bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "/foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo//bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', ".foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo.", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', "foo..bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', ".foo", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo.", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "foo..bar", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '/', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '.', '.', ".", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '/', '\0', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '/', "/", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi { '\0', '\0', "", -1 },
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi };
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_continue(test_cases, N_ELEMENTS(test_cases), ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_list_maildir_init(struct test_mail_storage_ctx *ctx,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *driver_opts, const char *sep)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *error ATTR_UNUSED;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi const char *const ns2[] = {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi "namespace=subspace",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi t_strdup_printf("namespace/subspace/separator=%s", sep),
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi t_strdup_printf("namespace/subspace/prefix=SubSpace%s", sep),
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi NULL
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi };
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (test_mail_init_user("testuser", "maildir", driver_opts, sep, ns2, ctx) < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_unreached();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir_continue(ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mail_namespace *ns =
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mail_namespace_find_prefix(ctx->user->namespaces,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi t_strdup_printf("SubSpace%s", sep));
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mailbox *box = mailbox_alloc(ns->list, "SubSpace", 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi int ret = mailbox_verify_name(box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_assert(ret == 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#ifdef DEBUG
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret < 0) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi error = mailbox_get_last_error(box, NULL);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("Failed test for mailbox %s: %s",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_get_vname(box), error);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#endif
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi box = mailbox_alloc(ns->list, t_strdup_printf("SubSpace%sInner", sep), 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ret = mailbox_verify_name(box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_assert(ret == 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#ifdef DEBUG
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (ret < 0) {
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi error = mailbox_get_last_error(box, NULL);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_debug("Failed test for mailbox %s: %s",
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_get_vname(box), error);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi }
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi#endif
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi mailbox_free(&box);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit_user(ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_list_maildir(void)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct test_mail_storage_ctx ctx;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_zero(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_init(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin("mailbox_verify_name (maildir SEP=.)");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir_init(&ctx, "", ".");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin("mailbox_verify_name (maildir SEP=/)");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir_init(&ctx, "", "/");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin("mailbox_verify_name (maildir SEP=. LAYOUT=FS)");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir_init(&ctx, "LAYOUT=FS", ".");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin("mailbox_verify_name (maildir SEP=/ LAYOUT=FS)");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir_init(&ctx, "LAYOUT=FS", "/");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomistatic void test_mailbox_list_mbox(void)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi{
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct test_mail_storage_ctx ctx;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mailbox_verify_test_cases test_case;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi struct mail_namespace *ns;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_zero(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_begin("mailbox_list_mbox");
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_init(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi /* check that .lock cannot be used */
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi if (test_mail_init_user("testuser", "mbox", "", ".", NULL, &ctx) < 0)
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi i_unreached();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case.list_sep = '/';
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case.ns_sep = '.';
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case.box = "INBOX/.lock";
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_case.ret = -1;
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi ns = mail_namespace_find_inbox(ctx.user->namespaces);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name_one(&test_case, ns, 0);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit_user(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mail_deinit(&ctx);
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_end();
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi}
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomiint main(int argc, char **argv)
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi{
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi int ret;
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi void (*const tests[])(void) = {
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_mail_storage_errors,
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen test_mail_storage_last_error_push_pop,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_verify_name,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_maildir,
66fa646212a2c1cfd3ec45f97488b6a8ae41cdc0Aki Tuomi test_mailbox_list_mbox,
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen NULL
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen };
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi master_service = master_service_init("test-mail-storage",
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MASTER_SERVICE_FLAG_STANDALONE |
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MASTER_SERVICE_FLAG_NO_CONFIG_SETTINGS |
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MASTER_SERVICE_FLAG_NO_SSL_INIT |
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi MASTER_SERVICE_FLAG_NO_INIT_DATASTACK_FRAME,
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi &argc, &argv, "");
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi ret = test_run(tests);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi master_service_deinit(&master_service);
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi
e3fd3e1a6aac7cba78b147bcdd06eedb34039f3dAki Tuomi return ret;
55ca77c6ae6a081cdf6cc4dd515959afb9840da9Timo Sirainen}