oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package imagefile
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9// One fuzz target per walker (IMG5). `go test` runs each seed corpus
10// (the fixtures) in CI; `go test -fuzz` explores. A walker must never
11// panic, and what it keeps must be readable by its own walk again.
12
13func seed(f *testing.F, names ...string) {
14 for _, name := range names {
15 raw, err := os.ReadFile(filepath.Join("testdata", name))
16 if err != nil {
17 f.Fatal(err)
18 }
19 f.Add(raw)
20 }
21}
22
23func fuzzWalker(f *testing.F, walk func([]byte) ([]byte, int, error)) {
24 f.Fuzz(func(t *testing.T, raw []byte) {
25 cleaned, _, err := walk(raw)
26 if err != nil {
27 return
28 }
29 if _, _, err := walk(cleaned); err != nil {
30 t.Errorf("a cleaned file did not walk again: %v", err)
31 }
32 })
33}
34
35func FuzzJPEG(f *testing.F) {
36 seed(f, "baseline.jpg", "progressive.jpg", "restart.jpg", "cmyk.jpg",
37 "thumbnail.jpg", "motion.jpg")
38 fuzzWalker(f, cleanJPEG)
39}
40
41func FuzzPNG(f *testing.F) {
42 seed(f, "meta.png", "anim.png")
43 fuzzWalker(f, cleanPNG)
44}
45
46func FuzzWebP(f *testing.F) {
47 seed(f, "lossy.webp", "lossless.webp", "anim.webp")
48 fuzzWalker(f, cleanWebP)
49}
50
51func FuzzGIF(f *testing.F) {
52 seed(f, "anim.gif")
53 fuzzWalker(f, cleanGIF)
54}
55
56func FuzzClean(f *testing.F) {
57 seed(f, "baseline.jpg", "meta.png", "lossy.webp", "anim.gif")
58 f.Fuzz(func(t *testing.T, raw []byte) {
59 _, _, _ = Clean(raw)
60 })
61}
62