From: Aki Tuomi Date: Mon, 2 Nov 2020 18:59:18 +0000 (+0200) Subject: lib-mail: test-mail-user-hash - Add tests for mail_user_hash X-Git-Tag: 2.3.14.rc1~350 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3490e6b47286ec2fb7c146cfc942ede3d3ed0dbb;p=thirdparty%2Fdovecot%2Fcore.git lib-mail: test-mail-user-hash - Add tests for mail_user_hash --- diff --git a/src/lib-mail/Makefile.am b/src/lib-mail/Makefile.am index 57d9e2b8c4..1b59a13c76 100644 --- a/src/lib-mail/Makefile.am +++ b/src/lib-mail/Makefile.am @@ -92,6 +92,7 @@ test_programs = \ test-istream-qp-decoder \ test-istream-qp-encoder \ test-mail-html2text \ + test-mail-user-hash \ test-mbox-from \ test-message-address \ test-message-date \ @@ -225,6 +226,10 @@ test_rfc822_parser_SOURCES = test-rfc822-parser.c test_rfc822_parser_LDADD = $(test_libs) test_rfc822_parser_DEPENDENCIES = $(test_deps) +test_mail_user_hash_SOURCES = test-mail-user-hash.c +test_mail_user_hash_LDADD = $(test_libs) +test_mail_user_hash_DEPENDENCIES = $(test_deps) + check-local: for bin in $(test_programs); do \ if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \ diff --git a/src/lib-mail/test-mail-user-hash.c b/src/lib-mail/test-mail-user-hash.c new file mode 100644 index 0000000000..7e2a49691c --- /dev/null +++ b/src/lib-mail/test-mail-user-hash.c @@ -0,0 +1,185 @@ +/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "buffer.h" +#include "str.h" +#include "mail-user-hash.h" +#include "test-common.h" + +#include "md5.h" + +static void test_mail_user_hash(void) +{ + struct test_case { + const char *username; + const char *format; + unsigned int hash; + } test_cases[] = { + { + .username = "", + .format = "", + .hash = 3558706393, + }, + { + .username = "testuser", + .format = "", + .hash = 3558706393, + }, + { + .username = "", + .format = "%u", + .hash = 3558706393, + }, + { + .username = "@", + .format = "%u", + .hash = 1368314517, + }, + { + .username = "", + .format = "%n@%d", + .hash = 1368314517, + }, + { + .username = "", + .format = "%n", + .hash = 3558706393, + }, + { + .username = "", + .format = "%d", + .hash = 3558706393, + }, + { + .username = "testuser", + .format = "%u", + .hash = 1570531526, + }, + { + .username = "testuser", + .format = "%n", + .hash = 1570531526, + }, + { + .username = "testuser", + .format = "%d", + .hash = 3558706393, + }, + { + .username = "@domain", + .format = "%u", + .hash = 3749630072, + }, + { + .username = "@domain", + .format = "%n@%d", + .hash = 3749630072, + }, + { + .username = "@domain", + .format = "%n", + .hash = 3558706393, + }, + { + .username = "@domain", + .format = "%d", + .hash = 2908717800, + }, + { + .username = "testuser@domain", + .format = "%u", + .hash = 3813799143, + }, + { + .username = "testuser@domain", + .format = "%n@%d", + .hash = 3813799143, + }, + { + .username = "testuser@domain", + .format = "%n", + .hash = 1570531526, + }, + { + .username = "testuser@domain", + .format = "%d", + .hash = 2908717800, + }, + { + .username = "test@user@domain", + .format = "%u", + .hash = 2029259821, + }, + { + .username = "test@user@domain", + .format = "%n@%d", + .hash = 2029259821, + }, + { + .username = "test@user@domain", + .format = "%n", + .hash = 160394189, + }, + { + .username = "test@user@domain", + .format = "%d", + .hash = 1841230927, + } + }; + + test_begin("mail_user_hash"); + + for (size_t i = 0; i < N_ELEMENTS(test_cases); i++) { + const struct test_case *tc = &test_cases[i]; + const char *error = NULL; + unsigned int hash; + test_assert_idx(mail_user_hash(tc->username, tc->format, &hash, + &error), i); + test_assert_idx(error == NULL, i); + test_assert_idx(hash == tc->hash, i); + } + + test_end(); +} + +static void test_mail_user_hash_errors(void) +{ + test_begin("mail_user_hash_errors"); + + struct test_case { + const char *username; + const char *format; + unsigned int hash; + const char *error; + } test_cases[] = { + { + .username = "testuser@domain", + .format = "%{invalid}", + .hash = 1466562296, + .error = "Unknown variable '%invalid'", + }, + }; + + for (size_t i = 0; i < N_ELEMENTS(test_cases); i++) { + const struct test_case *tc = &test_cases[i]; + const char *error = NULL; + unsigned int hash = 0; + test_assert_idx(mail_user_hash(tc->username, tc->format, &hash, + &error) == FALSE, i); + test_assert_idx(tc->hash == hash, i); + test_assert_strcmp_idx(tc->error, error, i); + test_assert_idx(tc->hash == hash, i); + } + + test_end(); +} + +int main(void) +{ + static void (*const test_functions[])(void) = { + test_mail_user_hash, + test_mail_user_hash_errors, + NULL + }; + return test_run(test_functions); +}