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