Home | History | Annotate | Line # | Download | only in test
      1  1.6  christos /*	$NetBSD: regress_zlib.c,v 1.7 2024/08/18 20:47:23 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
      5  1.1  christos  *
      6  1.1  christos  * Redistribution and use in source and binary forms, with or without
      7  1.1  christos  * modification, are permitted provided that the following conditions
      8  1.1  christos  * are met:
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     15  1.1  christos  *    derived from this software without specific prior written permission.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos /* The old tests here need assertions to work. */
     30  1.1  christos #undef NDEBUG
     31  1.1  christos 
     32  1.1  christos #ifdef _WIN32
     33  1.1  christos #include <winsock2.h>
     34  1.1  christos #include <windows.h>
     35  1.1  christos #endif
     36  1.1  christos 
     37  1.1  christos #include "event2/event-config.h"
     38  1.1  christos 
     39  1.1  christos #include <sys/types.h>
     40  1.1  christos #ifndef _WIN32
     41  1.1  christos #include <sys/socket.h>
     42  1.1  christos #include <sys/wait.h>
     43  1.1  christos #include <unistd.h>
     44  1.1  christos #include <netdb.h>
     45  1.1  christos #endif
     46  1.1  christos #include <signal.h>
     47  1.1  christos #include <stdio.h>
     48  1.1  christos #include <stdlib.h>
     49  1.1  christos #include <string.h>
     50  1.1  christos 
     51  1.1  christos #include <assert.h>
     52  1.1  christos #include <errno.h>
     53  1.1  christos 
     54  1.1  christos #include "event2/util.h"
     55  1.1  christos #include "event2/event.h"
     56  1.1  christos #include "event2/event_compat.h"
     57  1.1  christos #include "event2/buffer.h"
     58  1.1  christos #include "event2/bufferevent.h"
     59  1.1  christos 
     60  1.1  christos #include "regress.h"
     61  1.3  christos #include "mm-internal.h"
     62  1.1  christos 
     63  1.1  christos /* zlib 1.2.4 and 1.2.5 do some "clever" things with macros.  Instead of
     64  1.1  christos    saying "(defined(FOO) ? FOO : 0)" they like to say "FOO-0", on the theory
     65  1.1  christos    that nobody will care if the compile outputs a no-such-identifier warning.
     66  1.1  christos 
     67  1.1  christos    Sorry, but we like -Werror over here, so I guess we need to define these.
     68  1.1  christos    I hope that zlib 1.2.6 doesn't break these too.
     69  1.1  christos */
     70  1.1  christos #ifndef _LARGEFILE64_SOURCE
     71  1.1  christos #define _LARGEFILE64_SOURCE 0
     72  1.1  christos #endif
     73  1.1  christos #ifndef _LFS64_LARGEFILE
     74  1.1  christos #define _LFS64_LARGEFILE 0
     75  1.1  christos #endif
     76  1.1  christos #ifndef _FILE_OFFSET_BITS
     77  1.1  christos #define _FILE_OFFSET_BITS 0
     78  1.1  christos #endif
     79  1.1  christos #ifndef off64_t
     80  1.1  christos #define off64_t ev_int64_t
     81  1.1  christos #endif
     82  1.1  christos 
     83  1.1  christos #include <zlib.h>
     84  1.1  christos 
     85  1.1  christos static int infilter_calls;
     86  1.1  christos static int outfilter_calls;
     87  1.1  christos static int readcb_finished;
     88  1.1  christos static int writecb_finished;
     89  1.1  christos static int errorcb_invoked;
     90  1.1  christos 
     91  1.1  christos /*
     92  1.1  christos  * Zlib filters
     93  1.1  christos  */
     94  1.1  christos 
     95  1.1  christos static void
     96  1.1  christos zlib_deflate_free(void *ctx)
     97  1.1  christos {
     98  1.1  christos 	z_streamp p = ctx;
     99  1.1  christos 
    100  1.1  christos 	assert(deflateEnd(p) == Z_OK);
    101  1.3  christos 	mm_free(p);
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos static void
    105  1.1  christos zlib_inflate_free(void *ctx)
    106  1.1  christos {
    107  1.1  christos 	z_streamp p = ctx;
    108  1.1  christos 
    109  1.1  christos 	assert(inflateEnd(p) == Z_OK);
    110  1.3  christos 	mm_free(p);
    111  1.1  christos }
    112  1.1  christos 
    113  1.1  christos static int
    114  1.1  christos getstate(enum bufferevent_flush_mode state)
    115  1.1  christos {
    116  1.1  christos 	switch (state) {
    117  1.1  christos 	case BEV_FINISHED:
    118  1.1  christos 		return Z_FINISH;
    119  1.1  christos 	case BEV_FLUSH:
    120  1.1  christos 		return Z_SYNC_FLUSH;
    121  1.1  christos 	case BEV_NORMAL:
    122  1.1  christos 	default:
    123  1.1  christos 		return Z_NO_FLUSH;
    124  1.1  christos 	}
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos /*
    128  1.1  christos  * The input filter is triggered only on new input read from the network.
    129  1.1  christos  * That means all input data needs to be consumed or the filter needs to
    130  1.1  christos  * initiate its own triggering via a timeout.
    131  1.1  christos  */
    132  1.1  christos static enum bufferevent_filter_result
    133  1.1  christos zlib_input_filter(struct evbuffer *src, struct evbuffer *dst,
    134  1.1  christos     ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
    135  1.1  christos {
    136  1.1  christos 	struct evbuffer_iovec v_in[1];
    137  1.1  christos 	struct evbuffer_iovec v_out[1];
    138  1.1  christos 	int nread, nwrite;
    139  1.1  christos 	int res, n;
    140  1.1  christos 
    141  1.1  christos 	z_streamp p = ctx;
    142  1.1  christos 
    143  1.1  christos 	do {
    144  1.1  christos 		/* let's do some decompression */
    145  1.1  christos 		n = evbuffer_peek(src, -1, NULL, v_in, 1);
    146  1.1  christos 		if (n) {
    147  1.1  christos 			p->avail_in = v_in[0].iov_len;
    148  1.7  christos 			p->next_in = (unsigned char *)v_in[0].iov_base;
    149  1.1  christos 		} else {
    150  1.1  christos 			p->avail_in = 0;
    151  1.1  christos 			p->next_in = 0;
    152  1.1  christos 		}
    153  1.1  christos 
    154  1.1  christos 		evbuffer_reserve_space(dst, 4096, v_out, 1);
    155  1.7  christos 		p->next_out = (unsigned char *)v_out[0].iov_base;
    156  1.1  christos 		p->avail_out = v_out[0].iov_len;
    157  1.1  christos 
    158  1.1  christos 		/* we need to flush zlib if we got a flush */
    159  1.1  christos 		res = inflate(p, getstate(state));
    160  1.1  christos 
    161  1.1  christos 		/* let's figure out how much was compressed */
    162  1.1  christos 		nread = v_in[0].iov_len - p->avail_in;
    163  1.1  christos 		nwrite = v_out[0].iov_len - p->avail_out;
    164  1.1  christos 
    165  1.1  christos 		evbuffer_drain(src, nread);
    166  1.1  christos 		v_out[0].iov_len = nwrite;
    167  1.1  christos 		evbuffer_commit_space(dst, v_out, 1);
    168  1.1  christos 
    169  1.1  christos 		if (res==Z_BUF_ERROR) {
    170  1.1  christos 			/* We're out of space, or out of decodeable input.
    171  1.1  christos 			   Only if nwrite == 0 assume the latter.
    172  1.1  christos 			 */
    173  1.1  christos 			if (nwrite == 0)
    174  1.1  christos 				return BEV_NEED_MORE;
    175  1.1  christos 		} else {
    176  1.1  christos 			assert(res == Z_OK || res == Z_STREAM_END);
    177  1.1  christos 		}
    178  1.1  christos 
    179  1.1  christos 	} while (evbuffer_get_length(src) > 0);
    180  1.1  christos 
    181  1.1  christos 	++infilter_calls;
    182  1.1  christos 
    183  1.1  christos 	return (BEV_OK);
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos static enum bufferevent_filter_result
    187  1.1  christos zlib_output_filter(struct evbuffer *src, struct evbuffer *dst,
    188  1.1  christos     ev_ssize_t lim, enum bufferevent_flush_mode state, void *ctx)
    189  1.1  christos {
    190  1.1  christos 	struct evbuffer_iovec v_in[1];
    191  1.1  christos 	struct evbuffer_iovec v_out[1];
    192  1.1  christos 	int nread, nwrite;
    193  1.1  christos 	int res, n;
    194  1.1  christos 
    195  1.1  christos 	z_streamp p = ctx;
    196  1.1  christos 
    197  1.1  christos 	do {
    198  1.1  christos 		/* let's do some compression */
    199  1.1  christos 		n = evbuffer_peek(src, -1, NULL, v_in, 1);
    200  1.1  christos 		if (n) {
    201  1.1  christos 			p->avail_in = v_in[0].iov_len;
    202  1.7  christos 			p->next_in = (unsigned char *)v_in[0].iov_base;
    203  1.1  christos 		} else {
    204  1.1  christos 			p->avail_in = 0;
    205  1.1  christos 			p->next_in = 0;
    206  1.1  christos 		}
    207  1.1  christos 
    208  1.1  christos 		evbuffer_reserve_space(dst, 4096, v_out, 1);
    209  1.7  christos 		p->next_out = (unsigned char *)v_out[0].iov_base;
    210  1.1  christos 		p->avail_out = v_out[0].iov_len;
    211  1.1  christos 
    212  1.1  christos 		/* we need to flush zlib if we got a flush */
    213  1.1  christos 		res = deflate(p, getstate(state));
    214  1.1  christos 
    215  1.1  christos 		/* let's figure out how much was decompressed */
    216  1.1  christos 		nread = v_in[0].iov_len - p->avail_in;
    217  1.1  christos 		nwrite = v_out[0].iov_len - p->avail_out;
    218  1.1  christos 
    219  1.1  christos 		evbuffer_drain(src, nread);
    220  1.1  christos 		v_out[0].iov_len = nwrite;
    221  1.1  christos 		evbuffer_commit_space(dst, v_out, 1);
    222  1.1  christos 
    223  1.1  christos 		if (res==Z_BUF_ERROR) {
    224  1.1  christos 			/* We're out of space, or out of decodeable input.
    225  1.1  christos 			   Only if nwrite == 0 assume the latter.
    226  1.1  christos 			 */
    227  1.1  christos 			if (nwrite == 0)
    228  1.1  christos 				return BEV_NEED_MORE;
    229  1.1  christos 		} else {
    230  1.1  christos 			assert(res == Z_OK || res == Z_STREAM_END);
    231  1.1  christos 		}
    232  1.1  christos 
    233  1.1  christos 	} while (evbuffer_get_length(src) > 0);
    234  1.1  christos 
    235  1.1  christos 	++outfilter_calls;
    236  1.1  christos 
    237  1.1  christos 	return (BEV_OK);
    238  1.1  christos }
    239  1.1  christos 
    240  1.1  christos /*
    241  1.1  christos  * simple bufferevent test (over transparent zlib treatment)
    242  1.1  christos  */
    243  1.1  christos 
    244  1.1  christos static void
    245  1.1  christos readcb(struct bufferevent *bev, void *arg)
    246  1.1  christos {
    247  1.1  christos 	if (evbuffer_get_length(bufferevent_get_input(bev)) == 8333) {
    248  1.1  christos 		struct evbuffer *evbuf = evbuffer_new();
    249  1.1  christos 		assert(evbuf != NULL);
    250  1.1  christos 
    251  1.1  christos 		/* gratuitous test of bufferevent_read_buffer */
    252  1.1  christos 		bufferevent_read_buffer(bev, evbuf);
    253  1.1  christos 
    254  1.1  christos 		bufferevent_disable(bev, EV_READ);
    255  1.1  christos 
    256  1.1  christos 		if (evbuffer_get_length(evbuf) == 8333) {
    257  1.1  christos 			++readcb_finished;
    258  1.1  christos 		}
    259  1.1  christos 
    260  1.1  christos 		evbuffer_free(evbuf);
    261  1.1  christos 	}
    262  1.1  christos }
    263  1.1  christos 
    264  1.1  christos static void
    265  1.1  christos writecb(struct bufferevent *bev, void *arg)
    266  1.1  christos {
    267  1.1  christos 	if (evbuffer_get_length(bufferevent_get_output(bev)) == 0) {
    268  1.1  christos 		++writecb_finished;
    269  1.1  christos 	}
    270  1.1  christos }
    271  1.1  christos 
    272  1.1  christos static void
    273  1.1  christos errorcb(struct bufferevent *bev, short what, void *arg)
    274  1.1  christos {
    275  1.1  christos 	errorcb_invoked = 1;
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos void
    279  1.1  christos test_bufferevent_zlib(void *arg)
    280  1.1  christos {
    281  1.1  christos 	struct bufferevent *bev1=NULL, *bev2=NULL;
    282  1.1  christos 	char buffer[8333];
    283  1.3  christos 	z_stream *z_input, *z_output;
    284  1.1  christos 	int i, r;
    285  1.1  christos 	evutil_socket_t pair[2] = {-1, -1};
    286  1.1  christos 	(void)arg;
    287  1.1  christos 
    288  1.1  christos 	infilter_calls = outfilter_calls = readcb_finished = writecb_finished
    289  1.1  christos 	    = errorcb_invoked = 0;
    290  1.1  christos 
    291  1.1  christos 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
    292  1.1  christos 		tt_abort_perror("socketpair");
    293  1.1  christos 	}
    294  1.1  christos 
    295  1.1  christos 	evutil_make_socket_nonblocking(pair[0]);
    296  1.1  christos 	evutil_make_socket_nonblocking(pair[1]);
    297  1.1  christos 
    298  1.1  christos 	bev1 = bufferevent_socket_new(NULL, pair[0], 0);
    299  1.1  christos 	bev2 = bufferevent_socket_new(NULL, pair[1], 0);
    300  1.1  christos 
    301  1.3  christos 	z_output = mm_calloc(sizeof(*z_output), 1);
    302  1.3  christos 	r = deflateInit(z_output, Z_DEFAULT_COMPRESSION);
    303  1.1  christos 	tt_int_op(r, ==, Z_OK);
    304  1.3  christos 	z_input = mm_calloc(sizeof(*z_input), 1);
    305  1.3  christos 	r = inflateInit(z_input);
    306  1.1  christos 	tt_int_op(r, ==, Z_OK);
    307  1.1  christos 
    308  1.1  christos 	/* initialize filters */
    309  1.1  christos 	bev1 = bufferevent_filter_new(bev1, NULL, zlib_output_filter,
    310  1.3  christos 	    BEV_OPT_CLOSE_ON_FREE, zlib_deflate_free, z_output);
    311  1.1  christos 	bev2 = bufferevent_filter_new(bev2, zlib_input_filter,
    312  1.3  christos 	    NULL, BEV_OPT_CLOSE_ON_FREE, zlib_inflate_free, z_input);
    313  1.1  christos 	bufferevent_setcb(bev1, readcb, writecb, errorcb, NULL);
    314  1.1  christos 	bufferevent_setcb(bev2, readcb, writecb, errorcb, NULL);
    315  1.1  christos 
    316  1.1  christos 	bufferevent_disable(bev1, EV_READ);
    317  1.1  christos 	bufferevent_enable(bev1, EV_WRITE);
    318  1.1  christos 
    319  1.1  christos 	bufferevent_enable(bev2, EV_READ);
    320  1.1  christos 
    321  1.1  christos 	for (i = 0; i < (int)sizeof(buffer); i++)
    322  1.1  christos 		buffer[i] = i;
    323  1.1  christos 
    324  1.1  christos 	/* break it up into multiple buffer chains */
    325  1.1  christos 	bufferevent_write(bev1, buffer, 1800);
    326  1.1  christos 	bufferevent_write(bev1, buffer + 1800, sizeof(buffer) - 1800);
    327  1.1  christos 
    328  1.1  christos 	/* we are done writing - we need to flush everything */
    329  1.1  christos 	bufferevent_flush(bev1, EV_WRITE, BEV_FINISHED);
    330  1.1  christos 
    331  1.1  christos 	event_dispatch();
    332  1.1  christos 
    333  1.1  christos 	tt_want(infilter_calls);
    334  1.1  christos 	tt_want(outfilter_calls);
    335  1.1  christos 	tt_want(readcb_finished);
    336  1.1  christos 	tt_want(writecb_finished);
    337  1.1  christos 	tt_want(!errorcb_invoked);
    338  1.1  christos 
    339  1.1  christos 	test_ok = 1;
    340  1.1  christos end:
    341  1.1  christos 	if (bev1)
    342  1.1  christos 		bufferevent_free(bev1);
    343  1.1  christos 	if (bev2)
    344  1.1  christos 		bufferevent_free(bev2);
    345  1.1  christos 
    346  1.1  christos 	if (pair[0] >= 0)
    347  1.1  christos 		evutil_closesocket(pair[0]);
    348  1.1  christos 	if (pair[1] >= 0)
    349  1.1  christos 		evutil_closesocket(pair[1]);
    350  1.1  christos }
    351