xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_zlib.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*
2  * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /* The old tests here need assertions to work. */
28 #undef NDEBUG
29 
30 #ifdef _WIN32
31 #include <winsock2.h>
32 #include <windows.h>
33 #endif
34 
35 #include "event2/event-config.h"
36 
37 #include <sys/types.h>
38 #ifndef _WIN32
39 #include <sys/socket.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <netdb.h>
43 #endif
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <assert.h>
50 #include <errno.h>
51 
52 #include "event2/util.h"
53 #include "event2/event.h"
54 #include "event2/event_compat.h"
55 #include "event2/buffer.h"
56 #include "event2/bufferevent.h"
57 
58 #include "regress.h"
59 #include "mm-internal.h"
60 
61 /* zlib 1.2.4 and 1.2.5 do some "clever" things with macros.  Instead of
62    saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
63    that nobody will care if the compile outputs a no-such-identifier warning.
64 
65    Sorry, but we like -Werror over here, so I guess we need to define these.
66    I hope that zlib 1.2.6 doesn't break these too.
67 */
68 #ifndef _LARGEFILE64_SOURCE
69 #define _LARGEFILE64_SOURCE 0
70 #endif
71 #ifndef _LFS64_LARGEFILE
72 #define _LFS64_LARGEFILE 0
73 #endif
74 #ifndef _FILE_OFFSET_BITS
75 #define _FILE_OFFSET_BITS 0
76 #endif
77 #ifndef off64_t
78 #define off64_t ev_int64_t
79 #endif
80 
81 #include <zlib.h>
82 
83 static int infilter_calls;
84 static int outfilter_calls;
85 static int readcb_finished;
86 static int writecb_finished;
87 static int errorcb_invoked;
88 
89 /*
90  * Zlib filters
91  */
92 
93 static void
94 zlib_deflate_free(void *ctx)
95 {
96 	z_streamp p = ctx;
97 
98 	assert(deflateEnd(p) == Z_OK);
99 	mm_free(p);
100 }
101 
102 static void
103 zlib_inflate_free(void *ctx)
104 {
105 	z_streamp p = ctx;
106 
107 	assert(inflateEnd(p) == Z_OK);
108 	mm_free(p);
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 = (unsigned char *)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 = (unsigned char *)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 = (unsigned char *)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 = (unsigned char *)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 pair[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, pair) == -1) {
290 		tt_abort_perror("socketpair");
291 	}
292 
293 	evutil_make_socket_nonblocking(pair[0]);
294 	evutil_make_socket_nonblocking(pair[1]);
295 
296 	bev1 = bufferevent_socket_new(NULL, pair[0], 0);
297 	bev2 = bufferevent_socket_new(NULL, pair[1], 0);
298 
299 	z_output = mm_calloc(sizeof(*z_output), 1);
300 	r = deflateInit(z_output, Z_DEFAULT_COMPRESSION);
301 	tt_int_op(r, ==, Z_OK);
302 	z_input = mm_calloc(sizeof(*z_input), 1);
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 (pair[0] >= 0)
345 		evutil_closesocket(pair[0]);
346 	if (pair[1] >= 0)
347 		evutil_closesocket(pair[1]);
348 }
349