bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#include "test-lib.h"
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#include "istream.h"
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#include "istream-failure-at.h"
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#define TEST_DATA_LENGTH 128
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen#define TEST_ERRMSG "test-istream-failure-at error triggered"
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainenvoid test_istream_failure_at(void)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen{
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen struct istream *input, *data_input;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen unsigned char test_data[TEST_DATA_LENGTH];
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen unsigned int i;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen ssize_t ret;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_begin("istream failure at");
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen for (i = 0; i < sizeof(test_data); i++)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_data[i] = i;
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen data_input = i_stream_create_from_data(test_data, sizeof(test_data));
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen for (i = 0; i < TEST_DATA_LENGTH; i++) {
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_seek(data_input, 0);
a4985564b81d51caaddd38376792432428fd449bStephan Bosch input = i_stream_create_failure_at(data_input, i, EIO, TEST_ERRMSG);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen while ((ret = i_stream_read(input)) > 0)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_skip(input, ret);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_assert_idx(ret == -1 && input->v_offset == i &&
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen input->stream_errno == EIO &&
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen strcmp(i_stream_get_error(input), TEST_ERRMSG) == 0, i);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_destroy(&input);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen }
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen /* shouldn't fail */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_seek(data_input, 0);
a4985564b81d51caaddd38376792432428fd449bStephan Bosch input = i_stream_create_failure_at(data_input, TEST_DATA_LENGTH, EIO, TEST_ERRMSG);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen while ((ret = i_stream_read(input)) > 0)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_skip(input, ret);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_assert(ret == -1 && input->stream_errno == 0);
fddfa139c8f7621bd13d44537698ba58d1965078Phil Carmody i_stream_destroy(&input);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen /* fail at EOF */
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_seek(data_input, 0);
a4985564b81d51caaddd38376792432428fd449bStephan Bosch input = i_stream_create_failure_at_eof(data_input, EIO, TEST_ERRMSG);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen while ((ret = i_stream_read(input)) > 0)
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen i_stream_skip(input, ret);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_assert_idx(ret == -1 && input->v_offset == TEST_DATA_LENGTH &&
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen input->stream_errno == EIO &&
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen strcmp(i_stream_get_error(input), TEST_ERRMSG) == 0, i);
fddfa139c8f7621bd13d44537698ba58d1965078Phil Carmody i_stream_destroy(&input);
fddfa139c8f7621bd13d44537698ba58d1965078Phil Carmody i_stream_destroy(&data_input);
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen test_end();
289bb39856d74484c1622d096922acf126dd90fcTimo Sirainen}