|
| 1 | +package libwebp |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "image/jpeg" |
| 6 | + "image/png" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/bep/gowebp/libwebp/webpoptions" |
| 13 | +) |
| 14 | + |
| 15 | +func FuzzEncodePNG(f *testing.F) { |
| 16 | + names := []string{"bw-gopher.png", "fuzzy-cirlcle.png"} |
| 17 | + opts := webpoptions.EncodingOptions{Quality: 75} |
| 18 | + |
| 19 | + for _, name := range names { |
| 20 | + b, err := os.ReadFile(filepath.Join("..", "test_data", "images", name)) |
| 21 | + if err != nil { |
| 22 | + f.Fatal(err) |
| 23 | + } |
| 24 | + f.Add(b) |
| 25 | + } |
| 26 | + |
| 27 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 28 | + img, err := png.Decode(bytes.NewReader(data)) |
| 29 | + if err != nil { |
| 30 | + if img != nil { |
| 31 | + t.Fatalf("img != nil, but err: %s", err) |
| 32 | + } |
| 33 | + return |
| 34 | + } |
| 35 | + err = Encode(io.Discard, img, opts) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func FuzzEncodeJPG(f *testing.F) { |
| 43 | + names := []string{"source.jpg", "sunset.jpg"} |
| 44 | + opts := webpoptions.EncodingOptions{Quality: 75} |
| 45 | + |
| 46 | + for _, name := range names { |
| 47 | + b, err := os.ReadFile(filepath.Join("..", "test_data", "images", name)) |
| 48 | + if err != nil { |
| 49 | + f.Fatal(err) |
| 50 | + } |
| 51 | + f.Add(b) |
| 52 | + } |
| 53 | + |
| 54 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 55 | + img, err := jpeg.Decode(bytes.NewReader(data)) |
| 56 | + if err != nil { |
| 57 | + if img != nil { |
| 58 | + t.Fatalf("img != nil, but err: %s", err) |
| 59 | + } |
| 60 | + return |
| 61 | + } |
| 62 | + err = Encode(io.Discard, img, opts) |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
0 commit comments