test-message-header-parser.c revision ad4cd18ba6c43fe94987408f81ed6547b29132d6
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic const char *test1_msg =
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainentest_message_header_parser_one(struct message_header_parser_ctx *parser,
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) > 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0)
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 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);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(!hdr->continues && !hdr->continued && !hdr->eoh &&
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);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
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 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
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);
16cb5d65265dd0b216542803fd80c4b999ae118eTimo Sirainen test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if (use_full_value) hdr->use_full_value = TRUE;
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 if (use_full_value) hdr->use_full_value = TRUE;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
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 if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen } else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\n\tv3\n\tw3", 8) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen memcmp(hdr->full_value, "\r\n\tv3\n\tw3", 9) == 0);
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 &&
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(message_parse_header_next(parser, &hdr) < 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen static enum message_header_parser_flags max_hdr_flags =
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen for (hdr_flags = 0; hdr_flags <= max_hdr_flags; hdr_flags++) {
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen parser = message_parse_header_init(input, &hdr_size, hdr_flags);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_message_header_parser_one(parser, hdr_flags);
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 Sirainenstatic void hdr_write(string_t *str, struct message_header_line *hdr)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_n(str, hdr->middle, hdr->middle_len);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen str_append_n(str, hdr->value, hdr->value_len);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainenstatic void test_message_header_parser_partial(void)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen unsigned int i, max = (strlen(test1_msg)-TEST1_MSG_BODY_LEN)*2;
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen parser = message_parse_header_init(input, NULL, 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen for (i = 0; i <= max; i++) {
95d62f8d6d281cc488dc4f488d4388701e559012Josef 'Jeff' Sipek while ((ret = message_parse_header_next(parser, &hdr)) > 0)
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainen test_assert(strcmp(str_c(str), test1_msg) == 0);
01435c38e7d671d5a892c4b802cfb204881cd454Timo Sirainentest_message_header_parser_long_lines_str(const char *str,
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen test_istream_set_max_buffer_size(input, buffer_size);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen parser = message_parse_header_init(input, size_r, 0);
5d4a0dac041964a04405bc6b94de51315ca917afTimo Sirainen while (message_parse_header_next(parser, &hdr) > 0) ;
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainenstatic void test_message_header_parser_long_lines(void)
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 unsigned int i, len;
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen test_begin("message header parser long lines");
050acc3755ccc5bcf261a6de3250164a54a0b6f3Timo Sirainen test_message_header_parser_long_lines_str(lf_str, i, &hdr_size);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_assert(hdr_size.virtual_size == len + 2);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen test_message_header_parser_long_lines_str(crlf_str, i, &hdr_size);
cf35bb040d505e2fc333470850c65029992778a4Timo Sirainen static void (*test_functions[])(void) = {