]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Improved test istream.
authorTimo Sirainen <tss@iki.fi>
Thu, 25 Jun 2009 18:09:49 +0000 (14:09 -0400)
committerTimo Sirainen <tss@iki.fi>
Thu, 25 Jun 2009 18:09:49 +0000 (14:09 -0400)
--HG--
branch : HEAD

src/lib-imap/test-imap-parser.c
src/lib-mail/test-istream-dot.c
src/lib-test/test-common.c

index 499addead8a1ed1c592eb7ab72e7697fe8107833..31736269ac4111b1a09073ea2d3edc008042941c 100644 (file)
@@ -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);
 
index f07b58d2c6efc99f3ed59682ecd989dd93775afe..44abc55add96136a8dca49ba3bfe5261537eef6e 100644 (file)
@@ -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 struct dot_test *test,
        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 struct dot_test *test,
                }
        }
 
+       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 struct dot_test *test,
                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));
index 641c4b167fef02d5fec02e51f28663dba4c78519..db7c974aeefc6c0562720b8909cde5ea4cc1cdaf 100644 (file)
@@ -5,6 +5,7 @@
 #include "test-common.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #define OUT_NAME_ALIGN 70
 
@@ -13,40 +14,96 @@ static bool test_success;
 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);
+
+       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;
+       }
 
-       stream->istream.eof = TRUE;
-       return -1;
+       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);
-
-       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;
+       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;
+
+       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_size(struct istream *input, uoff_t size)
+void test_istream_set_allow_eof(struct istream *input, bool allow)
 {
-       input->real_stream->pos = size;
+       struct test_istream *tstream =
+               (struct test_istream *)input->real_stream;
+
+       tstream->allow_eof = allow;
 }
 
-void test_istream_set_allow_eof(struct istream *input, bool allow)
+void test_istream_set_size(struct istream *input, uoff_t size)
 {
-       input->real_stream->read = allow ? test_read : test_noread;
+       struct test_istream *tstream =
+               (struct test_istream *)input->real_stream;
+
+       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)