test-message-header-parser.c revision d4488f314d0c799c5f5b8a7890e17eef868f7a2c
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (c) 2007-2016 Dovecot authors, see the included COPYING file */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstatic const char *test1_msg =
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainentest_message_header_parser_one(struct message_header_parser_ctx *parser,
373492be949e159fda651807b3acda2c5c077027Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h1") == 0);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v1", 3) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->middle_len == 3 && memcmp(hdr->middle, ": ", 3) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->value_len == 2 && memcmp(hdr->value, "v1", 2) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(!hdr->continues && !hdr->continued && !hdr->eoh &&
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v2", 3) == 0);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tv3", 3) == 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr->continues && hdr->continued && !hdr->eoh &&
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tw3", 3) == 0);
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
380dbb60ae291cbe39d1f710284562ca9167150bTimo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen memcmp(hdr->full_value, "\n\tv3\n\tw3", 8) == 0);
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen memcmp(hdr->full_value, "\r\n\tv3\n\tw3", 9) == 0);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen test_assert(hdr->name_offset == 32 && hdr->full_value_offset == 32);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen test_assert(hdr->name_len == 0 && hdr->middle_len == 0 && hdr->value_len == 0);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen test_assert(!hdr->continues && !hdr->continued && hdr->eoh &&
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen static enum message_header_parser_flags max_hdr_flags =
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen for (hdr_flags = 0; hdr_flags <= max_hdr_flags; hdr_flags++) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen parser = message_parse_header_init(input, &hdr_size, hdr_flags);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_message_header_parser_one(parser, hdr_flags);
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainen test_assert(hdr_size.physical_size == strlen(test1_msg)-TEST1_MSG_BODY_LEN);
3cf67672fdc87583cb23ce088c95bb5dee60e74dTimo Sirainen test_assert(hdr_size.virtual_size == strlen(test1_msg) - TEST1_MSG_BODY_LEN + 4);
1b823b2b7790a1e1b7974fcf11a4c48a28e70f37Timo Sirainenstatic void hdr_write(string_t *str, struct message_header_line *hdr)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen str_append_n(str, hdr->middle, hdr->middle_len);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen str_append_n(str, hdr->value, hdr->value_len);
57d2429fae575e96ca276355af675deb66b76d00Timo Sirainenstatic void test_message_header_parser_partial(void)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen unsigned int i, max = (strlen(test1_msg)-TEST1_MSG_BODY_LEN)*2;
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen for (i = 0; i <= max; i++) {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen while ((ret = message_parse_header_next(parser, &hdr)) > 0)
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(strcmp(str_c(str), test1_msg) == 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainentest_message_header_parser_long_lines_str(const char *str,
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_istream_set_max_buffer_size(input, buffer_size);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen parser = message_parse_header_init(input, size_r, 0);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen while (message_parse_header_next(parser, &hdr) > 0) ;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainenstatic void test_message_header_parser_long_lines(void)
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen static const char *lf_str = "1234567890: 345\n\n";
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen static const char *crlf_str = "1234567890: 345\r\n\r\n";
c6f894e1522f7b0b6068c228900914073c145175Timo Sirainen unsigned int i, len;
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_begin("message header parser long lines");
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_message_header_parser_long_lines_str(lf_str, i, &hdr_size);
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen test_assert(hdr_size.virtual_size == len + 2);
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen test_message_header_parser_long_lines_str(crlf_str, i, &hdr_size);
383d0e8c24451468d6bea17e4b55d74de744abe6Timo Sirainenstatic void test_message_header_parser_extra_cr_in_eoh(void)
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen test_begin("message header parser extra CR in EOH");
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen parser = message_parse_header_init(input, NULL, 0);
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen strcmp(hdr->name, "\r") == 0 && hdr->middle_len == 0 &&
15f526e5ac611b4532568d131fcd0abf664abe41Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
0f62889d833767acf9c2ad010c3269806b4cfae3Timo Sirainenstatic void test_message_header_parser_no_eoh(void)
7289c5600711b45f30fe289ab5b0293b51d87041Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
9dd1c256910f1fb42823116a641e7edb3ad11970Timo Sirainen static void (*test_functions[])(void) = {