-
Notifications
You must be signed in to change notification settings - Fork 3
/
png_test.go
709 lines (499 loc) · 14.2 KB
/
png_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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package pngstructure
import (
"bytes"
"fmt"
"path"
"reflect"
"testing"
"io/ioutil"
"github.com/dsoprea/go-exif/v2"
"github.com/dsoprea/go-exif/v2/common"
"github.com/dsoprea/go-logging"
)
func TestChunk_Bytes(t *testing.T) {
c := Chunk{
Offset: 0,
Length: 5,
Type: "ABCD",
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55},
Crc: 0x5678,
}
actual := c.Bytes()
expected := []byte{
0x00, 0x00, 0x00, 0x05,
0x41, 0x42, 0x43, 0x44,
0x11, 0x22, 0x33, 0x44, 0x55,
0x00, 0x00, 0x56, 0x78,
}
if bytes.Compare(actual, expected) != 0 {
t.Fatalf("bytes not correct")
}
}
func ExampleChunk_Bytes() {
c := Chunk{
Offset: 0,
Length: 5,
Type: "ABCD",
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55},
Crc: 0x5678,
}
data := c.Bytes()
data = data
// Output:
}
func TestChunk_Write(t *testing.T) {
c := Chunk{
Offset: 0,
Length: 5,
Type: "ABCD",
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55},
Crc: 0x5678,
}
b := new(bytes.Buffer)
_, err := c.WriteTo(b)
log.PanicIf(err)
expected := c.Bytes()
if bytes.Compare(b.Bytes(), expected) != 0 {
t.Fatalf("bytes not correct")
}
}
func ExampleChunk_Write() {
c := Chunk{
Offset: 0,
Length: 5,
Type: "ABCD",
Data: []byte{0x11, 0x22, 0x33, 0x44, 0x55},
Crc: 0x5678,
}
b := new(bytes.Buffer)
_, err := c.WriteTo(b)
log.PanicIf(err)
data := c.Bytes()
data = data
// Output:
}
func TestChunkSlice_Index(t *testing.T) {
filepath := path.Join(assetsPath, "Selection_058.png")
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(filepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
index := cs.Index()
tallies := make(map[string]int)
for key, chunks := range index {
tallies[key] = len(chunks)
}
expected := map[string]int{
"IDAT": 222,
"IEND": 1,
"IHDR": 1,
"pHYs": 1,
"tIME": 1,
}
if reflect.DeepEqual(tallies, expected) != true {
t.Fatalf("index not correct")
}
}
func TestChunkSlice_FindExif_Miss(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
filepath := path.Join(assetsPath, "Selection_058.png")
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(filepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
_, err = cs.FindExif()
if err == nil {
t.Fatalf("expected error for missing EXIF")
} else if log.Is(err, ErrNoExif) == false {
log.Panic(err)
}
}
func TestChunkSlice_FindExif_Hit(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
testBasicFilepath := getTestBasicImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testBasicFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
exifChunk, err := cs.FindExif()
log.PanicIf(err)
exifFilepath := fmt.Sprintf("%s.exif", testBasicFilepath)
expectedExifData, err := ioutil.ReadFile(exifFilepath)
log.PanicIf(err)
if bytes.Compare(exifChunk.Data, expectedExifData) != 0 {
t.Fatalf("Exif not extract correctly.")
}
}
func TestChunkSlice_Exif(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
testExifFilepath := getTestExifImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testExifFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
rootIfd, _, err := cs.Exif()
log.PanicIf(err)
tags := rootIfd.Entries
if rootIfd.IfdIdentity().Equals(exifcommon.IfdStandardIfdIdentity) != true {
t.Fatalf("root-IFD not parsed correctly")
} else if len(tags) != 2 {
t.Fatalf("incorrect number of encoded tags")
} else if tags[0].TagId() != 0x0100 {
t.Fatalf("first tag is not correct")
} else if tags[1].TagId() != 0x0101 {
t.Fatalf("second tag is not correct")
}
}
func TestChunkSlice_SetExif_Existing(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
testBasicFilepath := getTestBasicImageFilepath()
// Build EXIF.
im := exif.NewIfdMappingWithStandard()
ti := exif.NewTagIndex()
ib := exif.NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
err := ib.AddStandardWithName("ImageWidth", []uint32{11})
log.PanicIf(err)
err = ib.AddStandardWithName("ImageLength", []uint32{22})
log.PanicIf(err)
// Replace into PNG.
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testBasicFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
err = cs.SetExif(ib)
log.PanicIf(err)
b := new(bytes.Buffer)
err = cs.WriteTo(b)
log.PanicIf(err)
updatedImageData := b.Bytes()
// Re-parse.
intfc, err = pmp.ParseBytes(updatedImageData)
log.PanicIf(err)
cs = intfc.(*ChunkSlice)
exifChunk, err := cs.FindExif()
log.PanicIf(err)
chunkData := exifChunk.Bytes()
// Chunk data length minus length, type, and CRC data.
expectedExifLen := len(chunkData) - 4 - 4 - 4
if int(exifChunk.Length) != expectedExifLen {
t.Fatalf("actual chunk data length does not match prescribed chunk data length: (%d) != (%d)", exifChunk.Length, len(exifChunk.Data))
} else if len(exifChunk.Data) != expectedExifLen {
t.Fatalf("chunk data length not correct")
}
// The first eight bytes belong to the PNG chunk structure.
offset := 8
_, index, err := exif.Collect(im, ti, chunkData[offset:offset+expectedExifLen])
log.PanicIf(err)
tags := index.RootIfd.Entries
if len(tags) != 2 {
t.Fatalf("incorrect number of encoded tags")
} else if tags[0].TagId() != 0x0100 {
t.Fatalf("first tag is not correct")
} else if tags[1].TagId() != 0x0101 {
t.Fatalf("second tag is not correct")
}
}
func TestChunkSlice_SetExif_Chunk(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
// Build EXIF.
im := exif.NewIfdMappingWithStandard()
ti := exif.NewTagIndex()
ib := exif.NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
err := ib.AddStandardWithName("ImageWidth", []uint32{11})
log.PanicIf(err)
err = ib.AddStandardWithName("ImageLength", []uint32{22})
log.PanicIf(err)
// Create PNG.
cs := NewPngChunkSlice()
err = cs.SetExif(ib)
log.PanicIf(err)
exifChunk, err := cs.FindExif()
log.PanicIf(err)
chunkData := exifChunk.Bytes()
// Chunk data length minus length, type, and CRC data.
expectedExifLen := len(chunkData) - 4 - 4 - 4
if int(exifChunk.Length) != expectedExifLen {
t.Fatalf("actual chunk data length does not match prescribed chunk data length: (%d) != (%d)", exifChunk.Length, len(exifChunk.Data))
} else if len(exifChunk.Data) != expectedExifLen {
t.Fatalf("chunk data length not correct")
}
// The first eight bytes belong to the PNG chunk structure.
offset := 8
_, index, err := exif.Collect(im, ti, chunkData[offset:offset+expectedExifLen])
log.PanicIf(err)
tags := index.RootIfd.Entries
if len(tags) != 2 {
t.Fatalf("incorrect number of encoded tags")
} else if tags[0].TagId() != 0x0100 {
t.Fatalf("first tag is not correct")
} else if tags[1].TagId() != 0x0101 {
t.Fatalf("second tag is not correct")
}
}
func ExampleChunkSlice_SetExif() {
// Build EXIF.
testBasicFilepath := getTestBasicImageFilepath()
im := exif.NewIfdMappingWithStandard()
ti := exif.NewTagIndex()
ib := exif.NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
err := ib.AddStandardWithName("ImageWidth", []uint32{11})
log.PanicIf(err)
err = ib.AddStandardWithName("ImageLength", []uint32{22})
log.PanicIf(err)
// Add/replace EXIF into PNG (overwrite existing).
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testBasicFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
err = cs.SetExif(ib)
log.PanicIf(err)
b := new(bytes.Buffer)
// Write to a `bytes.Buffer`.
err = cs.WriteTo(b)
log.PanicIf(err)
// Output:
}
func ExampleChunkSlice_Exif() {
testExifFilepath := getTestExifImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testExifFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
rootIfd, _, err := cs.Exif()
log.PanicIf(err)
rootIfd = rootIfd
// Output:
}
func ExampleChunkSlice_FindExif() {
testBasicFilepath := getTestBasicImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testBasicFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
exifChunk, err := cs.FindExif()
log.PanicIf(err)
exifChunk = exifChunk
// Output:
}
func ExampleChunkSlice_Index() {
filepath := path.Join(assetsPath, "Selection_058.png")
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(filepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
index := cs.Index()
index = index
// Output:
}
func TestChunk_Crc32_Cycle(t *testing.T) {
c := &Chunk{
Type: "pHYs",
Data: []byte{0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01},
}
c.UpdateCrc32()
if c.Crc != calculateCrc32(c) {
t.Fatalf("CRC value not consistently calculated")
} else if c.Crc != 0x9a9c18 {
t.Fatalf("CRC (1) not correct")
} else if c.CheckCrc32() != true {
t.Fatalf("CRC (1) check failed")
}
c.Type = "tIME"
c.Data = []byte{0x07, 0xcc, 0x06, 0x07, 0x11, 0x3a, 0x08}
c.UpdateCrc32()
if c.Crc != 0x8eff267a {
t.Fatalf("CRC (2) not correct")
} else if c.CheckCrc32() != true {
t.Fatalf("CRC (2) check failed")
}
c.Data = []byte{0x99, 0x99, 0x99, 0x99}
if c.CheckCrc32() != false {
t.Fatalf("CRC check didn't fail but should've")
}
}
func TestChunkSlice_ConstructExifBuilder(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintErrorf(err, "Test failure.")
}
}()
testExifFilepath := getTestExifImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testExifFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
// Add a new tag to the additional EXIF.
rootIb, err := cs.ConstructExifBuilder()
log.PanicIf(err)
err = rootIb.SetStandardWithName("ImageLength", []uint32{44})
log.PanicIf(err)
err = rootIb.AddStandardWithName("BitsPerSample", []uint16{33})
log.PanicIf(err)
// Update the image.
err = cs.SetExif(rootIb)
log.PanicIf(err)
b := new(bytes.Buffer)
err = cs.WriteTo(b)
log.PanicIf(err)
updatedImageData := b.Bytes()
// Re-parse.
pmp = NewPngMediaParser()
intfc, err = pmp.ParseBytes(updatedImageData)
log.PanicIf(err)
cs = intfc.(*ChunkSlice)
rootIfd, _, err := cs.Exif()
log.PanicIf(err)
tags := rootIfd.Entries
v1, err := tags[0].Value()
log.PanicIf(err)
v2, err := tags[1].Value()
log.PanicIf(err)
v3, err := tags[2].Value()
log.PanicIf(err)
if rootIfd.IfdIdentity().Equals(exifcommon.IfdStandardIfdIdentity) != true {
t.Fatalf("root-IFD not parsed correctly")
} else if len(tags) != 3 {
t.Fatalf("incorrect number of encoded tags")
} else if tags[0].TagId() != 0x0100 || reflect.DeepEqual(v1.([]uint32), []uint32{11}) != true {
t.Fatalf("first tag is not correct")
} else if tags[1].TagId() != 0x0101 || reflect.DeepEqual(v2.([]uint32), []uint32{44}) != true {
t.Fatalf("second tag is not correct")
} else if tags[2].TagId() != 0x0102 || reflect.DeepEqual(v3.([]uint16), []uint16{33}) != true {
t.Fatalf("third tag is not correct")
}
}
func ExampleChunkSlice_ConstructExifBuilder() {
testExifFilepath := getTestExifImageFilepath()
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(testExifFilepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
// Add a new tag to the additional EXIF.
rootIb, err := cs.ConstructExifBuilder()
log.PanicIf(err)
err = rootIb.SetStandardWithName("ImageLength", []uint32{44})
log.PanicIf(err)
err = rootIb.AddStandardWithName("BitsPerSample", []uint16{33})
log.PanicIf(err)
// Update the image.
err = cs.SetExif(rootIb)
log.PanicIf(err)
b := new(bytes.Buffer)
err = cs.WriteTo(b)
log.PanicIf(err)
updatedImageData := b.Bytes()
// Re-parse.
pmp = NewPngMediaParser()
intfc, err = pmp.ParseBytes(updatedImageData)
log.PanicIf(err)
cs = intfc.(*ChunkSlice)
rootIfd, _, err := cs.Exif()
log.PanicIf(err)
for i, ite := range rootIfd.Entries {
value, err := ite.Value()
log.PanicIf(err)
fmt.Printf("%d: (0x%04x) %v\n", i, ite.TagId(), value)
}
// Output:
// 0: (0x0100) [11]
// 1: (0x0101) [44]
// 2: (0x0102) [33]
}
func TestPngSplitter_Write(t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.PrintError(err)
}
}()
filepath := path.Join(assetsPath, "Selection_058.png")
original, err := ioutil.ReadFile(filepath)
log.PanicIf(err)
pmp := NewPngMediaParser()
intfc, err := pmp.ParseBytes(original)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
b := new(bytes.Buffer)
err = cs.WriteTo(b)
log.PanicIf(err)
written := b.Bytes()
if bytes.Compare(written, original) != 0 {
t.Fatalf("written bytes (%d) do not equal read bytes (%d)", len(written), len(original))
}
}
func ExampleChunkSlice_Write() {
filepath := path.Join(assetsPath, "Selection_058.png")
pmp := NewPngMediaParser()
intfc, err := pmp.ParseFile(filepath)
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
b := new(bytes.Buffer)
err = cs.WriteTo(b)
log.PanicIf(err)
// Output:
}
func TestChunkSlice_Write(t *testing.T) {
chunkData := []byte{
0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x05, 0xc0, 0x00, 0x00, 0x02, 0x56, 0x08, 0x02, 0x00, 0x00, 0x00,
0xf0, 0x49, 0xb3, 0x65,
0x00, 0x00, 0x00, 0x09,
0x70, 0x48, 0x59, 0x73,
0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01,
0x00, 0x9a, 0x9c, 0x18,
}
b := new(bytes.Buffer)
_, err := b.Write(PngSignature[:])
log.PanicIf(err)
_, err = b.Write(chunkData)
log.PanicIf(err)
originalFull := make([]byte, len(b.Bytes()))
copy(originalFull, b.Bytes())
br := bytes.NewReader(b.Bytes())
pmp := NewPngMediaParser()
intfc, err := pmp.Parse(br, len(b.Bytes()))
log.PanicIf(err)
cs := intfc.(*ChunkSlice)
chunks := cs.Chunks()
if len(chunks) != 2 {
t.Fatalf("number of chunks not correct")
}
b2 := new(bytes.Buffer)
err = cs.WriteTo(b2)
log.PanicIf(err)
actual := b2.Bytes()
if bytes.Compare(actual, originalFull) != 0 {
fmt.Printf("ACTUAL:\n")
DumpBytesClause(actual)
fmt.Printf("EXPECTED:\n")
DumpBytesClause(originalFull)
t.Fatalf("did not write correctly")
}
}