22607
|
1 /* Copyright (c) 2009-2017 Dovecot authors, see the included COPYING file */
|
|
2
|
|
3 #include "test-lib.h"
|
|
4 #include "istream.h"
|
|
5 #include "istream-try.h"
|
|
6
|
|
7 void test_istream_try(void)
|
|
8 {
|
|
9 bool finished = FALSE;
|
|
10
|
|
11 test_begin("istream try");
|
|
12 for (unsigned int test = 0; test <= 10; test++) {
|
|
13 struct istream *test_inputs[3], *try_input;
|
|
14
|
|
15 test_inputs[0] = test_istream_create("1");
|
|
16 test_inputs[1] = test_istream_create("2");
|
|
17 test_inputs[2] = NULL;
|
|
18 test_istream_set_size(test_inputs[0], 0);
|
|
19 test_istream_set_size(test_inputs[1], 0);
|
|
20 try_input = istream_try_create(test_inputs);
|
|
21
|
|
22 /* nonblocking read */
|
|
23 test_assert_idx(i_stream_read(try_input) == 0, test);
|
|
24
|
|
25 switch (test) {
|
|
26 case 0:
|
|
27 /* stream 0 is available */
|
|
28 test_istream_set_size(test_inputs[0], 1);
|
|
29 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
30 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
|
|
31 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
32 break;
|
|
33 case 1:
|
|
34 /* stream 1 is available, but not used before 0 */
|
|
35 test_istream_set_size(test_inputs[1], 1);
|
|
36 test_assert_idx(i_stream_read(try_input) == 0, test);
|
|
37 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
38 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
39 /* continue failing stream 0 -> 1 is available */
|
|
40 test_inputs[0]->stream_errno = EINVAL;
|
|
41 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
42 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
43 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
|
|
44 break;
|
|
45 case 2:
|
|
46 /* both streams are available - stream 0 is read */
|
|
47 test_istream_set_size(test_inputs[0], 1);
|
|
48 test_istream_set_size(test_inputs[1], 1);
|
|
49 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
50 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
|
|
51 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
52 break;
|
|
53 case 3:
|
|
54 /* stream 0 fails */
|
|
55 test_inputs[0]->stream_errno = EINVAL;
|
|
56 test_assert_idx(i_stream_read(try_input) == 0, test);
|
|
57 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
58 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
59 /* continue making stream 1 available */
|
|
60 test_istream_set_size(test_inputs[1], 1);
|
|
61 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
62 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
63 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
|
|
64 break;
|
|
65 case 4:
|
|
66 /* stream 1 fails */
|
|
67 test_inputs[1]->stream_errno = EINVAL;
|
|
68 test_assert_idx(i_stream_read(try_input) == 0, test);
|
|
69 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
70 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
71 break;
|
|
72 case 5:
|
|
73 /* stream 0 fails, stream 1 is available */
|
|
74 test_inputs[0]->stream_errno = EINVAL;
|
|
75 test_istream_set_size(test_inputs[1], 1);
|
|
76 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
77 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 0, test);
|
|
78 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 1, test);
|
|
79 break;
|
|
80 case 6:
|
|
81 /* stream 0 is available, stream 1 fails */
|
|
82 test_inputs[1]->stream_errno = EINVAL;
|
|
83 test_istream_set_size(test_inputs[0], 1);
|
|
84 test_assert_idx(i_stream_read(try_input) == 1, test);
|
|
85 test_assert_idx(i_stream_get_data_size(test_inputs[0]) == 1, test);
|
|
86 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
87 break;
|
|
88 case 7:
|
|
89 /* both streams fail */
|
|
90 test_inputs[0]->stream_errno = EINVAL;
|
|
91 test_inputs[1]->stream_errno = EINVAL;
|
|
92 test_assert_idx(i_stream_read(try_input) == -1, test);
|
|
93 test_assert_idx(try_input->stream_errno == EINVAL, test);
|
|
94 break;
|
|
95 case 8:
|
|
96 /* stream 0 fails with EINVAL, stream 1 with EIO */
|
|
97 test_inputs[0]->stream_errno = EINVAL;
|
|
98 test_inputs[1]->stream_errno = EIO;
|
|
99 test_assert_idx(i_stream_read(try_input) == -1, test);
|
|
100 test_assert_idx(try_input->stream_errno == EIO, test);
|
|
101 break;
|
|
102 case 9:
|
|
103 /* stream 0 fails with EIO, stream 1 with EINVAL */
|
|
104 test_inputs[0]->stream_errno = EIO;
|
|
105 test_inputs[1]->stream_errno = EINVAL;
|
|
106 test_assert_idx(i_stream_read(try_input) == -1, test);
|
|
107 test_assert_idx(try_input->stream_errno == EIO, test);
|
|
108 break;
|
|
109 case 10:
|
|
110 /* stream 0 fails with EIO, stream 1 would work.. */
|
|
111 test_inputs[0]->stream_errno = EIO;
|
|
112 test_istream_set_size(test_inputs[1], 1);
|
|
113 test_assert_idx(i_stream_read(try_input) == -1, test);
|
|
114 test_assert_idx(try_input->stream_errno == EIO, test);
|
|
115 test_assert_idx(i_stream_get_data_size(test_inputs[1]) == 0, test);
|
|
116
|
|
117 finished = TRUE;
|
|
118 break;
|
|
119 }
|
|
120
|
|
121 test_assert_idx(test_inputs[0]->v_offset == 0, test);
|
|
122 test_assert_idx(test_inputs[1]->v_offset == 0, test);
|
|
123
|
|
124 i_stream_unref(&test_inputs[0]);
|
|
125 i_stream_unref(&test_inputs[1]);
|
|
126 i_stream_unref(&try_input);
|
|
127 }
|
|
128 i_assert(finished);
|
|
129 test_end();
|
|
130 }
|