dovecot: Added istream-crlf unit test.

dovecot at dovecot.org dovecot at dovecot.org
Thu Nov 8 01:59:10 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/76fc7245e74b
changeset: 6724:76fc7245e74b
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Nov 08 01:59:05 2007 +0200
description:
Added istream-crlf unit test.

diffstat:

4 files changed, 106 insertions(+), 2 deletions(-)
src/tests/Makefile.am    |    5 ++
src/tests/test-istream.c |   95 ++++++++++++++++++++++++++++++++++++++++++++++
src/tests/test-lib.c     |    4 -
src/tests/test-lib.h     |    4 +

diffs (151 lines):

diff -r ba048bb01842 -r 76fc7245e74b src/tests/Makefile.am
--- a/src/tests/Makefile.am	Thu Nov 08 01:58:04 2007 +0200
+++ b/src/tests/Makefile.am	Thu Nov 08 01:59:05 2007 +0200
@@ -12,6 +12,7 @@ libtest_a_SOURCES = \
 	test-common.c
 
 test_lib_SOURCES = \
+	test-istream.c \
 	test-lib.c
 
 test_lib_LDADD = \
@@ -21,6 +22,10 @@ test_mail_SOURCES = \
 test_mail_SOURCES = \
 	test-mail.c
 
+noinst_HEADERS = \
+	test-common.h \
+	test-lib.h
+
 test_mail_LDADD = \
 	libtest.a \
 	../lib-mail/libmail.a \
diff -r ba048bb01842 -r 76fc7245e74b src/tests/test-istream.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tests/test-istream.c	Thu Nov 08 01:59:05 2007 +0200
@@ -0,0 +1,95 @@
+/* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "str.h"
+#include "istream-internal.h"
+#include "istream-crlf.h"
+
+static void test_istream_crlf_input(const char *input, unsigned int num)
+{
+	string_t *output;
+	const unsigned char *data;
+	size_t size;
+	ssize_t ret;
+	unsigned int i, j, pos, input_len = strlen(input);
+	struct istream *istream, *crlf_istream;
+	bool success;
+
+	output = t_str_new(256);
+
+	for (j = 0; j < 2; j++) {
+		istream = i_stream_create_from_data(input, input_len);
+		success = TRUE;
+		str_truncate(output, 0);
+		if (j == 0) {
+			/* drop CRs */
+			crlf_istream = i_stream_create_lf(istream);
+			for (i = 0; i < input_len; i++) {
+				if (input[i] == '\r' &&
+				    (i == input_len || input[i+1] == '\n'))
+					;
+				else
+					str_append_c(output, input[i]);
+			}
+		} else {
+			/* add missing CRs */
+			crlf_istream = i_stream_create_crlf(istream);
+			for (i = 0; i < input_len; i++) {
+				if (input[i] == '\n' &&
+				    (i == 0 || input[i-1] != '\r'))
+					str_append_c(output, '\r');
+				str_append_c(output, input[i]);
+			}
+		}
+
+		pos = 0;
+		for (i = 1; i <= input_len; i++) {
+			istream->real_stream->pos = i;
+			if (crlf_istream->real_stream->buffer_size != 0) {
+				/* this is pretty evil */
+				crlf_istream->real_stream->buffer_size =
+					I_MAX(crlf_istream->real_stream->pos, i);
+			}
+			ret = i_stream_read(crlf_istream);
+			data = i_stream_get_data(crlf_istream, &size);
+			if (ret > 0) {
+				if (pos + (unsigned int)ret != size) {
+					success = FALSE;
+					break;
+				}
+				pos += ret;
+			}
+			if (memcmp(data, str_data(output), size) != 0) {
+				success = FALSE;
+				break;
+			}
+		}
+		if (size != str_len(output))
+			success = FALSE;
+		i_stream_unref(&crlf_istream);
+		i_stream_unref(&istream);
+
+		test_out(t_strdup_printf("test_istream_crlf(%d)", num*2+j),
+			 success);
+	}
+}
+
+static void test_istream_crlf(void)
+{
+	const char *input[] = {
+		"foo\nbar\r\nbaz\r\r\n",
+		"\rfoo",
+		"\r\nfoo",
+		"\r\r\n",
+		"\nfoo"
+	};
+	unsigned int i;
+
+	for (i = 0; i < N_ELEMENTS(input); i++)
+		test_istream_crlf_input(input[i], i);
+}
+
+void test_istreams(void)
+{
+	test_istream_crlf();
+}
diff -r ba048bb01842 -r 76fc7245e74b src/tests/test-lib.c
--- a/src/tests/test-lib.c	Thu Nov 08 01:58:04 2007 +0200
+++ b/src/tests/test-lib.c	Thu Nov 08 01:59:05 2007 +0200
@@ -1,10 +1,9 @@
 /* Copyright (c) 2007 Dovecot authors, see the included COPYING file */
 
-#include "lib.h"
+#include "test-lib.h"
 #include "str.h"
 #include "base64.h"
 #include "bsearch-insert-pos.h"
-#include "test-common.h"
 
 static void test_base64_encode(void)
 {
@@ -128,5 +127,6 @@ int main(void)
 	test_base64_encode();
 	test_base64_decode();
 	test_bsearch_insert_pos();
+	test_istreams();
 	return test_deinit();
 }
diff -r ba048bb01842 -r 76fc7245e74b src/tests/test-lib.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tests/test-lib.h	Thu Nov 08 01:59:05 2007 +0200
@@ -0,0 +1,4 @@
+#include "lib.h"
+#include "test-common.h"
+
+void test_istreams(void);


More information about the dovecot-cvs mailing list