From 98b3d60a121982aff93268a349c0b775b53cb5eb Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Sat, 5 May 2018 15:15:17 -0400 Subject: [PATCH] type: Added encode and decode tests for all types but undefined. - Updated `(TagType).Encode()` to use the byte-order it already has. - TODO: Add tests for TypeUndefined. --- type.go | 4 +- type_test.go | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 type_test.go diff --git a/type.go b/type.go index c1ba776..eb5e131 100644 --- a/type.go +++ b/type.go @@ -725,14 +725,14 @@ func (tt TagType) Resolve(valueContext ValueContext) (value interface{}, err err } // Encode knows how to encode the given value to a byte slice. -func (tt TagType) Encode(byteOrder binary.ByteOrder, value interface{}) (encoded []byte, err error) { +func (tt TagType) Encode(value interface{}) (encoded []byte, err error) { defer func() { if state := recover(); state != nil { err = log.Wrap(state.(error)) } }() - ve := NewValueEncoder(byteOrder) + ve := NewValueEncoder(tt.byteOrder) ed, err := ve.EncodeWithType(tt, value) log.PanicIf(err) diff --git a/type_test.go b/type_test.go new file mode 100644 index 0000000..7769a26 --- /dev/null +++ b/type_test.go @@ -0,0 +1,178 @@ +package exif + +import ( + "testing" + "bytes" + "fmt" + "reflect" + + "github.com/dsoprea/go-logging" +) + +func Test_TagType_EncodeDecode_Byte(t *testing.T) { + tt := NewTagType(TypeByte, TestDefaultByteOrder) + + data := []byte { 0x11, 0x22, 0x33, 0x44, 0x55 } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, data) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseBytes(encoded, uint32(len(data))) + log.PanicIf(err) + + if bytes.Compare(restored, data) != 0 { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_Ascii(t *testing.T) { + tt := NewTagType(TypeAscii, TestDefaultByteOrder) + + data := "hello" + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if string(encoded) != fmt.Sprintf("%s\000", data) { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseAscii(encoded, uint32(len(data))) + log.PanicIf(err) + + if restored != data { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_Shorts(t *testing.T) { + tt := NewTagType(TypeShort, TestDefaultByteOrder) + + data := []uint16 { 0x11, 0x22, 0x33 } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, []byte { 0x00, 0x11, 0x00, 0x22, 0x00, 0x33 }) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseShorts(encoded, uint32(len(data))) + log.PanicIf(err) + + if reflect.DeepEqual(restored, data) != true { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_Long(t *testing.T) { + tt := NewTagType(TypeLong, TestDefaultByteOrder) + + data := []uint32 { 0x11, 0x22, 0x33 } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33 }) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseLongs(encoded, uint32(len(data))) + log.PanicIf(err) + + if reflect.DeepEqual(restored, data) != true { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_Rational(t *testing.T) { + tt := NewTagType(TypeRational, TestDefaultByteOrder) + + data := []Rational { + Rational{ Numerator: 0x11, Denominator: 0x22 }, + Rational{ Numerator: 0x33, Denominator: 0x44 }, + } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x44 }) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseRationals(encoded, uint32(len(data))) + log.PanicIf(err) + + if reflect.DeepEqual(restored, data) != true { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_SignedLong(t *testing.T) { + tt := NewTagType(TypeSignedLong, TestDefaultByteOrder) + + data := []int32 { 0x11, 0x22, 0x33 } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33 }) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseSignedLongs(encoded, uint32(len(data))) + log.PanicIf(err) + + if reflect.DeepEqual(restored, data) != true { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_SignedRational(t *testing.T) { + tt := NewTagType(TypeSignedRational, TestDefaultByteOrder) + + data := []SignedRational { + SignedRational{ Numerator: 0x11, Denominator: 0x22 }, + SignedRational{ Numerator: 0x33, Denominator: 0x44 }, + } + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if bytes.Compare(encoded, []byte { 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x44 }) != 0 { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseSignedRationals(encoded, uint32(len(data))) + log.PanicIf(err) + + if reflect.DeepEqual(restored, data) != true { + t.Fatalf("Data not decoded correctly.") + } +} + +func Test_TagType_EncodeDecode_AsciiNoNul(t *testing.T) { + tt := NewTagType(TypeAsciiNoNul, TestDefaultByteOrder) + + data := "hello" + + encoded, err := tt.Encode(data) + log.PanicIf(err) + + if string(encoded) != data { + t.Fatalf("Data not encoded correctly.") + } + + restored, err := tt.ParseAsciiNoNul(encoded, uint32(len(data))) + log.PanicIf(err) + + if restored != data { + t.Fatalf("Data not decoded correctly.") + } +} + +// TODO(dustin): Add tests for TypeUndefined.