From f7060b8499ed07205734cf7af94ad5405fa687b5 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Thu, 15 Sep 2016 11:34:30 +0300 Subject: [PATCH] lib: Add test for iostream-proxy --- src/lib/Makefile.am | 1 + src/lib/test-iostream-proxy.c | 113 ++++++++++++++++++++++++++++++++++ src/lib/test-lib.inc | 1 + 3 files changed, 115 insertions(+) create mode 100644 src/lib/test-iostream-proxy.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 0812a70e31..959985db45 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -329,6 +329,7 @@ test_lib_SOURCES = \ test-ioloop.c \ test-iso8601-date.c \ test-iostream-pump.c \ + test-iostream-proxy.c \ test-iostream-temp.c \ test-istream.c \ test-istream-base64-decoder.c \ diff --git a/src/lib/test-iostream-proxy.c b/src/lib/test-iostream-proxy.c new file mode 100644 index 0000000000..5151c46087 --- /dev/null +++ b/src/lib/test-iostream-proxy.c @@ -0,0 +1,113 @@ +/* Copyright (c) 2016 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" +#include "istream.h" +#include "ostream.h" +#include "buffer.h" +#include "ioloop.h" +#include "iostream-proxy.h" +#include "fd-set-nonblock.h" + +#include +#include +#include + +static +void completed(enum iostream_proxy_side side ATTR_UNUSED, bool success ATTR_UNUSED, int *u0) +{ + i_assert(*u0 > 0); + if (--*u0 == 0) + io_loop_stop(current_ioloop); +} + +static +void test_iostream_proxy_simple(void) +{ + size_t bytes; + + test_begin("iostream_proxy"); + int sfdl[2]; + int sfdr[2]; + + int counter; + + test_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sfdl) == 0); + test_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sfdr) == 0); + + fd_set_nonblock(sfdl[0], TRUE); + fd_set_nonblock(sfdl[1], TRUE); + fd_set_nonblock(sfdr[0], TRUE); + fd_set_nonblock(sfdr[1], TRUE); + + struct ioloop *ioloop = io_loop_create(); + + struct istream *left_in = i_stream_create_fd(sfdl[1], IO_BLOCK_SIZE); + struct ostream *left_out = o_stream_create_fd(sfdl[1], IO_BLOCK_SIZE); + + struct istream *right_in = i_stream_create_fd(sfdr[1], IO_BLOCK_SIZE); + struct ostream *right_out = o_stream_create_fd(sfdr[1], IO_BLOCK_SIZE); + + struct iostream_proxy *proxy; + + proxy = iostream_proxy_create(left_in, left_out, right_in, right_out); + i_stream_unref(&left_in); + o_stream_unref(&left_out); + i_stream_unref(&right_in); + o_stream_unref(&right_out); + + iostream_proxy_set_completion_callback(proxy, completed, &counter); + iostream_proxy_start(proxy); + + left_in = i_stream_create_fd(sfdl[0], IO_BLOCK_SIZE); + left_out = o_stream_create_fd(sfdl[0], IO_BLOCK_SIZE); + + right_in = i_stream_create_fd(sfdr[0], IO_BLOCK_SIZE); + right_out = o_stream_create_fd(sfdr[0], IO_BLOCK_SIZE); + + test_assert(proxy != NULL); + test_assert(o_stream_send_str(left_out, "hello, world") > 0); + o_stream_flush(left_out); + o_stream_unref(&left_out); + test_assert(shutdown(sfdl[0], SHUT_WR) == 0); + + counter = 1; + io_loop_run(ioloop); + + i_stream_read(right_in); + test_assert(strcmp((const char*)i_stream_get_data(right_in, &bytes), "hello, world") == 0); + i_stream_skip(right_in, bytes); + + test_assert(o_stream_send_str(right_out, "hello, world") > 0); + o_stream_flush(right_out); + o_stream_unref(&right_out); + test_assert(shutdown(sfdr[0], SHUT_WR) == 0); + + counter = 1; + io_loop_run(ioloop); + + i_stream_read(left_in); + test_assert(strcmp((const char*)i_stream_get_data(left_in, &bytes), "hello, world") == 0); + i_stream_skip(left_in, bytes); + + iostream_proxy_unref(&proxy); + + io_loop_destroy(&ioloop); + + i_stream_unref(&left_in); + i_stream_unref(&right_in); + + /* close fd */ + close(sfdl[0]); + close(sfdl[1]); + close(sfdr[0]); + close(sfdr[1]); + + test_end(); +} + +void test_iostream_proxy(void) +{ + T_BEGIN { + test_iostream_proxy_simple(); + } T_END; +} diff --git a/src/lib/test-lib.inc b/src/lib/test-lib.inc index a38b8d8f32..ffb46e39e6 100644 --- a/src/lib/test-lib.inc +++ b/src/lib/test-lib.inc @@ -22,6 +22,7 @@ TEST(test_hex_binary) TEST(test_ioloop) TEST(test_iso8601_date) TEST(test_iostream_pump) +TEST(test_iostream_proxy) TEST(test_iostream_temp) TEST(test_istream) TEST(test_istream_base64_decoder) -- 2.47.3