test-message-header-parser.c revision ad4cd18ba6c43fe94987408f81ed6547b29132d6
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "lib.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "str.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "istream.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "message-size.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "message-header-parser.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#include "test-common.h"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen#define TEST1_MSG_BODY_LEN 5
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic const char *test1_msg =
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen "h1: v1\n"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen "h2:\n"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen " v2\r\n"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen "h3: \r\n"
0db42260be85e797aa9909a29b20296996f52e75Timo Sirainen "\tv3\n"
0db42260be85e797aa9909a29b20296996f52e75Timo Sirainen "\tw3\r\n"
0db42260be85e797aa9909a29b20296996f52e75Timo Sirainen "\n"
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen " body";
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainentest_message_header_parser_one(struct message_header_parser_ctx *parser,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen enum message_header_parser_flags hdr_flags)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen{
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_line *hdr;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen bool use_full_value;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen use_full_value = hdr_flags != 0;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0)
46e917c9fa05cbe7bddf805d3a9838b61e3960e1Timo Sirainen test_assert(hdr->full_value_offset == 4);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen else
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_offset == 5);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h1") == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0) {
46e917c9fa05cbe7bddf805d3a9838b61e3960e1Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v1", 3) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->middle_len == 3 && memcmp(hdr->middle, ": ", 3) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 2 && memcmp(hdr->value, "v1", 2) == 0);
b49aa341d28c0eec1229e30baa2f89d5bae52ff8Phil Carmody }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(!hdr->continues && !hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen !hdr->no_newline && !hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen !hdr->no_newline && !hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v2", 3) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
0ca3b9cb0f2a322a25ce7f229dc3d3a0b46be17bTimo Sirainen !hdr->no_newline && hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 3 &&
7a88e726e7300fb0273cb4e55b43c27fbd90bdbdTimo Sirainen memcmp(hdr->full_value, " v2", 3) == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen } else if (use_full_value) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 4 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\n v2", 4) == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
56d1345c43bbd28c36b7faa85e4163bd9e874290Timo Sirainen !hdr->no_newline && hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tv3", 3) == 0);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->continues && hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen !hdr->no_newline && !hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 3 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, " v3", 3) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 4 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\n\tv3", 4) == 0);
56aa97d74071f3a2987140c2ff1cfd5a59cb35aaTimo Sirainen } else if (use_full_value) {
56aa97d74071f3a2987140c2ff1cfd5a59cb35aaTimo Sirainen test_assert(hdr->full_value_len == 5 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\r\n\tv3", 5) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tw3", 3) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen !hdr->no_newline && hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 6 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, " v3 w3", 6) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 8 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\n\tv3\n\tw3", 8) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else if (use_full_value) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->full_value_len == 9 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\r\n\tv3\n\tw3", 9) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr->name_offset == 32 && hdr->full_value_offset == 32);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek test_assert(hdr->name_len == 0 && hdr->middle_len == 0 && hdr->value_len == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(!hdr->continues && !hdr->continued && hdr->eoh &&
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen !hdr->no_newline && !hdr->crlf_newline);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen}
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void test_message_header_parser(void)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen{
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen static enum message_header_parser_flags max_hdr_flags =
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MESSAGE_HEADER_PARSER_FLAG_DROP_CR |
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen enum message_header_parser_flags hdr_flags;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_parser_ctx *parser;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_size hdr_size;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct istream *input;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_begin("message header parser");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen input = test_istream_create(test1_msg);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen for (hdr_flags = 0; hdr_flags <= max_hdr_flags; hdr_flags++) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_stream_seek(input, 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen parser = message_parse_header_init(input, &hdr_size, hdr_flags);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_message_header_parser_one(parser, hdr_flags);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen message_parse_header_deinit(&parser);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr_size.physical_size == strlen(test1_msg)-TEST1_MSG_BODY_LEN);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(hdr_size.virtual_size == strlen(test1_msg) - TEST1_MSG_BODY_LEN + 4);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_stream_unref(&input);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_end();
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen}
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void hdr_write(string_t *str, struct message_header_line *hdr)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen{
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (!hdr->continued) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append(str, hdr->name);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_n(str, hdr->middle, hdr->middle_len);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_n(str, hdr->value, hdr->value_len);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (!hdr->no_newline) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (hdr->crlf_newline)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_c(str, '\r');
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_c(str, '\n');
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen}
9a1f68e5ab08eabd352d533315cba1c69006e2c1Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void test_message_header_parser_partial(void)
bdb026e2dc8a7c77585ed5ba489f0056df8074d4Timo Sirainen{
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_parser_ctx *parser;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_line *hdr;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct istream *input;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int i, max = (strlen(test1_msg)-TEST1_MSG_BODY_LEN)*2;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen string_t *str;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen int ret;
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_begin("message header parser partial");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen input = test_istream_create(test1_msg);
60d1fdf2c17fd0c7020234590dbd73da81c3ce8fTimo Sirainen test_istream_set_allow_eof(input, FALSE);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str = t_str_new(max);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen for (i = 0; i <= max; i++) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_istream_set_size(input, i/2);
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek while ((ret = message_parse_header_next(parser, &hdr)) > 0)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen hdr_write(str, hdr);
c69a177207ed18d0f0210347430a60957136bd6cJosef 'Jeff' Sipek test_assert((ret == 0 && i < max) ||
5cdb246858f37469fe61351dbc147dabbdde342cTimo Sirainen (ret < 0 && i == max));
5cdb246858f37469fe61351dbc147dabbdde342cTimo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen message_parse_header_deinit(&parser);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append(str, " body");
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(strcmp(str_c(str), test1_msg) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen i_stream_unref(&input);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_end();
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen}
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainentest_message_header_parser_long_lines_str(const char *str,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int buffer_size,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_size *size_r)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen{
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_parser_ctx *parser;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct message_header_line *hdr;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen struct istream *input;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int i, len = strlen(str);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen input = test_istream_create(str);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen test_istream_set_max_buffer_size(input, buffer_size);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen parser = message_parse_header_init(input, size_r, 0);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen for (i = 1; i <= len; i++) {
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen test_istream_set_size(input, i);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen while (message_parse_header_next(parser, &hdr) > 0) ;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen }
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen message_parse_header_deinit(&parser);
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen i_stream_unref(&input);
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen}
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainenstatic void test_message_header_parser_long_lines(void)
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek{
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen static const char *lf_str = "1234567890: 345\n\n";
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen static const char *crlf_str = "1234567890: 345\r\n\r\n";
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen struct message_size hdr_size;
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen unsigned int i, len;
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen test_begin("message header parser long lines");
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen len = strlen(lf_str);
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen for (i = 2; i < len; i++) {
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen test_message_header_parser_long_lines_str(lf_str, i, &hdr_size);
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen test_assert(hdr_size.physical_size == len);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_assert(hdr_size.virtual_size == len + 2);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen }
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen len = strlen(crlf_str);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen for (i = 2; i < len; i++) {
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_message_header_parser_long_lines_str(crlf_str, i, &hdr_size);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_assert(hdr_size.physical_size == len);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_assert(hdr_size.virtual_size == len);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen }
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_end();
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen}
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainenint main(void)
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen{
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen static void (*test_functions[])(void) = {
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_message_header_parser,
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_message_header_parser_partial,
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_message_header_parser_long_lines,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen NULL
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen };
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen return test_run(test_functions);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen}
2e708f348f20caa47210cf46cf7654f167fa47bbTimo Sirainen