test-message-address.c revision 6b8f4863bb2b0938d40f774122baf6528a833ea0
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainenstatic bool cmp_addr(const struct message_address *a1,
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen return null_strcmp(a1->name, a2->name) == 0 &&
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen static const struct test {
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen /* user@domain -> <user@domain> */
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
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 /* <user@domain> -> <user@domain> */
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user name", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, NULL, NULL, "user\"name", "domain", FALSE } },
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 /* Display Name <user@domain> -> "Display Name" <user@domain> */
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 { NULL, "Display Name", NULL, "user", "domain", FALSE } },
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 /* <@route:user@domain> -> <@route:user@domain> */
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen { NULL, NULL, "@route", "user", "domain", FALSE } },
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 /* 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 /* other tests: */
6b8f4863bb2b0938d40f774122baf6528a833ea0Timo Sirainen { NULL, "foo: <a@b>;,", NULL, "user", "domain", FALSE } },
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static struct message_address group_prefix = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen static struct message_address group_suffix = {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen unsigned int i;
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = message_address_parse(pool_datastack_create(),
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen test_assert_idx(addr != NULL && addr->next == NULL &&
a8ebb72c0fba1a6a71104e530bf5903d5f149351Timo Sirainen test_assert_idx(strcmp(str_c(str), wanted_string) == 0, i);
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if (i != 0) {
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen if ((i % 2) == 0)
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_begin("message address parsing with groups");
39025a2dabfcfaeee3790988b9ea00d19887a3d3Timo Sirainen addr = message_address_parse(pool_datastack_create(), str_data(group),
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
18a2214eedb08d043277cf1d3e75c45762014663Timo Sirainen for (i = 0; i < N_ELEMENTS(tests) && addr != NULL; i++) {
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && addr->next == NULL &&
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_begin("message address parsing with empty group");
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen addr = message_address_parse(pool_datastack_create(), str_data(group),
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
a1973d0f171b027f9a7c642bc1c2134293731e1cTimo Sirainen test_assert(addr != NULL && addr->next == NULL &&
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {