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
4  *
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.
15  *
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.
26  */
27 
28 /* The old tests here need assertions to work. */
29 #undef NDEBUG
30 
31 #ifdef WIN32
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35 
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 $");
39 
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>
51 
52 #include <assert.h>
53 #include <errno.h>
54 
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"
60 
61 #include "regress.h"
62 
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.
66 
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.
69 */
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
82 
83 #include <zlib.h>
84 
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;
90 
91 /*
92  * Zlib filters
93  */
94 
95 static void
96 zlib_deflate_free(void *ctx)
97 {
98 	z_streamp p = ctx;
99 
100 	assert(deflateEnd(p) == Z_OK);
101 }
102 
103 static void
104 zlib_inflate_free(void *ctx)
105 {
106 	z_streamp p = ctx;
107 
108 	assert(inflateEnd(p) == Z_OK);
109 }
110 
111 static int
112 getstate(enum bufferevent_flush_mode state)
113 {
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;
122 	}
123 }
124 
125 /*
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.
129  */
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)
133 {
134 	struct evbuffer_iovec v_in[1];
135 	struct evbuffer_iovec v_out[1];
136 	int nread, nwrite;
137 	int res, n;
138 
139 	z_streamp p = ctx;
140 
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;
150 		}
151 
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;
155 
156 		/* we need to flush zlib if we got a flush */
157 		res = inflate(p, getstate(state));
158 
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;
162 
163 		evbuffer_drain(src, nread);
164 		v_out[0].iov_len = nwrite;
165 		evbuffer_commit_space(dst, v_out, 1);
166 
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.
170 			 */
171 			if (nwrite == 0)
172 				return BEV_NEED_MORE;
173 		} else {
174 			assert(res == Z_OK || res == Z_STREAM_END);
175 		}
176 
177 	} while (evbuffer_get_length(src) > 0);
178 
179 	++infilter_calls;
180 
181 	return (BEV_OK);
182 }
183 
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)
187 {
188 	struct evbuffer_iovec v_in[1];
189 	struct evbuffer_iovec v_out[1];
190 	int nread, nwrite;
191 	int res, n;
192 
193 	z_streamp p = ctx;
194 
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;
204 		}
205 
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;
209 
210 		/* we need to flush zlib if we got a flush */
211 		res = deflate(p, getstate(state));
212 
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;
216 
217 		evbuffer_drain(src, nread);
218 		v_out[0].iov_len = nwrite;
219 		evbuffer_commit_space(dst, v_out, 1);
220 
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.
224 			 */
225 			if (nwrite == 0)
226 				return BEV_NEED_MORE;
227 		} else {
228 			assert(res == Z_OK || res == Z_STREAM_END);
229 		}
230 
231 	} while (evbuffer_get_length(src) > 0);
232 
233 	++outfilter_calls;
234 
235 	return (BEV_OK);
236 }
237 
238 /*
239  * simple bufferevent test (over transparent zlib treatment)
240  */
241 
242 static void
243 readcb(struct bufferevent *bev, void *arg)
244 {
245 	if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
246 		struct evbuffer *evbuf = evbuffer_new();
247 		assert(evbuf != NULL);
248 
249 		/* gratuitous test of bufferevent_read_buffer */
250 		bufferevent_read_buffer(bev, evbuf);
251 
252 		bufferevent_disable(bev, EV_READ);
253 
254 		if (evbuffer_get_length(evbuf) == 8333) {
255 			++readcb_finished;
256 		}
257 
258 		evbuffer_free(evbuf);
259 	}
260 }
261 
262 static void
263 writecb(struct bufferevent *bev, void *arg)
264 {
265 	if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
266 		++writecb_finished;
267 	}
268 }
269 
270 static void
271 errorcb(struct bufferevent *bev, short what, void *arg)
272 {
273 	errorcb_invoked = 1;
274 }
275 
276 void
277 test_bufferevent_zlib(void *arg)
278 {
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;
285 
286 	infilter_calls = outfilter_calls = readcb_finished = writecb_finished
287 	    = errorcb_invoked = 0;
288 
289 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, xpair) == -1) {
290 		tt_abort_perror("socketpair");
291 	}
292 
293 	evutil_make_socket_nonblocking(xpair[0]);
294 	evutil_make_socket_nonblocking(xpair[1]);
295 
296 	bev1 = bufferevent_socket_new(NULL, xpair[0], 0);
297 	bev2 = bufferevent_socket_new(NULL, xpair[1], 0);
298 
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);
305 
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);
313 
314 	bufferevent_disable(bev1, EV_READ);
315 	bufferevent_enable(bev1, EV_WRITE);
316 
317 	bufferevent_enable(bev2, EV_READ);
318 
319 	for (i = 0; i < (int)sizeof(buffer); i++)
320 		buffer[i] = i;
321 
322 	/* break it up into multiple buffer chains */
323 	bufferevent_write(bev1, buffer, 1800);
324 	bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
325 
326 	/* we are done writing - we need to flush everything */
327 	bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
328 
329 	event_dispatch();
330 
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);
336 
337 	test_ok = 1;
338 end:
339 	if (bev1)
340 		bufferevent_free(bev1);
341 	if (bev2)
342 		bufferevent_free(bev2);
343 
344 	if (xpair[0] >= 0)
345 		evutil_closesocket(xpair[0]);
346 	if (xpair[1] >= 0)
347 		evutil_closesocket(xpair[1]);
348 }
349