test-mail-storage.c revision e3fd3e1a6aac7cba78b147bcdd06eedb34039f3d
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic void test_init_storage(struct mail_storage *storage_r)
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic void test_deinit_storage(struct mail_storage *storage)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if (array_is_created(&storage->error_stack)) {
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen i_assert(array_count(&storage->error_stack) == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen struct mail_storage_service_ctx *storage_service;
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen struct mail_storage_service_user *service_user;
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(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
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(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "error1") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* clear the error - asking for it afterwards is a bug */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen /* set internal error in preparation for the next test */
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen mail_storage_set_critical(&storage, "critical0");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical0") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* internal error without specifying what it is. this needs to clear
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen the previous internal error. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_internal_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* proper internal error */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen mail_storage_set_critical(&storage, "critical1");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical1") == 0);
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_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical2: critical1") == 0);
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_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
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 /* clear the error again and check that all is as expected */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "BUG: Unknown internal error") == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* use internal error as a regular error (although that really
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen shouldn't be done) */
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_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "critical4") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical4") == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenstatic void test_mail_storage_last_error_push_pop(void)
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_begin("mail_storage_last_error_push/pop()");
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* regular error 1 */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PERM, "regular error 1");
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 1 */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_critical(&storage, "critical error 1");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 2 */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen mail_storage_set_error(&storage, MAIL_ERROR_PARAMS, "regular error 2");
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 2 */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen mail_storage_set_critical(&storage, "critical error 2");
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* -- clear all errors -- */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen /* critical error 2 pop */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 2") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 2 pop */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 2") == 0);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 2") == 0);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* critical error 1 pop */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strstr(mail_storage_get_last_error(&storage, &mail_error), MAIL_ERRSTR_CRITICAL_MSG) != NULL);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "critical error 1") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen /* regular error 1 pop */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert(strcmp(mail_storage_get_last_error(&storage, &mail_error), "regular error 1") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen test_assert(strcmp(mail_storage_get_last_internal_error(&storage, &mail_error), "regular error 1") == 0);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenstatic void test_mail_init(struct test_mail_storage_ctx *ctx)
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen ctx->pool = pool_allocfree_create("test pool");
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen if (getcwd(path_buf, sizeof(path_buf)) == NULL)
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen ctx->mail_home = p_strdup_printf(ctx->pool, "%s/.test-dir%s/", path_buf,
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0 &&
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen i_warning("unlink_directory(%s) failed: %s", ctx->mail_home, error);
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen ctx->storage_service = mail_storage_service_init(master_service, NULL,
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen MAIL_STORAGE_SERVICE_FLAG_NO_RESTRICT_ACCESS |
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainenstatic void test_mail_deinit(struct test_mail_storage_ctx *ctx)
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainen mail_storage_service_deinit(&ctx->storage_service);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen i_fatal("chdir(%s) failed: %m", ctx->mail_home);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen if (unlink_directory(ctx->mail_home, UNLINK_DIRECTORY_FLAG_RMDIR,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen i_error("unlink_directory(%s) failed: %s", ctx->mail_home, error);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstatic int test_mail_init_user(const char *user, const char *driver,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const char *const *extra_input,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen home = t_strdup_printf("%s%s", ctx->mail_home, user);
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",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen t_strdup_printf("namespace/inbox/separator=%s", sep),
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen i_assert(mkdir_parents(home, S_IRWXU)==0 || errno == EEXIST);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen array_append(&opts, default_input, N_ELEMENTS(default_input));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen if (mail_storage_service_lookup_next(ctx->storage_service, &input,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen i_error("mail_storage_service_lookup_next(%s) failed: %s",
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)
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen mail_storage_service_user_unref(&ctx->service_user);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen void (*const tests[])(void) = {
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen master_service = master_service_init("test-mail-storage",