Artifact 986d146fbf2c73c8ed02827c0f1290394fb8122ba2267d949827a14bc8303c26:
- File
test/pic/test_http.c
— part of check-in
[cb38c63312]
at
2026-11-18 17:41:38
on branch stage-2
— Test and fuzz the HTTP response parser
Unit tests cover chunked, length-delimited and close-delimited bodies, interim responses, smuggling guards, chunk framing, truncation and the header cap, whole and bytewise. A libFuzzer target checks that chunking never changes the outcome. (user: dmestas, size: 4709) [annotate] [blame] [check-ins using] [more...]
/* HTTP/1.1 response parser tests. Every case is also fed byte by byte. */ #include <assert.h> #include <stdio.h> #include <string.h> #include "http.h" typedef struct { char body[1024]; size_t len; } sink; static int collect(void *ctx, const char *bytes, size_t n) { sink *s = ctx; assert(s->len + n < sizeof s->body); memcpy(s->body + s->len, bytes, n); s->len += n; s->body[s->len] = '\0'; return 0; } /* want_rc: 0 complete, -1 malformed (during feed or at end). */ static void check(const char *in, size_t n, int want_rc, int want_status, const char *want_body, const char *want_err) { for (int bytewise = 0; bytewise < 2; bytewise++) { pic_http_resp r; sink s; int rc = 0; memset(&s, 0, sizeof s); pic_http_resp_init(&r, collect, &s); if (bytewise) { for (size_t i = 0; i < n && rc == 0; i++) { rc = pic_http_feed(&r, in + i, 1); } } else { rc = pic_http_feed(&r, in, n); } if (rc == 0) { rc = pic_http_end(&r); } if (rc != want_rc || (want_rc == 0 && (r.status != want_status || strcmp(s.body, want_body) != 0)) || (want_err && !strstr(r.err, want_err))) { fprintf(stderr, "case %.50s (bytewise=%d): rc=%d status=%d body=[%s] err=[%s]\n", in, bytewise, rc, r.status, s.body, r.err); assert(0); } } } #define OK(in, status, body) check(in, sizeof(in) - 1, 0, status, body, NULL) #define BAD(in, err) check(in, sizeof(in) - 1, -1, 0, "", err) int main(void) { /* Chunked, with extensions, mixed-case hex and trailers. */ OK("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: text/event-stream\r\n\r\n" "5\r\nhello\r\nA;ext=1\r\n, world!!!\r\n0\r\nX-Trailer: y\r\n\r\n", 200, "hello, world!!!"); /* Content-Length, bare-LF lines, an interim 100 response first. */ OK("HTTP/1.1 100 Continue\r\n\r\nHTTP/1.1 401 Unauthorized\ncontent-length: 4\n\nnope", 401, "nope"); /* Body until the connection ends. */ OK("HTTP/1.0 200 OK\r\n\r\nall of it", 200, "all of it"); /* No body for 204; duplicate equal lengths are fine. */ OK("HTTP/1.1 204 No Content\r\n\r\n", 204, ""); OK("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Length: 2\r\n\r\nok", 200, "ok"); /* Headers captured. */ { const char in[] = "HTTP/1.1 429 Too Many\r\nRetry-After: 7 \r\nrequest-id: req_1\r\nContent-Length: 0\r\n\r\n"; pic_http_resp r; pic_http_resp_init(&r, NULL, NULL); assert(pic_http_feed(&r, in, sizeof in - 1) == 0 && pic_http_done(&r)); assert(r.status == 429 && strcmp(r.retry_after, "7") == 0 && strcmp(r.request_id, "req_1") == 0); } /* Smuggling and corruption guards. */ BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Length: 3\r\n\r\n", "both chunked"); BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip, chunked\r\n\r\n", "transfer encoding"); BAD("HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\n\r\n", "compressed"); BAD("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Length: 3\r\n\r\n", "conflicting"); BAD("HTTP/1.1 200 OK\r\nContent-Length: 1x\r\n\r\n", "content length"); BAD("HTTP/1.1 200 OK\r\nX: a\r\n folded\r\n\r\n", "folded"); BAD("HTTP/1.1 200 OK\r\nBad Name: a\r\n\r\n", "header name"); BAD("HTTP/2 200\r\n\r\n", "status line"); BAD("HTTP/1.1 20 OK\r\n\r\n", "status line"); BAD("HTTP/1.1 101 Switching\r\n\r\n", "protocol switch"); /* Chunk framing. */ BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nz\r\n", "chunk size"); BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n2\r\nabc\r\n", "longer than its size"); BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nFFFFFFFFFFFFFFFFF\r\n", "overflows"); /* Truncation at every kind of boundary is an error at the end. */ BAD("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhel", "ended before"); BAD("HTTP/1.1 200 OK\r\nContent-Length: 9\r\n\r\nshort", "ended before"); BAD("HTTP/1.1 200 OK\r\nContent-Le", "ended before"); /* Nothing may follow a complete response. */ BAD("HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\nab", "after the end"); /* Header size cap. */ { static char big[70000]; size_t n; memcpy(big, "HTTP/1.1 200 OK\r\n", 17); n = 17; while (n + 20 < sizeof big - 4) { memcpy(big + n, "X-Pad: aaaaaaaaaaa\r\n", 20); n += 20; } check(big, n, -1, 0, "", "exceed"); } puts("http: all cases pass, whole and byte by byte"); return 0; }