From: Stephan Bosch Date: Sat, 14 Jan 2017 12:33:05 +0000 (+0100) Subject: lib-imap: Created test-imap-envelope. X-Git-Tag: 2.3.0.rc1~2219 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30116399ebac74cf4ea0daefb65c9fba742e0773;p=thirdparty%2Fdovecot%2Fcore.git lib-imap: Created test-imap-envelope. --- diff --git a/src/lib-imap/Makefile.am b/src/lib-imap/Makefile.am index 7eb4cedc20..45b62973b0 100644 --- a/src/lib-imap/Makefile.am +++ b/src/lib-imap/Makefile.am @@ -44,6 +44,7 @@ pkginc_lib_HEADERS = $(headers) test_programs = \ test-imap-bodystructure \ + test-imap-envelope \ test-imap-match \ test-imap-parser \ test-imap-quote \ @@ -63,6 +64,10 @@ test_imap_bodystructure_SOURCES = test-imap-bodystructure.c test_imap_bodystructure_LDADD = imap-bodystructure.lo imap-envelope.lo imap-quote.lo imap-parser.lo imap-arg.lo ../lib-mail/libmail.la $(test_libs) test_imap_bodystructure_DEPENDENCIES = $(test_deps) ../lib-mail/libmail.la +test_imap_envelope_SOURCES = test-imap-envelope.c +test_imap_envelope_LDADD = imap-envelope.lo imap-quote.lo imap-parser.lo imap-arg.lo ../lib-mail/libmail.la $(test_libs) +test_imap_envelope_DEPENDENCIES = $(test_deps) ../lib-mail/libmail.la + test_imap_match_SOURCES = test-imap-match.c test_imap_match_LDADD = imap-match.lo $(test_libs) test_imap_match_DEPENDENCIES = $(test_deps) diff --git a/src/lib-imap/test-imap-envelope.c b/src/lib-imap/test-imap-envelope.c new file mode 100644 index 0000000000..0f9aaefc73 --- /dev/null +++ b/src/lib-imap/test-imap-envelope.c @@ -0,0 +1,203 @@ +/* Copyright (c) 2013-2017 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "istream.h" +#include "str.h" +#include "message-part-data.h" +#include "message-parser.h" +#include "imap-envelope.h" +#include "test-common.h" + +struct parse_test { + const char *message; + const char *envelope; +}; + +struct parse_test parse_tests[] = { + /* Tests copied from imaptest */ + { + .message = + "Message-ID: \n" + "In-Reply-To: \n" + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "Subject: subject header\n" + "From: From Real \n" + "To: To Real \n" + "Cc: Cc Real \n" + "Bcc: Bcc Real \n" + "Sender: Sender Real \n" + "Reply-To: ReplyTo Real \n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" " + "\"subject header\" " + "((\"From Real\" NIL \"fromuser\" \"fromdomain.org\")) " + "((\"Sender Real\" NIL \"senderuser\" \"senderdomain.org\")) " + "((\"ReplyTo Real\" NIL \"replytouser\" \"replytodomain.org\")) " + "((\"To Real\" NIL \"touser\" \"todomain.org\")) " + "((\"Cc Real\" NIL \"ccuser\" \"ccdomain.org\")) " + "((\"Bcc Real\" NIL \"bccuser\" \"bccdomain.org\")) " + "\"\" \"\"" + }, { + .message = + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "From: user@domain\n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" NIL " + "((NIL NIL \"user\" \"domain\")) " + "((NIL NIL \"user\" \"domain\")) " + "((NIL NIL \"user\" \"domain\")) NIL NIL NIL NIL NIL" + }, { + .message = + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "From: user@domain\n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" NIL " + "((NIL NIL \"user\" \"domain\")) " + "((NIL NIL \"user\" \"domain\")) " + "((NIL NIL \"user\" \"domain\")) NIL NIL NIL NIL NIL" + }, { + .message = + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "From: user@domain (Real Name)\n" + "To: group: g1@d1.org, g2@d2.org;, group2: g3@d3.org;\n" + "Cc: group:;, group2: (foo) ;\n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" NIL " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "((NIL NIL \"group\" NIL)" + "(NIL NIL \"g1\" \"d1.org\")" + "(NIL NIL \"g2\" \"d2.org\")" + "(NIL NIL NIL NIL)" + "(NIL NIL \"group2\" NIL)" + "(NIL NIL \"g3\" \"d3.org\")" + "(NIL NIL NIL NIL)) " + "((NIL NIL \"group\" NIL)(NIL NIL NIL NIL)" + "(NIL NIL \"group2\" NIL)(NIL NIL NIL NIL)) " + "NIL NIL NIL" + }, { + .message = + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "From: user@domain (Real Name)\n" + "Sender: \n" + "Reply-To: \n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" NIL " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "((\"Real Name\" NIL \"user\" \"domain\")) " + "NIL NIL NIL NIL NIL" + }, { + .message = + "Date: Thu, 15 Feb 2007 01:02:03 +0200\n" + "From: <@route:user@domain>\n" + "\n" + "body\n", + .envelope = + "\"Thu, 15 Feb 2007 01:02:03 +0200\" NIL " + "((NIL \"@route\" \"user\" \"domain\")) " + "((NIL \"@route\" \"user\" \"domain\")) " + "((NIL \"@route\" \"user\" \"domain\")) " + "NIL NIL NIL NIL NIL" + } +}; + +static const unsigned int parse_tests_count = N_ELEMENTS(parse_tests); + +static struct message_part_envelope * +msg_parse(pool_t pool, const char *message) +{ + struct message_parser_ctx *parser; + struct message_part_envelope *envlp = NULL; + struct istream *input; + struct message_block block; + struct message_part *parts; + int ret; + + input = i_stream_create_from_data(message, strlen(message)); + parser = message_parser_init(pool, input, + MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP | + MESSAGE_HEADER_PARSER_FLAG_DROP_CR, + MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK); + while ((ret = message_parser_parse_next_block(parser, &block)) > 0) { + i_assert(block.part->parent == NULL); + message_part_envelope_parse_from_header(pool, &envlp, block.hdr); + } + test_assert(ret < 0); + + message_parser_deinit(&parser, &parts); + i_stream_unref(&input); + return envlp; +} + +static void test_imap_envelope_write(void) +{ + struct message_part_envelope *envlp; + unsigned int i; + + for (i = 0; i < parse_tests_count; i++) T_BEGIN { + struct parse_test *test = &parse_tests[i]; + string_t *str = t_str_new(128); + pool_t pool = pool_alloconly_create("imap envelope write", 1024); + + test_begin(t_strdup_printf("imap envelope write [%u]", i)); + envlp = msg_parse(pool, test->message); + + imap_envelope_write(envlp, str); + test_assert(strcmp(str_c(str), test->envelope) == 0); + + pool_unref(&pool); + test_end(); + } T_END; +} + +static void test_imap_envelope_parse(void) +{ + struct message_part_envelope *envlp; + const char *error; + unsigned int i; + bool ret; + + for (i = 0; i < parse_tests_count; i++) T_BEGIN { + struct parse_test *test = &parse_tests[i]; + string_t *str = t_str_new(128); + pool_t pool = pool_alloconly_create("imap envelope parse", 1024); + + test_begin(t_strdup_printf("imap envelope parser [%u]", i)); + + ret = imap_envelope_parse(test->envelope, pool, &envlp, &error); + test_assert(ret); + + if (ret) { + str_truncate(str, 0); + imap_envelope_write(envlp, str); + test_assert(strcmp(str_c(str), test->envelope) == 0); + } else { + i_error("Invalid envelope: %s", error); + } + + pool_unref(&pool); + test_end(); + } T_END; +} + +int main(void) +{ + static void (*const test_functions[])(void) = { + test_imap_envelope_write, + test_imap_envelope_parse, + NULL + }; + return test_run(test_functions); +}