dovecot-2.0: Added unit test for message-header-parser.

dovecot at dovecot.org dovecot at dovecot.org
Tue May 26 23:52:04 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/75a8a2a8103f
changeset: 9392:75a8a2a8103f
user:      Timo Sirainen <tss at iki.fi>
date:      Tue May 26 16:51:58 2009 -0400
description:
Added unit test for message-header-parser.

diffstat:

2 files changed, 212 insertions(+)
src/lib-mail/Makefile.am                  |    5 
src/lib-mail/test-message-header-parser.c |  207 +++++++++++++++++++++++++++++

diffs (233 lines):

diff -r d9ce4c485dee -r 75a8a2a8103f src/lib-mail/Makefile.am
--- a/src/lib-mail/Makefile.am	Tue May 26 16:51:08 2009 -0400
+++ b/src/lib-mail/Makefile.am	Tue May 26 16:51:58 2009 -0400
@@ -53,6 +53,7 @@ test_programs = \
 	test-istream-header-filter \
 	test-message-address \
 	test-message-date \
+	test-message-header-parser \
 	test-message-parser \
 	test-rfc2231-parser
 
@@ -74,6 +75,10 @@ test_message_date_LDADD = message-date.l
 test_message_date_LDADD = message-date.lo rfc822-parser.lo $(test_libs)
 test_message_date_DEPENDENCIES = message-date.lo rfc822-parser.lo $(test_libs)
 
+test_message_header_parser_SOURCES = test-message-header-parser.c
+test_message_header_parser_LDADD = message-header-parser.lo $(test_libs)
+test_message_header_parser_DEPENDENCIES = message-header-parser.lo $(test_libs)
+
 test_message_parser_SOURCES = test-message-parser.c
 test_message_parser_LDADD = message-parser.lo message-header-parser.lo message-size.lo rfc822-parser.lo rfc2231-parser.lo $(test_libs)
 test_message_parser_DEPENDENCIES = message-parser.lo message-header-parser.lo message-size.lo rfc822-parser.lo rfc2231-parser.lo $(test_libs)
diff -r d9ce4c485dee -r 75a8a2a8103f src/lib-mail/test-message-header-parser.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-mail/test-message-header-parser.c	Tue May 26 16:51:58 2009 -0400
@@ -0,0 +1,207 @@
+/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "str.h"
+#include "istream.h"
+#include "message-size.h"
+#include "message-header-parser.h"
+#include "test-common.h"
+
+#define TEST1_MSG_BODY_LEN 5
+static const char *test1_msg =
+	"h1:  v1\n"
+	"h2:\n"
+	" v2\r\n"
+	"h3: \r\n"
+	"\tv3\n"
+	"\tw3\r\n"
+	"\n"
+	" body";
+
+static void
+test_message_header_parser_one(struct message_header_parser_ctx *parser,
+			       enum message_header_parser_flags hdr_flags)
+{
+	struct message_header_line *hdr;
+	bool use_full_value;
+
+	use_full_value = hdr_flags != 0;
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 0);
+	if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0)
+		test_assert(hdr->full_value_offset == 4);
+	else
+		test_assert(hdr->full_value_offset == 5);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h1") == 0);
+	if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP) == 0) {
+		test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
+		test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v1", 3) == 0);
+	} else {
+		test_assert(hdr->middle_len == 3 && memcmp(hdr->middle, ":  ", 3) == 0);
+		test_assert(hdr->value_len == 2 && memcmp(hdr->value, "v1", 2) == 0);
+	}
+	test_assert(!hdr->continues && !hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && !hdr->crlf_newline);
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
+	test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
+	test_assert(hdr->value_len == 0);
+	test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && !hdr->crlf_newline);
+	if (use_full_value) hdr->use_full_value = TRUE;
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 8 && hdr->full_value_offset == 11);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h2") == 0);
+	test_assert(hdr->middle_len == 1 && memcmp(hdr->middle, ":", 1) == 0);
+	test_assert(hdr->value_len == 3 && memcmp(hdr->value, " v2", 3) == 0);
+	test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && hdr->crlf_newline);
+	if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
+		test_assert(hdr->full_value_len == 3 &&
+			    memcmp(hdr->full_value, " v2", 3) == 0);
+	} else if (use_full_value) {
+		test_assert(hdr->full_value_len == 4 &&
+			    memcmp(hdr->full_value, "\n v2", 4) == 0);
+	}
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
+	test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
+	test_assert(hdr->value_len == 0);
+	test_assert(hdr->continues && !hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && hdr->crlf_newline);
+	if (use_full_value) hdr->use_full_value = TRUE;
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
+	test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
+	if (!(hdr->value_len == 3 && memcmp(hdr->value, "\tv3", 3) == 0))
+		i_warning("%d '%.*s'", hdr->value_len, 3, hdr->value);
+	test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tv3", 3) == 0);
+	test_assert(hdr->continues && hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && !hdr->crlf_newline);
+	if (use_full_value) hdr->use_full_value = TRUE;
+	if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
+		test_assert(hdr->full_value_len == 3 &&
+			    memcmp(hdr->full_value, " v3", 3) == 0);
+	} else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
+		test_assert(hdr->full_value_len == 4 &&
+			    memcmp(hdr->full_value, "\n\tv3", 4) == 0);
+	} else if (use_full_value) {
+		test_assert(hdr->full_value_len == 5 &&
+			    memcmp(hdr->full_value, "\r\n\tv3", 5) == 0);
+	}
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 17 && hdr->full_value_offset == 21);
+	test_assert(hdr->name_len == 2 && strcmp(hdr->name, "h3") == 0);
+	test_assert(hdr->middle_len == 2 && memcmp(hdr->middle, ": ", 2) == 0);
+	test_assert(hdr->value_len == 3 && memcmp(hdr->value, "\tw3", 3) == 0);
+	test_assert(!hdr->continues && hdr->continued && !hdr->eoh &&
+		    !hdr->no_newline && hdr->crlf_newline);
+	if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE) != 0) {
+		test_assert(hdr->full_value_len == 6 &&
+			    memcmp(hdr->full_value, " v3 w3", 6) == 0);
+	} else if ((hdr_flags & MESSAGE_HEADER_PARSER_FLAG_DROP_CR) != 0) {
+		test_assert(hdr->full_value_len == 8 &&
+			    memcmp(hdr->full_value, "\n\tv3\n\tw3", 8) == 0);
+	} else if (use_full_value) {
+		test_assert(hdr->full_value_len == 9 &&
+			    memcmp(hdr->full_value, "\r\n\tv3\n\tw3", 9) == 0);
+	}
+
+	test_assert(message_parse_header_next(parser, &hdr) > 0);
+	test_assert(hdr->name_offset == 32 && hdr->full_value_offset == 32);
+	test_assert(hdr->name_len == 0 && hdr->middle_len == 0 && hdr->value_len == 0);
+	test_assert(!hdr->continues && !hdr->continued && hdr->eoh &&
+		    !hdr->no_newline && !hdr->crlf_newline);
+
+	test_assert(message_parse_header_next(parser, &hdr) < 0);
+}
+
+static void test_message_header_parser(void)
+{
+	static enum message_header_parser_flags max_hdr_flags =
+		MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
+		MESSAGE_HEADER_PARSER_FLAG_DROP_CR |
+		MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE;
+	enum message_header_parser_flags hdr_flags;
+	struct message_header_parser_ctx *parser;
+	struct message_size hdr_size;
+	struct istream *input;
+
+	test_begin("message header parser");
+	input = test_istream_create(test1_msg);
+
+	for (hdr_flags = 0; hdr_flags <= max_hdr_flags; hdr_flags++) {
+		i_stream_seek(input, 0);
+		parser = message_parse_header_init(input, &hdr_size, hdr_flags);
+		test_message_header_parser_one(parser, hdr_flags);
+		message_parse_header_deinit(&parser);
+	}
+	test_assert(hdr_size.physical_size == strlen(test1_msg)-TEST1_MSG_BODY_LEN);
+	test_assert(hdr_size.virtual_size == strlen(test1_msg) - TEST1_MSG_BODY_LEN + 4);
+
+	i_stream_unref(&input);
+	test_end();
+}
+
+static void hdr_write(string_t *str, struct message_header_line *hdr)
+{
+	if (!hdr->continued) {
+		str_append(str, hdr->name);
+		str_append_n(str, hdr->middle, hdr->middle_len);
+	}
+	str_append_n(str, hdr->value, hdr->value_len);
+	if (!hdr->no_newline) {
+		if (hdr->crlf_newline)
+			str_append_c(str, '\r');
+		str_append_c(str, '\n');
+	}
+}
+
+static void test_message_header_parser_partial(void)
+{
+	struct message_header_parser_ctx *parser;
+	struct message_header_line *hdr;
+	struct istream *input;
+	unsigned int i, max = (strlen(test1_msg)-TEST1_MSG_BODY_LEN)*2;
+	string_t *str;
+	int ret;
+
+	test_begin("message header parser partial");
+	input = test_istream_create(test1_msg);
+	test_istream_set_allow_eof(input, FALSE);
+
+	str = t_str_new(max);
+	parser = message_parse_header_init(input, NULL, 0);
+	for (i = 0; i <= max; i++) {
+		test_istream_set_size(input, i/2);
+		while ((ret = message_parse_header_next(parser, &hdr)) > 0)
+			hdr_write(str, hdr);
+		test_assert((ret == 0 && i < max) ||
+			    (ret < 0 && i == max));
+	}
+	message_parse_header_deinit(&parser);
+
+	str_append(str, " body");
+	test_assert(strcmp(str_c(str), test1_msg) == 0);
+	i_stream_unref(&input);
+	test_end();
+}
+
+int main(void)
+{
+	static void (*test_functions[])(void) = {
+		test_message_header_parser,
+		test_message_header_parser_partial,
+		NULL
+	};
+	return test_run(test_functions);
+}


More information about the dovecot-cvs mailing list