test-message-address.c revision 6b8f4863bb2b0938d40f774122baf6528a833ea0
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "lib.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "str.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "message-address.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen#include "test-common.h"
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenstatic bool cmp_addr(const struct message_address *a1,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen const struct message_address *a2)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen return null_strcmp(a1->name, a2->name) == 0 &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen null_strcmp(a1->route, a2->route) == 0 &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen null_strcmp(a1->mailbox, a2->mailbox) == 0 &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen null_strcmp(a1->domain, a2->domain) == 0 &&
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen a1->invalid_syntax == a2->invalid_syntax;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen}
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenstatic void test_message_address(void)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen static const struct test {
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const char *input;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const char *wanted_output;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen struct message_address addr;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen } tests[] = {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* user@domain -> <user@domain> */
5a8ee853d0b62692a6e624b125d08d87a79e001fTimo Sirainen { "user@domain", "<user@domain>",
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"user\"@domain", "<user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"user name\"@domain", "<\"user name\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user name", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"user@na\\\\me\"@domain", "<\"user@na\\\\me\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"user\\\"name\"@domain", "<\"user\\\"name\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user\"name", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"\"@domain", "<\"\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "@domain", "<\"\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "", "domain", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* <user@domain> -> <user@domain> */
5a8ee853d0b62692a6e624b125d08d87a79e001fTimo Sirainen { "<user@domain>", NULL,
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<\"user\"@domain>", "<user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<\"user name\"@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user name", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<\"user@na\\\\me\"@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<\"user\\\"name\"@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user\"name", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<\"\"@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "", "domain", FALSE } },
58a770f1e0ab553a0dba9cad9d6f3a6cdf2dc855Timo Sirainen { "<@route>", "<@route:\"\">",
58a770f1e0ab553a0dba9cad9d6f3a6cdf2dc855Timo Sirainen { NULL, NULL, "@route", "", "", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* user@domain (Display Name) -> "Display Name" <user@domain> */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "user@domain (DisplayName)", "DisplayName <user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "DisplayName", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "user@domain (Display Name)", "\"Display Name\" <user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "user@domain (Display\"Name)", "\"Display\\\"Name\" <user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display\"Name", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "user (Display Name)", "\"Display Name\" <user>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "user", "", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "@domain (Display Name)", "\"Display Name\" <\"\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "", "domain", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* Display Name <user@domain> -> "Display Name" <user@domain> */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "DisplayName <user@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "DisplayName", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <user@domain>", "\"Display Name\" <user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"Display Name\" <user@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"Display\\\"Name\" <user@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display\"Name", NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <user>", "\"Display Name\" <user>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", NULL, "user", "", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* <@route:user@domain> -> <@route:user@domain> */
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { "<@route:user@domain>", NULL,
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, "@route", "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<@route,@route2:user@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, "@route,@route2", "user", "domain", FALSE } },
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { "<@route@route2:user@domain>", "<@route,@route2:user@domain>",
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, "@route,@route2", "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<@route@route2:user>", "<@route,@route2:user>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, "@route,@route2", "user", "", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "<@route@route2:\"\"@domain>", "<@route,@route2:\"\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, "@route,@route2", "", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* Display Name <@route:user@domain> ->
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen "Display Name" <@route:user@domain> */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <@route:user@domain>", "\"Display Name\" <@route:user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", "@route", "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <@route,@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <@route@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <@route@route2:user>", "\"Display Name\" <@route,@route2:user>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "", TRUE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "Display Name <@route@route2:\"\"@domain>", "\"Display Name\" <@route,@route2:\"\"@domain>",
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "Display Name", "@route,@route2", "", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* other tests: */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { "\"foo: <a@b>;,\" <user@domain>", NULL,
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "foo: <a@b>;,", NULL, "user", "domain", FALSE } },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static struct message_address group_prefix = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen NULL, NULL, NULL, "group", NULL, FALSE
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static struct message_address group_suffix = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen NULL, NULL, NULL, NULL, NULL, FALSE
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen struct message_address *addr;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen string_t *str, *group;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen const char *wanted_string;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen unsigned int i;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_begin("message address parsing");
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen str = t_str_new(128);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen group = t_str_new(256);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen str_append(group, "group: ");
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen for (i = 0; i < N_ELEMENTS(tests); i++) {
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const struct test *test = &tests[i];
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = message_address_parse(pool_datastack_create(),
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen (const unsigned char *)test->input,
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen strlen(test->input), UINT_MAX, FALSE);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen test_assert_idx(addr != NULL && addr->next == NULL &&
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen cmp_addr(addr, &test->addr), i);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen str_truncate(str, 0);
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen message_address_write(str, addr);
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen wanted_string = test->wanted_output != NULL ?
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen test->wanted_output : test->input;
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen test_assert_idx(strcmp(str_c(str), wanted_string) == 0, i);
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen if (!test->addr.invalid_syntax) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (i != 0) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if ((i % 2) == 0)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen str_append(group, ",");
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen else
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen str_append(group, " , \n ");
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen str_append(group, test->input);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen str_append_c(group, ';');
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_end();
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_begin("message address parsing with groups");
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = message_address_parse(pool_datastack_create(), str_data(group),
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen str_len(group), UINT_MAX, FALSE);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = addr->next;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen for (i = 0; i < N_ELEMENTS(tests) && addr != NULL; i++) {
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen const struct test *test = &tests[i];
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen if (test->addr.invalid_syntax)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen continue;
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen test_assert(cmp_addr(addr, &test->addr));
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = addr->next;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen }
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && addr->next == NULL &&
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen cmp_addr(addr, &group_suffix));
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_end();
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_begin("message address parsing with empty group");
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen str_truncate(group, 0);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen str_append(group, "group:;");
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen addr = message_address_parse(pool_datastack_create(), str_data(group),
8ae72ad7d0c69e972cfa65d1e2ce4e3e9a8b765cTimo Sirainen str_len(group), UINT_MAX, FALSE);
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár str_truncate(str, 0);
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár message_address_write(str, addr);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen addr = addr->next;
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && addr->next == NULL &&
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen cmp_addr(addr, &group_suffix));
99df8a838cd9c5257ea5a2554383a9a999191e38Pali Rohár test_assert(strcmp(str_c(str), "group:;") == 0);
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_end();
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen}
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenint main(void)
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen test_message_address,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen NULL
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen };
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen return test_run(test_functions);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen}