bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
39435f00a35a276d329283179b3e7e0351482939Timo Sirainenvoid completed(enum iostream_pump_status status, int *u0)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* to somehow discern between error and success .. */
39435f00a35a276d329283179b3e7e0351482939Timo Sirainen (*u0) -= (status == IOSTREAM_PUMP_STATUS_INPUT_EOF ? 1 : 2);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomistatic void pump_nonblocking_timeout(struct nonblock_ctx *ctx)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* allow more input */
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* allow more output. give always one byte less than the
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi input size so there's something in internal buffer. */
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_ostream_set_max_output_size(ctx->out, size-1);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomiconst char *run_pump(struct istream *in, struct ostream *out, int *counter, buffer_t *out_buffer)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(i_stream_get_size(in, TRUE, &ctx.max_size) > 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi to2 = timeout_add_short(0, pump_nonblocking_timeout, &ctx);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi iostream_pump_set_completion_callback(pump, completed, counter);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi struct timeout *to = timeout_add(3000, failed, counter);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi if (!ctx.in->blocking && ctx.in->stream_errno != 0 &&
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* input failed, finish flushing output */
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_ostream_set_max_output_size(ctx.out, (size_t)-1);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_setup(bool block, struct istream **in_r,
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi *in_r = test_istream_create_data(data, sizeof(data));
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi *out_r = test_ostream_create_nonblocking(*out_buffer_r, 1);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump (%sblocking)", block ? "" : "non-"));
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "hello, world") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_start_read(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump failure start-read (%sblocking)", block ? "" : "non-"));
a4985564b81d51caaddd38376792432428fd449bStephan Bosch struct istream *in = i_stream_create_failure_at(in_2, 0, EIO, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_mid_read(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump failure mid-read (%sblocking)", block ? "" : "non-"));
a4985564b81d51caaddd38376792432428fd449bStephan Bosch struct istream *in = i_stream_create_failure_at(in_2, 4, EIO, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "hell") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_end_read(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump failure mid-read (%sblocking)", block ? "" : "non-"));
a4985564b81d51caaddd38376792432428fd449bStephan Bosch struct istream *in = i_stream_create_failure_at_eof(in_2, EIO, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "hello, world") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_start_write(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump failure start-write (%sblocking)", block ? "" : "non-"));
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi struct ostream *out = o_stream_create_failure_at(out_2, 0, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_mid_write(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin(t_strdup_printf("iostream_pump failure mid-write (%sblocking)", block ? "" : "non-"));
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi struct ostream *out = o_stream_create_failure_at(out_2, 4, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* "hel" because the last byte is only in internal buffer */
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), block ? "" : "hel") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomivoid test_iostream_pump_failure_end_write(bool block)
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi /* we'll get flushes constantly */
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_begin("iostream_pump failure end-write (blocking)");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi struct ostream *out = o_stream_create_failure_at_flush(out_2, "test pump fail");
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi test_assert(strcmp(run_pump(in, out, &counter, buffer), "hello, world") == 0);
4d9181518769b1f78d8078f7fae899fd0b95907fAki Tuomi for(int i = 0; i < 2; i++) {