test-smtp-params.c revision 39bf54004e8ef15baa4c18c7969e87c51b8f197b
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher/* Copyright (c) 2013-2017 Dovecot authors, see the included COPYING file */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagher#include "test-lib.h"
33396dc46ea52c18f47db1b5d590880806521005Sumit Bose#include "str.h"
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagher#include "array.h"
33396dc46ea52c18f47db1b5d590880806521005Sumit Bose#include "test-common.h"
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher#include "smtp-common.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "smtp-address.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose#include "smtp-params.h"
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bose
324fb26ba803a999bedc29e93c46c84f27abf5b7Sumit Bosestatic struct smtp_address test_address1 =
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { .localpart = NULL, .domain = NULL };
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic struct smtp_address test_address2 =
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher { .localpart = "user+detail", .domain = NULL };
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagherstatic struct smtp_address test_address3 =
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher { .localpart = "e=mc2", .domain = "example.com" };
e65df5b966b27e13283c65f59f99ac44781e0333Simo Sorce
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic struct smtp_param test_params1[] = {
672f430c2e5d55226261a281bc3fa77311ace5a4Jakub Hrozek { .keyword = "FROP", .value = "friep" }
672f430c2e5d55226261a281bc3fa77311ace5a4Jakub Hrozek};
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic struct smtp_param test_params2[] = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { .keyword = "FROP", .value = "friep" },
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher { .keyword = "FRUP", .value = "frml" }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher};
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic struct buffer test_params_buffer1 = {
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher .data = (void*)&test_params1,
cc98edd9479d4622634a1275c98058916c14059aStephen Gallagher .used = sizeof(test_params1)
ee359fe1384507fed6c2274e7bfe81d288de4542Stephen Gallagher};
cc98edd9479d4622634a1275c98058916c14059aStephen Gallagherstatic struct buffer test_params_buffer2 = {
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina .data = (void*)&test_params2,
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina .used = sizeof(test_params2)
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina};
1183d29d87c5c7439cf2364b7d7324d4a13b6e35Stephen Gallagher
1183d29d87c5c7439cf2364b7d7324d4a13b6e35Stephen Gallagher/* Valid mail params tests */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstruct valid_mail_params_parse_test {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char *input, *output;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher enum smtp_capability caps;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher bool extensions;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct smtp_params_mail params;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher};
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic const struct valid_mail_params_parse_test
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghervalid_mail_params_parse_tests[] = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* AUTH */
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce {
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce .input = "AUTH=<>",
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce .caps = SMTP_CAPABILITY_AUTH,
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .params = {
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .auth = &test_address1
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek }
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce },{
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce .input = "AUTH=user+2Bdetail",
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .caps = SMTP_CAPABILITY_AUTH,
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .params = {
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .auth = &test_address2
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek }
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek },{
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .input = "AUTH=e+3Dmc2@example.com",
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .caps = SMTP_CAPABILITY_AUTH,
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .params = {
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek .auth = &test_address3
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek }
86b61156743b7ebdc049450a6f88452890fd9a61Jakub Hrozek /* BODY */
48130eef6c5c64a07094b9e8582ba358b2048f24Jakub Hrozek },{
48130eef6c5c64a07094b9e8582ba358b2048f24Jakub Hrozek .input = "",
48130eef6c5c64a07094b9e8582ba358b2048f24Jakub Hrozek .caps = SMTP_CAPABILITY_8BITMIME,
48130eef6c5c64a07094b9e8582ba358b2048f24Jakub Hrozek .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .body = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .type = SMTP_PARAM_MAIL_BODY_TYPE_UNSPECIFIED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "BODY=7BIT",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_8BITMIME,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .body = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .type = SMTP_PARAM_MAIL_BODY_TYPE_7BIT,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
d921c1eba437662437847279f251a0a5d8f70127Maxim .input = "BODY=8BITMIME",
d921c1eba437662437847279f251a0a5d8f70127Maxim .caps = SMTP_CAPABILITY_8BITMIME,
d921c1eba437662437847279f251a0a5d8f70127Maxim .params = {
d921c1eba437662437847279f251a0a5d8f70127Maxim .body = {
d921c1eba437662437847279f251a0a5d8f70127Maxim .type = SMTP_PARAM_MAIL_BODY_TYPE_8BITMIME,
d921c1eba437662437847279f251a0a5d8f70127Maxim }
d921c1eba437662437847279f251a0a5d8f70127Maxim }
327127bb7fcc07f882209f029e14026de1b23c94Maxim },{
327127bb7fcc07f882209f029e14026de1b23c94Maxim .input = "BODY=BINARYMIME",
327127bb7fcc07f882209f029e14026de1b23c94Maxim .caps = SMTP_CAPABILITY_8BITMIME |
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher SMTP_CAPABILITY_BINARYMIME |
d3da1c165cdb4c1ec126a8f4b6b544ca415b9d20Pavel Březina SMTP_CAPABILITY_CHUNKING,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .body = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .type = SMTP_PARAM_MAIL_BODY_TYPE_BINARYMIME,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
eb2e21b764d03544d8161e9956d7f70b07b75f77Simo Sorce .input = "BODY=FROP",
bc9235cfb80bd64a3bfa959e8d26d5ad1be0bdf4Jakub Hrozek .caps = SMTP_CAPABILITY_8BITMIME |
bc9235cfb80bd64a3bfa959e8d26d5ad1be0bdf4Jakub Hrozek SMTP_CAPABILITY_BINARYMIME |
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher SMTP_CAPABILITY_CHUNKING,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .extensions = TRUE,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .body = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .type = SMTP_PARAM_MAIL_BODY_TYPE_EXTENSION,
4b6a0d0b3d42e5fdb457f47d9adfa5e66b160256Stephen Gallagher .ext = "FROP"
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose }
03713859dffacc7142393e53c73d8d4cf7dee8d5Pavel Březina }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* ENVID */
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_DSN,
068dbee9ca7bf5b37330eff91c94ae10f288d09fJakub Hrozek .params = {
98ce3c3e85a4bb2e1822bf8ab2a1c2ab9e3dd61dJakub Hrozek .envid = NULL,
be65f065fef1d387281096ef095a2acef39ecc12Jakub Hrozek }
e124844907ed6973915e4d56f5442ecd07535a12Jakub Hrozek },{
f36078af138f052cd9a30360867b0ebd0805af5eJakub Hrozek .input = "ENVID=",
34c78b745eb349eef2b0f13ef2b722632aebe619Jan Cholasta .caps = SMTP_CAPABILITY_DSN,
e07a94a66985b674c5df11ca466792902164c4e2George McCollister .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .envid = "",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "ENVID=AABBCCDD",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_DSN,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .envid = "AABBCCDD",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "ENVID=AA+2BBB+3DCC+2BDD",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_DSN,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .envid = "AA+BB=CC+DD",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* RET */
a5077712fc8c24e8cad08207b7b5a6603bde6a7cJakub Hrozek },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_DSN,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .ret = SMTP_PARAM_MAIL_RET_UNSPECIFIED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "RET=HDRS",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_DSN,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek .ret = SMTP_PARAM_MAIL_RET_HDRS,
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose }
6b0a7c72bb841d6885a620c68bd51d55109b66c7Jakub Hrozek },{
a679f0167b646cffdae86546ed77e105576991b0Pavel Březina .input = "RET=FULL",
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .caps = SMTP_CAPABILITY_DSN,
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .params = {
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .ret = SMTP_PARAM_MAIL_RET_FULL,
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher }
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher /* SIZE */
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher },{
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .input = "",
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .caps = SMTP_CAPABILITY_SIZE,
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .params = {
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .size = 0
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher }
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher },{
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher .input = "SIZE=267914296",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .caps = SMTP_CAPABILITY_SIZE,
539b1be3507abdf8ac235b06eeed5011b0b5cde2Ondrej Kos .params = {
539b1be3507abdf8ac235b06eeed5011b0b5cde2Ondrej Kos .size = 267914296
574a1c20f114851071ae74112b34488c3d1aeeb3Ondrej Kos }
574a1c20f114851071ae74112b34488c3d1aeeb3Ondrej Kos /* <extensions> */
574a1c20f114851071ae74112b34488c3d1aeeb3Ondrej Kos },{
574a1c20f114851071ae74112b34488c3d1aeeb3Ondrej Kos .input = "FROP=friep",
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek .caps = SMTP_CAPABILITY_SIZE,
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek .extensions = TRUE,
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek .params = {
e6e26182d58c05d896f72f2925426658a6dc70b5Jakub Hrozek .extra_params = {
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek .arr = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .buffer = &test_params_buffer1,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .element_size = sizeof(struct smtp_param)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher },{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .input = "FROP=friep FRUP=frml",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .extensions = TRUE,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .extra_params = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .arr = {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .buffer = &test_params_buffer2,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher .element_size = sizeof(struct smtp_param)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
b50dffea929ee5cd0c59ba3c4822337cc162ff92Kamil Dudka }
b50dffea929ee5cd0c59ba3c4822337cc162ff92Kamil Dudka};
b50dffea929ee5cd0c59ba3c4822337cc162ff92Kamil Dudka
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherunsigned int valid_mail_params_parse_test_count =
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher N_ELEMENTS(valid_mail_params_parse_tests);
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozekstatic void
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghertest_smtp_mail_params_auth(const struct smtp_params_mail *test,
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek const struct smtp_params_mail *parsed)
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek{
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek if (parsed->auth->localpart == NULL ||
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek test->auth->localpart == NULL) {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek test_out(t_strdup_printf("params.auth->localpart = %s",
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek parsed->auth->localpart),
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek (parsed->auth->localpart == test->auth->localpart));
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek } else {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek test_out(t_strdup_printf("params.auth->localpart = \"%s\"",
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek parsed->auth->localpart),
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek strcmp(parsed->auth->localpart,
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek test->auth->localpart) == 0);
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek }
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek if (parsed->auth->domain == NULL ||
6f51c802311fd81a409a26763ed45b28a3234d0dJakub Hrozek test->auth->domain == NULL) {
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek test_out(t_strdup_printf("params.auth->domain = %s",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher parsed->auth->domain),
4d81fe27ced3d2e96866aeaf61661a925cb8edf1Jakub Hrozek (parsed->auth->domain == test->auth->domain));
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher } else {
f5b6f977d4144c28e9c66f3f1c9d634d595d1117Marko Myllynen test_out(t_strdup_printf("params.auth->domain = \"%s\"",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher parsed->auth->domain),
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher strcmp(parsed->auth->domain,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test->auth->domain) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozekstatic void
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallaghertest_smtp_mail_params_body(const struct smtp_params_mail *test,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const struct smtp_params_mail *parsed)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher{
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const char *type_name = NULL;
3d038d2e0dc7af04ec2f7c85ae325accb39f6237Jakub Hrozek
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher switch (parsed->body.type) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher case SMTP_PARAM_MAIL_BODY_TYPE_UNSPECIFIED:
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher type_name = "<UNSPECIFIED>";
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher break;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher case SMTP_PARAM_MAIL_BODY_TYPE_7BIT:
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher type_name = "7BIT";
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek break;
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek case SMTP_PARAM_MAIL_BODY_TYPE_8BITMIME:
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek type_name = "8BITMIME";
2a5790216f57e9bdfb2930d52860bb5300366536Jakub Hrozek break;
e07a94a66985b674c5df11ca466792902164c4e2George McCollister case SMTP_PARAM_MAIL_BODY_TYPE_BINARYMIME:
e07a94a66985b674c5df11ca466792902164c4e2George McCollister type_name = "BINARYMIME";
e07a94a66985b674c5df11ca466792902164c4e2George McCollister break;
e07a94a66985b674c5df11ca466792902164c4e2George McCollister case SMTP_PARAM_MAIL_BODY_TYPE_EXTENSION:
e07a94a66985b674c5df11ca466792902164c4e2George McCollister type_name = parsed->body.ext;
e07a94a66985b674c5df11ca466792902164c4e2George McCollister break;
172c07013d1ea99447a780fd36f49d5c3a76981bJakub Hrozek default:
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_unreached();
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_out(t_strdup_printf("params.body.type = %s", type_name),
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce (parsed->body.type == test->body.type &&
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce (parsed->body.type != SMTP_PARAM_MAIL_BODY_TYPE_EXTENSION ||
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce (parsed->body.ext != NULL &&
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce strcmp(parsed->body.ext, test->body.ext) == 0))));
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce}
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce
336879aabae137f9a81304f147fb0d43001654b0Simo Sorcestatic void
336879aabae137f9a81304f147fb0d43001654b0Simo Sorcetest_smtp_mail_params_envid(const struct smtp_params_mail *test,
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce const struct smtp_params_mail *parsed)
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce{
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce if (parsed->envid == NULL ||
336879aabae137f9a81304f147fb0d43001654b0Simo Sorce test->envid == NULL) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_out(t_strdup_printf("params.auth->localpart = %s",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher parsed->envid),
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher (parsed->envid == test->envid));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher } else {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_out(t_strdup_printf("params.auth->localpart = \"%s\"",
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher parsed->envid),
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher strcmp(parsed->envid, test->envid) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher}
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic void
50fe3d79ab12b795a687b676761bef265701626aStephen Gallaghertest_smtp_mail_params_ret(const struct smtp_params_mail *test,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const struct smtp_params_mail *parsed)
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher{
8c294c1cd4d721818a59684cf7f2b36123f79163Stephen Gallagher const char *ret_name = NULL;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher switch (parsed->ret) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher case SMTP_PARAM_MAIL_RET_UNSPECIFIED:
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher ret_name = "<UNSPECIFIED>";
96c73559adfbdac96720008fc022cb1d540b53c3Jakub Hrozek break;
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher case SMTP_PARAM_MAIL_RET_HDRS:
70e59ed31c5a9c9ed02d9065ddf92be87c887efbJakub Hrozek ret_name = "HDRS";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher case SMTP_PARAM_MAIL_RET_FULL:
2a9af1f71887f02935e2fb6ad5023afba5b6d43eSumit Bose ret_name = "FULL";
2a9af1f71887f02935e2fb6ad5023afba5b6d43eSumit Bose break;
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher default:
f1ce53a3b5656361557f80f61dfd42a371230c65Stephen Gallagher i_unreached();
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher }
test_out(t_strdup_printf("params.ret = %s", ret_name),
parsed->ret == test->ret);
}
static void
test_smtp_mail_params_size(const struct smtp_params_mail *test,
const struct smtp_params_mail *parsed)
{
test_out(t_strdup_printf("params.size = %"PRIuUOFF_T, parsed->size),
parsed->size == test->size);
}
static void
test_smtp_mail_params_extensions(const struct smtp_params_mail *test,
const struct smtp_params_mail *parsed)
{
const struct smtp_param *tparam, *pparam;
unsigned int i;
if (!array_is_created(&test->extra_params) ||
array_count(&test->extra_params) == 0) {
test_out(t_strdup_printf("params.extra_params.count = %u",
(!array_is_created(&parsed->extra_params) ? 0 :
array_count(&parsed->extra_params))),
(!array_is_created(&parsed->extra_params) ||
array_count(&parsed->extra_params) == 0));
return;
}
if (!array_is_created(&parsed->extra_params) ||
array_count(&parsed->extra_params) == 0) {
test_out("params.extra_params.count = 0", FALSE);
return;
}
if (array_count(&test->extra_params) !=
array_count(&parsed->extra_params)) {
test_out(t_strdup_printf("params.extra_params.count = %u",
(!array_is_created(&parsed->extra_params) ? 0 :
array_count(&parsed->extra_params))), FALSE);
return;
}
for (i = 0; i < array_count(&test->extra_params); i++) {
tparam = array_idx(&test->extra_params, i);
pparam = array_idx(&parsed->extra_params, i);
test_out(t_strdup_printf(
"params.extra_params[%u] = [\"%s\"=\"%s\"]", i,
pparam->keyword, pparam->value),
strcmp(pparam->keyword, tparam->keyword) == 0 &&
((pparam->value == NULL && tparam->value == NULL) ||
(pparam->value != NULL && tparam->value != NULL &&
strcmp(pparam->value, tparam->value) == 0)));
}
}
static void test_smtp_mail_params_parse_valid(void)
{
unsigned int i;
for (i = 0; i < valid_mail_params_parse_test_count; i++) T_BEGIN {
const struct valid_mail_params_parse_test *test;
struct smtp_params_mail params;
enum smtp_param_parse_error error_code;
const char *error = NULL, *output;
int ret;
test = &valid_mail_params_parse_tests[i];
ret = smtp_params_mail_parse(pool_datastack_create(),
test->input, test->caps, test->extensions, &params,
&error_code, &error);
test_begin(t_strdup_printf("smtp mail params valid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")",
test->input), ret >= 0, error);
if (ret >= 0) {
string_t *encoded;
/* AUTH */
if ((test->caps & SMTP_CAPABILITY_AUTH) != 0)
test_smtp_mail_params_auth(&test->params, &params);
/* BODY */
if ((test->caps & SMTP_CAPABILITY_8BITMIME) != 0 ||
(test->caps & SMTP_CAPABILITY_BINARYMIME) != 0)
test_smtp_mail_params_body(&test->params, &params);
/* ENVID */
if ((test->caps & SMTP_CAPABILITY_DSN) != 0)
test_smtp_mail_params_envid(&test->params, &params);
/* RET */
if ((test->caps & SMTP_CAPABILITY_DSN) != 0)
test_smtp_mail_params_ret(&test->params, &params);
/* SIZE */
if ((test->caps & SMTP_CAPABILITY_SIZE) != 0)
test_smtp_mail_params_size(&test->params, &params);
/* <extensions> */
if (test->extensions)
test_smtp_mail_params_extensions(&test->params, &params);
encoded = t_str_new(256);
smtp_params_mail_write(encoded, test->caps, &params);
output = (test->output == NULL ? test->input : test->output);
test_out(t_strdup_printf
("encode() = \"%s\"", str_c(encoded)),
strcmp(str_c(encoded), output) == 0);
}
test_end();
} T_END;
}
/* Invalid mail params tests */
struct invalid_mail_params_parse_test {
const char *input;
enum smtp_capability caps;
bool extensions;
};
static const struct invalid_mail_params_parse_test
invalid_mail_params_parse_tests[] = {
/* AUTH */
{
.input = "AUTH=<>",
},{
.input = "AUTH=++",
.caps = SMTP_CAPABILITY_AUTH
/* BODY */
},{
.input = "BODY=8BITMIME",
},{
.input = "BODY=BINARYMIME",
},{
.input = "BODY=BINARYMIME",
.caps = SMTP_CAPABILITY_BINARYMIME
},{
.input = "BODY=FROP",
.caps = SMTP_CAPABILITY_8BITMIME
/* ENVID */
},{
.input = "ENVID=AABBCC",
},{
.input = "ENVID=++",
.caps = SMTP_CAPABILITY_DSN
/* RET */
},{
.input = "RET=FULL",
},{
.input = "RET=HDR",
},{
.input = "RET=FROP",
.caps = SMTP_CAPABILITY_DSN
/* SIZE */
},{
.input = "SIZE=13",
},{
.input = "SIZE=ABC",
.caps = SMTP_CAPABILITY_SIZE
}
};
unsigned int invalid_mail_params_parse_test_count =
N_ELEMENTS(invalid_mail_params_parse_tests);
static void test_smtp_mail_params_parse_invalid(void)
{
unsigned int i;
for (i = 0; i < invalid_mail_params_parse_test_count; i++) T_BEGIN {
const struct invalid_mail_params_parse_test *test;
struct smtp_params_mail params;
enum smtp_param_parse_error error_code;
const char *error = NULL;
int ret;
test = &invalid_mail_params_parse_tests[i];
ret = smtp_params_mail_parse(pool_datastack_create(),
test->input, test->caps, test->extensions, &params,
&error_code, &error);
test_begin(t_strdup_printf("smtp mail params invalid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")",
test->input), ret < 0, error);
test_end();
} T_END;
}
/* Valid rcpt params tests */
struct valid_rcpt_params_parse_test {
const char *input, *output;
enum smtp_capability caps;
bool extensions;
struct smtp_params_rcpt params;
};
static const struct valid_rcpt_params_parse_test
valid_rcpt_params_parse_tests[] = {
/* AUTH */
{
#if 0 // FIXME: message_address_parser() does not allow bare localpart
// addresses.
.input = "ORCPT=rfc822;user+2Bdetail",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.orcpt = {
.addr = &test_address2
}
}
},{
#endif
.input = "ORCPT=rfc822;e+3Dmc2@example.com",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.orcpt = {
.addr = &test_address3
}
}
/* NOTIFY */
},{
.input = "",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_UNSPECIFIED,
}
},{
.input = "NOTIFY=SUCCESS",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_SUCCESS,
}
},{
.input = "NOTIFY=FAILURE",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_FAILURE,
}
},{
.input = "NOTIFY=DELAY",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_DELAY,
}
},{
.input = "NOTIFY=NEVER",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_NEVER,
}
},{
.input = "NOTIFY=SUCCESS,FAILURE,DELAY",
.caps = SMTP_CAPABILITY_DSN,
.params = {
.notify = SMTP_PARAM_RCPT_NOTIFY_SUCCESS |
SMTP_PARAM_RCPT_NOTIFY_FAILURE |
SMTP_PARAM_RCPT_NOTIFY_DELAY,
}
/* <extensions> */
},{
.input = "FROP=friep",
.caps = SMTP_CAPABILITY_SIZE,
.extensions = TRUE,
.params = {
.extra_params = {
.arr = {
.buffer = &test_params_buffer1,
.element_size = sizeof(struct smtp_param)
}
}
}
},{
.input = "FROP=friep FRUP=frml",
.extensions = TRUE,
.params = {
.extra_params = {
.arr = {
.buffer = &test_params_buffer2,
.element_size = sizeof(struct smtp_param)
}
}
}
}
};
unsigned int valid_rcpt_params_parse_test_count =
N_ELEMENTS(valid_rcpt_params_parse_tests);
static void
test_smtp_rcpt_params_orcpt(const struct smtp_params_rcpt *test,
const struct smtp_params_rcpt *parsed)
{
if (parsed->orcpt.addr == NULL) {
test_out("params.orcpt.addr = NULL",
test->orcpt.addr == NULL);
return;
}
if (parsed->orcpt.addr->localpart == NULL ||
test->orcpt.addr->localpart == NULL) {
test_out(t_strdup_printf("params.orcpt.addr->localpart = %s",
parsed->orcpt.addr->localpart),
(parsed->orcpt.addr->localpart ==
test->orcpt.addr->localpart));
} else {
test_out(t_strdup_printf("params.orcpt.addr->localpart = \"%s\"",
parsed->orcpt.addr->localpart),
strcmp(parsed->orcpt.addr->localpart,
test->orcpt.addr->localpart) == 0);
}
if (parsed->orcpt.addr->domain == NULL ||
test->orcpt.addr->domain == NULL) {
test_out(t_strdup_printf("params.orcpt.addr->domain = %s",
parsed->orcpt.addr->domain),
(parsed->orcpt.addr->domain ==
test->orcpt.addr->domain));
} else {
test_out(t_strdup_printf("params.orcpt.addr->domain = \"%s\"",
parsed->orcpt.addr->domain),
strcmp(parsed->orcpt.addr->domain,
test->orcpt.addr->domain) == 0);
}
}
static void
test_smtp_rcpt_params_notify(const struct smtp_params_rcpt *test,
const struct smtp_params_rcpt *parsed)
{
string_t *notify_name;
notify_name = t_str_new(64);
if (parsed->notify == 0) {
str_append(notify_name, "<UNSPECIFIED>");
} else if ((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_NEVER) != 0) {
i_assert((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_SUCCESS) == 0);
i_assert((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_FAILURE) == 0);
i_assert((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_DELAY) == 0);
str_append(notify_name, "NEVER");
} else {
if ((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_SUCCESS) != 0)
str_append(notify_name, "SUCCESS");
if ((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_FAILURE) != 0) {
if (str_len(notify_name) > 0)
str_append_c(notify_name, ',');
str_append(notify_name, "FAILURE");
}
if ((parsed->notify & SMTP_PARAM_RCPT_NOTIFY_DELAY) != 0) {
if (str_len(notify_name) > 0)
str_append_c(notify_name, ',');
str_append(notify_name, "DELAY");
}
}
test_out(t_strdup_printf("params.notify = %s", str_c(notify_name)),
parsed->notify == test->notify);
}
static void
test_smtp_rcpt_params_extensions(const struct smtp_params_rcpt *test,
const struct smtp_params_rcpt *parsed)
{
const struct smtp_param *tparam, *pparam;
unsigned int i;
if (!array_is_created(&test->extra_params) ||
array_count(&test->extra_params) == 0) {
test_out(t_strdup_printf("params.extra_params.count = %u",
(!array_is_created(&parsed->extra_params) ? 0 :
array_count(&parsed->extra_params))),
(!array_is_created(&parsed->extra_params) ||
array_count(&parsed->extra_params) == 0));
return;
}
if (!array_is_created(&parsed->extra_params) ||
array_count(&parsed->extra_params) == 0) {
test_out("params.extra_params.count = 0", FALSE);
return;
}
if (array_count(&test->extra_params) !=
array_count(&parsed->extra_params)) {
test_out(t_strdup_printf("params.extra_params.count = %u",
(!array_is_created(&parsed->extra_params) ? 0 :
array_count(&parsed->extra_params))), FALSE);
return;
}
for (i = 0; i < array_count(&test->extra_params); i++) {
tparam = array_idx(&test->extra_params, i);
pparam = array_idx(&parsed->extra_params, i);
test_out(t_strdup_printf(
"params.extra_params[%u] = [\"%s\"=\"%s\"]", i,
pparam->keyword, pparam->value),
strcmp(pparam->keyword, tparam->keyword) == 0 &&
((pparam->value == NULL && tparam->value == NULL) ||
(pparam->value != NULL && tparam->value != NULL &&
strcmp(pparam->value, tparam->value) == 0)));
}
}
static void test_smtp_rcpt_params_parse_valid(void)
{
unsigned int i;
for (i = 0; i < valid_rcpt_params_parse_test_count; i++) T_BEGIN {
const struct valid_rcpt_params_parse_test *test;
struct smtp_params_rcpt params;
enum smtp_param_parse_error error_code;
const char *error = NULL, *output;
int ret;
test = &valid_rcpt_params_parse_tests[i];
ret = smtp_params_rcpt_parse(pool_datastack_create(),
test->input, test->caps, test->extensions, &params,
&error_code, &error);
test_begin(t_strdup_printf("smtp rcpt params valid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")",
test->input), ret >= 0, error);
if (ret >= 0) {
string_t *encoded;
/* ORCPT */
if ((test->caps & SMTP_CAPABILITY_DSN) != 0)
test_smtp_rcpt_params_orcpt(&test->params, &params);
/* NOTIFY */
if ((test->caps & SMTP_CAPABILITY_DSN) != 0)
test_smtp_rcpt_params_notify(&test->params, &params);
/* <extensions> */
if (test->extensions)
test_smtp_rcpt_params_extensions(&test->params, &params);
encoded = t_str_new(256);
smtp_params_rcpt_write(encoded, test->caps, &params);
output = (test->output == NULL ? test->input : test->output);
test_out(t_strdup_printf("encode() = \"%s\"",
str_c(encoded)),
strcmp(str_c(encoded), output) == 0);
}
test_end();
} T_END;
}
/* Invalid rcpt params tests */
struct invalid_rcpt_params_parse_test {
const char *input;
enum smtp_capability caps;
bool extensions;
};
static const struct invalid_rcpt_params_parse_test
invalid_rcpt_params_parse_tests[] = {
/* DSN */
{
.input = "ORCPT=rfc822;frop@example.com",
},{
.input = "ORCPT=++",
.caps = SMTP_CAPABILITY_DSN
},{
.input = "ORCPT=rfc822;++",
.caps = SMTP_CAPABILITY_DSN
},{
.input = "NOTIFY=SUCCESS",
},{
.input = "NOTIFY=FROP",
.caps = SMTP_CAPABILITY_DSN
},{
.input = "NOTIFY=NEVER,SUCCESS",
.caps = SMTP_CAPABILITY_DSN
}
};
unsigned int invalid_rcpt_params_parse_test_count =
N_ELEMENTS(invalid_rcpt_params_parse_tests);
static void test_smtp_rcpt_params_parse_invalid(void)
{
unsigned int i;
for (i = 0; i < invalid_rcpt_params_parse_test_count; i++) T_BEGIN {
const struct invalid_rcpt_params_parse_test *test;
struct smtp_params_rcpt params;
enum smtp_param_parse_error error_code;
const char *error = NULL;
int ret;
test = &invalid_rcpt_params_parse_tests[i];
ret = smtp_params_rcpt_parse(pool_datastack_create(),
test->input, test->caps, test->extensions, &params,
&error_code, &error);
test_begin(t_strdup_printf("smtp rcpt params invalid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")",
test->input), ret < 0, error);
test_end();
} T_END;
}
int main(void)
{
static void (*test_functions[])(void) = {
test_smtp_mail_params_parse_valid,
test_smtp_mail_params_parse_invalid,
test_smtp_rcpt_params_parse_valid,
test_smtp_rcpt_params_parse_invalid,
NULL
};
return test_run(test_functions);
}