|
| 1 | +package imagemeta_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/bep/imagemeta" |
| 10 | +) |
| 11 | + |
| 12 | +func FuzzDecodeJPG(f *testing.F) { |
| 13 | + filenames := []string{"sunrise.jpg", "goexif/geodegrees_as_string.jpg", "metadata_demo_exif_only.jpg", "metadata_demo_iim_and_xmp_only.jpg"} |
| 14 | + for _, filename := range filenames { |
| 15 | + f.Add(readTestDataFileAll(f, filename)) |
| 16 | + } |
| 17 | + |
| 18 | + f.Fuzz(func(t *testing.T, imageBytes []byte) { |
| 19 | + fuzzDecodeBytes(t, imageBytes, imagemeta.JPEG) |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func FuzzDecodeWebP(f *testing.F) { |
| 24 | + filenames := []string{"sunrise.webp"} |
| 25 | + |
| 26 | + for _, filename := range filenames { |
| 27 | + f.Add(readTestDataFileAll(f, filename)) |
| 28 | + } |
| 29 | + |
| 30 | + f.Fuzz(func(t *testing.T, imageBytes []byte) { |
| 31 | + fuzzDecodeBytes(t, imageBytes, imagemeta.WebP) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func FuzzDecodePNG(f *testing.F) { |
| 36 | + filenames := []string{"sunrise.png", "metadata-extractor-images/png/issue614.png"} |
| 37 | + |
| 38 | + for _, filename := range filenames { |
| 39 | + f.Add(readTestDataFileAll(f, filename)) |
| 40 | + } |
| 41 | + |
| 42 | + f.Fuzz(func(t *testing.T, imageBytes []byte) { |
| 43 | + fuzzDecodeBytes(t, imageBytes, imagemeta.PNG) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +func FuzzDecodeTIFF(f *testing.F) { |
| 48 | + filenames := []string{"sunrise.tif"} |
| 49 | + |
| 50 | + for _, filename := range filenames { |
| 51 | + f.Add(readTestDataFileAll(f, filename)) |
| 52 | + } |
| 53 | + |
| 54 | + f.Fuzz(func(t *testing.T, imageBytes []byte) { |
| 55 | + fuzzDecodeBytes(t, imageBytes, imagemeta.TIFF) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func fuzzDecodeBytes(t *testing.T, imageBytes []byte, f imagemeta.ImageFormat) error { |
| 60 | + r := bytes.NewReader(imageBytes) |
| 61 | + err := imagemeta.Decode(imagemeta.Options{R: r, ImageFormat: f, Sources: imagemeta.EXIF | imagemeta.IPTC | imagemeta.XMP}) |
| 62 | + if err != nil { |
| 63 | + if !imagemeta.IsInvalidFormat(err) { |
| 64 | + t.Fatalf("unknown error in Decode: %v %T", err, err) |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func readTestDataFileAll(t testing.TB, filename string) []byte { |
| 71 | + t.Helper() |
| 72 | + b, err := os.ReadFile(filepath.Join("testdata", filename)) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("failed to read file %q: %v", filename, err) |
| 75 | + } |
| 76 | + return b |
| 77 | +} |
0 commit comments