Skip to content

Commit

Permalink
Add FuzzEncodePNG and FuzzEncodeJPG
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 7, 2024
1 parent de18ce6 commit 8b9e603
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions libwebp/encode_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

0 comments on commit 8b9e603

Please sign in to comment.