bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid mailbox parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_mailbox_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = NULL, .domain = NULL },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = NULL },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "127.0.0.1" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch /* Japanese deviations */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email", .domain = "-example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = ".email", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email.", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email..email", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "Abc..123", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_mailbox_parse_valid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_mailbox_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_mailbox(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp mailbox valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = %s", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch (address->localpart == test->address.localpart));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid path parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_path_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = NULL, .domain = NULL }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = NULL }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@otherdomain.tld,@yetanotherdomain.tld:user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_path_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp path valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch /* nothing */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid username parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_username_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "127.0.0.1" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_username_parse_valid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_username_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_username(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp username valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch /* nothing */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid mailbox parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_mailbox_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#if 0 /* These deviations are allowed (maybe implement strict mode) */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int invalid_mailbox_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_mailbox_parse_invalid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < invalid_mailbox_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct invalid_mailbox_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_mailbox(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp mailbox invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid path parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_path_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@otherdomain.tld,@yetanotherdomain.tld.user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@###domain.tld,@yetanotherdomain.tld.user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < invalid_path_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp path invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid username parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_username_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int invalid_username_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_username_parse_invalid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < invalid_username_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct invalid_username_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_username(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp username invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch * Address detail parsing
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschstatic const struct address_detail_parse_test
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "test+address", "test+address", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "\"test:address\"", "test:address", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "\"test-address:another+delim\"", "test-address:another+delim", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "test@domain", "test@domain", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "test+address@domain", "test+address@domain", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "\"test:address\"@domain", "test:address@domain", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "\"test-address:another+delim\"@domain", "test-address:another+delim@domain", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "test+address", "test", "address", '+' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "\"test:address\"", "test", "address", ':' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "\"test-address:another+delim\"", "test", "address:another+delim", '-' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "test@domain", "test@domain", "", '\0' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "test+address@domain", "test@domain", "address", '+' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "\"test:address\"@domain", "test@domain", "address", ':' },
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "\"test-address:another+delim\"@domain", "test@domain", "address:another+delim", '-' }
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschunsigned int addresss_detail_parse_test_count =
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschstatic void test_smtp_address_detail_parse(void)
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch unsigned int i;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch for (i = 0; i < N_ELEMENTS(address_detail_parse_tests); i++) T_BEGIN {
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const struct address_detail_parse_test *test =
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch "smtp address detail parsing [%d]", i));
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(), test->address,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_out_reason("address parse", ret > 0, error);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch smtp_address_detail_parse_temp(test->delimiters, address,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_assert(strcmp(username, test->username) == 0);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_assert(strcmp(detail, test->detail) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch static void (*test_functions[])(void) = {