Mercurial > dovecot > core-2.2
changeset 9531:cdd6ef3c356c HEAD
Improved test istream.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 25 Jun 2009 14:09:49 -0400 |
parents | 5ca3318cd3e2 |
children | 29733d23d903 |
files | src/lib-imap/test-imap-parser.c src/lib-mail/test-istream-dot.c src/lib-test/test-common.c |
diffstat | 3 files changed, 120 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ /* 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 @@ 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);
--- 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 @@ 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 @@ } } + 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 @@ 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));
--- 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 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) - return 0; + struct test_istream *tstream = (struct test_istream *)stream; + unsigned int new_skip_diff; + ssize_t ret; + + i_assert(stream->skip <= stream->pos); - stream->istream.eof = TRUE; - return -1; + 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; + 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; + + tstream = i_new(struct test_istream, 1); + tstream->istream.buffer = (const void *)data; + + tstream->istream.read = test_read; + tstream->istream.seek = test_seek; - 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->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)