1/*	$NetBSD: regress_zlib.c,v 1.3 2017/01/31 23:17:40 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.3 2017/01/31 23:17:40 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#include "mm-internal.h"
63
64/* zlib 1.2.4 and 1.2.5 do some "clever" things with macros.  Instead of
65   saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
66   that nobody will care if the compile outputs a no-such-identifier warning.
67
68   Sorry, but we like -Werror over here, so I guess we need to define these.
69   I hope that zlib 1.2.6 doesn't break these too.
70*/
71#ifndef _LARGEFILE64_SOURCE
72#define _LARGEFILE64_SOURCE 0
73#endif
74#ifndef _LFS64_LARGEFILE
75#define _LFS64_LARGEFILE 0
76#endif
77#ifndef _FILE_OFFSET_BITS
78#define _FILE_OFFSET_BITS 0
79#endif
80#ifndef off64_t
81#define off64_t ev_int64_t
82#endif
83
84#include <zlib.h>
85
86static int infilter_calls;
87static int outfilter_calls;
88static int readcb_finished;
89static int writecb_finished;
90static int errorcb_invoked;
91
92/*
93 * Zlib filters
94 */
95
96static void
97zlib_deflate_free(void *ctx)
98{
99	z_streamp p = ctx;
100
101	assert(deflateEnd(p) == Z_OK);
102	mm_free(p);
103}
104
105static void
106zlib_inflate_free(void *ctx)
107{
108	z_streamp p = ctx;
109
110	assert(inflateEnd(p) == Z_OK);
111	mm_free(p);
112}
113
114static int
115getstate(enum bufferevent_flush_mode state)
116{
117	switch (state) {
118	case BEV_FINISHED:
119		return Z_FINISH;
120	case BEV_FLUSH:
121		return Z_SYNC_FLUSH;
122	case BEV_NORMAL:
123	default:
124		return Z_NO_FLUSH;
125	}
126}
127
128/*
129 * The input filter is triggered only on new input read from the network.
130 * That means all input data needs to be consumed or the filter needs to
131 * initiate its own triggering via a timeout.
132 */
133static enum bufferevent_filter_result
134zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
135    ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
136{
137	struct evbuffer_iovec v_in[1];
138	struct evbuffer_iovec v_out[1];
139	int nread, nwrite;
140	int res, n;
141
142	z_streamp p = ctx;
143
144	do {
145		/* let's do some decompression */
146		n = evbuffer_peek(src, -1, NULL, v_in, 1);
147		if (n) {
148			p->avail_in = v_in[0].iov_len;
149			p->next_in = (unsigned char *)v_in[0].iov_base;
150		} else {
151			p->avail_in = 0;
152			p->next_in = 0;
153		}
154
155		evbuffer_reserve_space(dst, 4096, v_out, 1);
156		p->next_out = (unsigned char *)v_out[0].iov_base;
157		p->avail_out = v_out[0].iov_len;
158
159		/* we need to flush zlib if we got a flush */
160		res = inflate(p, getstate(state));
161
162		/* let's figure out how much was compressed */
163		nread = v_in[0].iov_len - p->avail_in;
164		nwrite = v_out[0].iov_len - p->avail_out;
165
166		evbuffer_drain(src, nread);
167		v_out[0].iov_len = nwrite;
168		evbuffer_commit_space(dst, v_out, 1);
169
170		if (res==Z_BUF_ERROR) {
171			/* We're out of space, or out of decodeable input.
172			   Only if nwrite == 0 assume the latter.
173			 */
174			if (nwrite == 0)
175				return BEV_NEED_MORE;
176		} else {
177			assert(res == Z_OK || res == Z_STREAM_END);
178		}
179
180	} while (evbuffer_get_length(src) > 0);
181
182	++infilter_calls;
183
184	return (BEV_OK);
185}
186
187static enum bufferevent_filter_result
188zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
189    ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
190{
191	struct evbuffer_iovec v_in[1];
192	struct evbuffer_iovec v_out[1];
193	int nread, nwrite;
194	int res, n;
195
196	z_streamp p = ctx;
197
198	do {
199		/* let's do some compression */
200		n = evbuffer_peek(src, -1, NULL, v_in, 1);
201		if (n) {
202			p->avail_in = v_in[0].iov_len;
203			p->next_in = (unsigned char *)v_in[0].iov_base;
204		} else {
205			p->avail_in = 0;
206			p->next_in = 0;
207		}
208
209		evbuffer_reserve_space(dst, 4096, v_out, 1);
210		p->next_out = (unsigned char *)v_out[0].iov_base;
211		p->avail_out = v_out[0].iov_len;
212
213		/* we need to flush zlib if we got a flush */
214		res = deflate(p, getstate(state));
215
216		/* let's figure out how much was decompressed */
217		nread = v_in[0].iov_len - p->avail_in;
218		nwrite = v_out[0].iov_len - p->avail_out;
219
220		evbuffer_drain(src, nread);
221		v_out[0].iov_len = nwrite;
222		evbuffer_commit_space(dst, v_out, 1);
223
224		if (res==Z_BUF_ERROR) {
225			/* We're out of space, or out of decodeable input.
226			   Only if nwrite == 0 assume the latter.
227			 */
228			if (nwrite == 0)
229				return BEV_NEED_MORE;
230		} else {
231			assert(res == Z_OK || res == Z_STREAM_END);
232		}
233
234	} while (evbuffer_get_length(src) > 0);
235
236	++outfilter_calls;
237
238	return (BEV_OK);
239}
240
241/*
242 * simple bufferevent test (over transparent zlib treatment)
243 */
244
245static void
246readcb(struct bufferevent *bev, void *arg)
247{
248	if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
249		struct evbuffer *evbuf = evbuffer_new();
250		assert(evbuf != NULL);
251
252		/* gratuitous test of bufferevent_read_buffer */
253		bufferevent_read_buffer(bev, evbuf);
254
255		bufferevent_disable(bev, EV_READ);
256
257		if (evbuffer_get_length(evbuf) == 8333) {
258			++readcb_finished;
259		}
260
261		evbuffer_free(evbuf);
262	}
263}
264
265static void
266writecb(struct bufferevent *bev, void *arg)
267{
268	if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
269		++writecb_finished;
270	}
271}
272
273static void
274errorcb(struct bufferevent *bev, short what, void *arg)
275{
276	errorcb_invoked = 1;
277}
278
279void
280test_bufferevent_zlib(void *arg)
281{
282	struct bufferevent *bev1=NULL, *bev2=NULL;
283	char buffer[8333];
284	z_stream *z_input, *z_output;
285	int i, r;
286	evutil_socket_t xpair[2] = {-1, -1};
287	(void)arg;
288
289	infilter_calls = outfilter_calls = readcb_finished = writecb_finished
290	    = errorcb_invoked = 0;
291
292	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, xpair) == -1) {
293		tt_abort_perror("socketpair");
294	}
295
296	evutil_make_socket_nonblocking(xpair[0]);
297	evutil_make_socket_nonblocking(xpair[1]);
298
299	bev1 = bufferevent_socket_new(NULL, xpair[0], 0);
300	bev2 = bufferevent_socket_new(NULL, xpair[1], 0);
301
302	z_output = mm_calloc(sizeof(*z_output), 1);
303	r = deflateInit(z_output, Z_DEFAULT_COMPRESSION);
304	tt_int_op(r, ==, Z_OK);
305	z_input = mm_calloc(sizeof(*z_input), 1);
306	r = inflateInit(z_input);
307	tt_int_op(r, ==, Z_OK);
308
309	/* initialize filters */
310	bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
311	    BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, z_output);
312	bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
313	    NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, z_input);
314	bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
315	bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
316
317	bufferevent_disable(bev1, EV_READ);
318	bufferevent_enable(bev1, EV_WRITE);
319
320	bufferevent_enable(bev2, EV_READ);
321
322	for (i = 0; i < (int)sizeof(buffer); i++)
323		buffer[i] = i;
324
325	/* break it up into multiple buffer chains */
326	bufferevent_write(bev1, buffer, 1800);
327	bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
328
329	/* we are done writing - we need to flush everything */
330	bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
331
332	event_dispatch();
333
334	tt_want(infilter_calls);
335	tt_want(outfilter_calls);
336	tt_want(readcb_finished);
337	tt_want(writecb_finished);
338	tt_want(!errorcb_invoked);
339
340	test_ok = 1;
341end:
342	if (bev1)
343		bufferevent_free(bev1);
344	if (bev2)
345		bufferevent_free(bev2);
346
347	if (xpair[0] >= 0)
348		evutil_closesocket(xpair[0]);
349	if (xpair[1] >= 0)
350		evutil_closesocket(xpair[1]);
351}
352