bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi static const char data[] = "\x00\x00\x00\x00\x06Hello\x00"
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi "\x01\x00\x00\x00\x03Wor"
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi "\x00\x00\x00\x00\x00"
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi "\x01\x00\x00\x00\x03ld\x00";
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *input = test_istream_create_data(data, data_len);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan0 = i_stream_create_multiplex(input, (size_t)-1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan1 = i_stream_multiplex_add_channel(chan0, 1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* nothing to read until the first byte */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* partial read of the first packet */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan0, &siz), "Hel", 3) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* read the rest of the first packet and the second packet.
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi read chan1 before chan0 to see that it works. */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan0, &siz), "Hello\0", 6) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan1, &siz), "Wor", 3) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* 0-sized packet is ignored */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* read the final packet */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* we should have the final data in all channels now */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan0, &siz), "Hello\0", 6) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan1, &siz), "World\0", 6) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* all channels should return EOF */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(i_stream_read(chan0) == -1 && chan0->stream_errno == 0);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(i_stream_read(chan1) == -1 && chan1->stream_errno == 0);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi static const char data[] = "\x00\x00\x00\x00\x06Hello\x00"
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi "\x01\x00\x00\x00\x06World\x00";
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *input = test_istream_create_data(data, data_len);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan0 = i_stream_create_multiplex(input, 5);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan1 = i_stream_multiplex_add_channel(chan0, 1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* we get data for channel 0 and congest */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* we read data for channel 0 */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* and now it's congested */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan0, &siz), "Hello", 5) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* consume data */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* we read data for channel 1 */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan1, &siz), "World", 5) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* consume data */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* read last byte */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* now we get byte for channel 1 */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* now we read byte for channel 1 */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* and everything should return EOF now */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi unsigned int i;
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi buffer_t *buf = buffer_create_dynamic(default_pool, 10240);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi for (i = 0; i < packets_count; i++) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi crc32_data_more(input_crc[channel], packet_data, len);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *input = test_istream_create_data(buf->data, buf->used);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi chan[0] = i_stream_create_multiplex(input, 1024/4);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi chan[i] = i_stream_multiplex_add_channel(chan[0], i);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* read from each stream, 1 byte at a time */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi for (i = 0;;) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi output_crc[i] = crc32_data_more(output_crc[i], data, size);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi for (i = 0; i < max_channel; i++) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert_idx(input_crc[i] == output_crc[i], i);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void test_istream_multiplex_stream_read(struct istream *channel)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi uint8_t cid = i_stream_multiplex_get_channel_id(channel);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi while((line = i_stream_next_line(channel)) != NULL) {
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert_idx(siz > 0 && siz < N_ELEMENTS(msgs),
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi if (channel_counter[0] > 100 && channel_counter[1] > 100)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void test_send_msg(struct ostream *os, uint8_t cid, const char *msg)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi { "\n", 1 } /* newline added for i_stream_next_line */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void test_istream_multiplex_stream_write(struct ostream *channel)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_send_msg(channel, cid, msgs[1 + i_rand() % (N_ELEMENTS(msgs) - 1)]);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct ostream *os = o_stream_create_fd(fds[1], (size_t)-1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *is = i_stream_create_fd(fds[0], 10 + i_rand() % 10);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan0 = i_stream_create_multiplex(is, (size_t)-1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan1 = i_stream_multiplex_add_channel(chan0, 1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi io_add_istream(chan0, test_istream_multiplex_stream_read, chan0);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi io_add_istream(chan1, test_istream_multiplex_stream_read, chan1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi io_add(fds[1], IO_WRITE, test_istream_multiplex_stream_write, os);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomistatic void test_istream_multiplex_close_channel(void)
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi static const char *data = "\x00\x00\x00\x00\x06Hello\x00"
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi "\x01\x00\x00\x00\x06World\x00";
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *input = test_istream_create_data(data, data_len);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan0 = i_stream_create_multiplex(input, (size_t)-1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi struct istream *chan1 = i_stream_multiplex_add_channel(chan0, 1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan0, &siz), "Hello\0", 6) == 0 &&
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi chan0 = i_stream_create_multiplex(input, (size_t)-1);
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi /* this is needed to populate chan1 data */
1de7b73a11afda43323410a4481d866930b1e632Aki Tuomi test_assert(memcmp(i_stream_get_data(chan1, &siz), "World\0", 6) == 0 &&