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