1 /* $NetBSD: regress_zlib.c,v 1.2 2013/04/11 16:56:42 christos Exp $ */
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
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. */
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>
42 #include <sys/socket.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"
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
73 #ifndef _LFS64_LARGEFILE
74 #define _LFS64_LARGEFILE 0
76 #ifndef _FILE_OFFSET_BITS
77 #define _FILE_OFFSET_BITS 0
80 #define off64_t ev_int64_t
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
;
96 zlib_deflate_free(void *ctx
)
100 assert(deflateEnd(p
) == Z_OK
);
104 zlib_inflate_free(void *ctx
)
108 assert(inflateEnd(p
) == Z_OK
);
112 getstate(enum bufferevent_flush_mode state
)
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];
142 /* let's do some decompression */
143 n
= evbuffer_peek(src
, -1, NULL
, v_in
, 1);
145 p
->avail_in
= v_in
[0].iov_len
;
146 p
->next_in
= v_in
[0].iov_base
;
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.
172 return BEV_NEED_MORE
;
174 assert(res
== Z_OK
|| res
== Z_STREAM_END
);
177 } while (evbuffer_get_length(src
) > 0);
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];
196 /* let's do some compression */
197 n
= evbuffer_peek(src
, -1, NULL
, v_in
, 1);
199 p
->avail_in
= v_in
[0].iov_len
;
200 p
->next_in
= v_in
[0].iov_base
;
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.
226 return BEV_NEED_MORE
;
228 assert(res
== Z_OK
|| res
== Z_STREAM_END
);
231 } while (evbuffer_get_length(src
) > 0);
239 * simple bufferevent test (over transparent zlib treatment)
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) {
258 evbuffer_free(evbuf
);
263 writecb(struct bufferevent
*bev
, void *arg
)
265 if (evbuffer_get_length(bufferevent_get_output(bev
)) == 0) {
271 errorcb(struct bufferevent
*bev
, short what
, void *arg
)
277 test_bufferevent_zlib(void *arg
)
279 struct bufferevent
*bev1
=NULL
, *bev2
=NULL
;
281 z_stream z_input
, z_output
;
283 evutil_socket_t xpair
[2] = {-1, -1};
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
++)
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
);
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
);
340 bufferevent_free(bev1
);
342 bufferevent_free(bev2
);
345 evutil_closesocket(xpair
[0]);
347 evutil_closesocket(xpair
[1]);