dovecot-2.0: Improved test istream.

dovecot at dovecot.org dovecot at dovecot.org
Thu Jun 25 21:09:55 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/cdd6ef3c356c
changeset: 9531:cdd6ef3c356c
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Jun 25 14:09:49 2009 -0400
description:
Improved test istream.

diffstat:

3 files changed, 120 insertions(+), 25 deletions(-)
src/lib-imap/test-imap-parser.c |    5 ++
src/lib-mail/test-istream-dot.c |   47 ++++++++++++++++---
src/lib-test/test-common.c      |   93 +++++++++++++++++++++++++++++++--------

diffs (237 lines):

diff -r 5ca3318cd3e2 -r cdd6ef3c356c src/lib-imap/test-imap-parser.c
--- a/src/lib-imap/test-imap-parser.c	Thu Jun 25 14:09:12 2009 -0400
+++ b/src/lib-imap/test-imap-parser.c	Thu Jun 25 14:09:49 2009 -0400
@@ -21,9 +21,11 @@ static void test_imap_parser_crlf(void)
 	/* must return -2 until LF is read */
 	for (i = 0; test_input[i] != '\n'; i++) {
 		test_istream_set_size(input, i+1);
+		(void)i_stream_read(input);
 		test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
 	}
 	test_istream_set_size(input, i+1);
+	(void)i_stream_read(input);
 	test_assert(imap_parser_read_args(parser, 0, 0, &args) == 1);
 	test_assert(args[0].type == IMAP_ARG_ATOM);
 	test_assert(args[1].type == IMAP_ARG_EOL);
@@ -32,10 +34,13 @@ static void test_imap_parser_crlf(void)
 	imap_parser_reset(parser);
 	i_stream_seek(input, ++i);
 	test_istream_set_size(input, ++i);
+	(void)i_stream_read(input);
 	test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
 	test_istream_set_size(input, ++i);
+	(void)i_stream_read(input);
 	test_assert(imap_parser_read_args(parser, 0, 0, &args) == -2);
 	test_istream_set_size(input, ++i);
+	(void)i_stream_read(input);
 	test_assert(imap_parser_read_args(parser, 0, 0, &args) == -1);
 	test_assert(strcmp(imap_parser_get_error(parser, &fatal), "CR sent without LF") == 0 && !fatal);
 
diff -r 5ca3318cd3e2 -r cdd6ef3c356c src/lib-mail/test-istream-dot.c
--- a/src/lib-mail/test-istream-dot.c	Thu Jun 25 14:09:12 2009 -0400
+++ b/src/lib-mail/test-istream-dot.c	Thu Jun 25 14:09:49 2009 -0400
@@ -1,6 +1,7 @@
 /* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "str.h"
 #include "istream.h"
 #include "istream-dot.h"
 #include "test-common.h"
@@ -17,7 +18,10 @@ static void test_istream_dot_one(const s
 	struct istream *test_input, *input;
 	const unsigned char *data;
 	size_t size;
-	unsigned int i, input_len, output_len;
+	unsigned int i, outsize, input_len, output_len;
+	string_t *str;
+	uoff_t offset;
+	int ret;
 
 	test_input = test_istream_create(test->input);
 	input = i_stream_create_dot(test_input, send_last_lf);
@@ -34,12 +38,39 @@ static void test_istream_dot_one(const s
 		}
 	}
 
+	str = t_str_new(256);
 	input_len = strlen(test->input);
 	if (!test_bufsize) {
-		for (i = 1; i <= input_len; i++) {
-			test_istream_set_size(test_input, i);
-			(void)i_stream_read(input);
+		outsize = 1; i = 0;
+		i_stream_set_max_buffer_size(input, outsize);
+		test_istream_set_size(test_input, 1);
+		while ((ret = i_stream_read(input)) != -1) {
+			switch (ret) {
+			case -2:
+				i_stream_set_max_buffer_size(input, ++outsize);
+				offset = test_input->v_offset;
+				/* seek one byte backwards so stream gets
+				   reset */
+				i_stream_seek(test_input, offset - 1);
+				/* go back to original position */
+				test_istream_set_size(test_input, offset);
+				i_stream_skip(test_input, 1);
+				/* and finally allow reading one more byte */
+				test_istream_set_size(test_input, offset + 1);
+				break;
+			case 0:
+				test_istream_set_size(test_input, ++i);
+				break;
+			default:
+				test_assert(ret > 0);
+
+				data = i_stream_get_data(input, &size);
+				str_append_n(str, data, size);
+				i_stream_skip(input, size);
+			}
 		}
+		test_istream_set_size(test_input, input_len);
+		i_stream_read(test_input);
 	} else {
 		test_istream_set_size(test_input, input_len);
 		size = 0;
@@ -54,10 +85,12 @@ static void test_istream_dot_one(const s
 		if (size < output_len)
 			test_assert(i_stream_read(input) == 1);
 		test_assert(i_stream_read(input) == -1);
+
+		data = i_stream_get_data(input, &size);
+		str_append_n(str, data, size);
 	}
-	data = i_stream_get_data(input, &size);
-	test_assert(size == output_len);
-	test_assert(memcmp(data, test->output, size) == 0);
+	test_assert(str_len(str) == output_len);
+	test_assert(memcmp(str_data(str), test->output, size) == 0);
 
 	data = i_stream_get_data(test_input, &size);
 	test_assert(size == strlen(test->parent_input));
diff -r 5ca3318cd3e2 -r cdd6ef3c356c src/lib-test/test-common.c
--- a/src/lib-test/test-common.c	Thu Jun 25 14:09:12 2009 -0400
+++ b/src/lib-test/test-common.c	Thu Jun 25 14:09:49 2009 -0400
@@ -5,6 +5,7 @@
 #include "test-common.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #define OUT_NAME_ALIGN 70
 
@@ -13,40 +14,96 @@ static unsigned int failure_count;
 static unsigned int failure_count;
 static unsigned int total_count;
 
+struct test_istream {
+	struct istream_private istream;
+	unsigned int skip_diff;
+	size_t max_pos;
+	bool allow_eof;
+};
+
 static ssize_t test_read(struct istream_private *stream)
 {
-	if (stream->pos < (uoff_t)stream->statbuf.st_size)
+	struct test_istream *tstream = (struct test_istream *)stream;
+	unsigned int new_skip_diff;
+	ssize_t ret;
+
+	i_assert(stream->skip <= stream->pos);
+
+	if (tstream->max_pos < stream->pos) {
+		/* we seeked past the end of file. */
+		ret = 0;
+	} else {
+		/* move around the buffer */
+		new_skip_diff = rand() % 128;
+		stream->buffer = (stream->buffer + tstream->skip_diff) -
+			new_skip_diff;
+		stream->skip = (stream->skip - tstream->skip_diff) +
+			new_skip_diff;
+		stream->pos = (stream->pos - tstream->skip_diff) +
+			new_skip_diff;
+		tstream->max_pos = (tstream->max_pos - tstream->skip_diff) +
+			new_skip_diff;
+		tstream->skip_diff = new_skip_diff;
+
+		ret = tstream->max_pos - stream->pos;
+		stream->pos = tstream->max_pos;
+	}
+
+	if (ret > 0)
+		return ret;
+	else if (!tstream->allow_eof ||
+		 stream->pos - tstream->skip_diff < (uoff_t)stream->statbuf.st_size)
 		return 0;
-
-	stream->istream.eof = TRUE;
-	return -1;
+	else {
+		stream->istream.eof = TRUE;
+		return -1;
+	}
 }
 
-static ssize_t test_noread(struct istream_private *stream ATTR_UNUSED)
+static void test_seek(struct istream_private *stream, uoff_t v_offset,
+		      bool mark ATTR_UNUSED)
 {
-	return 0;
+	struct test_istream *tstream = (struct test_istream *)stream;
+
+	stream->istream.v_offset = v_offset;
+	stream->skip = v_offset + tstream->skip_diff;
+	stream->pos = stream->skip;
 }
 
 struct istream *test_istream_create(const char *data)
 {
-	struct istream *input;
-	unsigned int len = strlen(data);
+	struct test_istream *tstream;
 
-	input = i_stream_create_from_data(data, len);
-	input->blocking = FALSE;
-	input->real_stream->statbuf.st_size = len;
-	input->real_stream->read = test_read;
-	return input;
+	tstream = i_new(struct test_istream, 1);
+	tstream->istream.buffer = (const void *)data;
+
+	tstream->istream.read = test_read;
+	tstream->istream.seek = test_seek;
+
+	tstream->istream.istream.blocking = FALSE;
+	tstream->istream.istream.seekable = TRUE;
+	(void)i_stream_create(&tstream->istream, NULL, -1);
+	tstream->istream.statbuf.st_size = tstream->max_pos = strlen(data);
+	tstream->allow_eof = TRUE;
+	return &tstream->istream.istream;
+}
+
+void test_istream_set_allow_eof(struct istream *input, bool allow)
+{
+	struct test_istream *tstream =
+		(struct test_istream *)input->real_stream;
+
+	tstream->allow_eof = allow;
 }
 
 void test_istream_set_size(struct istream *input, uoff_t size)
 {
-	input->real_stream->pos = size;
-}
+	struct test_istream *tstream =
+		(struct test_istream *)input->real_stream;
 
-void test_istream_set_allow_eof(struct istream *input, bool allow)
-{
-	input->real_stream->read = allow ? test_read : test_noread;
+	if (size > (uoff_t)tstream->istream.statbuf.st_size)
+		size = (uoff_t)tstream->istream.statbuf.st_size;
+	tstream->max_pos = size + tstream->skip_diff;
 }
 
 void test_begin(const char *name)


More information about the dovecot-cvs mailing list