]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Improve test-message-decoder unit test
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 11 Feb 2020 13:51:47 +0000 (15:51 +0200)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 12 Feb 2020 13:28:33 +0000 (15:28 +0200)
src/lib-mail/test-message-decoder.c

index 4d661a0a424f4f9c31fd6caaa0f14bacf5b2437d..e1faca29b4fe740678fe7af1bafebae5d94c6592 100644 (file)
@@ -1,7 +1,8 @@
 /* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
-#include "buffer.h"
+#include "str.h"
+#include "istream.h"
 #include "charset-utf8.h"
 #include "message-parser.h"
 #include "message-header-decode.h"
@@ -82,6 +83,66 @@ static void test_message_decoder(void)
        test_end();
 }
 
+static void test_message_decoder_multipart(void)
+{
+       static const char test_message_input[] =
+               "Content-Type: multipart/mixed; boundary=foo\n"
+               "\n"
+               "--foo\n"
+               "Content-Transfer-Encoding: quoted-printable\n"
+               "Content-Type: text/plain; charset=utf-8\n"
+               "\n"
+               "p=C3=A4iv=C3=A4=C3=A4\n"
+               "\n"
+               "--foo\n"
+               "Content-Transfer-Encoding: base64\n"
+               "Content-Type: text/plain; charset=utf-8\n"
+               "\n"
+               "ecO2dMOkIHZhYW4uCg== ignored\n"
+               "--foo\n"
+               "Content-Transfer-Encoding: base64\n"
+               "Content-Type: text/plain; charset=utf-8\n"
+               "\n"
+               "?garbage\n"
+               "--foo--\n";
+       struct message_parser_ctx *parser;
+       struct message_decoder_context *decoder;
+       struct message_part *parts;
+       struct message_block input, output;
+       struct istream *istream;
+       string_t *str_out = t_str_new(20);
+       int ret;
+
+       test_begin("message decoder multipart");
+
+       istream = test_istream_create(test_message_input);
+       parser = message_parser_init(pool_datastack_create(), istream, 0, 0);
+       decoder = message_decoder_init(NULL, 0);
+
+       test_istream_set_allow_eof(istream, FALSE);
+       for (size_t i = 0; i < sizeof(test_message_input); i++) {
+               if (i == sizeof(test_message_input)-1)
+                       test_istream_set_allow_eof(istream, TRUE);
+               test_istream_set_size(istream, i);
+               while ((ret = message_parser_parse_next_block(parser, &input)) > 0) {
+                       if (message_decoder_decode_next_block(decoder, &input, &output) &&
+                           output.hdr == NULL && output.size > 0)
+                               str_append_data(str_out, output.data, output.size);
+               }
+               if (i == sizeof(test_message_input)-1)
+                       test_assert(ret == -1);
+               else
+                       test_assert(ret == 0);
+       }
+       /* NOTE: qp-decoder decoder changes \n into \r\n */
+       test_assert_strcmp(str_c(str_out), "p\xC3\xA4iv\xC3\xA4\xC3\xA4\r\ny\xC3\xB6t\xC3\xA4 vaan.\n");
+
+       message_decoder_deinit(&decoder);
+       message_parser_deinit(&parser, &parts);
+       i_stream_unref(&istream);
+       test_end();
+}
+
 static void test_message_decoder_current_content_type(void)
 {
        struct message_decoder_context *ctx;
@@ -149,6 +210,7 @@ int main(void)
 {
        static void (*const test_functions[])(void) = {
                test_message_decoder,
+               test_message_decoder_multipart,
                test_message_decoder_current_content_type,
                NULL
        };