test-message-address.c revision 6b8f4863bb2b0938d40f774122baf6528a833ea0
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007-2017 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "lib.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "str.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "message-address.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "test-common.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic bool cmp_addr(const struct message_address *a1,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct message_address *a2)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return null_strcmp(a1->name, a2->name) == 0 &&
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen null_strcmp(a1->route, a2->route) == 0 &&
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen null_strcmp(a1->mailbox, a2->mailbox) == 0 &&
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen null_strcmp(a1->domain, a2->domain) == 0 &&
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen a1->invalid_syntax == a2->invalid_syntax;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void test_message_address(void)
b30584ea5916834e73dcb3672e870291968a9054Timo Sirainen{
b30584ea5916834e73dcb3672e870291968a9054Timo Sirainen static const struct test {
b30584ea5916834e73dcb3672e870291968a9054Timo Sirainen const char *input;
b30584ea5916834e73dcb3672e870291968a9054Timo Sirainen const char *wanted_output;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct message_address addr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } tests[] = {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* user@domain -> <user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "user@domain", "<user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"user\"@domain", "<user@domain>",
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { "\"user name\"@domain", "<\"user name\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user name", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"user@na\\\\me\"@domain", "<\"user@na\\\\me\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"user\\\"name\"@domain", "<\"user\\\"name\"@domain>",
fc40a9a002458e372ff4b9f6f4e15239520c0bcdTimo Sirainen { NULL, NULL, NULL, "user\"name", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"\"@domain", "<\"\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "@domain", "<\"\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "", "domain", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* <user@domain> -> <user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { "<\"user\"@domain>", "<user@domain>",
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE } },
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { "<\"user name\"@domain>", NULL,
d665e2948cb6fe8016706752a9e4940d64f4a61dTimo Sirainen { NULL, NULL, NULL, "user name", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<\"user@na\\\\me\"@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<\"user\\\"name\"@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "user\"name", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<\"\"@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, NULL, "", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route>", "<@route:\"\">",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route", "", "", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* user@domain (Display Name) -> "Display Name" <user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "user@domain (DisplayName)", "DisplayName <user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "DisplayName", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "user@domain (Display Name)", "\"Display Name\" <user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "user@domain (Display\"Name)", "\"Display\\\"Name\" <user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display\"Name", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "user (Display Name)", "\"Display Name\" <user>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "user", "", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "@domain (Display Name)", "\"Display Name\" <\"\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "", "domain", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* Display Name <user@domain> -> "Display Name" <user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "DisplayName <user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "DisplayName", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <user@domain>", "\"Display Name\" <user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"Display Name\" <user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "\"Display\\\"Name\" <user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display\"Name", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <user>", "\"Display Name\" <user>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", NULL, "user", "", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* <@route:user@domain> -> <@route:user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route:user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route", "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route,@route2:user@domain>", NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route,@route2", "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route@route2:user@domain>", "<@route,@route2:user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route,@route2", "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route@route2:user>", "<@route,@route2:user>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route,@route2", "user", "", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "<@route@route2:\"\"@domain>", "<@route,@route2:\"\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, NULL, "@route,@route2", "", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* Display Name <@route:user@domain> ->
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen "Display Name" <@route:user@domain> */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <@route:user@domain>", "\"Display Name\" <@route:user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", "@route", "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <@route,@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <@route@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "domain", FALSE } },
9af6cc9ebc9986c1275ebdfa29c39e152af1557eTimo Sirainen { "Display Name <@route@route2:user>", "\"Display Name\" <@route,@route2:user>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", "@route,@route2", "user", "", TRUE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { "Display Name <@route@route2:\"\"@domain>", "\"Display Name\" <@route,@route2:\"\"@domain>",
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen { NULL, "Display Name", "@route,@route2", "", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* other tests: */
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen { "\"foo: <a@b>;,\" <user@domain>", NULL,
6564208826b0f46a00f010d1b5711d85944c3c88Timo Sirainen { NULL, "foo: <a@b>;,", NULL, "user", "domain", FALSE } },
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen };
static struct message_address group_prefix = {
NULL, NULL, NULL, "group", NULL, FALSE
};
static struct message_address group_suffix = {
NULL, NULL, NULL, NULL, NULL, FALSE
};
struct message_address *addr;
string_t *str, *group;
const char *wanted_string;
unsigned int i;
test_begin("message address parsing");
str = t_str_new(128);
group = t_str_new(256);
str_append(group, "group: ");
for (i = 0; i < N_ELEMENTS(tests); i++) {
const struct test *test = &tests[i];
addr = message_address_parse(pool_datastack_create(),
(const unsigned char *)test->input,
strlen(test->input), UINT_MAX, FALSE);
test_assert_idx(addr != NULL && addr->next == NULL &&
cmp_addr(addr, &test->addr), i);
str_truncate(str, 0);
message_address_write(str, addr);
wanted_string = test->wanted_output != NULL ?
test->wanted_output : test->input;
test_assert_idx(strcmp(str_c(str), wanted_string) == 0, i);
if (!test->addr.invalid_syntax) {
if (i != 0) {
if ((i % 2) == 0)
str_append(group, ",");
else
str_append(group, " , \n ");
}
str_append(group, test->input);
}
}
str_append_c(group, ';');
test_end();
test_begin("message address parsing with groups");
addr = message_address_parse(pool_datastack_create(), str_data(group),
str_len(group), UINT_MAX, FALSE);
test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
addr = addr->next;
for (i = 0; i < N_ELEMENTS(tests) && addr != NULL; i++) {
const struct test *test = &tests[i];
if (test->addr.invalid_syntax)
continue;
test_assert(cmp_addr(addr, &test->addr));
addr = addr->next;
}
test_assert(addr != NULL && addr->next == NULL &&
cmp_addr(addr, &group_suffix));
test_end();
test_begin("message address parsing with empty group");
str_truncate(group, 0);
str_append(group, "group:;");
addr = message_address_parse(pool_datastack_create(), str_data(group),
str_len(group), UINT_MAX, FALSE);
str_truncate(str, 0);
message_address_write(str, addr);
test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
addr = addr->next;
test_assert(addr != NULL && addr->next == NULL &&
cmp_addr(addr, &group_suffix));
test_assert(strcmp(str_c(str), "group:;") == 0);
test_end();
}
int main(void)
{
static void (*const test_functions[])(void) = {
test_message_address,
NULL
};
return test_run(test_functions);
}