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