etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libevent / dist / test / regress_zlib.c
blobfafa83c5f7f451e48562ab1246e9c7dc5235b1b6
1 /* $NetBSD: regress_zlib.c,v 1.2 2013/04/11 16:56:42 christos Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* The old tests here need assertions to work. */
29 #undef NDEBUG
31 #ifdef WIN32
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
36 #include "event2/event-config.h"
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: regress_zlib.c,v 1.2 2013/04/11 16:56:42 christos Exp $");
40 #include <sys/types.h>
41 #ifndef WIN32
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45 #include <netdb.h>
46 #endif
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
52 #include <assert.h>
53 #include <errno.h>
55 #include "event2/util.h"
56 #include "event2/event.h"
57 #include "event2/event_compat.h"
58 #include "event2/buffer.h"
59 #include "event2/bufferevent.h"
61 #include "regress.h"
63 /* zlib 1.2.4 and 1.2.5 do some "clever" things with macros. Instead of
64 saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
65 that nobody will care if the compile outputs a no-such-identifier warning.
67 Sorry, but we like -Werror over here, so I guess we need to define these.
68 I hope that zlib 1.2.6 doesn't break these too.
70 #ifndef _LARGEFILE64_SOURCE
71 #define _LARGEFILE64_SOURCE 0
72 #endif
73 #ifndef _LFS64_LARGEFILE
74 #define _LFS64_LARGEFILE 0
75 #endif
76 #ifndef _FILE_OFFSET_BITS
77 #define _FILE_OFFSET_BITS 0
78 #endif
79 #ifndef off64_t
80 #define off64_t ev_int64_t
81 #endif
83 #include <zlib.h>
85 static int infilter_calls;
86 static int outfilter_calls;
87 static int readcb_finished;
88 static int writecb_finished;
89 static int errorcb_invoked;
92 * Zlib filters
95 static void
96 zlib_deflate_free(void *ctx)
98 z_streamp p = ctx;
100 assert(deflateEnd(p) == Z_OK);
103 static void
104 zlib_inflate_free(void *ctx)
106 z_streamp p = ctx;
108 assert(inflateEnd(p) == Z_OK);
111 static int
112 getstate(enum bufferevent_flush_mode state)
114 switch (state) {
115 case BEV_FINISHED:
116 return Z_FINISH;
117 case BEV_FLUSH:
118 return Z_SYNC_FLUSH;
119 case BEV_NORMAL:
120 default:
121 return Z_NO_FLUSH;
126 * The input filter is triggered only on new input read from the network.
127 * That means all input data needs to be consumed or the filter needs to
128 * initiate its own triggering via a timeout.
130 static enum bufferevent_filter_result
131 zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
132 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
134 struct evbuffer_iovec v_in[1];
135 struct evbuffer_iovec v_out[1];
136 int nread, nwrite;
137 int res, n;
139 z_streamp p = ctx;
141 do {
142 /* let's do some decompression */
143 n = evbuffer_peek(src, -1, NULL, v_in, 1);
144 if (n) {
145 p->avail_in = v_in[0].iov_len;
146 p->next_in = v_in[0].iov_base;
147 } else {
148 p->avail_in = 0;
149 p->next_in = 0;
152 evbuffer_reserve_space(dst, 4096, v_out, 1);
153 p->next_out = v_out[0].iov_base;
154 p->avail_out = v_out[0].iov_len;
156 /* we need to flush zlib if we got a flush */
157 res = inflate(p, getstate(state));
159 /* let's figure out how much was compressed */
160 nread = v_in[0].iov_len - p->avail_in;
161 nwrite = v_out[0].iov_len - p->avail_out;
163 evbuffer_drain(src, nread);
164 v_out[0].iov_len = nwrite;
165 evbuffer_commit_space(dst, v_out, 1);
167 if (res==Z_BUF_ERROR) {
168 /* We're out of space, or out of decodeable input.
169 Only if nwrite == 0 assume the latter.
171 if (nwrite == 0)
172 return BEV_NEED_MORE;
173 } else {
174 assert(res == Z_OK || res == Z_STREAM_END);
177 } while (evbuffer_get_length(src) > 0);
179 ++infilter_calls;
181 return (BEV_OK);
184 static enum bufferevent_filter_result
185 zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
186 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
188 struct evbuffer_iovec v_in[1];
189 struct evbuffer_iovec v_out[1];
190 int nread, nwrite;
191 int res, n;
193 z_streamp p = ctx;
195 do {
196 /* let's do some compression */
197 n = evbuffer_peek(src, -1, NULL, v_in, 1);
198 if (n) {
199 p->avail_in = v_in[0].iov_len;
200 p->next_in = v_in[0].iov_base;
201 } else {
202 p->avail_in = 0;
203 p->next_in = 0;
206 evbuffer_reserve_space(dst, 4096, v_out, 1);
207 p->next_out = v_out[0].iov_base;
208 p->avail_out = v_out[0].iov_len;
210 /* we need to flush zlib if we got a flush */
211 res = deflate(p, getstate(state));
213 /* let's figure out how much was decompressed */
214 nread = v_in[0].iov_len - p->avail_in;
215 nwrite = v_out[0].iov_len - p->avail_out;
217 evbuffer_drain(src, nread);
218 v_out[0].iov_len = nwrite;
219 evbuffer_commit_space(dst, v_out, 1);
221 if (res==Z_BUF_ERROR) {
222 /* We're out of space, or out of decodeable input.
223 Only if nwrite == 0 assume the latter.
225 if (nwrite == 0)
226 return BEV_NEED_MORE;
227 } else {
228 assert(res == Z_OK || res == Z_STREAM_END);
231 } while (evbuffer_get_length(src) > 0);
233 ++outfilter_calls;
235 return (BEV_OK);
239 * simple bufferevent test (over transparent zlib treatment)
242 static void
243 readcb(struct bufferevent *bev, void *arg)
245 if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
246 struct evbuffer *evbuf = evbuffer_new();
247 assert(evbuf != NULL);
249 /* gratuitous test of bufferevent_read_buffer */
250 bufferevent_read_buffer(bev, evbuf);
252 bufferevent_disable(bev, EV_READ);
254 if (evbuffer_get_length(evbuf) == 8333) {
255 ++readcb_finished;
258 evbuffer_free(evbuf);
262 static void
263 writecb(struct bufferevent *bev, void *arg)
265 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
266 ++writecb_finished;
270 static void
271 errorcb(struct bufferevent *bev, short what, void *arg)
273 errorcb_invoked = 1;
276 void
277 test_bufferevent_zlib(void *arg)
279 struct bufferevent *bev1=NULL, *bev2=NULL;
280 char buffer[8333];
281 z_stream z_input, z_output;
282 int i, r;
283 evutil_socket_t xpair[2] = {-1, -1};
284 (void)arg;
286 infilter_calls = outfilter_calls = readcb_finished = writecb_finished
287 = errorcb_invoked = 0;
289 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, xpair) == -1) {
290 tt_abort_perror("socketpair");
293 evutil_make_socket_nonblocking(xpair[0]);
294 evutil_make_socket_nonblocking(xpair[1]);
296 bev1 = bufferevent_socket_new(NULL, xpair[0], 0);
297 bev2 = bufferevent_socket_new(NULL, xpair[1], 0);
299 memset(&z_output, 0, sizeof(z_output));
300 r = deflateInit(&z_output, Z_DEFAULT_COMPRESSION);
301 tt_int_op(r, ==, Z_OK);
302 memset(&z_input, 0, sizeof(z_input));
303 r = inflateInit(&z_input);
304 tt_int_op(r, ==, Z_OK);
306 /* initialize filters */
307 bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
308 BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, &z_output);
309 bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
310 NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, &z_input);
311 bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
312 bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
314 bufferevent_disable(bev1, EV_READ);
315 bufferevent_enable(bev1, EV_WRITE);
317 bufferevent_enable(bev2, EV_READ);
319 for (i = 0; i < (int)sizeof(buffer); i++)
320 buffer[i] = i;
322 /* break it up into multiple buffer chains */
323 bufferevent_write(bev1, buffer, 1800);
324 bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
326 /* we are done writing - we need to flush everything */
327 bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
329 event_dispatch();
331 tt_want(infilter_calls);
332 tt_want(outfilter_calls);
333 tt_want(readcb_finished);
334 tt_want(writecb_finished);
335 tt_want(!errorcb_invoked);
337 test_ok = 1;
338 end:
339 if (bev1)
340 bufferevent_free(bev1);
341 if (bev2)
342 bufferevent_free(bev2);
344 if (xpair[0] >= 0)
345 evutil_closesocket(xpair[0]);
346 if (xpair[1] >= 0)
347 evutil_closesocket(xpair[1]);