Mercurial > dovecot > core-2.2
annotate src/lib-test/test-ostream.c @ 23017:c1d36f2575c7 default tip
lib-imap: Fix "Don't accept strings with NULs" cherry-pick
author | Timo Sirainen <timo.sirainen@open-xchange.com> |
---|---|
date | Thu, 29 Aug 2019 09:55:25 +0300 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */ |
20810
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
2 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
4 #include "buffer.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
5 #include "ostream-private.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
6 #include "test-common.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
7 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
8 struct test_ostream { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
9 struct ostream_private ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
10 buffer_t *internal_buf; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
11 buffer_t *output_buf; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
12 size_t max_output_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
13 struct timeout *to; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
14 bool flush_pending; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
15 }; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
16 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
17 static void o_stream_test_destroy(struct iostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
18 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
19 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
20 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
21 if (tstream->to != NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
22 timeout_remove(&tstream->to); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
23 if (tstream->internal_buf != NULL) |
20813
6e6163782590
lib-test: Fix memory leak in test-ostream
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20812
diff
changeset
|
24 buffer_free(&tstream->internal_buf); |
20810
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
25 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
26 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
27 static int o_stream_test_flush(struct ostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
28 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
29 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
30 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
31 if (tstream->internal_buf == NULL || tstream->internal_buf->used == 0) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
32 return 1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
33 if (tstream->output_buf->used >= tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
34 return 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
35 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
36 size_t left = tstream->max_output_size - tstream->output_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
37 size_t n = I_MIN(left, tstream->internal_buf->used); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
38 buffer_append(tstream->output_buf, tstream->internal_buf->data, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
39 buffer_delete(tstream->internal_buf, 0, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
40 return tstream->internal_buf->used == 0 ? 1 : 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
41 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
42 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
43 static ssize_t |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
44 o_stream_test_sendv(struct ostream_private *stream, |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
45 const struct const_iovec *iov, unsigned int iov_count) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
46 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
47 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
48 struct const_iovec cur_iov = { NULL, 0 }; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
49 size_t left, n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
50 ssize_t ret = 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
51 unsigned int i; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
52 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
53 /* first we need to try to flush the internal buffer */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
54 if ((ret = o_stream_test_flush(stream)) <= 0) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
55 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
56 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
57 /* append to output_buf until max_output_size is reached */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
58 ret = 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
59 for (i = 0; i < iov_count; i++) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
60 left = tstream->max_output_size < tstream->output_buf->used ? 0 : |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
61 tstream->max_output_size - tstream->output_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
62 n = I_MIN(left, iov[i].iov_len); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
63 buffer_append(tstream->output_buf, iov[i].iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
64 stream->ostream.offset += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
65 ret += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
66 if (n != iov[i].iov_len) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
67 cur_iov.iov_base = CONST_PTR_OFFSET(iov[i].iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
68 cur_iov.iov_len = iov[i].iov_len - n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
69 break; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
70 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
71 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
72 /* if we've internal_buf, append to it until max_buffer_size is |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
73 reached */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
74 if (i == iov_count || tstream->internal_buf == NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
75 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
76 do { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
77 left = tstream->ostream.max_buffer_size - |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
78 tstream->internal_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
79 n = I_MIN(left, cur_iov.iov_len); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
80 buffer_append(tstream->internal_buf, cur_iov.iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
81 stream->ostream.offset += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
82 ret += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
83 if (n != cur_iov.iov_len) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
84 break; |
20812
d2969430234a
lib-test: Fix illegal memory access in test-ostream
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20810
diff
changeset
|
85 if (++i < iov_count) |
d2969430234a
lib-test: Fix illegal memory access in test-ostream
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
20810
diff
changeset
|
86 cur_iov = iov[i]; |
20810
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
87 } while (i < iov_count); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
88 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
89 tstream->flush_pending = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
90 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
91 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
92 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
93 static void test_ostream_send_more(struct test_ostream *tstream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
94 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
95 struct ostream *ostream = &tstream->ostream.ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
96 int ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
97 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
98 o_stream_ref(ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
99 tstream->flush_pending = FALSE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
100 if (tstream->ostream.callback != NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
101 ret = tstream->ostream.callback(tstream->ostream.context); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
102 else |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
103 ret = o_stream_test_flush(&tstream->ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
104 if (ret == 0 || (tstream->internal_buf != NULL && |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
105 tstream->internal_buf->used > 0)) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
106 tstream->flush_pending = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
107 if (!tstream->flush_pending || |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
108 tstream->output_buf->used >= tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
109 timeout_remove(&tstream->to); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
110 o_stream_unref(&ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
111 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
112 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
113 static void test_ostream_set_send_more_timeout(struct test_ostream *tstream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
114 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
115 if (tstream->to == NULL && tstream->flush_pending && |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
116 tstream->output_buf->used < tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
117 tstream->to = timeout_add_short(0, test_ostream_send_more, tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
118 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
119 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
120 static void |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
121 o_stream_test_flush_pending(struct ostream_private *stream, bool set) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
122 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
123 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
124 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
125 if (tstream->internal_buf != NULL && tstream->internal_buf->used > 0) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
126 /* we have internal data, won't reset flush_pending */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
127 i_assert(tstream->flush_pending); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
128 } else { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
129 tstream->flush_pending = set; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
130 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
131 if (set) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
132 test_ostream_set_send_more_timeout(tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
133 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
134 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
135 static size_t |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
136 o_stream_test_get_used_size(const struct ostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
137 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
138 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
139 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
140 return tstream->internal_buf == NULL ? 0 : |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
141 tstream->internal_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
142 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
143 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
144 struct ostream *test_ostream_create(buffer_t *output) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
145 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
146 struct test_ostream *tstream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
147 struct ostream *ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
148 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
149 tstream = i_new(struct test_ostream, 1); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
150 tstream->ostream.max_buffer_size = (size_t)-1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
151 tstream->ostream.iostream.destroy = o_stream_test_destroy; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
152 tstream->ostream.sendv = o_stream_test_sendv; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
153 tstream->ostream.flush = o_stream_test_flush; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
154 tstream->ostream.flush_pending = o_stream_test_flush_pending; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
155 tstream->ostream.get_used_size = o_stream_test_get_used_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
156 tstream->ostream.ostream.blocking = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
157 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
158 tstream->output_buf = output; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
159 tstream->max_output_size = (size_t)-1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
160 ostream = o_stream_create(&tstream->ostream, NULL, -1); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
161 o_stream_set_name(ostream, "(test-ostream)"); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
162 return ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
163 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
164 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
165 struct ostream *test_ostream_create_nonblocking(buffer_t *output, |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
166 size_t max_internal_buffer_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
167 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
168 struct test_ostream *tstream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
169 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
170 tstream = (struct test_ostream *)test_ostream_create(output)->real_stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
171 tstream->internal_buf = buffer_create_dynamic(default_pool, 128); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
172 tstream->ostream.ostream.blocking = FALSE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
173 tstream->ostream.max_buffer_size = max_internal_buffer_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
174 return &tstream->ostream.ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
175 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
176 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
177 static struct test_ostream *test_ostream_find(struct ostream *output) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
178 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
179 struct ostream *out; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
180 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
181 for (out = output; out != NULL; out = out->real_stream->parent) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
182 if (out->real_stream->sendv == o_stream_test_sendv) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
183 return (struct test_ostream *)out->real_stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
184 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
185 i_panic("%s isn't test-ostream", o_stream_get_name(output)); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
186 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
187 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
188 void test_ostream_set_max_output_size(struct ostream *output, size_t max_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
189 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
190 struct test_ostream *tstream = test_ostream_find(output); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
191 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
192 tstream->max_output_size = max_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
193 test_ostream_set_send_more_timeout(tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
194 } |