]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Created basic test suite for ostream-buffer.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Sun, 2 Jul 2017 10:05:36 +0000 (12:05 +0200)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 14 Aug 2017 08:57:36 +0000 (11:57 +0300)
src/lib/Makefile.am
src/lib/test-lib.c
src/lib/test-lib.h
src/lib/test-ostream-buffer.c [new file with mode: 0644]

index 693d522fc3f6520d708589287e414b54fe9c535d..5be485f8cb2ad8db4198f4be48938a307acc23ee 100644 (file)
@@ -352,6 +352,7 @@ test_lib_SOURCES = \
        test-pkcs5.c \
        test-net.c \
        test-numpack.c \
+       test-ostream-buffer.c \
        test-ostream-escaped.c \
        test-ostream-failure-at.c \
        test-ostream-file.c \
index 9a1c44f8fd79b282fa848f86bf3c92a506bfdd33..56d07a42b837fe95bb813e08084f34de51299354 100644 (file)
@@ -46,6 +46,7 @@ int main(void)
                test_net,
                test_numpack,
                test_pkcs5_pbkdf2,
+               test_ostream_buffer,
                test_ostream_escaped,
                test_ostream_failure_at,
                test_ostream_file,
index 142b2f5359bbcec26a8526a6b2d657e8da3a987d..62867782062d7906ce582611e56f7ef7bc63d671 100644 (file)
@@ -50,6 +50,7 @@ enum fatal_test_state fatal_mempool_alloconly(unsigned int);
 void test_pkcs5_pbkdf2(void);
 void test_net(void);
 void test_numpack(void);
+void test_ostream_buffer(void);
 void test_ostream_escaped(void);
 void test_ostream_failure_at(void);
 void test_ostream_file(void);
diff --git a/src/lib/test-ostream-buffer.c b/src/lib/test-ostream-buffer.c
new file mode 100644 (file)
index 0000000..3e1a75e
--- /dev/null
@@ -0,0 +1,108 @@
+/* Copyright (c) 2017 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "buffer.h"
+#include "str.h"
+#include "randgen.h"
+#include "istream.h"
+#include "ostream.h"
+
+#define MAX_BUFSIZE 256
+
+static void test_ostream_buffer_random_once(void)
+{
+       buffer_t *buffer;
+       struct ostream *output;
+       char buf[MAX_BUFSIZE*4], randbuf[MAX_BUFSIZE];
+       unsigned int i, offset, size;
+
+       buffer = buffer_create_dynamic(default_pool, 8);
+
+       memset(buf, 0, sizeof(buf));
+
+       output = o_stream_create_buffer(buffer);
+       o_stream_cork(output);
+
+       size = (rand() % MAX_BUFSIZE) + 1;
+       random_fill_weak(randbuf, size);
+       memcpy(buf, randbuf, size);
+       test_assert(o_stream_send(output, buf, size) > 0);
+
+       for (i = 0; i < 10; i++) {
+               offset = rand() % (MAX_BUFSIZE*3);
+               size = (rand() % MAX_BUFSIZE) + 1;
+               random_fill_weak(randbuf, size);
+               memcpy(buf + offset, randbuf, size);
+               test_assert(o_stream_pwrite(output, randbuf, size, offset) == 0);
+               if (rand() % 10 == 0)
+                       test_assert(o_stream_flush(output) > 0);
+       }
+
+       test_assert(o_stream_flush(output) > 0);
+       o_stream_uncork(output);
+
+       i_assert(buffer->used <= MAX_BUFSIZE*4);
+       test_assert(memcmp(buf, buffer->data, buffer->used) == 0);
+
+       o_stream_unref(&output);
+       buffer_free(&buffer);
+}
+
+static void test_ostream_buffer_random(void)
+{
+       unsigned int i;
+
+       test_begin("ostream buffer pwrite random");
+       for (i = 0; i < 100; i++) T_BEGIN {
+               test_ostream_buffer_random_once();
+       } T_END;
+       test_end();
+}
+
+static void test_ostream_buffer_size(void)
+{
+       struct ostream *output;
+       string_t *str = t_str_new(64);
+
+       test_begin("ostream buffer size/available");
+       output = o_stream_create_buffer(str);
+       test_assert(o_stream_get_buffer_used_size(output) == 0);
+       test_assert(o_stream_get_buffer_avail_size(output) == (size_t)-1);
+
+       /* test shrinking sink's max buffer size */
+       o_stream_set_max_buffer_size(output, 10);
+       test_assert(o_stream_get_buffer_used_size(output) == 0);
+       test_assert(o_stream_get_buffer_avail_size(output) == 10);
+
+       /* partial send */
+       const char *partial_input = "01234567890123456789";
+       ssize_t ret = o_stream_send_str(output, partial_input);
+       test_assert(ret == 10);
+       test_assert(o_stream_get_buffer_used_size(output) == 10);
+       test_assert(o_stream_get_buffer_avail_size(output) == 0);
+       
+       /* increase max buffer size so that it can hold the whole message */
+       o_stream_set_max_buffer_size(output, 100);
+       test_assert(o_stream_get_buffer_used_size(output) == 10);
+       test_assert(o_stream_get_buffer_avail_size(output) == 90);
+
+       /* send the rest */
+       ret += o_stream_send_str(output, partial_input + ret);
+       test_assert(ret == (ssize_t)strlen(partial_input));
+       test_assert(output->offset == str_len(str));
+       test_assert(o_stream_get_buffer_used_size(output) == 20);
+       test_assert(o_stream_get_buffer_avail_size(output) == 80);
+
+       /* check buffered data */
+       test_assert(strcmp(str_c(str), partial_input) == 0);
+
+       o_stream_unref(&output);
+
+       test_end();
+}
+
+void test_ostream_buffer(void)
+{
+       test_ostream_buffer_random();
+       test_ostream_buffer_size();
+}