From 6dc59ae63e3b370afa008a886339107fc0b45f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 7 Aug 2024 16:50:17 +0200 Subject: [PATCH] Add FuzzEncodePNG and FuzzEncodeJPG --- libwebp/encode_fuzz_test.go | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 libwebp/encode_fuzz_test.go diff --git a/libwebp/encode_fuzz_test.go b/libwebp/encode_fuzz_test.go new file mode 100644 index 0000000..0ac49ac --- /dev/null +++ b/libwebp/encode_fuzz_test.go @@ -0,0 +1,67 @@ +package libwebp + +import ( + "bytes" + "image/jpeg" + "image/png" + "io" + "os" + "path/filepath" + "testing" + + "github.com/bep/gowebp/libwebp/webpoptions" +) + +func FuzzEncodePNG(f *testing.F) { + names := []string{"bw-gopher.png", "fuzzy-cirlcle.png"} + opts := webpoptions.EncodingOptions{Quality: 75} + + for _, name := range names { + b, err := os.ReadFile(filepath.Join("..", "test_data", "images", name)) + if err != nil { + f.Fatal(err) + } + f.Add(b) + } + + f.Fuzz(func(t *testing.T, data []byte) { + img, err := png.Decode(bytes.NewReader(data)) + if err != nil { + if img != nil { + t.Fatalf("img != nil, but err: %s", err) + } + return + } + err = Encode(io.Discard, img, opts) + if err != nil { + t.Fatal(err) + } + }) +} + +func FuzzEncodeJPG(f *testing.F) { + names := []string{"source.jpg", "sunset.jpg"} + opts := webpoptions.EncodingOptions{Quality: 75} + + for _, name := range names { + b, err := os.ReadFile(filepath.Join("..", "test_data", "images", name)) + if err != nil { + f.Fatal(err) + } + f.Add(b) + } + + f.Fuzz(func(t *testing.T, data []byte) { + img, err := jpeg.Decode(bytes.NewReader(data)) + if err != nil { + if img != nil { + t.Fatalf("img != nil, but err: %s", err) + } + return + } + err = Encode(io.Discard, img, opts) + if err != nil { + t.Fatal(err) + } + }) +}