2 * Copyright (c) 2008-2010 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>
53 #include "event2/util.h"
54 #include "event2/event.h"
55 #include "event2/event_compat.h"
56 #include "event2/buffer.h"
57 #include "event2/bufferevent.h"
61 static int infilter_calls;
62 static int outfilter_calls;
63 static int readcb_finished;
64 static int writecb_finished;
65 static int errorcb_invoked;
72 zlib_deflate_free(void *ctx)
76 assert(deflateEnd(p) == Z_OK);
80 zlib_inflate_free(void *ctx)
84 assert(inflateEnd(p) == Z_OK);
88 getstate(enum bufferevent_flush_mode state)
102 * The input filter is triggered only on new input read from the network.
103 * That means all input data needs to be consumed or the filter needs to
104 * initiate its own triggering via a timeout.
106 static enum bufferevent_filter_result
107 zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
108 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
110 struct evbuffer_iovec v_in[1];
111 struct evbuffer_iovec v_out[1];
118 /* let's do some decompression */
119 n = evbuffer_peek(src, -1, NULL, v_in, 1);
121 p->avail_in = v_in[0].iov_len;
122 p->next_in = v_in[0].iov_base;
128 evbuffer_reserve_space(dst, 4096, v_out, 1);
129 p->next_out = v_out[0].iov_base;
130 p->avail_out = v_out[0].iov_len;
132 /* we need to flush zlib if we got a flush */
133 res = inflate(p, getstate(state));
135 /* let's figure out how much was compressed */
136 nread = v_in[0].iov_len - p->avail_in;
137 nwrite = v_out[0].iov_len - p->avail_out;
139 evbuffer_drain(src, nread);
140 v_out[0].iov_len = nwrite;
141 evbuffer_commit_space(dst, v_out, 1);
143 if (res==Z_BUF_ERROR) {
144 /* We're out of space, or out of decodeable input.
145 Only if nwrite == 0 assume the latter.
148 return BEV_NEED_MORE;
150 assert(res == Z_OK || res == Z_STREAM_END);
153 } while (evbuffer_get_length(src) > 0);
160 static enum bufferevent_filter_result
161 zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
162 ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
164 struct evbuffer_iovec v_in[1];
165 struct evbuffer_iovec v_out[1];
172 /* let's do some compression */
173 n = evbuffer_peek(src, -1, NULL, v_in, 1);
175 p->avail_in = v_in[0].iov_len;
176 p->next_in = v_in[0].iov_base;
182 evbuffer_reserve_space(dst, 4096, v_out, 1);
183 p->next_out = v_out[0].iov_base;
184 p->avail_out = v_out[0].iov_len;
186 /* we need to flush zlib if we got a flush */
187 res = deflate(p, getstate(state));
189 /* let's figure out how much was decompressed */
190 nread = v_in[0].iov_len - p->avail_in;
191 nwrite = v_out[0].iov_len - p->avail_out;
193 evbuffer_drain(src, nread);
194 v_out[0].iov_len = nwrite;
195 evbuffer_commit_space(dst, v_out, 1);
197 if (res==Z_BUF_ERROR) {
198 /* We're out of space, or out of decodeable input.
199 Only if nwrite == 0 assume the latter.
202 return BEV_NEED_MORE;
204 assert(res == Z_OK || res == Z_STREAM_END);
207 } while (evbuffer_get_length(src) > 0);
215 * simple bufferevent test (over transparent zlib treatment)
219 readcb(struct bufferevent *bev, void *arg)
221 if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
222 struct evbuffer *evbuf = evbuffer_new();
223 assert(evbuf != NULL);
225 /* gratuitous test of bufferevent_read_buffer */
226 bufferevent_read_buffer(bev, evbuf);
228 bufferevent_disable(bev, EV_READ);
230 if (evbuffer_get_length(evbuf) == 8333) {
234 evbuffer_free(evbuf);
239 writecb(struct bufferevent *bev, void *arg)
241 if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
247 errorcb(struct bufferevent *bev, short what, void *arg)
253 test_bufferevent_zlib(void *arg)
255 struct bufferevent *bev1=NULL, *bev2=NULL;
257 z_stream z_input, z_output;
258 int i, pair[2]={-1,-1}, r;
261 infilter_calls = outfilter_calls = readcb_finished = writecb_finished
262 = errorcb_invoked = 0;
264 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
265 tt_abort_perror("socketpair");
268 evutil_make_socket_nonblocking(pair[0]);
269 evutil_make_socket_nonblocking(pair[1]);
271 bev1 = bufferevent_socket_new(NULL, pair[0], 0);
272 bev2 = bufferevent_socket_new(NULL, pair[1], 0);
274 memset(&z_output, 0, sizeof(z_output));
275 r = deflateInit(&z_output, Z_DEFAULT_COMPRESSION);
276 tt_int_op(r, ==, Z_OK);
277 memset(&z_input, 0, sizeof(z_input));
278 r = inflateInit(&z_input);
280 /* initialize filters */
281 bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
282 BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, &z_output);
283 bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
284 NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, &z_input);
285 bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
286 bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
288 bufferevent_disable(bev1, EV_READ);
289 bufferevent_enable(bev1, EV_WRITE);
291 bufferevent_enable(bev2, EV_READ);
293 for (i = 0; i < (int)sizeof(buffer); i++)
296 /* break it up into multiple buffer chains */
297 bufferevent_write(bev1, buffer, 1800);
298 bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
300 /* we are done writing - we need to flush everything */
301 bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
305 tt_want(infilter_calls);
306 tt_want(outfilter_calls);
307 tt_want(readcb_finished);
308 tt_want(writecb_finished);
309 tt_want(!errorcb_invoked);
314 bufferevent_free(bev1);
316 bufferevent_free(bev2);
319 evutil_closesocket(pair[0]);
321 evutil_closesocket(pair[1]);