-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorpus_test.go
More file actions
304 lines (271 loc) · 8.11 KB
/
Copy pathcorpus_test.go
File metadata and controls
304 lines (271 loc) · 8.11 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package gometadata
import (
"bytes"
"encoding/binary"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/FlavioCFOliveira/GoMetadata/format"
)
// corpusAllFiles returns all files from all corpus subdirectories.
func corpusAllFiles(t *testing.T) []string {
t.Helper()
base := filepath.Join("testdata", "corpus")
if _, err := os.Stat(base); os.IsNotExist(err) {
t.Skip("testdata/corpus not present; skipping corpus tests")
}
var paths []string
subdirs := []string{"jpeg", "png", "tiff", "webp", "heif", "raw"}
for _, sub := range subdirs {
dir := filepath.Join(base, sub)
if _, err := os.Stat(dir); os.IsNotExist(err) {
continue
}
_ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
paths = append(paths, path)
return nil
})
}
if len(paths) == 0 {
t.Skip("no corpus files found")
}
return paths
}
// TestCorpusReadAll exercises Read() against the full real-world corpus.
// It verifies that no file causes a panic or a CorruptMetadataError.
// UnsupportedFormatError and TruncatedFileError are benign and skipped.
func TestCorpusReadAll(t *testing.T) {
t.Parallel()
files := corpusAllFiles(t)
t.Logf("corpus: %d files", len(files))
var ok, skipped, failed int
for _, path := range files {
f, err := os.Open(path)
if err != nil {
t.Errorf("open %s: %v", path, err)
continue
}
_, rerr := Read(f)
_ = f.Close()
if rerr == nil {
ok++
continue
}
var corrupt *CorruptMetadataError
if errors.As(rerr, &corrupt) {
t.Errorf("CorruptMetadataError on %s: %v", path, rerr)
failed++
continue
}
// Unsupported format or truncated file — skip gracefully.
skipped++
}
t.Logf("corpus: ok=%d skipped=%d failed=%d", ok, skipped, failed)
if failed > 0 {
t.Fatalf("%d corpus files produced unexpected errors", failed)
}
}
// TestCorpusGPS verifies that GPS coordinates extracted from corpus files are
// within valid WGS-84 ranges.
func TestCorpusGPS(t *testing.T) {
t.Parallel()
files := corpusAllFiles(t)
for _, path := range files {
f, err := os.Open(path)
if err != nil {
continue
}
m, rerr := Read(f)
_ = f.Close()
if rerr != nil || m == nil {
continue
}
lat, lon, ok := m.GPS()
if !ok {
continue
}
if lat < -90 || lat > 90 {
t.Errorf("%s: GPS lat %f out of range [-90, 90]", path, lat)
}
if lon < -180 || lon > 180 {
t.Errorf("%s: GPS lon %f out of range [-180, 180]", path, lon)
}
}
}
// TestCorpusRoundTrip reads each corpus file without parsing metadata (raw mode),
// writes it back unchanged, and verifies that the raw segments are byte-for-byte
// identical. Using WithoutEXIF/WithoutIPTC/WithoutXMP ensures Write() passes the
// original raw bytes through without re-encoding.
func TestCorpusRoundTrip(t *testing.T) {
t.Parallel()
files := corpusAllFiles(t)
t.Logf("round-trip: testing %d corpus files", len(files))
var ok, skipped, failed int
for _, path := range files {
data, err := os.ReadFile(path)
if err != nil {
t.Errorf("read %s: %v", path, err)
continue
}
// Read in raw-only mode: parsed structs are nil, raw bytes are preserved.
m, rerr := Read(bytes.NewReader(data),
WithoutEXIF(), WithoutIPTC(), WithoutXMP())
if rerr != nil {
skipped++
continue
}
var out bytes.Buffer
werr := Write(bytes.NewReader(data), &out, m)
if werr != nil {
// Some corpus files are intentionally malformed (fuzzing inputs from
// exiv2 test suites). Write() correctly rejects them; treat as skipped.
skipped++
continue
}
// Re-read and compare raw segments byte-for-byte.
m2, rerr2 := Read(bytes.NewReader(out.Bytes()),
WithoutEXIF(), WithoutIPTC(), WithoutXMP())
if rerr2 != nil {
t.Errorf("re-read after Write %s: %v", path, rerr2)
failed++
continue
}
// TIFF-family files: rawEXIF = entire file; when embedded IPTC/XMP are
// present Write() re-encodes the TIFF (known limitation). Skip byte
// comparison for these cases and only verify the output is readable.
fm := m.Format()
isTIFFFamily := fm == format.FormatTIFF || fm == format.FormatCR2 ||
fm == format.FormatNEF || fm == format.FormatARW ||
fm == format.FormatDNG || fm == format.FormatORF || fm == format.FormatRW2
if isTIFFFamily && (m.RawIPTC() != nil || m.RawXMP() != nil) {
ok++
continue
}
if !bytes.Equal(m.RawEXIF(), m2.RawEXIF()) {
t.Errorf("%s: rawEXIF changed after round-trip (%d → %d bytes)",
path, len(m.RawEXIF()), len(m2.RawEXIF()))
failed++
continue
}
if !bytes.Equal(m.RawXMP(), m2.RawXMP()) {
t.Errorf("%s: rawXMP changed after round-trip", path)
failed++
continue
}
if !bytes.Equal(m.RawIPTC(), m2.RawIPTC()) {
t.Errorf("%s: rawIPTC changed after round-trip", path)
failed++
continue
}
ok++
}
t.Logf("round-trip: ok=%d skipped=%d failed=%d", ok, skipped, failed)
if failed > 0 {
t.Fatalf("%d files failed round-trip", failed)
}
}
// TestWriteModifyRoundTrip tests the full modify-write-read cycle on a synthetic
// JPEG. It modifies the EXIF ImageDescription, writes, and reads back to confirm.
func TestWriteModifyRoundTrip(t *testing.T) {
t.Parallel()
const originalCaption = "original caption"
const newCaption = "updated caption"
jpeg := buildJPEGWithCaption(originalCaption)
m, err := Read(bytes.NewReader(jpeg))
if err != nil {
t.Fatalf("Read: %v", err)
}
if m.EXIF == nil {
t.Fatal("no EXIF parsed")
}
if got := m.Caption(); got != originalCaption {
t.Fatalf("Caption before write: got %q, want %q", got, originalCaption)
}
// Modify ImageDescription in-place.
for i, e := range m.EXIF.IFD0.Entries {
if e.Tag == 0x010E { // TagImageDescription
v := []byte(newCaption + "\x00")
m.EXIF.IFD0.Entries[i].Value = v
m.EXIF.IFD0.Entries[i].Count = uint32(len(v)) //nolint:gosec // G115: test helper, intentional type cast
break
}
}
var out bytes.Buffer
if err := Write(bytes.NewReader(jpeg), &out, m); err != nil {
t.Fatalf("Write: %v", err)
}
m2, err := Read(bytes.NewReader(out.Bytes()))
if err != nil {
t.Fatalf("Read after Write: %v", err)
}
if got := m2.Caption(); got != newCaption {
t.Errorf("Caption after Write: got %q, want %q", got, newCaption)
}
}
// buildJPEGWithCaption builds a minimal JPEG with an EXIF ImageDescription tag.
func buildJPEGWithCaption(caption string) []byte {
captionBytes := []byte(caption + "\x00")
captionLen := uint32(len(captionBytes)) //nolint:gosec // G115: test helper, intentional type cast
// Layout: TIFF header(8) + IFD count(2) + 1 entry(12) + next-IFD(4) + value area
valueOffset := uint32(8 + 2 + 1*12 + 4)
tiff := make([]byte, valueOffset+captionLen)
order := binary.LittleEndian
// TIFF header
tiff[0], tiff[1] = 'I', 'I'
order.PutUint16(tiff[2:], 0x002A)
order.PutUint32(tiff[4:], 8) // IFD0 at offset 8
// IFD: 1 entry
order.PutUint16(tiff[8:], 1) // entry count
// Entry: tag 0x010E ImageDescription, type ASCII, count captionLen, offset
e := tiff[10:]
order.PutUint16(e[0:], 0x010E) // tag
order.PutUint16(e[2:], 0x0002) // TypeASCII
order.PutUint32(e[4:], captionLen) // count
order.PutUint32(e[8:], valueOffset) // value offset
// Next-IFD pointer = 0
order.PutUint32(tiff[10+12:], 0)
// Value area
copy(tiff[valueOffset:], captionBytes)
return buildMinimalJPEG(tiff)
}
// BenchmarkReadFile measures end-to-end performance of ReadFile on a real JPEG.
func BenchmarkReadFile(b *testing.B) {
// Find the first available JPEG corpus file.
dir := filepath.Join("testdata", "corpus", "jpeg")
if _, err := os.Stat(dir); os.IsNotExist(err) {
b.Skip("no JPEG corpus available")
}
var target string
_ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || target != "" {
return nil
}
if ext := strings.ToLower(filepath.Ext(path)); ext == ".jpg" || ext == ".jpeg" {
target = path
}
return nil
})
if target == "" {
b.Skip("no JPEG corpus files")
}
data, err := os.ReadFile(target)
if err != nil {
b.Fatalf("read corpus file: %v", err)
}
b.ResetTimer()
b.SetBytes(int64(len(data)))
for range b.N {
if _, err := Read(bytes.NewReader(data)); err != nil {
b.Fatal(err)
}
}
}