bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen#include "lib.h"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen#include "istream.h"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen#include "message-parser.h"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen#include "test-common.h"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainenstatic const char test_msg[] =
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"From user@domain Fri Feb 22 17:06:23 2008\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"From: user@domain.org\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Date: Sat, 24 Mar 2007 23:00:00 +0200\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Mime-Version: 1.0\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Content-Type: multipart/mixed; boundary=\"foo bar\"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Root MIME prologue\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--foo bar\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Content-Type: text/x-myown; charset=us-ascii\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"hello\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--foo bar\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Content-Type: message/rfc822\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"From: sub@domain.org\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Date: Sun, 12 Aug 2012 12:34:56 +0300\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Subject: submsg\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Content-Type: multipart/alternative; boundary=\"sub1\"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Sub MIME prologue\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--sub1\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Content-Type: text/html\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"<p>Hello world</p>\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--sub1\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"Content-Type: multipart/alternative; boundary=\"sub2\"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--sub2\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"Content-Type: multipart/alternative; boundary=\"sub3\"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--sub3\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"sub3 text\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--sub3\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"sub3 text2\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--sub3--\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"sub2 text\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--sub2\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"sub2 text2\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--sub1--\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Sub MIME epilogue\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"--foo bar\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"Content-Type: text/plain\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"\n"
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen"Another part\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"--foo bar--\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"Root MIME epilogue\n"
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen"\n";
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen#define TEST_MSG_LEN (sizeof(test_msg)-1)
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainenstatic void test_message_part_idx(void)
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen{
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen struct message_parser_ctx *parser;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen struct istream *input;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen struct message_part *parts, *part, *prev_part;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen struct message_block block;
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen unsigned int i, prev_idx = 0, part_idx;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen pool_t pool;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen int ret;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_begin("message part indexes");
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen pool = pool_alloconly_create("message parser", 10240);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen input = i_stream_create_from_data(test_msg, TEST_MSG_LEN);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen parser = message_parser_init(pool, input, 0, 0);
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen part_idx = message_part_to_idx(block.part);
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen test_assert(part_idx >= prev_idx);
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen prev_idx = part_idx;
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen }
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_assert(ret < 0);
12e5ac049bd74f8b98d9dc62adcb0bf3217beef6Martti Rannanjärvi message_parser_deinit(&parser, &parts);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen part = message_part_by_idx(parts, 0);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_assert(part == parts);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_assert(message_part_by_idx(parts, 1) == parts->children);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
5199b576507dab5a82100e450920899ea21b3e43Timo Sirainen for (i = 1; i < 11; i++) {
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen prev_part = part;
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen part = message_part_by_idx(parts, i);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_assert(part != NULL);
d8fc51fca32881dd6d63519122eec15e185350bfTimo Sirainen test_assert(part != NULL && message_part_to_idx(part) == i);
846adf6cf365b18b899829307106db408f5954a3Timo Sirainen test_assert(part != NULL && prev_part != NULL &&
d8fc51fca32881dd6d63519122eec15e185350bfTimo Sirainen prev_part->physical_pos < part->physical_pos);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen }
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_assert(message_part_by_idx(parts, i) == NULL);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen i_stream_unref(&input);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen pool_unref(&pool);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_end();
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen}
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainenint main(void)
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen{
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen test_message_part_idx,
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen NULL
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen };
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen return test_run(test_functions);
6b8d5b8d02bd888609f06366ffb5a5a1e6e7d251Timo Sirainen}