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