generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagemeta_fuzz_test.go
86 lines (70 loc) · 2.06 KB
/
imagemeta_fuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2024 Bjørn Erik Pedersen
// SPDX-License-Identifier: MIT
package imagemeta_test
import (
"bytes"
"os"
"path/filepath"
"testing"
"time"
"github.com/bep/imagemeta"
)
func FuzzDecodeJPG(f *testing.F) {
filenames := []string{
"sunrise.jpg", "goexif/geodegrees_as_string.jpg",
"metadata_demo_exif_only.jpg", "metadata_demo_iim_and_xmp_only.jpg",
"corrupt/infinite_loop_exif.jpg",
"corrupt/max_uint32_exif.jpg",
}
for _, filename := range filenames {
f.Add(readTestDataFileAll(f, filename))
}
f.Fuzz(func(t *testing.T, imageBytes []byte) {
fuzzDecodeBytes(t, imageBytes, imagemeta.JPEG)
})
}
func FuzzDecodeWebP(f *testing.F) {
filenames := []string{"sunrise.webp"}
for _, filename := range filenames {
f.Add(readTestDataFileAll(f, filename))
}
f.Fuzz(func(t *testing.T, imageBytes []byte) {
fuzzDecodeBytes(t, imageBytes, imagemeta.WebP)
})
}
func FuzzDecodePNG(f *testing.F) {
filenames := []string{"sunrise.png", "metadata-extractor-images/png/issue614.png"}
for _, filename := range filenames {
f.Add(readTestDataFileAll(f, filename))
}
f.Fuzz(func(t *testing.T, imageBytes []byte) {
fuzzDecodeBytes(t, imageBytes, imagemeta.PNG)
})
}
func FuzzDecodeTIFF(f *testing.F) {
filenames := []string{"sunrise.tif"}
for _, filename := range filenames {
f.Add(readTestDataFileAll(f, filename))
}
f.Fuzz(func(t *testing.T, imageBytes []byte) {
fuzzDecodeBytes(t, imageBytes, imagemeta.TIFF)
})
}
func fuzzDecodeBytes(t *testing.T, imageBytes []byte, f imagemeta.ImageFormat) error {
r := bytes.NewReader(imageBytes)
err := imagemeta.Decode(imagemeta.Options{R: r, ImageFormat: f, Sources: imagemeta.EXIF | imagemeta.IPTC | imagemeta.XMP, Timeout: 10 * time.Second})
if err != nil {
if !imagemeta.IsInvalidFormat(err) {
t.Fatalf("unknown error in Decode: %v %T", err, err)
}
}
return nil
}
func readTestDataFileAll(t testing.TB, filename string) []byte {
t.Helper()
b, err := os.ReadFile(filepath.Join("testdata", "images", filename))
if err != nil {
t.Fatalf("failed to read file %q: %v", filename, err)
}
return b
}