2 * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 /* The old tests here need assertions to work. */
35 #include "event2/event-config.h"
37 #include <sys/types.h>
39 #include <sys/socket.h>
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"
60 /* zlib 1.2.4 and 1.2.5 do some "clever" things with macros. Instead of
61 saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
62 that nobody will care if the compile outputs a no-such-identifier warning.
64 Sorry, but we like -Werror over here, so I guess we need to define these.
65 I hope that zlib 1.2.6 doesn't break these too.
67 #ifndef _LARGEFILE64_SOURCE
68 #define _LARGEFILE64_SOURCE 0
70 #ifndef _LFS64_LARGEFILE
71 #define _LFS64_LARGEFILE 0
73 #ifndef _FILE_OFFSET_BITS
74 #define _FILE_OFFSET_BITS 0
77 #define off64_t ev_int64_t
82 static int infilter_calls;
83 static int outfilter_calls;
84 static int readcb_finished;
85 static int writecb_finished;
86 static int errorcb_invoked;
93 zlib_deflate_free(void *ctx)
97 assert(deflateEnd(p) == Z_OK);
101 zlib_inflate_free(void *ctx)
105 assert(inflateEnd(p) == Z_OK);
109 getstate(enum bufferevent_flush_mode state)
123 * The input filter is triggered only on new input read from the network.
124 * That means all input data needs to be consumed or the filter needs to
125 * initiate its own triggering via a timeout.
127 static enum bufferevent_filter_result
128 zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
129 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
131 struct evbuffer_iovec v_in[1];
132 struct evbuffer_iovec v_out[1];
139 /* let's do some decompression */
140 n = evbuffer_peek(src, -1, NULL, v_in, 1);
142 p->avail_in = v_in[0].iov_len;
143 p->next_in = v_in[0].iov_base;
149 evbuffer_reserve_space(dst, 4096, v_out, 1);
150 p->next_out = v_out[0].iov_base;
151 p->avail_out = v_out[0].iov_len;
153 /* we need to flush zlib if we got a flush */
154 res = inflate(p, getstate(state));
156 /* let's figure out how much was compressed */
157 nread = v_in[0].iov_len - p->avail_in;
158 nwrite = v_out[0].iov_len - p->avail_out;
160 evbuffer_drain(src, nread);
161 v_out[0].iov_len = nwrite;
162 evbuffer_commit_space(dst, v_out, 1);
164 if (res==Z_BUF_ERROR) {
165 /* We're out of space, or out of decodeable input.
166 Only if nwrite == 0 assume the latter.
169 return BEV_NEED_MORE;
171 assert(res == Z_OK || res == Z_STREAM_END);
174 } while (evbuffer_get_length(src) > 0);
181 static enum bufferevent_filter_result
182 zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
183 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
185 struct evbuffer_iovec v_in[1];
186 struct evbuffer_iovec v_out[1];
193 /* let's do some compression */
194 n = evbuffer_peek(src, -1, NULL, v_in, 1);
196 p->avail_in = v_in[0].iov_len;
197 p->next_in = v_in[0].iov_base;
203 evbuffer_reserve_space(dst, 4096, v_out, 1);
204 p->next_out = v_out[0].iov_base;
205 p->avail_out = v_out[0].iov_len;
207 /* we need to flush zlib if we got a flush */
208 res = deflate(p, getstate(state));
210 /* let's figure out how much was decompressed */
211 nread = v_in[0].iov_len - p->avail_in;
212 nwrite = v_out[0].iov_len - p->avail_out;
214 evbuffer_drain(src, nread);
215 v_out[0].iov_len = nwrite;
216 evbuffer_commit_space(dst, v_out, 1);
218 if (res==Z_BUF_ERROR) {
219 /* We're out of space, or out of decodeable input.
220 Only if nwrite == 0 assume the latter.
223 return BEV_NEED_MORE;
225 assert(res == Z_OK || res == Z_STREAM_END);
228 } while (evbuffer_get_length(src) > 0);
236 * simple bufferevent test (over transparent zlib treatment)
240 readcb(struct bufferevent *bev, void *arg)
242 if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
243 struct evbuffer *evbuf = evbuffer_new();
244 assert(evbuf != NULL);
246 /* gratuitous test of bufferevent_read_buffer */
247 bufferevent_read_buffer(bev, evbuf);
249 bufferevent_disable(bev, EV_READ);
251 if (evbuffer_get_length(evbuf) == 8333) {
255 evbuffer_free(evbuf);
260 writecb(struct bufferevent *bev, void *arg)
262 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
268 errorcb(struct bufferevent *bev, short what, void *arg)
274 test_bufferevent_zlib(void *arg)
276 struct bufferevent *bev1=NULL, *bev2=NULL;
278 z_stream z_input, z_output;
279 int i, pair[2]={-1,-1}, r;
282 infilter_calls = outfilter_calls = readcb_finished = writecb_finished
283 = errorcb_invoked = 0;
285 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
286 tt_abort_perror("socketpair");
289 evutil_make_socket_nonblocking(pair[0]);
290 evutil_make_socket_nonblocking(pair[1]);
292 bev1 = bufferevent_socket_new(NULL, pair[0], 0);
293 bev2 = bufferevent_socket_new(NULL, pair[1], 0);
295 memset(&z_output, 0, sizeof(z_output));
296 r = deflateInit(&z_output, Z_DEFAULT_COMPRESSION);
297 tt_int_op(r, ==, Z_OK);
298 memset(&z_input, 0, sizeof(z_input));
299 r = inflateInit(&z_input);
300 tt_int_op(r, ==, Z_OK);
302 /* initialize filters */
303 bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
304 BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, &z_output);
305 bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
306 NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, &z_input);
307 bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
308 bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
310 bufferevent_disable(bev1, EV_READ);
311 bufferevent_enable(bev1, EV_WRITE);
313 bufferevent_enable(bev2, EV_READ);
315 for (i = 0; i < (int)sizeof(buffer); i++)
318 /* break it up into multiple buffer chains */
319 bufferevent_write(bev1, buffer, 1800);
320 bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
322 /* we are done writing - we need to flush everything */
323 bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
327 tt_want(infilter_calls);
328 tt_want(outfilter_calls);
329 tt_want(readcb_finished);
330 tt_want(writecb_finished);
331 tt_want(!errorcb_invoked);
336 bufferevent_free(bev1);
338 bufferevent_free(bev2);
341 evutil_closesocket(pair[0]);
343 evutil_closesocket(pair[1]);