278N/A/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
278N/A
278N/A#include "test-lib.h"
278N/A#include "buffer.h"
278N/A#include "ostream.h"
278N/A#include "ostream-failure-at.h"
278N/A
278N/A#define TEST_DATA_LENGTH 128
278N/A#define TEST_ERRMSG "test-ostream-failure-at error triggered"
278N/A
278N/Avoid test_ostream_failure_at(void)
278N/A{
278N/A unsigned char test_data[TEST_DATA_LENGTH];
278N/A struct ostream *output, *buf_output;
278N/A buffer_t *buf = t_buffer_create(256);
278N/A unsigned int i;
278N/A
278N/A test_begin("ostream failure at");
278N/A for (i = 0; i < sizeof(test_data); i++)
278N/A test_data[i] = i;
844N/A for (i = 0; i < TEST_DATA_LENGTH; i++) {
278N/A buf_output = o_stream_create_buffer(buf);
278N/A output = o_stream_create_failure_at(buf_output, i, TEST_ERRMSG);
278N/A if (i > 0)
278N/A test_assert(o_stream_send(output, test_data, sizeof(test_data)) == (int)i);
278N/A test_assert_idx(o_stream_send(output, test_data, sizeof(test_data)) == -1 &&
618N/A output->offset == i &&
278N/A output->stream_errno == EIO &&
278N/A strcmp(o_stream_get_error(output), TEST_ERRMSG) == 0, i);
844N/A o_stream_destroy(&output);
844N/A o_stream_destroy(&buf_output);
278N/A }
278N/A /* shouldn't fail */
618N/A buf_output = o_stream_create_buffer(buf);
278N/A output = o_stream_create_failure_at(buf_output, TEST_DATA_LENGTH, TEST_ERRMSG);
1273N/A test_assert(o_stream_send(output, test_data, sizeof(test_data)) == TEST_DATA_LENGTH);
1273N/A test_assert(o_stream_flush(output) > 0 &&
278N/A output->offset == TEST_DATA_LENGTH &&
278N/A output->stream_errno == 0);
278N/A o_stream_destroy(&output);
278N/A o_stream_destroy(&buf_output);
278N/A
278N/A /* fail at flush */
278N/A buf_output = o_stream_create_buffer(buf);
278N/A output = o_stream_create_failure_at_flush(buf_output, TEST_ERRMSG);
278N/A test_assert(o_stream_send(output, test_data, sizeof(test_data)) == TEST_DATA_LENGTH);
278N/A test_assert(o_stream_flush(output) < 0 && output->stream_errno == EIO &&
278N/A strcmp(o_stream_get_error(output), TEST_ERRMSG) == 0);
278N/A o_stream_destroy(&output);
278N/A o_stream_destroy(&buf_output);
278N/A test_end();
278N/A}
278N/A