test-message-address.c revision 39025a2dabfcfaeee3790988b9ea00d19887a3d3
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen#include "lib.h"
0536ccb51d41e3078c3a9fa33e509fb4b2420f95Timo Sirainen#include "str.h"
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen#include "message-address.h"
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen#include "test-common.h"
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainenstatic bool cmp_addr(const struct message_address *a1,
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen const struct message_address *a2)
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen{
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen return null_strcmp(a1->name, a2->name) == 0 &&
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen null_strcmp(a1->route, a2->route) == 0 &&
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen null_strcmp(a1->mailbox, a2->mailbox) == 0 &&
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen null_strcmp(a1->domain, a2->domain) == 0 &&
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen a1->invalid_syntax == a2->invalid_syntax;
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen}
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainenstatic void test_message_address(void)
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen{
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen static const char *input[] = {
42dbeeb3462895b03e7633dbc59e8e191199734bTimo Sirainen "user@domain",
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen "<user@domain>",
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen "foo bar <user@domain>",
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen "\"foo bar\" <user@domain>",
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen "<@route:user@domain>",
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen "<@route@route2:user@domain>",
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen "hello <@route ,@route2:user@domain>",
e815af0640b38444b31eadfaa1673bcb422e1573Timo Sirainen "user (hello)",
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen "hello <user>",
1a266561b099269bef75eee1a3742e61130ef780Timo Sirainen "@domain"
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen };
5fe5ea74285e2fc0fbf7568c53f251aa894650fbTimo Sirainen static struct message_address group_prefix = {
dd2df6a67f10792ce31a3666197c0b6885893a3aTimo Sirainen NULL, NULL, NULL, "group", NULL, FALSE
14175321ddb88619015866978c05a27786ca4814Timo Sirainen };
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen static struct message_address group_suffix = {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen NULL, NULL, NULL, NULL, NULL, FALSE
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen };
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen static struct message_address output[] = {
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, NULL, NULL, "user", "domain", FALSE },
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen { NULL, "foo bar", NULL, "user", "domain", FALSE },
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen { NULL, "foo bar", NULL, "user", "domain", FALSE },
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen { NULL, NULL, "@route", "user", "domain", FALSE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, NULL, "@route,@route2", "user", "domain", FALSE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, "hello", "@route,@route2", "user", "domain", FALSE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, "hello", NULL, "user", "", TRUE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, "hello", NULL, "user", "", TRUE },
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen { NULL, NULL, NULL, "", "domain", TRUE }
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen };
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen struct message_address *addr;
1de2b5a16a455e018d8cbf72ee114d4b5d557a48Timo Sirainen string_t *group;
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen unsigned int i;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen bool success;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen group = t_str_new(256);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_append(group, "group: ");
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen for (i = 0; i < N_ELEMENTS(input); i++) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen addr = message_address_parse(pool_datastack_create(),
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen (const unsigned char *)input[i],
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen strlen(input[i]), -1U, FALSE);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen success = addr != NULL && addr->next == NULL &&
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen cmp_addr(addr, &output[i]);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen test_out(t_strdup_printf("message_address_parse(%d)", i),
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen success);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (!output[i].invalid_syntax) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (i != 0) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if ((i % 2) == 0)
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_append(group, ",");
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen else
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_append(group, " , \n ");
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_append(group, input[i]);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_append_c(group, ';');
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen addr = message_address_parse(pool_datastack_create(), str_data(group),
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen str_len(group), -1U, FALSE);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen success = addr != NULL && cmp_addr(addr, &group_prefix);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen addr = addr->next;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen for (i = 0; i < N_ELEMENTS(input) && addr != NULL; i++) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (output[i].invalid_syntax)
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen continue;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (!cmp_addr(addr, &output[i])) {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen success = FALSE;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen break;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen addr = addr->next;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen }
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen if (addr == NULL || addr->next != NULL ||
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen !cmp_addr(addr, &group_suffix))
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen success = FALSE;
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen test_out("message_address_parse(group)", success);
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen}
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainenint main(void)
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen{
d4fe93a9c242d745e0cf2e6cc58d5caf265de2a0Timo Sirainen static void (*test_functions[])(void) = {
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen test_message_address,
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen NULL
a33b41b1dc19c692b1283049ec4de492fdadeb9aTimo Sirainen };
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen return test_run(test_functions);
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen}
8ad2759cf4073e3bf4fcb9222a86e2153ed31875Timo Sirainen