bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#include "test-lib.h"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#include "str.h"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#include "test-common.h"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#include "smtp-address.h"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid mailbox parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct valid_mailbox_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input, *output;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch enum smtp_address_parse_flags flags;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_mailbox_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschvalid_mailbox_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = NULL, .domain = NULL },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = NULL },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "1234567890@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "1234567890",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "_______@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "_______",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname.lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname.lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname+lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname+lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname-lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname-lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user\"@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "user@domain.tld"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user@frop\"@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"user@frop\"@domain.tld"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@127.0.0.1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "127.0.0.1" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0.1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::127.0.0.1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch /* Japanese deviations */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@-example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email", .domain = "-example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = ".email@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\".email\"@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = ".email", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email.@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"email.\"@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email.", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email..email@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"email..email\"@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "email..email", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "Abc..123@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"Abc..123\"@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "Abc..123", .domain = "example.com" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int valid_mailbox_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(valid_mailbox_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_mailbox_parse_valid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_mailbox_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct valid_mailbox_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL, *output, *encoded;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &valid_mailbox_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_mailbox(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, test->flags, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp mailbox valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input), ret > 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch if (ret > 0) {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch if (address->localpart == NULL ||
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->address.localpart == NULL) {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = %s", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch (address->localpart == test->address.localpart));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch if (address->domain == NULL ||
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->address.domain == NULL) {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = %s", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch (address->domain == test->address.domain));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch encoded = smtp_address_encode(address);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(encoded, output) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid path parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct valid_path_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input, *output;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch enum smtp_address_parse_flags flags;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_path_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschvalid_path_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = NULL, .domain = NULL }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = NULL }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@otherdomain.tld,@yetanotherdomain.tld:user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "<user@domain.tld>"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int valid_path_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(valid_path_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_path_parse_valid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_path_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct valid_path_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL, *output, *encoded;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &valid_path_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, test->flags, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp path valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input), ret > 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch if (ret > 0) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch if (smtp_address_isnull(address) ||
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(&test->address)) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch test_out("address = <>",
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(address) &&
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(&test->address));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch if (smtp_address_isnull(address)) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch /* nothing */
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch } else if (address->domain == NULL ||
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->address.domain == NULL) {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = %s", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch (address->domain == test->address.domain));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch encoded = smtp_address_encode_path(address);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(encoded, output) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Valid username parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct valid_username_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input, *output;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct valid_username_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschvalid_username_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = NULL },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "1234567890@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "1234567890",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "_______@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "_______",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname.lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname.lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname+lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname+lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "firstname-lastname@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .localpart = "firstname-lastname",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user\"@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "user@domain.tld"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user@frop\"@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"user@frop\"@domain.tld"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@frop@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user@frop", .domain = "domain.tld" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .output = "\"user@frop\"@domain.tld"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@127.0.0.1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "127.0.0.1" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0.1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::127.0.0.1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .address = { .localpart = "user", .domain = "[IPv6:::127.0.0.1]" },
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int valid_username_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(valid_username_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_username_parse_valid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < valid_username_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct valid_username_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL, *output, *encoded;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &valid_username_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_username(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp username valid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf("parse(\"%s\")",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input), ret > 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch if (ret > 0) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch if (smtp_address_isnull(address) ||
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(&test->address)) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch test_out("address = <>",
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(address) &&
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch smtp_address_isnull(&test->address));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->localpart = \"%s\"", address->localpart),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->localpart, test->address.localpart) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch if (smtp_address_isnull(address)) {
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch /* nothing */
aba05d93fe13aebb76d3ab8ca235a40f75b4afc4Stephan Bosch } else if (address->domain == NULL ||
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->address.domain == NULL) {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = %s", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch (address->domain == test->address.domain));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } else {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("address->domain = \"%s\"", address->domain),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(address->domain, test->address.domain) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch encoded = smtp_address_encode(address);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch output = (test->output == NULL ? test->input : test->output);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out(
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch t_strdup_printf("encode() = \"%s\"", encoded),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch strcmp(encoded, output) == 0);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid mailbox parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct invalid_mailbox_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch enum smtp_address_parse_flags flags;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_mailbox_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschinvalid_mailbox_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "us\"er@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@frop@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@a$.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@a..tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[AA]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[AA",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[256.256.256.256]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0.1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:flierp]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:aa:bb::cc::dd]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen:]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen:Hopsa",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen-:Hopsa]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "#@%^%#$@#$@#.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "Eric Mail <email@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email.example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "あいうえお@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example.com (Eric Mail)",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example..com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#if 0 /* These deviations are allowed (maybe implement strict mode) */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@-example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = ".email@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email.@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email..email@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "Abc..123@example.com"
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch#endif
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int invalid_mailbox_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(invalid_mailbox_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_mailbox_parse_invalid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
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 struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &invalid_mailbox_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_mailbox(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, test->flags, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp mailbox invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid path parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct invalid_path_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch enum smtp_address_parse_flags flags;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_path_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschinvalid_path_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "\"user@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "us\"er@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@frop@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@a$.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@a..tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[AA]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[AA",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[256.256.256.256]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[127.0.0.1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:flierp]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:aa:bb::cc::dd]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6::1]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[IPv6:::1",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen:]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen:Hopsa",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "user@[Gen-:Hopsa]",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "#@%^%#$@#$@#.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "Eric Mail <email@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email.example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "あいうえお@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example.com (Eric Mail)",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example..com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<\"user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<us\"er@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@frop@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@a$.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@a..tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[AA]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[AA>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[127.0.0]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[256.256.256.256]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[127.0.0.1>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[::1]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[IPv6:flierp]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[IPv6:aa:bb::cc::dd]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[IPv6::1]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[IPv6:::1>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[Gen:]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[Gen:Hopsa>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user@[Gen-:Hopsa]>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<#@%^%#$@#$@#.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "Eric Mail <email@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<email.example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<email@example@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<あいうえお@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<email@example.com> (Eric Mail)",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<email@example..com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<email@example.com",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "email@example.com>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<user",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .flags = SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@otherdomain.tld,@yetanotherdomain.tld.user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch },{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "<@###domain.tld,@yetanotherdomain.tld.user@domain.tld>",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int invalid_path_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(invalid_path_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_path_parse_invalid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch for (i = 0; i < invalid_path_parse_test_count; i++) T_BEGIN {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const struct invalid_path_parse_test *test;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &invalid_path_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, test->flags, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp path invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Invalid username parse tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstruct invalid_username_parse_test {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *input;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch enum smtp_address_parse_flags flags;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic const struct invalid_username_parse_test
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschinvalid_username_parse_tests[] = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "frop@$%^$%^.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "fr op@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }, {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch .input = "f r o p@domain.tld",
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch }
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch};
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschunsigned int invalid_username_parse_test_count =
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch N_ELEMENTS(invalid_username_parse_tests);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschstatic void test_smtp_username_parse_invalid(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch unsigned int i;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
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 struct smtp_address *address;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch const char *error = NULL;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch int ret;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test = &invalid_username_parse_tests[i];
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ret = smtp_address_parse_username(pool_datastack_create(),
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test->input, &address, &error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_begin(t_strdup_printf("smtp username invalid [%d]", i));
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_out_reason(t_strdup_printf
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch ("parse(\"%s\")", test->input), ret < 0, error);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_end();
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch } T_END;
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch/*
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch * Address detail parsing
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch */
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschstruct address_detail_parse_test {
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const char *delimiters;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const char *address;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const char *username;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const char *detail;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch char delim;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch};
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschstatic const struct address_detail_parse_test
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschaddress_detail_parse_tests[] = {
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "", "test", "test", "", '\0' },
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
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch { "+-:", "test", "test", "", '\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 Bosch};
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschunsigned int addresss_detail_parse_test_count =
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch N_ELEMENTS(address_detail_parse_tests);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Boschstatic void test_smtp_address_detail_parse(void)
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch{
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch unsigned int i;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
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 &address_detail_parse_tests[i];
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch struct smtp_address *address;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch const char *username, *detail, *error;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch char delim;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch int ret;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_begin(t_strdup_printf(
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch "smtp address detail parsing [%d]", i));
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch ret = smtp_address_parse_path(pool_datastack_create(), test->address,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART |
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch &address, &error);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_out_reason("address parse", ret > 0, error);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch if (!test_has_failed()) {
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch smtp_address_detail_parse_temp(test->delimiters, address,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch &username, &delim, &detail);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_assert(strcmp(username, test->username) == 0);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_assert(strcmp(detail, test->detail) == 0);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_assert(delim == test->delim);
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch }
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_end();
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch } T_END;
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch}
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch/*
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch * Tests
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch */
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Boschint main(void)
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch{
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch static void (*test_functions[])(void) = {
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_mailbox_parse_valid,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_path_parse_valid,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_username_parse_valid,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_mailbox_parse_invalid,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_path_parse_invalid,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch test_smtp_username_parse_invalid,
61ae3dd363d32db75d2c3effaab4ba093ce0f245Stephan Bosch test_smtp_address_detail_parse,
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch NULL
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch };
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch return test_run(test_functions);
03f08e4abef2afc782069bdfef305f84e98f0922Stephan Bosch}