test-istream.c revision d47bd663deb1846980e6759c87297741a5459890
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* Copyright (c) 2014-2015 Dovecot authors, see the included COPYING file */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen#include "test-lib.h"
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen#include "istream.h"
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainenstatic void test_istream_children(void)
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen{
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen struct istream *parent, *child1, *child2;
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen const unsigned char *data;
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen size_t size;
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_begin("istream children");
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen parent = test_istream_create_data("123456789", 9);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_istream_set_max_buffer_size(parent, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen child1 = i_stream_create_limit(parent, (uoff_t)-1);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen child2 = i_stream_create_limit(parent, (uoff_t)-1);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child1 read beginning */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child1) == 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen data = i_stream_get_data(child1, &size);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(size == 3 && memcmp(data, "123", 3) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_skip(child1, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child1 read middle.. */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child1) == 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen data = i_stream_get_data(child1, &size);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(size == 3 && memcmp(data, "456", 3) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child2 read beginning.. */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child2) == 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen data = i_stream_get_data(child2, &size);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(size == 3 && memcmp(data, "123", 3) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child1 check middle again.. the parent has been modified,
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen so it can't return the original data (without some code changes). */
d47bd663deb1846980e6759c87297741a5459890Timo Sirainen test_assert(i_stream_get_data_size(child1) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_skip(child1, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child1 read end */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child1) == 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen data = i_stream_get_data(child1, &size);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(size == 3 && memcmp(data, "789", 3) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_skip(child1, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child1) == -1);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child2 check beginning again.. */
d47bd663deb1846980e6759c87297741a5459890Timo Sirainen test_assert(i_stream_get_data_size(child1) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_skip(child2, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen /* child2 read middle */
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(i_stream_read(child2) == 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen data = i_stream_get_data(child2, &size);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_assert(size == 3 && memcmp(data, "456", 3) == 0);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_skip(child2, 3);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_destroy(&child1);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_destroy(&child2);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen i_stream_destroy(&parent);
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_end();
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen}
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainenvoid test_istream(void)
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen{
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen test_istream_children();
f5982bb5b0a704e88fa2b44b0b74e365d13103b9Timo Sirainen}