summaryrefslogtreecommitdiffstats
path: root/src/lib/test-istream-try.c
blob: 888e00f64744dac1a0189ba732f5fcb91ceb7e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "istream-private.h"
#include "istream-base64.h"
#include "istream-try.h"

#define MIN_FULL_SIZE 1

static void test_istream_try_normal(void)
{
	bool finished = FALSE;

	test_begin("istream try");
	for (unsigned int test = 0; test <= 10; test++) {
		struct istream *test_inputs[3], *try_input;

		test_inputs[0] = test_istream_create("1");
		test_inputs[1] = test_istream_create("2");
		test_inputs[2] = NULL;
		test_istream_set_size(test_inputs[0], 0);
		test_istream_set_size(test_inputs[1], 0);
		try_input = istream_try_create(test_inputs, MIN_FULL_SIZE);

		/* nonblocking read */
		test_assert_idx(i_stream_read(try_input) == 0, test);

		switch (test) {
		case 0:
			/* stream 0 is available */
			test_istream_set_size(test_inputs[0], 1);
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			break;
		case 1:
			/* stream 1 is available, but not used before 0 */
			test_istream_set_size(test_inputs[1], 1);
			test_assert_idx(i_stream_read(try_input) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			/* continue failing stream 0 -> 1 is available */
			test_inputs[0]->stream_errno = EINVAL;
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
			break;
		case 2:
			/* both streams are available - stream 0 is read */
			test_istream_set_size(test_inputs[0], 1);
			test_istream_set_size(test_inputs[1], 1);
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			break;
		case 3:
			/* stream 0 fails */
			test_inputs[0]->stream_errno = EINVAL;
			test_assert_idx(i_stream_read(try_input) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			/* continue making stream 1 available */
			test_istream_set_size(test_inputs[1], 1);
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
			break;
		case 4:
			/* stream 1 fails */
			test_inputs[1]->stream_errno = EINVAL;
			test_assert_idx(i_stream_read(try_input) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			break;
		case 5:
			/* stream 0 fails, stream 1 is available */
			test_inputs[0]->stream_errno = EINVAL;
			test_istream_set_size(test_inputs[1], 1);
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
			break;
		case 6:
			/* stream 0 is available, stream 1 fails */
			test_inputs[1]->stream_errno = EINVAL;
			test_istream_set_size(test_inputs[0], 1);
			test_assert_idx(i_stream_read(try_input) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
			break;
		case 7:
			/* both streams fail */
			test_inputs[0]->stream_errno = EINVAL;
			test_inputs[1]->stream_errno = EINVAL;
			test_assert_idx(i_stream_read(try_input) == -1, test);
			test_assert_idx(try_input->stream_errno == EINVAL, test);
			break;
		case 8:
			/* stream 0 fails with EINVAL, stream 1 with EIO */
			test_inputs[0]->stream_errno = EINVAL;
			test_inputs[1]->stream_errno = EIO;
			test_assert_idx(i_stream_read(try_input) == -1, test);
			test_assert_idx(try_input->stream_errno == EIO, test);
			break;
		case 9:
			/* stream 0 fails with EIO, stream 1 with EINVAL */
			test_inputs[0]->stream_errno = EIO;
			test_inputs[1]->stream_errno = EINVAL;
			test_assert_idx(i_stream_read(try_input) == -1, test);
			test_assert_idx(try_input->stream_errno == EIO, test);
			break;
		case 10:
			/* stream 0 fails with EIO, stream 1 would work.. */
			test_inputs[0]->stream_errno = EIO;
			test_istream_set_size(test_inputs[1], 1);
			test_assert_idx(i_stream_read(try_input) == -1, test);
			test_assert_idx(try_input->stream_errno == EIO, test);
			test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);

			finished = TRUE;
			break;
		}

		test_assert_idx(test_inputs[0]->v_offset == 0, test);
		test_assert_idx(test_inputs[1]->v_offset == 0, test);

		i_stream_unref(&test_inputs[0]);
		i_stream_unref(&test_inputs[1]);
		i_stream_unref(&try_input);
	}
	i_assert(finished);
	test_end();
}

static void test_istream_try_empty(void)
{
	test_begin("istream try empty stream");
	struct istream *test_inputs[] = {
		test_istream_create(""),
		test_istream_create(""),
		NULL
	};
	struct istream *try_input =
		istream_try_create(test_inputs, MIN_FULL_SIZE);
	test_assert(i_stream_read(try_input) == -1);
	test_assert(try_input->eof);
	test_assert(try_input->stream_errno == 0);
	i_stream_unref(&test_inputs[0]);
	i_stream_unref(&test_inputs[1]);
	i_stream_unref(&try_input);
	test_end();
}

static void test_istream_try_buffer_full(void)
{
	const char *test_strings[] = { "Zm9v", "YmFy" };
	struct istream *test_inputs[3], *try_input, *input, *input2;

	test_begin("istream try buffer full");

	for (unsigned int i = 0; i < 2; i++) {
		input = test_istream_create(test_strings[i]);
		test_istream_set_size(input, 1);
		test_istream_set_max_buffer_size(input, 1);
		input2 = i_stream_create_base64_decoder(input);
		i_stream_unref(&input);
		test_inputs[i] = input2;
	};
	test_inputs[2] = NULL;

	try_input = istream_try_create(test_inputs, MIN_FULL_SIZE);

	test_assert(i_stream_read(try_input) == 0);
	test_assert(try_input->real_stream->parent != NULL);
	test_assert(i_stream_get_data_size(test_inputs[0]) == 0);
	test_assert(i_stream_get_data_size(test_inputs[1]) == 0);

	test_istream_set_size(test_inputs[0], 2);
	test_assert(i_stream_read(try_input) == 1);
	test_assert(i_stream_get_data_size(test_inputs[0]) == 1);
	test_assert(i_stream_get_data_size(test_inputs[1]) == 0);

	i_stream_unref(&test_inputs[0]);
	i_stream_unref(&test_inputs[1]);
	i_stream_unref(&try_input);

	test_end();
}

void test_istream_try(void)
{
	test_istream_try_normal();
	test_istream_try_empty();
	test_istream_try_buffer_full();
}