From 7d26e69f8d17abfba4c11582e3e5aca56f8e4e4b Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Sat, 11 Jan 2020 04:34:25 -0500 Subject: [PATCH] Add tested for undefined-tag types --- v2/undefined/exif_8828_oecf.go | 2 +- v2/undefined/exif_8828_oecf_test.go | 88 +++++++++++++++++++ v2/undefined/exif_9000_exif_version_test.go | 64 ++++++++++++++ .../exif_9101_components_configuration.go | 4 +- ...exif_9101_components_configuration_test.go | 82 +++++++++++++++++ 5 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 v2/undefined/exif_8828_oecf_test.go create mode 100644 v2/undefined/exif_9000_exif_version_test.go create mode 100644 v2/undefined/exif_9101_components_configuration_test.go diff --git a/v2/undefined/exif_8828_oecf.go b/v2/undefined/exif_8828_oecf.go index 1ac9e7a..8865d6f 100644 --- a/v2/undefined/exif_8828_oecf.go +++ b/v2/undefined/exif_8828_oecf.go @@ -95,7 +95,7 @@ func (Codec8828Oecf) Decode(valueContext *exifcommon.ValueContext) (value Encode startAt := 4 // offset is our current position. - offset := 4 + offset := startAt currentColumnNumber := uint16(0) diff --git a/v2/undefined/exif_8828_oecf_test.go b/v2/undefined/exif_8828_oecf_test.go new file mode 100644 index 0000000..2e36b3e --- /dev/null +++ b/v2/undefined/exif_8828_oecf_test.go @@ -0,0 +1,88 @@ +package exifundefined + +import ( + "bytes" + "reflect" + "testing" + + "github.com/dsoprea/go-logging" + + "github.com/dsoprea/go-exif/v2/common" +) + +func TestTag8828Oecf_String(t *testing.T) { + ut := Tag8828Oecf{ + Columns: 11, + Rows: 22, + } + + s := ut.String() + + if s != "Tag8828Oecf" { + t.Fatalf("String not correct: [%s]", s) + } +} + +func TestCodec8828Oecf_Encode(t *testing.T) { + ut := Tag8828Oecf{ + Columns: 2, + Rows: 22, + ColumnNames: []string{"aa", "bb"}, + Values: []exifcommon.SignedRational{exifcommon.SignedRational{11, 22}}, + } + + codec := Codec8828Oecf{} + + encoded, unitCount, err := codec.Encode(ut, exifcommon.TestDefaultByteOrder) + log.PanicIf(err) + + expectedBytes := []byte{ + 0x00, 0x02, + 0x00, 0x16, + 0x61, 0x61, 0x00, 0x62, 0x62, 0x00, + 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x16} + + if bytes.Equal(encoded, expectedBytes) != true { + exifcommon.DumpBytesClause(encoded) + + t.Fatalf("Encoded bytes not correct.") + } else if unitCount != 18 { + t.Fatalf("Unit-count not correct: (%d)", unitCount) + } +} + +func TestCodec8828Oecf_Decode(t *testing.T) { + encoded := []byte{ + 0x00, 0x02, + 0x00, 0x16, + 0x61, 0x61, 0x00, 0x62, 0x62, 0x00, + 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x16} + + addressableData := encoded + + valueContext := exifcommon.NewValueContext( + "", + 0, + uint32(len(encoded)), + 0, + nil, + addressableData, + exifcommon.TypeUndefined, + exifcommon.TestDefaultByteOrder) + + codec := Codec8828Oecf{} + + value, err := codec.Decode(valueContext) + log.PanicIf(err) + + expectedValue := Tag8828Oecf{ + Columns: 2, + Rows: 22, + ColumnNames: []string{"aa", "bb"}, + Values: []exifcommon.SignedRational{exifcommon.SignedRational{11, 22}}, + } + + if reflect.DeepEqual(value, expectedValue) != true { + t.Fatalf("Decoded value not correct: %s", value) + } +} diff --git a/v2/undefined/exif_9000_exif_version_test.go b/v2/undefined/exif_9000_exif_version_test.go new file mode 100644 index 0000000..08ac0ac --- /dev/null +++ b/v2/undefined/exif_9000_exif_version_test.go @@ -0,0 +1,64 @@ +package exifundefined + +import ( + "bytes" + "reflect" + "testing" + + "github.com/dsoprea/go-logging" + + "github.com/dsoprea/go-exif/v2/common" +) + +func TestTag9000ExifVersion_String(t *testing.T) { + ut := Tag9000ExifVersion{"abc"} + s := ut.String() + + if s != "abc" { + t.Fatalf("String not correct: [%s]", s) + } +} + +func TestCodec9000ExifVersion_Encode(t *testing.T) { + s := "abc" + ut := Tag9000ExifVersion{s} + + codec := Codec9000ExifVersion{} + + encoded, unitCount, err := codec.Encode(ut, exifcommon.TestDefaultByteOrder) + log.PanicIf(err) + + if bytes.Equal(encoded, []byte(s)) != true { + t.Fatalf("Encoded bytes not correct: %v", encoded) + } else if unitCount != uint32(len(s)) { + t.Fatalf("Unit-count not correct: (%d)", unitCount) + } +} + +func TestCodec9000ExifVersion_Decode(t *testing.T) { + s := "abc" + ut := Tag9000ExifVersion{s} + + encoded := []byte(s) + + rawValueOffset := encoded + + valueContext := exifcommon.NewValueContext( + "", + 0, + uint32(len(encoded)), + 0, + rawValueOffset, + nil, + exifcommon.TypeUndefined, + exifcommon.TestDefaultByteOrder) + + codec := Codec9000ExifVersion{} + + value, err := codec.Decode(valueContext) + log.PanicIf(err) + + if reflect.DeepEqual(value, ut) != true { + t.Fatalf("Decoded value not correct: %s\n", value) + } +} diff --git a/v2/undefined/exif_9101_components_configuration.go b/v2/undefined/exif_9101_components_configuration.go index 3b3f9bc..d352ec2 100644 --- a/v2/undefined/exif_9101_components_configuration.go +++ b/v2/undefined/exif_9101_components_configuration.go @@ -78,8 +78,6 @@ func (CodecExif9101ComponentsConfiguration) Encode(value interface{}, byteOrder log.Panicf("can only encode a TagExif9101ComponentsConfiguration") } - // TODO(dustin): Confirm this size against the specification. - return cc.ConfigurationBytes, uint32(len(cc.ConfigurationBytes)), nil } @@ -96,7 +94,7 @@ func (CodecExif9101ComponentsConfiguration) Decode(valueContext *exifcommon.Valu log.PanicIf(err) for configurationId, configurationBytes := range TagUndefinedType_9101_ComponentsConfiguration_Configurations { - if bytes.Compare(valueBytes, configurationBytes) == 0 { + if bytes.Equal(configurationBytes, valueBytes) == true { cc := TagExif9101ComponentsConfiguration{ ConfigurationId: configurationId, ConfigurationBytes: valueBytes, diff --git a/v2/undefined/exif_9101_components_configuration_test.go b/v2/undefined/exif_9101_components_configuration_test.go new file mode 100644 index 0000000..00876bf --- /dev/null +++ b/v2/undefined/exif_9101_components_configuration_test.go @@ -0,0 +1,82 @@ +package exifundefined + +import ( + "bytes" + "reflect" + "testing" + + "github.com/dsoprea/go-logging" + + "github.com/dsoprea/go-exif/v2/common" +) + +func TestTagExif9101ComponentsConfiguration_String(t *testing.T) { + ut := TagExif9101ComponentsConfiguration{ + ConfigurationId: TagUndefinedType_9101_ComponentsConfiguration_RGB, + ConfigurationBytes: []byte{0x11, 0x22, 0x33, 0x44}, + } + + s := ut.String() + + if s != "Exif9101ComponentsConfiguration" { + t.Fatalf("String not correct: [%s]", s) + } +} + +func TestCodecExif9101ComponentsConfiguration_Encode(t *testing.T) { + configurationBytes := []byte(TagUndefinedType_9101_ComponentsConfiguration_Names[TagUndefinedType_9101_ComponentsConfiguration_RGB]) + + ut := TagExif9101ComponentsConfiguration{ + ConfigurationId: TagUndefinedType_9101_ComponentsConfiguration_RGB, + ConfigurationBytes: configurationBytes, + } + + codec := CodecExif9101ComponentsConfiguration{} + + encoded, unitCount, err := codec.Encode(ut, exifcommon.TestDefaultByteOrder) + log.PanicIf(err) + + if bytes.Equal(encoded, configurationBytes) != true { + exifcommon.DumpBytesClause(encoded) + + t.Fatalf("Encoded bytes not correct: %v", encoded) + } else if unitCount != uint32(len(configurationBytes)) { + t.Fatalf("Unit-count not correct: (%d)", unitCount) + } + + s := string(configurationBytes) + + if s != TagUndefinedType_9101_ComponentsConfiguration_Names[TagUndefinedType_9101_ComponentsConfiguration_RGB] { + t.Fatalf("Recovered configuration name not correct: [%s]", s) + } +} + +func TestCodecExif9101ComponentsConfiguration_Decode(t *testing.T) { + configurationBytes := TagUndefinedType_9101_ComponentsConfiguration_Configurations[TagUndefinedType_9101_ComponentsConfiguration_RGB] + + ut := TagExif9101ComponentsConfiguration{ + ConfigurationId: TagUndefinedType_9101_ComponentsConfiguration_RGB, + ConfigurationBytes: configurationBytes, + } + + rawValueOffset := configurationBytes + + valueContext := exifcommon.NewValueContext( + "", + 0, + uint32(len(configurationBytes)), + 0, + rawValueOffset, + nil, + exifcommon.TypeUndefined, + exifcommon.TestDefaultByteOrder) + + codec := CodecExif9101ComponentsConfiguration{} + + value, err := codec.Decode(valueContext) + log.PanicIf(err) + + if reflect.DeepEqual(value, ut) != true { + t.Fatalf("Decoded value not correct: %s != %s\n", value, ut) + } +}