1 package exifcommon
2
3 import (
4 "bytes"
5 "math"
6 "reflect"
7 "testing"
8
9 "io/ioutil"
10
11 "github.com/dsoprea/go-logging"
12 "github.com/dsoprea/go-utility/v2/filesystem"
13 )
14
15 func TestNewValueContext(t *testing.T) {
16 rawValueOffset := []byte{0, 0, 0, 22}
17 addressableData := []byte{1, 2, 3, 4}
18 sb := rifs.NewSeekableBufferWithBytes(addressableData)
19
20 vc := NewValueContext(
21 "aa/bb",
22 0x1234,
23 11,
24 22,
25 rawValueOffset,
26 sb,
27 TypeLong,
28 TestDefaultByteOrder)
29
30 if vc.ifdPath != "aa/bb" {
31 t.Fatalf("ifdPath not correct: [%s]", vc.ifdPath)
32 } else if vc.tagId != 0x1234 {
33 t.Fatalf("tagId not correct: (0x%04x)", vc.tagId)
34 } else if vc.unitCount != 11 {
35 t.Fatalf("unitCount not correct: (%d)", vc.unitCount)
36 } else if vc.valueOffset != 22 {
37 t.Fatalf("valueOffset not correct: (%d)", vc.valueOffset)
38 } else if bytes.Equal(vc.rawValueOffset, rawValueOffset) != true {
39 t.Fatalf("rawValueOffset not correct: %v", vc.rawValueOffset)
40 } else if vc.tagType != TypeLong {
41 t.Fatalf("tagType not correct: (%d)", vc.tagType)
42 } else if vc.byteOrder != TestDefaultByteOrder {
43 t.Fatalf("byteOrder not correct: %v", vc.byteOrder)
44 }
45
46 recoveredBytes, err := ioutil.ReadAll(vc.AddressableData())
47 log.PanicIf(err)
48
49 if bytes.Equal(recoveredBytes, addressableData) != true {
50 t.Fatalf("AddressableData() not correct: %v", recoveredBytes)
51 }
52 }
53
54 func TestValueContext_SetUndefinedValueType__ErrorWhenNotUndefined(t *testing.T) {
55 defer func() {
56 if errRaw := recover(); errRaw != nil {
57 err := errRaw.(error)
58 if err.Error() != "can not set effective type for unknown-type tag because this is *not* an unknown-type tag" {
59 t.Fatalf("Error not expected: [%s]", err.Error())
60 }
61
62 return
63 }
64
65 t.Fatalf("Expected error.")
66 }()
67
68 rawValueOffset := []byte{0, 0, 0, 22}
69
70 addressableData := []byte{1, 2, 3, 4}
71 sb := rifs.NewSeekableBufferWithBytes(addressableData)
72
73 vc := NewValueContext(
74 "aa/bb",
75 0x1234,
76 11,
77 22,
78 rawValueOffset,
79 sb,
80 TypeLong,
81 TestDefaultByteOrder)
82
83 vc.SetUndefinedValueType(TypeLong)
84 }
85
86 func TestValueContext_SetUndefinedValueType__Ok(t *testing.T) {
87 rawValueOffset := []byte{0, 0, 0, 22}
88
89 addressableData := []byte{1, 2, 3, 4}
90 sb := rifs.NewSeekableBufferWithBytes(addressableData)
91
92 vc := NewValueContext(
93 "aa/bb",
94 0x1234,
95 11,
96 22,
97 rawValueOffset,
98 sb,
99 TypeUndefined,
100 TestDefaultByteOrder)
101
102 vc.SetUndefinedValueType(TypeLong)
103
104 if vc.tagType != TypeUndefined {
105 t.Fatalf("Internal type not still 'undefined': (%d)", vc.tagType)
106 } else if vc.undefinedValueTagType != TypeLong {
107 t.Fatalf("Internal undefined-type not correct: (%d)", vc.undefinedValueTagType)
108 } else if vc.effectiveValueType() != TypeLong {
109 t.Fatalf("Effective tag not correct: (%d)", vc.effectiveValueType())
110 }
111 }
112
113 func TestValueContext_effectiveValueType(t *testing.T) {
114 rawValueOffset := []byte{0, 0, 0, 22}
115
116 addressableData := []byte{1, 2, 3, 4}
117 sb := rifs.NewSeekableBufferWithBytes(addressableData)
118
119 vc := NewValueContext(
120 "aa/bb",
121 0x1234,
122 11,
123 22,
124 rawValueOffset,
125 sb,
126 TypeUndefined,
127 TestDefaultByteOrder)
128
129 vc.SetUndefinedValueType(TypeLong)
130
131 if vc.tagType != TypeUndefined {
132 t.Fatalf("Internal type not still 'undefined': (%d)", vc.tagType)
133 } else if vc.undefinedValueTagType != TypeLong {
134 t.Fatalf("Internal undefined-type not correct: (%d)", vc.undefinedValueTagType)
135 } else if vc.effectiveValueType() != TypeLong {
136 t.Fatalf("Effective tag not correct: (%d)", vc.effectiveValueType())
137 }
138 }
139
140 func TestValueContext_UnitCount(t *testing.T) {
141 rawValueOffset := []byte{0, 0, 0, 22}
142
143 addressableData := []byte{1, 2, 3, 4}
144 sb := rifs.NewSeekableBufferWithBytes(addressableData)
145
146 vc := NewValueContext(
147 "aa/bb",
148 0x1234,
149 11,
150 22,
151 rawValueOffset,
152 sb,
153 TypeUndefined,
154 TestDefaultByteOrder)
155
156 if vc.UnitCount() != 11 {
157 t.Fatalf("UnitCount() not correct: (%d)", vc.UnitCount())
158 }
159 }
160
161 func TestValueContext_ValueOffset(t *testing.T) {
162 rawValueOffset := []byte{0, 0, 0, 22}
163
164 addressableData := []byte{1, 2, 3, 4}
165 sb := rifs.NewSeekableBufferWithBytes(addressableData)
166
167 vc := NewValueContext(
168 "aa/bb",
169 0x1234,
170 11,
171 22,
172 rawValueOffset,
173 sb,
174 TypeUndefined,
175 TestDefaultByteOrder)
176
177 if vc.ValueOffset() != 22 {
178 t.Fatalf("ValueOffset() not correct: (%d)", vc.ValueOffset())
179 }
180 }
181
182 func TestValueContext_RawValueOffset(t *testing.T) {
183 rawValueOffset := []byte{0, 0, 0, 22}
184
185 addressableData := []byte{1, 2, 3, 4}
186 sb := rifs.NewSeekableBufferWithBytes(addressableData)
187
188 vc := NewValueContext(
189 "aa/bb",
190 0x1234,
191 11,
192 22,
193 rawValueOffset,
194 sb,
195 TypeUndefined,
196 TestDefaultByteOrder)
197
198 if bytes.Equal(vc.RawValueOffset(), rawValueOffset) != true {
199 t.Fatalf("RawValueOffset() not correct: %v", vc.RawValueOffset())
200 }
201 }
202
203 func TestValueContext_AddressableData(t *testing.T) {
204 rawValueOffset := []byte{0, 0, 0, 22}
205
206 addressableData := []byte{1, 2, 3, 4}
207 sb := rifs.NewSeekableBufferWithBytes(addressableData)
208
209 vc := NewValueContext(
210 "aa/bb",
211 0x1234,
212 11,
213 22,
214 rawValueOffset,
215 sb,
216 TypeUndefined,
217 TestDefaultByteOrder)
218
219 recoveredBytes, err := ioutil.ReadAll(vc.AddressableData())
220 log.PanicIf(err)
221
222 if bytes.Equal(recoveredBytes, addressableData) != true {
223 t.Fatalf("AddressableData() not correct: %v", recoveredBytes)
224 }
225 }
226
227 func TestValueContext_ByteOrder(t *testing.T) {
228 rawValueOffset := []byte{0, 0, 0, 22}
229
230 addressableData := []byte{1, 2, 3, 4}
231 sb := rifs.NewSeekableBufferWithBytes(addressableData)
232
233 vc := NewValueContext(
234 "aa/bb",
235 0x1234,
236 11,
237 22,
238 rawValueOffset,
239 sb,
240 TypeUndefined,
241 TestDefaultByteOrder)
242
243 if vc.ByteOrder() != TestDefaultByteOrder {
244 t.Fatalf("ByteOrder() not correct: %v", vc.ByteOrder())
245 }
246 }
247
248 func TestValueContext_IfdPath(t *testing.T) {
249 rawValueOffset := []byte{0, 0, 0, 22}
250
251 addressableData := []byte{1, 2, 3, 4}
252 sb := rifs.NewSeekableBufferWithBytes(addressableData)
253
254 vc := NewValueContext(
255 "aa/bb",
256 0x1234,
257 11,
258 22,
259 rawValueOffset,
260 sb,
261 TypeUndefined,
262 TestDefaultByteOrder)
263
264 if vc.IfdPath() != "aa/bb" {
265 t.Fatalf("IfdPath() not correct: [%s]", vc.IfdPath())
266 }
267 }
268
269 func TestValueContext_TagId(t *testing.T) {
270 rawValueOffset := []byte{0, 0, 0, 22}
271
272 addressableData := []byte{1, 2, 3, 4}
273 sb := rifs.NewSeekableBufferWithBytes(addressableData)
274
275 vc := NewValueContext(
276 "aa/bb",
277 0x1234,
278 11,
279 22,
280 rawValueOffset,
281 sb,
282 TypeUndefined,
283 TestDefaultByteOrder)
284
285 if vc.TagId() != 0x1234 {
286 t.Fatalf("TagId() not correct: (%d)", vc.TagId())
287 }
288 }
289
290 func TestValueContext_isEmbedded__True(t *testing.T) {
291 unitCount := uint32(4)
292 rawValueOffset := []byte{0, 0, 0, 22}
293
294 addressableData := []byte{1, 2, 3, 4}
295 sb := rifs.NewSeekableBufferWithBytes(addressableData)
296
297 vc := NewValueContext(
298 "aa/bb",
299 0x1234,
300 unitCount,
301 22,
302 rawValueOffset,
303 sb,
304 TypeByte,
305 TestDefaultByteOrder)
306
307 if vc.isEmbedded() != true {
308 t.Fatalf("isEmbedded() not correct: %v", vc.isEmbedded())
309 }
310 }
311
312 func TestValueContext_isEmbedded__False(t *testing.T) {
313 unitCount := uint32(5)
314 rawValueOffset := []byte{0, 0, 0, 22}
315
316 addressableData := []byte{1, 2, 3, 4}
317 sb := rifs.NewSeekableBufferWithBytes(addressableData)
318
319 vc := NewValueContext(
320 "aa/bb",
321 0x1234,
322 unitCount,
323 22,
324 rawValueOffset,
325 sb,
326 TypeByte,
327 TestDefaultByteOrder)
328
329 if vc.isEmbedded() != false {
330 t.Fatalf("isEmbedded() not correct: %v", vc.isEmbedded())
331 }
332 }
333
334 func TestValueContext_readRawEncoded__IsEmbedded(t *testing.T) {
335 unitCount := uint32(4)
336
337 rawValueOffset := []byte{1, 2, 3, 4}
338
339
340 valueOffset := uint32(0)
341
342 addressableData := []byte{}
343 sb := rifs.NewSeekableBufferWithBytes(addressableData)
344
345 vc := NewValueContext(
346 "aa/bb",
347 0x1234,
348 unitCount,
349 valueOffset,
350 rawValueOffset,
351 sb,
352 TypeByte,
353 TestDefaultByteOrder)
354
355 recovered, err := vc.readRawEncoded()
356 log.PanicIf(err)
357
358 if bytes.Equal(recovered, rawValueOffset) != true {
359 t.Fatalf("Embedded value bytes not recovered correctly: %v", recovered)
360 }
361 }
362
363 func TestValueContext_readRawEncoded__IsRelative(t *testing.T) {
364 unitCount := uint32(5)
365
366
367 rawValueOffset := []byte{0, 0, 0, 0}
368
369 valueOffset := uint32(4)
370
371 data := []byte{5, 6, 7, 8, 9}
372
373 addressableData := []byte{1, 2, 3, 4}
374 addressableData = append(addressableData, data...)
375 sb := rifs.NewSeekableBufferWithBytes(addressableData)
376
377 vc := NewValueContext(
378 "aa/bb",
379 0x1234,
380 unitCount,
381 valueOffset,
382 rawValueOffset,
383 sb,
384 TypeByte,
385 TestDefaultByteOrder)
386
387 recovered, err := vc.readRawEncoded()
388 log.PanicIf(err)
389
390 if bytes.Equal(recovered, data) != true {
391 t.Fatalf("Relative value bytes not recovered correctly: %v", recovered)
392 }
393 }
394
395 func TestValueContext_Format__Byte(t *testing.T) {
396 unitCount := uint32(8)
397
398 rawValueOffset := []byte{0, 0, 0, 4}
399 valueOffset := uint32(4)
400
401 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
402
403 addressableData := []byte{0, 0, 0, 0}
404 addressableData = append(addressableData, data...)
405 sb := rifs.NewSeekableBufferWithBytes(addressableData)
406
407 vc := NewValueContext(
408 "aa/bb",
409 0x1234,
410 unitCount,
411 valueOffset,
412 rawValueOffset,
413 sb,
414 TypeByte,
415 TestDefaultByteOrder)
416
417 value, err := vc.Format()
418 log.PanicIf(err)
419
420 if value != "61 62 63 64 65 66 67 68" {
421 t.Fatalf("Format not correct for bytes: [%s]", value)
422 }
423 }
424
425 func TestValueContext_Format__Ascii(t *testing.T) {
426 unitCount := uint32(8)
427
428 rawValueOffset := []byte{0, 0, 0, 4}
429 valueOffset := uint32(4)
430
431 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
432
433 addressableData := []byte{0, 0, 0, 0}
434 addressableData = append(addressableData, data...)
435 sb := rifs.NewSeekableBufferWithBytes(addressableData)
436
437 vc := NewValueContext(
438 "aa/bb",
439 0x1234,
440 unitCount,
441 valueOffset,
442 rawValueOffset,
443 sb,
444 TypeAscii,
445 TestDefaultByteOrder)
446
447 value, err := vc.Format()
448 log.PanicIf(err)
449
450 if value != "abcdefg" {
451 t.Fatalf("Format not correct for ASCII: [%s]", value)
452 }
453 }
454
455 func TestValueContext_Format__AsciiNoNul(t *testing.T) {
456 unitCount := uint32(8)
457
458 rawValueOffset := []byte{0, 0, 0, 4}
459 valueOffset := uint32(4)
460
461 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
462
463 addressableData := []byte{0, 0, 0, 0}
464 addressableData = append(addressableData, data...)
465 sb := rifs.NewSeekableBufferWithBytes(addressableData)
466
467 vc := NewValueContext(
468 "aa/bb",
469 0x1234,
470 unitCount,
471 valueOffset,
472 rawValueOffset,
473 sb,
474 TypeAsciiNoNul,
475 TestDefaultByteOrder)
476
477 value, err := vc.Format()
478 log.PanicIf(err)
479
480 if value != "abcdefgh" {
481 t.Fatalf("Format not correct for ASCII (no NUL): [%s]", value)
482 }
483 }
484
485 func TestValueContext_Format__Short(t *testing.T) {
486 unitCount := uint32(4)
487
488 rawValueOffset := []byte{0, 0, 0, 4}
489 valueOffset := uint32(4)
490
491 data := []byte{0, 1, 0, 2, 0, 3, 0, 4}
492
493 addressableData := []byte{0, 0, 0, 0}
494 addressableData = append(addressableData, data...)
495 sb := rifs.NewSeekableBufferWithBytes(addressableData)
496
497 vc := NewValueContext(
498 "aa/bb",
499 0x1234,
500 unitCount,
501 valueOffset,
502 rawValueOffset,
503 sb,
504 TypeShort,
505 TestDefaultByteOrder)
506
507 value, err := vc.Format()
508 log.PanicIf(err)
509
510 if value != "[1 2 3 4]" {
511 t.Fatalf("Format not correct for shorts: [%s]", value)
512 }
513 }
514
515 func TestValueContext_Format__Long(t *testing.T) {
516 unitCount := uint32(2)
517
518 rawValueOffset := []byte{0, 0, 0, 4}
519 valueOffset := uint32(4)
520
521 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
522
523 addressableData := []byte{0, 0, 0, 0}
524 addressableData = append(addressableData, data...)
525 sb := rifs.NewSeekableBufferWithBytes(addressableData)
526
527 vc := NewValueContext(
528 "aa/bb",
529 0x1234,
530 unitCount,
531 valueOffset,
532 rawValueOffset,
533 sb,
534 TypeLong,
535 TestDefaultByteOrder)
536
537 value, err := vc.Format()
538 log.PanicIf(err)
539
540 if value != "[1 2]" {
541 t.Fatalf("Format not correct for longs: [%s]", value)
542 }
543 }
544
545 func TestValueContext_Format__Rational(t *testing.T) {
546 unitCount := uint32(2)
547
548 rawValueOffset := []byte{0, 0, 0, 4}
549 valueOffset := uint32(4)
550
551 data := []byte{
552 0, 0, 0, 1, 0, 0, 0, 2,
553 0, 0, 0, 3, 0, 0, 0, 4,
554 }
555
556 addressableData := []byte{0, 0, 0, 0}
557 addressableData = append(addressableData, data...)
558 sb := rifs.NewSeekableBufferWithBytes(addressableData)
559
560 vc := NewValueContext(
561 "aa/bb",
562 0x1234,
563 unitCount,
564 valueOffset,
565 rawValueOffset,
566 sb,
567 TypeRational,
568 TestDefaultByteOrder)
569
570 value, err := vc.Format()
571 log.PanicIf(err)
572
573 if value != "[1/2 3/4]" {
574 t.Fatalf("Format not correct for rationals: [%s]", value)
575 }
576 }
577
578 func TestValueContext_Format__SignedLong(t *testing.T) {
579 unitCount := uint32(2)
580
581 rawValueOffset := []byte{0, 0, 0, 4}
582 valueOffset := uint32(4)
583
584 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
585
586 addressableData := []byte{0, 0, 0, 0}
587 addressableData = append(addressableData, data...)
588 sb := rifs.NewSeekableBufferWithBytes(addressableData)
589
590 vc := NewValueContext(
591 "aa/bb",
592 0x1234,
593 unitCount,
594 valueOffset,
595 rawValueOffset,
596 sb,
597 TypeSignedLong,
598 TestDefaultByteOrder)
599
600 value, err := vc.Format()
601 log.PanicIf(err)
602
603 if value != "[1 2]" {
604 t.Fatalf("Format not correct for signed-longs: [%s]", value)
605 }
606 }
607
608 func TestValueContext_Format__SignedRational(t *testing.T) {
609 unitCount := uint32(2)
610
611 rawValueOffset := []byte{0, 0, 0, 4}
612 valueOffset := uint32(4)
613
614 data := []byte{
615 0, 0, 0, 1, 0, 0, 0, 2,
616 0, 0, 0, 3, 0, 0, 0, 4,
617 }
618
619 addressableData := []byte{0, 0, 0, 0}
620 addressableData = append(addressableData, data...)
621 sb := rifs.NewSeekableBufferWithBytes(addressableData)
622
623 vc := NewValueContext(
624 "aa/bb",
625 0x1234,
626 unitCount,
627 valueOffset,
628 rawValueOffset,
629 sb,
630 TypeSignedRational,
631 TestDefaultByteOrder)
632
633 value, err := vc.Format()
634 log.PanicIf(err)
635
636 if value != "[1/2 3/4]" {
637 t.Fatalf("Format not correct for signed-rationals: [%s]", value)
638 }
639 }
640
641 func TestValueContext_Format__Undefined__NoEffectiveType(t *testing.T) {
642 defer func() {
643 if errRaw := recover(); errRaw != nil {
644 err := errRaw.(error)
645 if err.Error() != "undefined-value type not set" {
646 t.Fatalf("Error not expected: [%s]", err.Error())
647 }
648
649 return
650 }
651
652 t.Fatalf("Expected error.")
653 }()
654
655 unitCount := uint32(8)
656
657 rawValueOffset := []byte{0, 0, 0, 4}
658 valueOffset := uint32(4)
659
660 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
661
662 addressableData := []byte{0, 0, 0, 0}
663 addressableData = append(addressableData, data...)
664 sb := rifs.NewSeekableBufferWithBytes(addressableData)
665
666 vc := NewValueContext(
667 "aa/bb",
668 0x1234,
669 unitCount,
670 valueOffset,
671 rawValueOffset,
672 sb,
673 TypeUndefined,
674 TestDefaultByteOrder)
675
676 value, err := vc.Format()
677 log.PanicIf(err)
678
679 if value != "61 62 63 64 65 66 67 68" {
680 t.Fatalf("Format not correct for bytes: [%s]", value)
681 }
682 }
683
684 func TestValueContext_Format__Undefined__HasEffectiveType(t *testing.T) {
685 unitCount := uint32(8)
686
687 rawValueOffset := []byte{0, 0, 0, 4}
688 valueOffset := uint32(4)
689
690 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
691
692 addressableData := []byte{0, 0, 0, 0}
693 addressableData = append(addressableData, data...)
694 sb := rifs.NewSeekableBufferWithBytes(addressableData)
695
696 vc := NewValueContext(
697 "aa/bb",
698 0x1234,
699 unitCount,
700 valueOffset,
701 rawValueOffset,
702 sb,
703 TypeUndefined,
704 TestDefaultByteOrder)
705
706 vc.SetUndefinedValueType(TypeAscii)
707
708 value, err := vc.Format()
709 log.PanicIf(err)
710
711 if value != "abcdefg" {
712 t.Fatalf("Format not correct for undefined (with effective type of string): [%s]", value)
713 }
714 }
715
716 func TestValueContext_FormatFirst__Bytes(t *testing.T) {
717 unitCount := uint32(8)
718
719 rawValueOffset := []byte{0, 0, 0, 4}
720 valueOffset := uint32(4)
721
722 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
723
724 addressableData := []byte{0, 0, 0, 0}
725 addressableData = append(addressableData, data...)
726 sb := rifs.NewSeekableBufferWithBytes(addressableData)
727
728 vc := NewValueContext(
729 "aa/bb",
730 0x1234,
731 unitCount,
732 valueOffset,
733 rawValueOffset,
734 sb,
735 TypeByte,
736 TestDefaultByteOrder)
737
738 value, err := vc.FormatFirst()
739 log.PanicIf(err)
740
741 if value != "61 62 63 64 65 66 67 68" {
742 t.Fatalf("FormatFirst not correct for bytes: [%s]", value)
743 }
744 }
745
746 func TestValueContext_FormatFirst__String(t *testing.T) {
747 unitCount := uint32(8)
748
749 rawValueOffset := []byte{0, 0, 0, 4}
750 valueOffset := uint32(4)
751
752 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
753
754 addressableData := []byte{0, 0, 0, 0}
755 addressableData = append(addressableData, data...)
756 sb := rifs.NewSeekableBufferWithBytes(addressableData)
757
758 vc := NewValueContext(
759 "aa/bb",
760 0x1234,
761 unitCount,
762 valueOffset,
763 rawValueOffset,
764 sb,
765 TypeAscii,
766 TestDefaultByteOrder)
767
768 value, err := vc.FormatFirst()
769 log.PanicIf(err)
770
771 if value != "abcdefg" {
772 t.Fatalf("FormatFirst not correct for ASCII: [%s]", value)
773 }
774 }
775
776 func TestValueContext_FormatFirst__List(t *testing.T) {
777 unitCount := uint32(4)
778
779 rawValueOffset := []byte{0, 0, 0, 4}
780 valueOffset := uint32(4)
781
782 data := []byte{0, 1, 0, 2, 0, 3, 0, 4}
783
784 addressableData := []byte{0, 0, 0, 0}
785 addressableData = append(addressableData, data...)
786 sb := rifs.NewSeekableBufferWithBytes(addressableData)
787
788 vc := NewValueContext(
789 "aa/bb",
790 0x1234,
791 unitCount,
792 valueOffset,
793 rawValueOffset,
794 sb,
795 TypeShort,
796 TestDefaultByteOrder)
797
798 value, err := vc.FormatFirst()
799 log.PanicIf(err)
800
801 if value != "1..." {
802 t.Fatalf("FormatFirst not correct for shorts: [%s]", value)
803 }
804 }
805
806 func TestValueContext_ReadBytes(t *testing.T) {
807 unitCount := uint32(8)
808
809 rawValueOffset := []byte{0, 0, 0, 4}
810 valueOffset := uint32(4)
811
812 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
813 addressableData := []byte{0, 0, 0, 0}
814 addressableData = append(addressableData, data...)
815 sb := rifs.NewSeekableBufferWithBytes(addressableData)
816
817 vc := NewValueContext(
818 "aa/bb",
819 0x1234,
820 unitCount,
821 valueOffset,
822 rawValueOffset,
823 sb,
824 TypeByte,
825 TestDefaultByteOrder)
826
827 value, err := vc.ReadBytes()
828 log.PanicIf(err)
829
830 if bytes.Equal(value, data) != true {
831 t.Fatalf("ReadBytes not correct: %v", value)
832 }
833 }
834
835 func TestValueContext_ReadAscii(t *testing.T) {
836 unitCount := uint32(8)
837
838 rawValueOffset := []byte{0, 0, 0, 4}
839 valueOffset := uint32(4)
840
841 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
842
843 addressableData := []byte{0, 0, 0, 0}
844 addressableData = append(addressableData, data...)
845 sb := rifs.NewSeekableBufferWithBytes(addressableData)
846
847 vc := NewValueContext(
848 "aa/bb",
849 0x1234,
850 unitCount,
851 valueOffset,
852 rawValueOffset,
853 sb,
854 TypeAscii,
855 TestDefaultByteOrder)
856
857 value, err := vc.ReadAscii()
858 log.PanicIf(err)
859
860 if value != "abcdefg" {
861 t.Fatalf("ReadAscii not correct: [%s]", value)
862 }
863 }
864
865 func TestValueContext_ReadAsciiNoNul(t *testing.T) {
866 unitCount := uint32(8)
867
868 rawValueOffset := []byte{0, 0, 0, 4}
869 valueOffset := uint32(4)
870
871 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
872 addressableData := []byte{0, 0, 0, 0}
873 addressableData = append(addressableData, data...)
874 sb := rifs.NewSeekableBufferWithBytes(addressableData)
875
876 vc := NewValueContext(
877 "aa/bb",
878 0x1234,
879 unitCount,
880 valueOffset,
881 rawValueOffset,
882 sb,
883 TypeAsciiNoNul,
884 TestDefaultByteOrder)
885
886 value, err := vc.ReadAsciiNoNul()
887 log.PanicIf(err)
888
889 if value != "abcdefgh" {
890 t.Fatalf("ReadAsciiNoNul not correct: [%s]", value)
891 }
892 }
893
894 func TestValueContext_ReadShorts(t *testing.T) {
895 unitCount := uint32(4)
896
897 rawValueOffset := []byte{0, 0, 0, 4}
898 valueOffset := uint32(4)
899
900 data := []byte{0, 1, 0, 2, 0, 3, 0, 4}
901
902 addressableData := []byte{0, 0, 0, 0}
903 addressableData = append(addressableData, data...)
904 sb := rifs.NewSeekableBufferWithBytes(addressableData)
905
906 vc := NewValueContext(
907 "aa/bb",
908 0x1234,
909 unitCount,
910 valueOffset,
911 rawValueOffset,
912 sb,
913 TypeShort,
914 TestDefaultByteOrder)
915
916 value, err := vc.ReadShorts()
917 log.PanicIf(err)
918
919 if reflect.DeepEqual(value, []uint16{1, 2, 3, 4}) != true {
920 t.Fatalf("ReadShorts not correct: %v", value)
921 }
922 }
923
924 func TestValueContext_ReadLongs(t *testing.T) {
925 unitCount := uint32(2)
926
927 rawValueOffset := []byte{0, 0, 0, 4}
928 valueOffset := uint32(4)
929
930 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
931
932 addressableData := []byte{0, 0, 0, 0}
933 addressableData = append(addressableData, data...)
934 sb := rifs.NewSeekableBufferWithBytes(addressableData)
935
936 vc := NewValueContext(
937 "aa/bb",
938 0x1234,
939 unitCount,
940 valueOffset,
941 rawValueOffset,
942 sb,
943 TypeLong,
944 TestDefaultByteOrder)
945
946 value, err := vc.ReadLongs()
947 log.PanicIf(err)
948
949 if reflect.DeepEqual(value, []uint32{1, 2}) != true {
950 t.Fatalf("ReadLongs not correct: %v", value)
951 }
952 }
953
954 func TestValueContext_ReadFloats(t *testing.T) {
955 unitCount := uint32(2)
956
957 rawValueOffset := []byte{0, 0, 0, 4}
958 valueOffset := uint32(4)
959
960 data := []byte{0x40, 0x49, 0x0f, 0xdb, 0x40, 0x2d, 0xf8, 0x54}
961
962 addressableData := []byte{0, 0, 0, 0}
963 addressableData = append(addressableData, data...)
964 sb := rifs.NewSeekableBufferWithBytes(addressableData)
965
966 vc := NewValueContext(
967 "aa/bb",
968 0x1234,
969 unitCount,
970 valueOffset,
971 rawValueOffset,
972 sb,
973 TypeFloat,
974 TestDefaultByteOrder)
975
976 value, err := vc.ReadFloats()
977 log.PanicIf(err)
978
979 expectedResult := []float32{3.14159265, 2.71828182}
980 for i, v := range value {
981 if v < expectedResult[i] || v >= math.Nextafter32(expectedResult[i], expectedResult[i]+1) {
982 t.Fatalf("ReadFloats expecting %v, received %v", expectedResult[i], v)
983 }
984 }
985 }
986 func TestValueContext_ReadDoubles(t *testing.T) {
987 unitCount := uint32(2)
988
989 rawValueOffset := []byte{0, 0, 0, 4}
990 valueOffset := uint32(4)
991
992 data := []byte{0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1,
993 0x40, 0x05, 0xbf, 0x0a, 0x89, 0xf1, 0xb0, 0xdd}
994
995 addressableData := []byte{0, 0, 0, 0}
996 addressableData = append(addressableData, data...)
997 sb := rifs.NewSeekableBufferWithBytes(addressableData)
998
999 vc := NewValueContext(
1000 "aa/bb",
1001 0x1234,
1002 unitCount,
1003 valueOffset,
1004 rawValueOffset,
1005 sb,
1006 TypeDouble,
1007 TestDefaultByteOrder)
1008
1009 value, err := vc.ReadDoubles()
1010 log.PanicIf(err)
1011
1012 expectedResult := []float64{3.14159265, 2.71828182}
1013 for i, v := range value {
1014 if v < expectedResult[i] || v >= math.Nextafter(expectedResult[i], expectedResult[i]+1) {
1015 t.Fatalf("ReadDoubles expecting %v, received %v", expectedResult[i], v)
1016 }
1017 }
1018 }
1019
1020 func TestValueContext_ReadRationals(t *testing.T) {
1021 unitCount := uint32(2)
1022
1023 rawValueOffset := []byte{0, 0, 0, 4}
1024 valueOffset := uint32(4)
1025
1026 data := []byte{
1027 0, 0, 0, 1, 0, 0, 0, 2,
1028 0, 0, 0, 3, 0, 0, 0, 4,
1029 }
1030
1031 addressableData := []byte{0, 0, 0, 0}
1032 addressableData = append(addressableData, data...)
1033 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1034
1035 vc := NewValueContext(
1036 "aa/bb",
1037 0x1234,
1038 unitCount,
1039 valueOffset,
1040 rawValueOffset,
1041 sb,
1042 TypeRational,
1043 TestDefaultByteOrder)
1044
1045 value, err := vc.ReadRationals()
1046 log.PanicIf(err)
1047
1048 expected := []Rational{
1049 {Numerator: 1, Denominator: 2},
1050 {Numerator: 3, Denominator: 4},
1051 }
1052
1053 if reflect.DeepEqual(value, expected) != true {
1054 t.Fatalf("ReadRationals not correct: %v", value)
1055 }
1056 }
1057
1058 func TestValueContext_ReadSignedLongs(t *testing.T) {
1059 unitCount := uint32(2)
1060
1061 rawValueOffset := []byte{0, 0, 0, 4}
1062 valueOffset := uint32(4)
1063
1064 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
1065
1066 addressableData := []byte{0, 0, 0, 0}
1067 addressableData = append(addressableData, data...)
1068 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1069
1070 vc := NewValueContext(
1071 "aa/bb",
1072 0x1234,
1073 unitCount,
1074 valueOffset,
1075 rawValueOffset,
1076 sb,
1077 TypeSignedLong,
1078 TestDefaultByteOrder)
1079
1080 value, err := vc.ReadSignedLongs()
1081 log.PanicIf(err)
1082
1083 if reflect.DeepEqual(value, []int32{1, 2}) != true {
1084 t.Fatalf("ReadSignedLongs not correct: %v", value)
1085 }
1086 }
1087
1088 func TestValueContext_ReadSignedRationals(t *testing.T) {
1089 unitCount := uint32(2)
1090
1091 rawValueOffset := []byte{0, 0, 0, 4}
1092 valueOffset := uint32(4)
1093
1094 data := []byte{
1095 0, 0, 0, 1, 0, 0, 0, 2,
1096 0, 0, 0, 3, 0, 0, 0, 4,
1097 }
1098
1099 addressableData := []byte{0, 0, 0, 0}
1100 addressableData = append(addressableData, data...)
1101 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1102
1103 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeSignedRational, TestDefaultByteOrder)
1104
1105 value, err := vc.ReadSignedRationals()
1106 log.PanicIf(err)
1107
1108 expected := []SignedRational{
1109 {Numerator: 1, Denominator: 2},
1110 {Numerator: 3, Denominator: 4},
1111 }
1112
1113 if reflect.DeepEqual(value, expected) != true {
1114 t.Fatalf("ReadSignedRationals not correct: %v", value)
1115 }
1116 }
1117
1118 func TestValueContext_Values__Byte(t *testing.T) {
1119 unitCount := uint32(8)
1120
1121 rawValueOffset := []byte{0, 0, 0, 4}
1122 valueOffset := uint32(4)
1123
1124 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
1125
1126 addressableData := []byte{0, 0, 0, 0}
1127 addressableData = append(addressableData, data...)
1128 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1129
1130 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeByte, TestDefaultByteOrder)
1131
1132 value, err := vc.Values()
1133 log.PanicIf(err)
1134
1135 if reflect.DeepEqual(value, data) != true {
1136 t.Fatalf("Values not correct (bytes): %v", value)
1137 }
1138 }
1139
1140 func TestValueContext_Values__Ascii(t *testing.T) {
1141 unitCount := uint32(8)
1142
1143 rawValueOffset := []byte{0, 0, 0, 4}
1144 valueOffset := uint32(4)
1145
1146 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
1147
1148 addressableData := []byte{0, 0, 0, 0}
1149 addressableData = append(addressableData, data...)
1150 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1151
1152 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeAscii, TestDefaultByteOrder)
1153
1154 value, err := vc.Values()
1155 log.PanicIf(err)
1156
1157 if reflect.DeepEqual(value, "abcdefg") != true {
1158 t.Fatalf("Values not correct (ASCII): [%s]", value)
1159 }
1160 }
1161
1162 func TestValueContext_Values__AsciiNoNul(t *testing.T) {
1163 unitCount := uint32(8)
1164
1165 rawValueOffset := []byte{0, 0, 0, 4}
1166 valueOffset := uint32(4)
1167
1168 data := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
1169
1170 addressableData := []byte{0, 0, 0, 0}
1171 addressableData = append(addressableData, data...)
1172 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1173
1174 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeAsciiNoNul, TestDefaultByteOrder)
1175
1176 value, err := vc.Values()
1177 log.PanicIf(err)
1178
1179 if reflect.DeepEqual(value, "abcdefgh") != true {
1180 t.Fatalf("Values not correct (ASCII no-NUL): [%s]", value)
1181 }
1182 }
1183
1184 func TestValueContext_Values__Short(t *testing.T) {
1185 unitCount := uint32(4)
1186
1187 rawValueOffset := []byte{0, 0, 0, 4}
1188 valueOffset := uint32(4)
1189
1190 data := []byte{0, 1, 0, 2, 0, 3, 0, 4}
1191
1192 addressableData := []byte{0, 0, 0, 0}
1193 addressableData = append(addressableData, data...)
1194 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1195
1196 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeShort, TestDefaultByteOrder)
1197
1198 value, err := vc.Values()
1199 log.PanicIf(err)
1200
1201 if reflect.DeepEqual(value, []uint16{1, 2, 3, 4}) != true {
1202 t.Fatalf("Values not correct (shorts): %v", value)
1203 }
1204 }
1205
1206 func TestValueContext_Values__Long(t *testing.T) {
1207 unitCount := uint32(2)
1208
1209 rawValueOffset := []byte{0, 0, 0, 4}
1210 valueOffset := uint32(4)
1211
1212 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
1213
1214 addressableData := []byte{0, 0, 0, 0}
1215 addressableData = append(addressableData, data...)
1216 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1217
1218 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeLong, TestDefaultByteOrder)
1219
1220 value, err := vc.Values()
1221 log.PanicIf(err)
1222
1223 if reflect.DeepEqual(value, []uint32{1, 2}) != true {
1224 t.Fatalf("Values not correct (longs): %v", value)
1225 }
1226 }
1227
1228 func TestValueContext_Values__Rational(t *testing.T) {
1229 unitCount := uint32(2)
1230
1231 rawValueOffset := []byte{0, 0, 0, 4}
1232 valueOffset := uint32(4)
1233
1234 data := []byte{
1235 0, 0, 0, 1, 0, 0, 0, 2,
1236 0, 0, 0, 3, 0, 0, 0, 4,
1237 }
1238
1239 addressableData := []byte{0, 0, 0, 0}
1240 addressableData = append(addressableData, data...)
1241 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1242
1243 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeRational, TestDefaultByteOrder)
1244
1245 value, err := vc.Values()
1246 log.PanicIf(err)
1247
1248 expected := []Rational{
1249 {Numerator: 1, Denominator: 2},
1250 {Numerator: 3, Denominator: 4},
1251 }
1252
1253 if reflect.DeepEqual(value, expected) != true {
1254 t.Fatalf("Values not correct (rationals): %v", value)
1255 }
1256 }
1257
1258 func TestValueContext_Values__SignedLong(t *testing.T) {
1259 unitCount := uint32(2)
1260
1261 rawValueOffset := []byte{0, 0, 0, 4}
1262 valueOffset := uint32(4)
1263
1264 data := []byte{0, 0, 0, 1, 0, 0, 0, 2}
1265
1266 addressableData := []byte{0, 0, 0, 0}
1267 addressableData = append(addressableData, data...)
1268 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1269
1270 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeSignedLong, TestDefaultByteOrder)
1271
1272 value, err := vc.Values()
1273 log.PanicIf(err)
1274
1275 if reflect.DeepEqual(value, []int32{1, 2}) != true {
1276 t.Fatalf("Values not correct (signed longs): %v", value)
1277 }
1278 }
1279
1280 func TestValueContext_Values__SignedRational(t *testing.T) {
1281 unitCount := uint32(2)
1282
1283 rawValueOffset := []byte{0, 0, 0, 4}
1284 valueOffset := uint32(4)
1285
1286 data := []byte{
1287 0, 0, 0, 1, 0, 0, 0, 2,
1288 0, 0, 0, 3, 0, 0, 0, 4,
1289 }
1290
1291 addressableData := []byte{0, 0, 0, 0}
1292 addressableData = append(addressableData, data...)
1293 sb := rifs.NewSeekableBufferWithBytes(addressableData)
1294
1295 vc := NewValueContext("aa/bb", 0x1234, unitCount, valueOffset, rawValueOffset, sb, TypeSignedRational, TestDefaultByteOrder)
1296
1297 value, err := vc.Values()
1298 log.PanicIf(err)
1299
1300 expected := []SignedRational{
1301 {Numerator: 1, Denominator: 2},
1302 {Numerator: 3, Denominator: 4},
1303 }
1304
1305 if reflect.DeepEqual(value, expected) != true {
1306 t.Fatalf("Values not correct (signed rationals): %v", value)
1307 }
1308 }
1309
View as plain text