bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainentest_message_header_parser_one(struct message_header_parser_ctx *parser,
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0)
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h1") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v1", 3) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 3 && memcmp(hdr->middle, ": ", 3) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->value_len == 2 && memcmp(hdr->value, "v1", 2) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(!hdr->continues && !hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v2", 3) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tv3", 3) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->continues && hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tw3", 3) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen memcmp(hdr->full_value, "\n\tv3\n\tw3", 8) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen memcmp(hdr->full_value, "\r\n\tv3\n\tw3", 9) == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_offset == 32 && hdr->full_value_offset == 32);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr->name_len == 0 && hdr->middle_len == 0 && hdr->value_len == 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(!hdr->continues && !hdr->continued && hdr->eoh &&
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen static enum message_header_parser_flags max_hdr_flags =
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen for (hdr_flags = 0; hdr_flags <= max_hdr_flags; hdr_flags++) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen parser = message_parse_header_init(input, &hdr_size, hdr_flags);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_message_header_parser_one(parser, hdr_flags);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr_size.physical_size == strlen(test1_msg)-TEST1_MSG_BODY_LEN);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(hdr_size.virtual_size == strlen(test1_msg) - TEST1_MSG_BODY_LEN + 4);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainenstatic void hdr_write(string_t *str, struct message_header_line *hdr)
d4488f314d0c799c5f5b8a7890e17eef868f7a2cMartti Rannanjärvi str_append_n(str, hdr->middle, hdr->middle_len);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen str_append_n(str, hdr->value, hdr->value_len);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainenstatic void test_message_header_parser_partial(void)
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen unsigned int i, max = (strlen(test1_msg)-TEST1_MSG_BODY_LEN)*2;
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen for (i = 0; i <= max; i++) {
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen while ((ret = message_parse_header_next(parser, &hdr)) > 0)
cf29b7d24ada0c23d9c2ad2fee75b164583572f5Timo Sirainen test_assert(strcmp(str_c(str), test1_msg) == 0);
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainentest_message_header_parser_long_lines_str(const char *str,
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen unsigned int i;
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen test_istream_set_max_buffer_size(input, buffer_size);
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen parser = message_parse_header_init(input, size_r, 0);
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen while (message_parse_header_next(parser, &hdr) > 0) ;
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainenstatic void test_message_header_parser_long_lines(void)
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen static const char *lf_str = "1234567890: 345\n\n";
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen static const char *crlf_str = "1234567890: 345\r\n\r\n";
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen test_begin("message header parser long lines");
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen test_message_header_parser_long_lines_str(lf_str, i, &hdr_size);
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen test_assert(hdr_size.virtual_size == len + 2);
f16712057c1b82c6d2a3a4267c4521d357cd4b4cTimo Sirainen test_message_header_parser_long_lines_str(crlf_str, i, &hdr_size);
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainenstatic void test_message_header_parser_extra_cr_in_eoh(void)
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen test_begin("message header parser extra CR in EOH");
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen strcmp(hdr->name, "\r") == 0 && hdr->middle_len == 0 &&
bca734e149473b13bb4d454865e9e23d7a05ca10Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
4b994089fc7e6f7f42f1d7932e893c2b13ed1451Timo Sirainenstatic void test_message_header_parser_no_eoh(void)
4b994089fc7e6f7f42f1d7932e893c2b13ed1451Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
4b994089fc7e6f7f42f1d7932e893c2b13ed1451Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0 &&
4b994089fc7e6f7f42f1d7932e893c2b13ed1451Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {