summaryrefslogtreecommitdiff
path: root/test/compress.c
blob: bc9ffea8b15022da40ec455f722d6e1026b22689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* 
   tests for compressed response handling.
   Copyright (C) 2001-2008, Joe Orton <joe@manyfish.co.uk>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "config.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "ne_compress.h"
#include "ne_auth.h"

#include "tests.h"
#include "child.h"
#include "utils.h"

static enum { f_partial = 0, f_mismatch, f_complete } failed;

static const char *newsfn = "random.txt";

static int init(void)
{
    return lookup_localhost();
}

#define EXTRA_DEBUG 0 /* disabled by default */

static int reader(void *ud, const char *block, size_t len)
{
    struct string *b = ud;

#if EXTRA_DEBUG
    NE_DEBUG(NE_DBG_HTTP, "reader: got (%d): [[[%.*s]]]\n", (int)len,
             (int)len, block);
#endif

    if (failed == f_mismatch) return -1;

    /* catch multiple len == 0 call as issued by 0.25.0 only: */
    if (failed == f_complete) {
        NE_DEBUG(NE_DBG_HTTP, "reader: called after complete, len=%d\n",
                 (int)len);
        failed = f_mismatch;
        return -1;
    }

    if (failed == f_partial && len == 0) {
        if (b->len != 0) {
            NE_DEBUG(NE_DBG_HTTP, "reader: got length %d at EOF\n",
                     (int)b->len);
            failed = f_mismatch;
        } else {
            failed = f_complete;
        }
        return 0;
    }

    if (len > b->len || memcmp(b->data, block, len) != 0) {
        NE_DEBUG(NE_DBG_HTTP, "reader: failed, got [[%.*s]] not [[%.*s]]\n",
                 (int)len, block, (int)b->len, b->data);
	failed = f_mismatch;
        return -1;
    } else {
	b->data += len;
	b->len -= len;
#if EXTRA_DEBUG
        NE_DEBUG(NE_DBG_HTTP, "reader: OK, %d bytes remaining\n", 
                 (int)b->len);
#endif
    }

    return 0;
}

static int do_fetch(const char *realfn, const char *gzipfn,
		    int chunked, int expect_fail)
{
    ne_session *sess;
    ne_request *req;
    int ret;
    ne_buffer *buf = ne_buffer_create();
    struct serve_file_args sfargs;
    ne_decompress *dc;
    struct string body;
    
    CALL(file_to_buffer(realfn, buf));

    body.data = buf->data;
    body.len = buf->used - 1;
    
    failed = f_partial;

    if (gzipfn) {
	sfargs.fname = gzipfn;
	sfargs.headers = "Content-Encoding: gzip\r\n";
    } else {
	sfargs.fname = realfn;
	sfargs.headers = NULL;
    }
    sfargs.chunks = chunked;
    
    CALL(make_session(&sess, serve_file, &sfargs));
    
    req = ne_request_create(sess, "GET", "/");
    dc = ne_decompress_reader(req, ne_accept_2xx, reader, &body);

#ifdef NE_DEBUGGING
    ne_debug_init(ne_debug_stream, ne_debug_mask & ~NE_DBG_HTTPBODY);
#endif

    ret = ne_request_dispatch(req);

#ifdef NE_DEBUGGING
    ne_debug_init(ne_debug_stream, ne_debug_mask | NE_DBG_HTTPBODY);
#endif

    ONN("file not served", ne_get_status(req)->code != 200);
    
    ONN("decompress succeeded", expect_fail && !ret);
    ONV(!expect_fail && ret, ("decompress failed: %s", ne_get_error(sess)));

    NE_DEBUG(NE_DBG_HTTP, "session error: %s\n", ne_get_error(sess));

    ne_decompress_destroy(dc);
    ne_request_destroy(req);
    ne_session_destroy(sess);
    ne_buffer_destroy(buf);

    if (expect_fail) {
        /* if the decompress callback fails, the connection may
         * be aborted and hence the server will abort. */
        reap_server();
    } else {
        CALL(await_server());
    }

    if (!expect_fail) {
        ONN("inflated response truncated", failed == f_partial);
        ONN("inflated response mismatch", failed == f_mismatch);
    }

    return OK;
}

static int fetch(const char *realfn, const char *gzipfn, int chunked)
{
    return do_fetch(realfn, gzipfn, chunked, 0);
}

/* Test the no-compression case. */
static int not_compressed(void)
{
    return fetch(newsfn, NULL, 0);
}

static int simple(void)
{
    return fetch(newsfn, "file1.gz", 0);
}

/* file1.gz has an embedded filename. */
static int withname(void)
{
    return fetch(newsfn, "file2.gz", 0);
}

/* deliver various different sizes of chunks: tests the various
 * decoding cases. */
static int chunked_1b_wn(void)
{
    return fetch(newsfn, "file2.gz", 1);
}

static int chunked_1b(void)
{
    return fetch(newsfn, "file1.gz", 1);
}

static int chunked_12b(void)
{
    return fetch(newsfn, "file2.gz", 12);
}

static int chunked_20b(void)
{
    return fetch(newsfn, "file2.gz", 20);
}

static int chunked_10b(void)
{
    return fetch(newsfn, "file1.gz", 10);
}

static int chunked_10b_wn(void)
{
    return fetch(newsfn, "file2.gz", 10);
}

static int fail_trailing(void)
{
    return do_fetch(newsfn, "trailing.gz", 0, 1);
}

static int fail_trailing_1b(void)
{
    return do_fetch(newsfn, "trailing.gz", 1, 1);
}

static int fail_truncate(void)
{
    return do_fetch(newsfn, "truncated.gz", 0, 1);
}

static int fail_bad_csum(void)
{
    return do_fetch(newsfn, "badcsum.gz", 0, 1);
}

static int fail_corrupt1(void)
{
    return do_fetch(newsfn, "corrupt1.gz", 0, 1);
}

static int fail_corrupt2(void)
{
    return do_fetch(newsfn, "corrupt2.gz", 0, 1);
}

static int fail_empty(void)
{
    return do_fetch(newsfn, "empty.gz", 0, 1);
}

static int notcomp_empty(void)
{
    return fetch("empty.gz", NULL, 0);
}

static int auth_cb(void *userdata, const char *realm, int tries, 
		   char *un, char *pw)
{
    strcpy(un, "foo");
    strcpy(pw, "bar");
    return tries;
}

static int retry_compress_helper(ne_accept_response acceptor,
                                 struct double_serve_args *args, 
                                 struct string *expect)
{
    ne_session *sess;
    ne_request *req;
    ne_decompress *dc;
    
    CALL(make_session(&sess, double_serve_sstring, args));

    ne_set_server_auth(sess, auth_cb, NULL);

    req = ne_request_create(sess, "GET", "/");
    dc = ne_decompress_reader(req, acceptor, reader, expect);
    failed = f_partial;

    ONREQ(ne_request_dispatch(req));
    
    ne_decompress_destroy(dc);

    ONN("got bad response body", failed != f_complete);

    CALL(await_server());

    ne_request_destroy(req);
    ne_session_destroy(sess);

    return OK;
}

#define SSTRING(x) { x, sizeof(x) - 1 }

static struct double_serve_args retry_gz_args = { 
    SSTRING("HTTP/1.1 401 Get Away\r\n"
            "Content-Encoding: gzip\r\n"
            "WWW-Authenticate: Basic realm=WallyWorld\r\n"
            "Content-Length: 5\r\n"
            "\r\n"
            "abcde"),
    SSTRING("HTTP/1.1 200 OK\r\n"
            "Server: foo\r\n"
            "Content-Length: 5\r\n"
            "Connection: close\r\n"
            "\r\n"
            "hello")
};

/* Test where the response to the retried request does *not* have
 * a content-encoding, whereas the original 401 response did. */
static int retry_notcompress(void)
{
    struct string expect = { "hello", 5 };
    return retry_compress_helper(ne_accept_2xx, &retry_gz_args, &expect);
}

static struct double_serve_args retry_gz_args2 = {
    SSTRING("HTTP/1.1 401 Get Away\r\n"
            "Content-Encoding: gzip\r\n"
            "WWW-Authenticate: Basic realm=WallyWorld\r\n"
            "Content-Length: 25\r\n"
            "\r\n"
            "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcb\x48\xcd\xc9\xc9\x07"
            "\x00\x86\xa6\x10\x36\x05\x00\x00\x00"),
    SSTRING("HTTP/1.1 200 OK\r\n"
            "Server: foo\r\n"
            "Content-Encoding: gzip\r\n"
            "Content-Length: 25\r\n"
            "Connection: close\r\n"
            "\r\n"
            "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x2b\xcf\x2f\xca\x49\x01"
            "\x00\x43\x11\x77\x3a\x05\x00\x00\x00")
};

static int retry_accept(void *ud, ne_request *req, const ne_status *st)
{
    struct string *expect = ud;

    NE_DEBUG(NE_DBG_HTTP, "retry_accept callback for %d response\n",
             st->code);

    if (expect->len == 4 && strcmp(expect->data, "fish") == 0) {
        /* first time through */
        expect->data = "hello";
    } else {
        expect->data = "world";
    }

    expect->len = 5;
    failed = f_partial; /* reset the state */
    return 1;
}

/* Test where the response to the retried request *does* have a
 * content-encoding, as did the original 401 response. */
static int retry_compress(void)
{
    struct string expect = { "fish", 4 };
    return retry_compress_helper(retry_accept, &retry_gz_args2, &expect);
}

#define READER_ABORT_ERR "reader_abort error string"

static int reader_abort(void *ud, const char *buf, size_t len)
{
    ne_session *sess = ud;
    ne_set_error(sess, READER_ABORT_ERR);
    return len;
}

/* check that a callback abort does abort the response */
static int compress_abort(void)
{
    ne_session *sess;
    ne_request *req;
    struct serve_file_args sfargs;
    ne_decompress *dc;
    int ret;

    sfargs.fname = "file1.gz";
    sfargs.headers = "Content-Encoding: gzip\r\n";
    sfargs.chunks = 0;

    CALL(make_session(&sess, serve_file, &sfargs));

    req = ne_request_create(sess, "GET", "/abort");

    dc = ne_decompress_reader(req, ne_accept_2xx, reader_abort, sess);

    ret = ne_request_dispatch(req);

    reap_server();

    ONN("request was not aborted", ret != NE_ERROR);
    ONV(strcmp(ne_get_error(sess), READER_ABORT_ERR),
        ("session error was %s not %s",
         ne_get_error(sess), READER_ABORT_ERR));
    
    reap_server();
    ne_decompress_destroy(dc);
    ne_request_destroy(req);
    ne_session_destroy(sess);

    return OK;

}

ne_test tests[] = {
    T_LEAKY(init),
    T(not_compressed),
    T(simple),
    T(withname),
    T(fail_trailing),
    T(fail_trailing_1b),
    T(fail_bad_csum),
    T(fail_truncate),
    T(fail_corrupt1),
    T(fail_corrupt2),
    T(fail_empty),
    T(notcomp_empty),
    T(chunked_1b), 
    T(chunked_1b_wn),
    T(chunked_12b), 
    T(chunked_20b),
    T(chunked_10b),
    T(chunked_10b_wn),
    T(retry_notcompress),
    T(retry_compress),
    T(compress_abort),
    T(NULL)
};