From a04cd8c39f312da01be79e085b0cb4b643f9614b Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Tue, 9 Jan 2024 13:01:31 +0100 Subject: Move Fuzz tests into their own file This is required for https://github.com/google/oss-fuzz/pull/11453 --- cookie_test.go | 22 -------------- fuzz_test.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ header_test.go | 20 ------------- http_test.go | 26 ---------------- uri_test.go | 19 ------------ 5 files changed, 93 insertions(+), 87 deletions(-) create mode 100644 fuzz_test.go diff --git a/cookie_test.go b/cookie_test.go index df9568c..b4b81ac 100644 --- a/cookie_test.go +++ b/cookie_test.go @@ -1,7 +1,6 @@ package fasthttp import ( - "bytes" "strings" "testing" "time" @@ -16,27 +15,6 @@ func TestCookiePanic(t *testing.T) { } } -func FuzzCookieParse(f *testing.F) { - inputs := []string{ - `xxx=yyy`, - `xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b`, - " \n\t\"", - } - for _, input := range inputs { - f.Add([]byte(input)) - } - c := AcquireCookie() - defer ReleaseCookie(c) - f.Fuzz(func(t *testing.T, cookie []byte) { - _ = c.ParseBytes(cookie) - - w := bytes.Buffer{} - if _, err := c.WriteTo(&w); err != nil { - t.Fatalf("unexpected error: %v", err) - } - }) -} - func TestCookieValueWithEqualAndSpaceChars(t *testing.T) { t.Parallel() diff --git a/fuzz_test.go b/fuzz_test.go new file mode 100644 index 0000000..72fa1a2 --- /dev/null +++ b/fuzz_test.go @@ -0,0 +1,93 @@ +package fasthttp + +import ( + "bufio" + "bytes" + "testing" +) + +func FuzzCookieParse(f *testing.F) { + inputs := []string{ + `xxx=yyy`, + `xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b`, + " \n\t\"", + } + for _, input := range inputs { + f.Add([]byte(input)) + } + c := AcquireCookie() + defer ReleaseCookie(c) + f.Fuzz(func(t *testing.T, cookie []byte) { + _ = c.ParseBytes(cookie) + + w := bytes.Buffer{} + if _, err := c.WriteTo(&w); err != nil { + t.Fatalf("unexpected error: %v", err) + } + }) +} + +func FuzzVisitHeaderParams(f *testing.F) { + inputs := []string{ + `application/json; v=1; foo=bar; q=0.938; param=param; param="big fox"; q=0.43`, + `*/*`, + `\\`, + `text/plain; foo="\\\"\'\\''\'"`, + } + for _, input := range inputs { + f.Add([]byte(input)) + } + f.Fuzz(func(t *testing.T, header []byte) { + VisitHeaderParams(header, func(key, value []byte) bool { + if len(key) == 0 { + t.Errorf("Unexpected length zero parameter, failed input was: %s", header) + } + return true + }) + }) +} + +func FuzzResponseReadLimitBody(f *testing.F) { + res := AcquireResponse() + defer ReleaseResponse(res) + + f.Add([]byte("HTTP/1.1 200 OK\r\nContent-Type: aa\r\nContent-Length: 10\r\n\r\n9876543210"), 1024*1024) + + f.Fuzz(func(t *testing.T, body []byte, max int) { + _ = res.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max) + w := bytes.Buffer{} + _, _ = res.WriteTo(&w) + }) +} + +func FuzzRequestReadLimitBody(f *testing.F) { + req := AcquireRequest() + defer ReleaseRequest(req) + + f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa\r\n\r\n6\r\nfoobar\r\n3\r\nbaz\r\n0\r\nfoobar\r\n\r\n"), 1024*1024) + + f.Fuzz(func(t *testing.T, body []byte, max int) { + _ = req.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max) + w := bytes.Buffer{} + _, _ = req.WriteTo(&w) + }) +} + +func FuzzURIUpdateBytes(f *testing.F) { + u := AcquireURI() + defer ReleaseURI(u) + + f.Add([]byte(`http://foobar.com/aaa/bb?cc`)) + f.Add([]byte(`//foobar.com/aaa/bb?cc`)) + f.Add([]byte(`/aaa/bb?cc`)) + f.Add([]byte(`xx?yy=abc`)) + + f.Fuzz(func(t *testing.T, uri []byte) { + u.UpdateBytes(uri) + + w := bytes.Buffer{} + if _, err := u.WriteTo(&w); err != nil { + t.Fatalf("unexpected error: %v", err) + } + }) +} diff --git a/header_test.go b/header_test.go index 83b8a9c..163fdd7 100644 --- a/header_test.go +++ b/header_test.go @@ -1110,26 +1110,6 @@ func testVisitHeaderParams(t *testing.T, header string, expectedParams [][2]stri } } -func FuzzVisitHeaderParams(f *testing.F) { - inputs := []string{ - `application/json; v=1; foo=bar; q=0.938; param=param; param="big fox"; q=0.43`, - `*/*`, - `\\`, - `text/plain; foo="\\\"\'\\''\'"`, - } - for _, input := range inputs { - f.Add([]byte(input)) - } - f.Fuzz(func(t *testing.T, header []byte) { - VisitHeaderParams(header, func(key, value []byte) bool { - if len(key) == 0 { - t.Errorf("Unexpected length zero parameter, failed input was: %s", header) - } - return true - }) - }) -} - func TestRequestMultipartFormBoundary(t *testing.T) { t.Parallel() diff --git a/http_test.go b/http_test.go index 57f0220..098990a 100644 --- a/http_test.go +++ b/http_test.go @@ -1700,32 +1700,6 @@ func testRequestReadLimitBodySuccess(t *testing.T, s string, maxBodySize int) { } } -func FuzzResponseReadLimitBody(f *testing.F) { - res := AcquireResponse() - defer ReleaseResponse(res) - - f.Add([]byte("HTTP/1.1 200 OK\r\nContent-Type: aa\r\nContent-Length: 10\r\n\r\n9876543210"), 1024*1024) - - f.Fuzz(func(t *testing.T, body []byte, max int) { - _ = res.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max) - w := bytes.Buffer{} - _, _ = res.WriteTo(&w) - }) -} - -func FuzzRequestReadLimitBody(f *testing.F) { - req := AcquireRequest() - defer ReleaseRequest(req) - - f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa\r\n\r\n6\r\nfoobar\r\n3\r\nbaz\r\n0\r\nfoobar\r\n\r\n"), 1024*1024) - - f.Fuzz(func(t *testing.T, body []byte, max int) { - _ = req.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), max) - w := bytes.Buffer{} - _, _ = req.WriteTo(&w) - }) -} - func TestRequestString(t *testing.T) { t.Parallel() diff --git a/uri_test.go b/uri_test.go index c3f6d9b..5996bcb 100644 --- a/uri_test.go +++ b/uri_test.go @@ -9,25 +9,6 @@ import ( "time" ) -func FuzzURIUpdateBytes(f *testing.F) { - u := AcquireURI() - defer ReleaseURI(u) - - f.Add([]byte(`http://foobar.com/aaa/bb?cc`)) - f.Add([]byte(`//foobar.com/aaa/bb?cc`)) - f.Add([]byte(`/aaa/bb?cc`)) - f.Add([]byte(`xx?yy=abc`)) - - f.Fuzz(func(t *testing.T, uri []byte) { - u.UpdateBytes(uri) - - w := bytes.Buffer{} - if _, err := u.WriteTo(&w); err != nil { - t.Fatalf("unexpected error: %v", err) - } - }) -} - func TestURICopyToQueryArgs(t *testing.T) { t.Parallel() -- cgit v1.2.3