]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: test-mail-user-hash - Add tests for mail_user_hash
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 2 Nov 2020 18:59:18 +0000 (20:59 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Wed, 11 Nov 2020 11:53:37 +0000 (11:53 +0000)
src/lib-mail/Makefile.am
src/lib-mail/test-mail-user-hash.c [new file with mode: 0644]

index 57d9e2b8c41d4e86cfe0c8f3e7e9f9c8fd150670..1b59a13c76e4db22b6662b7ab1c98b74749cb6c6 100644 (file)
@@ -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 (file)
index 0000000..7e2a496
--- /dev/null
@@ -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);
+}