Mercurial > dovecot > core-2.2
annotate src/lib/test-istream-try.c @ 23007:36e01285b5b8
lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author | Stephan Bosch <stephan.bosch@dovecot.fi> |
---|---|
date | Mon, 18 Mar 2019 00:52:37 +0100 |
parents | 1611d0ce5001 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
22607
diff
changeset
|
1 /* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */ |
22607 | 2 |
3 #include "test-lib.h" | |
4 #include "istream.h" | |
5 #include "istream-try.h" | |
6 | |
22987
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
7 static void test_istream_try_normal(void) |
22607 | 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 } | |
22987
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
131 |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
132 static void test_istream_try_empty(void) |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
133 { |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
134 test_begin("istream try empty stream"); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
135 struct istream *test_inputs[] = { |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
136 test_istream_create(""), |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
137 test_istream_create(""), |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
138 NULL |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
139 }; |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
140 struct istream *try_input = istream_try_create(test_inputs); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
141 test_assert(i_stream_read(try_input) == -1); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
142 test_assert(try_input->eof); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
143 test_assert(try_input->stream_errno == 0); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
144 i_stream_unref(&test_inputs[0]); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
145 i_stream_unref(&test_inputs[1]); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
146 i_stream_unref(&try_input); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
147 test_end(); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
148 } |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
149 |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
150 void test_istream_try(void) |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
151 { |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
152 test_istream_try_normal(); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
153 test_istream_try_empty(); |
1611d0ce5001
lib: istream-try - Don't assert-crash with empty parent istream
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22713
diff
changeset
|
154 } |