]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_zlib.c
Add libevent
[netatalk.git] / libevent / test / regress_zlib.c
1 /*
2  * Copyright (c) 2008-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /* The old tests here need assertions to work. */
28 #undef NDEBUG
29
30 #ifdef WIN32
31 #include <winsock2.h>
32 #include <windows.h>
33 #endif
34
35 #include "event2/event-config.h"
36
37 #include <sys/types.h>
38 #ifndef WIN32
39 #include <sys/socket.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <netdb.h>
43 #endif
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include <zlib.h>
50 #include <assert.h>
51 #include <errno.h>
52
53 #include "event2/util.h"
54 #include "event2/event.h"
55 #include "event2/event_compat.h"
56 #include "event2/buffer.h"
57 #include "event2/bufferevent.h"
58
59 #include "regress.h"
60
61 static int infilter_calls;
62 static int outfilter_calls;
63 static int readcb_finished;
64 static int writecb_finished;
65 static int errorcb_invoked;
66
67 /*
68  * Zlib filters
69  */
70
71 static void
72 zlib_deflate_free(void *ctx)
73 {
74         z_streamp p = ctx;
75
76         assert(deflateEnd(p) == Z_OK);
77 }
78
79 static void
80 zlib_inflate_free(void *ctx)
81 {
82         z_streamp p = ctx;
83
84         assert(inflateEnd(p) == Z_OK);
85 }
86
87 static int
88 getstate(enum bufferevent_flush_mode state)
89 {
90         switch (state) {
91         case BEV_FINISHED:
92                 return Z_FINISH;
93         case BEV_FLUSH:
94                 return Z_SYNC_FLUSH;
95         case BEV_NORMAL:
96         default:
97                 return Z_NO_FLUSH;
98         }
99 }
100
101 /*
102  * The input filter is triggered only on new input read from the network.
103  * That means all input data needs to be consumed or the filter needs to
104  * initiate its own triggering via a timeout.
105  */
106 static enum bufferevent_filter_result
107 zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
108     ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
109 {
110         struct evbuffer_iovec v_in[1];
111         struct evbuffer_iovec v_out[1];
112         int nread, nwrite;
113         int res, n;
114
115         z_streamp p = ctx;
116
117         do {
118                 /* let's do some decompression */
119                 n = evbuffer_peek(src, -1, NULL, v_in, 1);
120                 if (n) {
121                         p->avail_in = v_in[0].iov_len;
122                         p->next_in = v_in[0].iov_base;
123                 } else {
124                         p->avail_in = 0;
125                         p->next_in = 0;
126                 }
127
128                 evbuffer_reserve_space(dst, 4096, v_out, 1);
129                 p->next_out = v_out[0].iov_base;
130                 p->avail_out = v_out[0].iov_len;
131
132                 /* we need to flush zlib if we got a flush */
133                 res = inflate(p, getstate(state));
134
135                 /* let's figure out how much was compressed */
136                 nread = v_in[0].iov_len - p->avail_in;
137                 nwrite = v_out[0].iov_len - p->avail_out;
138
139                 evbuffer_drain(src, nread);
140                 v_out[0].iov_len = nwrite;
141                 evbuffer_commit_space(dst, v_out, 1);
142
143                 if (res==Z_BUF_ERROR) {
144                         /* We're out of space, or out of decodeable input.
145                            Only if nwrite == 0 assume the latter.
146                          */
147                         if (nwrite == 0)
148                                 return BEV_NEED_MORE;
149                 } else {
150                         assert(res == Z_OK || res == Z_STREAM_END);
151                 }
152
153         } while (evbuffer_get_length(src) > 0);
154
155         ++infilter_calls;
156
157         return (BEV_OK);
158 }
159
160 static enum bufferevent_filter_result
161 zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
162     ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
163 {
164         struct evbuffer_iovec v_in[1];
165         struct evbuffer_iovec v_out[1];
166         int nread, nwrite;
167         int res, n;
168
169         z_streamp p = ctx;
170
171         do {
172                 /* let's do some compression */
173                 n = evbuffer_peek(src, -1, NULL, v_in, 1);
174                 if (n) {
175                         p->avail_in = v_in[0].iov_len;
176                         p->next_in = v_in[0].iov_base;
177                 } else {
178                         p->avail_in = 0;
179                         p->next_in = 0;
180                 }
181
182                 evbuffer_reserve_space(dst, 4096, v_out, 1);
183                 p->next_out = v_out[0].iov_base;
184                 p->avail_out = v_out[0].iov_len;
185
186                 /* we need to flush zlib if we got a flush */
187                 res = deflate(p, getstate(state));
188
189                 /* let's figure out how much was decompressed */
190                 nread = v_in[0].iov_len - p->avail_in;
191                 nwrite = v_out[0].iov_len - p->avail_out;
192
193                 evbuffer_drain(src, nread);
194                 v_out[0].iov_len = nwrite;
195                 evbuffer_commit_space(dst, v_out, 1);
196
197                 if (res==Z_BUF_ERROR) {
198                         /* We're out of space, or out of decodeable input.
199                            Only if nwrite == 0 assume the latter.
200                          */
201                         if (nwrite == 0)
202                                 return BEV_NEED_MORE;
203                 } else {
204                         assert(res == Z_OK || res == Z_STREAM_END);
205                 }
206
207         } while (evbuffer_get_length(src) > 0);
208
209         ++outfilter_calls;
210
211         return (BEV_OK);
212 }
213
214 /*
215  * simple bufferevent test (over transparent zlib treatment)
216  */
217
218 static void
219 readcb(struct bufferevent *bev, void *arg)
220 {
221         if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
222                 struct evbuffer *evbuf = evbuffer_new();
223                 assert(evbuf != NULL);
224
225                 /* gratuitous test of bufferevent_read_buffer */
226                 bufferevent_read_buffer(bev, evbuf);
227
228                 bufferevent_disable(bev, EV_READ);
229
230                 if (evbuffer_get_length(evbuf) == 8333) {
231                         ++readcb_finished;
232                 }
233
234                 evbuffer_free(evbuf);
235         }
236 }
237
238 static void
239 writecb(struct bufferevent *bev, void *arg)
240 {
241         if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
242                 ++writecb_finished;
243         }
244 }
245
246 static void
247 errorcb(struct bufferevent *bev, short what, void *arg)
248 {
249         errorcb_invoked = 1;
250 }
251
252 void
253 test_bufferevent_zlib(void *arg)
254 {
255         struct bufferevent *bev1=NULL, *bev2=NULL;
256         char buffer[8333];
257         z_stream z_input, z_output;
258         int i, pair[2]={-1,-1}, r;
259         (void)arg;
260
261         infilter_calls = outfilter_calls = readcb_finished = writecb_finished
262             = errorcb_invoked = 0;
263
264         if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
265                 tt_abort_perror("socketpair");
266         }
267
268         evutil_make_socket_nonblocking(pair[0]);
269         evutil_make_socket_nonblocking(pair[1]);
270
271         bev1 = bufferevent_socket_new(NULL, pair[0], 0);
272         bev2 = bufferevent_socket_new(NULL, pair[1], 0);
273
274         memset(&z_output, 0, sizeof(z_output));
275         r = deflateInit(&z_output, Z_DEFAULT_COMPRESSION);
276         tt_int_op(r, ==, Z_OK);
277         memset(&z_input, 0, sizeof(z_input));
278         r = inflateInit(&z_input);
279
280         /* initialize filters */
281         bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
282             BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, &z_output);
283         bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
284             NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, &z_input);
285         bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
286         bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
287
288         bufferevent_disable(bev1, EV_READ);
289         bufferevent_enable(bev1, EV_WRITE);
290
291         bufferevent_enable(bev2, EV_READ);
292
293         for (i = 0; i < (int)sizeof(buffer); i++)
294                 buffer[i] = i;
295
296         /* break it up into multiple buffer chains */
297         bufferevent_write(bev1, buffer, 1800);
298         bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
299
300         /* we are done writing - we need to flush everything */
301         bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
302
303         event_dispatch();
304
305         tt_want(infilter_calls);
306         tt_want(outfilter_calls);
307         tt_want(readcb_finished);
308         tt_want(writecb_finished);
309         tt_want(!errorcb_invoked);
310
311         test_ok = 1;
312 end:
313         if (bev1)
314                 bufferevent_free(bev1);
315         if (bev2)
316                 bufferevent_free(bev2);
317
318         if (pair[0] >= 0)
319                 evutil_closesocket(pair[0]);
320         if (pair[1] >= 0)
321                 evutil_closesocket(pair[1]);
322 }