-
Notifications
You must be signed in to change notification settings - Fork 250
/
test.c
529 lines (449 loc) · 21.4 KB
/
test.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* use `make test` to run the test */
/*
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
* Shigeo Mitsunari
*
* The software is licensed under either the MIT License (below) or the Perl
* license.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "picotest/picotest.h"
#include "picohttpparser.h"
static int bufis(const char *s, size_t l, const char *t)
{
return strlen(t) == l && memcmp(s, t, l) == 0;
}
static char *inputbuf; /* point to the end of the buffer */
static void test_request(void)
{
const char *method;
size_t method_len;
const char *path;
size_t path_len;
int minor_version;
struct phr_header headers[4];
size_t num_headers;
#define PARSE(s, last_len, exp, comment) \
do { \
size_t slen = sizeof(s) - 1; \
note(comment); \
num_headers = sizeof(headers) / sizeof(headers[0]); \
memcpy(inputbuf - slen, s, slen); \
ok(phr_parse_request(inputbuf - slen, slen, &method, &method_len, &path, &path_len, &minor_version, headers, &num_headers, \
last_len) == (exp == 0 ? (int)slen : exp)); \
} while (0)
PARSE("GET / HTTP/1.0\r\n\r\n", 0, 0, "simple");
ok(num_headers == 0);
ok(bufis(method, method_len, "GET"));
ok(bufis(path, path_len, "/"));
ok(minor_version == 0);
PARSE("GET / HTTP/1.0\r\n\r", 0, -2, "partial");
PARSE("GET /hoge HTTP/1.1\r\nHost: example.com\r\nCookie: \r\n\r\n", 0, 0, "parse headers");
ok(num_headers == 2);
ok(bufis(method, method_len, "GET"));
ok(bufis(path, path_len, "/hoge"));
ok(minor_version == 1);
ok(bufis(headers[0].name, headers[0].name_len, "Host"));
ok(bufis(headers[0].value, headers[0].value_len, "example.com"));
ok(bufis(headers[1].name, headers[1].name_len, "Cookie"));
ok(bufis(headers[1].value, headers[1].value_len, ""));
PARSE("GET /hoge HTTP/1.1\r\nHost: example.com\r\nUser-Agent: \343\201\262\343/1.0\r\n\r\n", 0, 0, "multibyte included");
ok(num_headers == 2);
ok(bufis(method, method_len, "GET"));
ok(bufis(path, path_len, "/hoge"));
ok(minor_version == 1);
ok(bufis(headers[0].name, headers[0].name_len, "Host"));
ok(bufis(headers[0].value, headers[0].value_len, "example.com"));
ok(bufis(headers[1].name, headers[1].name_len, "User-Agent"));
ok(bufis(headers[1].value, headers[1].value_len, "\343\201\262\343/1.0"));
PARSE("GET / HTTP/1.0\r\nfoo: \r\nfoo: b\r\n \tc\r\n\r\n", 0, 0, "parse multiline");
ok(num_headers == 3);
ok(bufis(method, method_len, "GET"));
ok(bufis(path, path_len, "/"));
ok(minor_version == 0);
ok(bufis(headers[0].name, headers[0].name_len, "foo"));
ok(bufis(headers[0].value, headers[0].value_len, ""));
ok(bufis(headers[1].name, headers[1].name_len, "foo"));
ok(bufis(headers[1].value, headers[1].value_len, "b"));
ok(headers[2].name == NULL);
ok(bufis(headers[2].value, headers[2].value_len, " \tc"));
PARSE("GET / HTTP/1.0\r\nfoo : ab\r\n\r\n", 0, -1, "parse header name with trailing space");
PARSE("GET", 0, -2, "incomplete 1");
ok(method == NULL);
PARSE("GET ", 0, -2, "incomplete 2");
ok(bufis(method, method_len, "GET"));
PARSE("GET /", 0, -2, "incomplete 3");
ok(path == NULL);
PARSE("GET / ", 0, -2, "incomplete 4");
ok(bufis(path, path_len, "/"));
PARSE("GET / H", 0, -2, "incomplete 5");
PARSE("GET / HTTP/1.", 0, -2, "incomplete 6");
PARSE("GET / HTTP/1.0", 0, -2, "incomplete 7");
ok(minor_version == -1);
PARSE("GET / HTTP/1.0\r", 0, -2, "incomplete 8");
ok(minor_version == 0);
PARSE("GET /hoge HTTP/1.0\r\n\r", strlen("GET /hoge HTTP/1.0\r\n\r") - 1, -2, "slowloris (incomplete)");
PARSE("GET /hoge HTTP/1.0\r\n\r\n", strlen("GET /hoge HTTP/1.0\r\n\r\n") - 1, 0, "slowloris (complete)");
PARSE(" / HTTP/1.0\r\n\r\n", 0, -1, "empty method");
PARSE("GET HTTP/1.0\r\n\r\n", 0, -1, "empty request-target");
PARSE("GET / HTTP/1.0\r\n:a\r\n\r\n", 0, -1, "empty header name");
PARSE("GET / HTTP/1.0\r\n :a\r\n\r\n", 0, -1, "header name (space only)");
PARSE("G\0T / HTTP/1.0\r\n\r\n", 0, -1, "NUL in method");
PARSE("G\tT / HTTP/1.0\r\n\r\n", 0, -1, "tab in method");
PARSE(":GET / HTTP/1.0\r\n\r\n", 0, -1, "invalid method");
PARSE("GET /\x7fhello HTTP/1.0\r\n\r\n", 0, -1, "DEL in uri-path");
PARSE("GET / HTTP/1.0\r\na\0b: c\r\n\r\n", 0, -1, "NUL in header name");
PARSE("GET / HTTP/1.0\r\nab: c\0d\r\n\r\n", 0, -1, "NUL in header value");
PARSE("GET / HTTP/1.0\r\na\033b: c\r\n\r\n", 0, -1, "CTL in header name");
PARSE("GET / HTTP/1.0\r\nab: c\033\r\n\r\n", 0, -1, "CTL in header value");
PARSE("GET / HTTP/1.0\r\n/: 1\r\n\r\n", 0, -1, "invalid char in header value");
PARSE("GET /\xa0 HTTP/1.0\r\nh: c\xa2y\r\n\r\n", 0, 0, "accept MSB chars");
ok(num_headers == 1);
ok(bufis(method, method_len, "GET"));
ok(bufis(path, path_len, "/\xa0"));
ok(minor_version == 0);
ok(bufis(headers[0].name, headers[0].name_len, "h"));
ok(bufis(headers[0].value, headers[0].value_len, "c\xa2y"));
PARSE("GET / HTTP/1.0\r\n\x7c\x7e: 1\r\n\r\n", 0, 0, "accept |~ (though forbidden by SSE)");
ok(num_headers == 1);
ok(bufis(headers[0].name, headers[0].name_len, "\x7c\x7e"));
ok(bufis(headers[0].value, headers[0].value_len, "1"));
PARSE("GET / HTTP/1.0\r\n\x7b: 1\r\n\r\n", 0, -1, "disallow {");
PARSE("GET / HTTP/1.0\r\nfoo: a \t \r\n\r\n", 0, 0, "exclude leading and trailing spaces in header value");
ok(bufis(headers[0].value, headers[0].value_len, "a"));
PARSE("GET / HTTP/1.0\r\n\r\n", 0, 0, "accept multiple spaces between tokens");
#undef PARSE
}
static void test_response(void)
{
int minor_version;
int status;
const char *msg;
size_t msg_len;
struct phr_header headers[4];
size_t num_headers;
#define PARSE(s, last_len, exp, comment) \
do { \
size_t slen = sizeof(s) - 1; \
note(comment); \
num_headers = sizeof(headers) / sizeof(headers[0]); \
memcpy(inputbuf - slen, s, slen); \
ok(phr_parse_response(inputbuf - slen, slen, &minor_version, &status, &msg, &msg_len, headers, &num_headers, last_len) == \
(exp == 0 ? (int)slen : exp)); \
} while (0)
PARSE("HTTP/1.0 200 OK\r\n\r\n", 0, 0, "simple");
ok(num_headers == 0);
ok(status == 200);
ok(minor_version == 0);
ok(bufis(msg, msg_len, "OK"));
PARSE("HTTP/1.0 200 OK\r\n\r", 0, -2, "partial");
PARSE("HTTP/1.1 200 OK\r\nHost: example.com\r\nCookie: \r\n\r\n", 0, 0, "parse headers");
ok(num_headers == 2);
ok(minor_version == 1);
ok(status == 200);
ok(bufis(msg, msg_len, "OK"));
ok(bufis(headers[0].name, headers[0].name_len, "Host"));
ok(bufis(headers[0].value, headers[0].value_len, "example.com"));
ok(bufis(headers[1].name, headers[1].name_len, "Cookie"));
ok(bufis(headers[1].value, headers[1].value_len, ""));
PARSE("HTTP/1.0 200 OK\r\nfoo: \r\nfoo: b\r\n \tc\r\n\r\n", 0, 0, "parse multiline");
ok(num_headers == 3);
ok(minor_version == 0);
ok(status == 200);
ok(bufis(msg, msg_len, "OK"));
ok(bufis(headers[0].name, headers[0].name_len, "foo"));
ok(bufis(headers[0].value, headers[0].value_len, ""));
ok(bufis(headers[1].name, headers[1].name_len, "foo"));
ok(bufis(headers[1].value, headers[1].value_len, "b"));
ok(headers[2].name == NULL);
ok(bufis(headers[2].value, headers[2].value_len, " \tc"));
PARSE("HTTP/1.0 500 Internal Server Error\r\n\r\n", 0, 0, "internal server error");
ok(num_headers == 0);
ok(minor_version == 0);
ok(status == 500);
ok(bufis(msg, msg_len, "Internal Server Error"));
ok(msg_len == sizeof("Internal Server Error") - 1);
PARSE("H", 0, -2, "incomplete 1");
PARSE("HTTP/1.", 0, -2, "incomplete 2");
PARSE("HTTP/1.1", 0, -2, "incomplete 3");
ok(minor_version == -1);
PARSE("HTTP/1.1 ", 0, -2, "incomplete 4");
ok(minor_version == 1);
PARSE("HTTP/1.1 2", 0, -2, "incomplete 5");
PARSE("HTTP/1.1 200", 0, -2, "incomplete 6");
ok(status == 0);
PARSE("HTTP/1.1 200 ", 0, -2, "incomplete 7");
ok(status == 200);
PARSE("HTTP/1.1 200 O", 0, -2, "incomplete 8");
PARSE("HTTP/1.1 200 OK\r", 0, -2, "incomplete 9");
ok(msg == NULL);
PARSE("HTTP/1.1 200 OK\r\n", 0, -2, "incomplete 10");
ok(bufis(msg, msg_len, "OK"));
PARSE("HTTP/1.1 200 OK\n", 0, -2, "incomplete 11");
ok(bufis(msg, msg_len, "OK"));
PARSE("HTTP/1.1 200 OK\r\nA: 1\r", 0, -2, "incomplete 11");
ok(num_headers == 0);
PARSE("HTTP/1.1 200 OK\r\nA: 1\r\n", 0, -2, "incomplete 12");
ok(num_headers == 1);
ok(bufis(headers[0].name, headers[0].name_len, "A"));
ok(bufis(headers[0].value, headers[0].value_len, "1"));
PARSE("HTTP/1.0 200 OK\r\n\r", strlen("HTTP/1.0 200 OK\r\n\r") - 1, -2, "slowloris (incomplete)");
PARSE("HTTP/1.0 200 OK\r\n\r\n", strlen("HTTP/1.0 200 OK\r\n\r\n") - 1, 0, "slowloris (complete)");
PARSE("HTTP/1. 200 OK\r\n\r\n", 0, -1, "invalid http version");
PARSE("HTTP/1.2z 200 OK\r\n\r\n", 0, -1, "invalid http version 2");
PARSE("HTTP/1.1 OK\r\n\r\n", 0, -1, "no status code");
PARSE("HTTP/1.1 200\r\n\r\n", 0, 0, "accept missing trailing whitespace in status-line");
ok(bufis(msg, msg_len, ""));
PARSE("HTTP/1.1 200X\r\n\r\n", 0, -1, "garbage after status 1");
PARSE("HTTP/1.1 200X \r\n\r\n", 0, -1, "garbage after status 2");
PARSE("HTTP/1.1 200X OK\r\n\r\n", 0, -1, "garbage after status 3");
PARSE("HTTP/1.1 200 OK\r\nbar: \t b\t \t\r\n\r\n", 0, 0, "exclude leading and trailing spaces in header value");
ok(bufis(headers[0].value, headers[0].value_len, "b"));
PARSE("HTTP/1.1 200 OK\r\n\r\n", 0, 0, "accept multiple spaces between tokens");
#undef PARSE
}
static void test_headers(void)
{
/* only test the interface; the core parser is tested by the tests above */
struct phr_header headers[4];
size_t num_headers;
#define PARSE(s, last_len, exp, comment) \
do { \
note(comment); \
num_headers = sizeof(headers) / sizeof(headers[0]); \
ok(phr_parse_headers(s, strlen(s), headers, &num_headers, last_len) == (exp == 0 ? (int)strlen(s) : exp)); \
} while (0)
PARSE("Host: example.com\r\nCookie: \r\n\r\n", 0, 0, "simple");
ok(num_headers == 2);
ok(bufis(headers[0].name, headers[0].name_len, "Host"));
ok(bufis(headers[0].value, headers[0].value_len, "example.com"));
ok(bufis(headers[1].name, headers[1].name_len, "Cookie"));
ok(bufis(headers[1].value, headers[1].value_len, ""));
PARSE("Host: example.com\r\nCookie: \r\n\r\n", 1, 0, "slowloris");
ok(num_headers == 2);
ok(bufis(headers[0].name, headers[0].name_len, "Host"));
ok(bufis(headers[0].value, headers[0].value_len, "example.com"));
ok(bufis(headers[1].name, headers[1].name_len, "Cookie"));
ok(bufis(headers[1].value, headers[1].value_len, ""));
PARSE("Host: example.com\r\nCookie: \r\n\r", 0, -2, "partial");
PARSE("Host: e\7fample.com\r\nCookie: \r\n\r", 0, -1, "error");
#undef PARSE
}
static void test_chunked_at_once(int line, int consume_trailer, const char *encoded, const char *decoded, ssize_t expected)
{
struct phr_chunked_decoder dec = {0};
char *buf;
size_t bufsz;
ssize_t ret;
dec.consume_trailer = consume_trailer;
note("testing at-once, source at line %d", line);
buf = strdup(encoded);
bufsz = strlen(buf);
ret = phr_decode_chunked(&dec, buf, &bufsz);
ok(ret == expected);
ok(bufsz == strlen(decoded));
ok(bufis(buf, bufsz, decoded));
if (expected >= 0) {
if (ret == expected)
ok(bufis(buf + bufsz, ret, encoded + strlen(encoded) - ret));
else
ok(0);
}
free(buf);
}
static void test_chunked_per_byte(int line, int consume_trailer, const char *encoded, const char *decoded, ssize_t expected)
{
struct phr_chunked_decoder dec = {0};
char *buf = malloc(strlen(encoded) + 1);
size_t bytes_to_consume = strlen(encoded) - (expected >= 0 ? expected : 0), bytes_ready = 0, bufsz, i;
ssize_t ret;
dec.consume_trailer = consume_trailer;
note("testing per-byte, source at line %d", line);
for (i = 0; i < bytes_to_consume - 1; ++i) {
buf[bytes_ready] = encoded[i];
bufsz = 1;
ret = phr_decode_chunked(&dec, buf + bytes_ready, &bufsz);
if (ret != -2) {
ok(0);
goto cleanup;
}
bytes_ready += bufsz;
}
strcpy(buf + bytes_ready, encoded + bytes_to_consume - 1);
bufsz = strlen(buf + bytes_ready);
ret = phr_decode_chunked(&dec, buf + bytes_ready, &bufsz);
ok(ret == expected);
bytes_ready += bufsz;
ok(bytes_ready == strlen(decoded));
ok(bufis(buf, bytes_ready, decoded));
if (expected >= 0) {
if (ret == expected)
ok(bufis(buf + bytes_ready, expected, encoded + bytes_to_consume));
else
ok(0);
}
cleanup:
free(buf);
}
static void test_chunked_failure(int line, const char *encoded, ssize_t expected)
{
struct phr_chunked_decoder dec = {0};
char *buf = strdup(encoded);
size_t bufsz, i;
ssize_t ret;
note("testing failure at-once, source at line %d", line);
bufsz = strlen(buf);
ret = phr_decode_chunked(&dec, buf, &bufsz);
ok(ret == expected);
note("testing failure per-byte, source at line %d", line);
memset(&dec, 0, sizeof(dec));
for (i = 0; encoded[i] != '\0'; ++i) {
buf[0] = encoded[i];
bufsz = 1;
ret = phr_decode_chunked(&dec, buf, &bufsz);
if (ret == -1) {
ok(ret == expected);
goto cleanup;
} else if (ret == -2) {
/* continue */
} else {
ok(0);
goto cleanup;
}
}
ok(ret == expected);
cleanup:
free(buf);
}
static void (*chunked_test_runners[])(int, int, const char *, const char *, ssize_t) = {test_chunked_at_once, test_chunked_per_byte,
NULL};
static void test_chunked(void)
{
size_t i;
for (i = 0; chunked_test_runners[i] != NULL; ++i) {
chunked_test_runners[i](__LINE__, 0, "b\r\nhello world\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6;comment=hi\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6 ; comment\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 0, "6\r\nhello \r\n5\r\nworld\r\n0\r\na: b\r\nc: d\r\n\r\n", "hello world",
sizeof("a: b\r\nc: d\r\n\r\n") - 1);
chunked_test_runners[i](__LINE__, 0, "b\r\nhello world\r\n0\r\n", "hello world", 0);
}
note("failures");
test_chunked_failure(__LINE__, "z\r\nabcdefg", -1);
if (sizeof(size_t) == 8) {
test_chunked_failure(__LINE__, "6\r\nhello \r\nffffffffffffffff\r\nabcdefg", -2);
test_chunked_failure(__LINE__, "6\r\nhello \r\nfffffffffffffffff\r\nabcdefg", -1);
}
test_chunked_failure(__LINE__, "1x\r\na\r\n0\r\n", -1);
}
static void test_chunked_consume_trailer(void)
{
size_t i;
for (i = 0; chunked_test_runners[i] != NULL; ++i) {
chunked_test_runners[i](__LINE__, 1, "b\r\nhello world\r\n0\r\n", "hello world", -2);
chunked_test_runners[i](__LINE__, 1, "6\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", -2);
chunked_test_runners[i](__LINE__, 1, "6;comment=hi\r\nhello \r\n5\r\nworld\r\n0\r\n", "hello world", -2);
chunked_test_runners[i](__LINE__, 1, "b\r\nhello world\r\n0\r\n\r\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 1, "b\nhello world\n0\n\n", "hello world", 0);
chunked_test_runners[i](__LINE__, 1, "6\r\nhello \r\n5\r\nworld\r\n0\r\na: b\r\nc: d\r\n\r\n", "hello world", 0);
}
}
static void test_chunked_leftdata(void)
{
#define NEXT_REQ "GET / HTTP/1.1\r\n\r\n"
struct phr_chunked_decoder dec = {0};
dec.consume_trailer = 1;
char buf[] = "5\r\nabcde\r\n0\r\n\r\n" NEXT_REQ;
size_t bufsz = sizeof(buf) - 1;
ssize_t ret = phr_decode_chunked(&dec, buf, &bufsz);
ok(ret >= 0);
ok(bufsz == 5);
ok(memcmp(buf, "abcde", 5) == 0);
ok(ret == sizeof(NEXT_REQ) - 1);
ok(memcmp(buf + bufsz, NEXT_REQ, sizeof(NEXT_REQ) - 1) == 0);
#undef NEXT_REQ
}
static ssize_t do_test_chunked_overhead(size_t chunk_len, size_t chunk_count, const char *extra)
{
struct phr_chunked_decoder dec = {0};
char buf[1024];
size_t bufsz;
ssize_t ret;
for (size_t i = 0; i < chunk_count; ++i) {
/* build and feed the chunk header */
bufsz = (size_t)sprintf(buf, "%zx%s\r\n", chunk_len, extra);
if ((ret = phr_decode_chunked(&dec, buf, &bufsz)) != -2)
goto Exit;
assert(bufsz == 0);
/* build and feed the chunk boby */
memset(buf, 'A', chunk_len);
bufsz = chunk_len;
if ((ret = phr_decode_chunked(&dec, buf, &bufsz)) != -2)
goto Exit;
assert(bufsz == chunk_len);
/* build and feed the chunk end (CRLF) */
strcpy(buf, "\r\n");
bufsz = 2;
if ((ret = phr_decode_chunked(&dec, buf, &bufsz)) != -2)
goto Exit;
assert(bufsz == 0);
}
/* build and feed the end chunk */
strcpy(buf, "0\r\n\r\n");
bufsz = 5;
ret = phr_decode_chunked(&dec, buf, &bufsz);
assert(bufsz == 0);
Exit:
return ret;
}
static void test_chunked_overhead(void)
{
ok(do_test_chunked_overhead(100, 10000, "") == 2 /* consume trailer is not set */);
ok(do_test_chunked_overhead(10, 100000, "") == 2 /* consume trailer is not set */);
ok(do_test_chunked_overhead(1, 1000000, "") == -1);
ok(do_test_chunked_overhead(10, 100000, "; tiny=1") == 2 /* consume trailer is not set */);
ok(do_test_chunked_overhead(10, 100000, "; large=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") == -1);
}
int main(void)
{
long pagesize = sysconf(_SC_PAGESIZE);
assert(pagesize >= 1);
inputbuf = mmap(NULL, pagesize * 3, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
assert(inputbuf != MAP_FAILED);
inputbuf += pagesize * 2;
ok(mprotect(inputbuf - pagesize, pagesize, PROT_READ | PROT_WRITE) == 0);
subtest("request", test_request);
subtest("response", test_response);
subtest("headers", test_headers);
subtest("chunked", test_chunked);
subtest("chunked-consume-trailer", test_chunked_consume_trailer);
subtest("chunked-leftdata", test_chunked_leftdata);
subtest("chunked-overhead", test_chunked_overhead);
munmap(inputbuf - pagesize * 2, pagesize * 3);
return done_testing();
}