Skip to content

Commit

Permalink
Merge pull request #7 from mercadolibre/feature/skip-unexpected-tlv-tags
Browse files Browse the repository at this point in the history
Feature/skip unexpected tlv tags
  • Loading branch information
milandaji authored Feb 10, 2023
2 parents aff45d7 + 08a5ffd commit 8a849fd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 12 additions & 2 deletions field/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/mercadolibre/iso8583/encoding"
"github.com/mercadolibre/iso8583/prefix"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -342,7 +344,7 @@ func (f *Composite) UnmarshalJSON(b []byte) error {
}

for tag, rawMsg := range data {
if _, ok := f.spec.Subfields[tag]; !ok {
if _, ok := f.spec.Subfields[tag]; !ok && !f.spec.Tag.SkipUnknownTLVTags {
return fmt.Errorf("failed to unmarshal subfield %v: received subfield not defined in spec", tag)
}

Expand Down Expand Up @@ -491,12 +493,20 @@ func (f *Composite) unpackSubfieldsByTag(data []byte) (int, error) {
tagBytes = f.spec.Tag.Pad.Unpad(tagBytes)
}
tag := string(tagBytes)
if _, ok := f.spec.Subfields[tag]; !ok {
if _, ok := f.spec.Subfields[tag]; !ok && !f.spec.Tag.SkipUnknownTLVTags {
return 0, fmt.Errorf("failed to unpack subfield %v: field not defined in Spec", tag)
}

field, ok := f.subfields[tag]
if !ok {
// Obtain the length of the unknown tag and add it to the offset.
if f.spec.Tag.SkipUnknownTLVTags && f.spec.Tag.Enc == encoding.BerTLVTag {
fieldLength, readed, err := prefix.BerTLV.DecodeLength(999, data[offset:])
if err != nil {
return 0, err
}
offset += fieldLength + readed
}
continue
}

Expand Down
28 changes: 28 additions & 0 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,24 @@ func TestTLVPacking(t *testing.T) {
require.Equal(t, "000000000501", data.F9F02.Value())
})

t.Run("Unpack correctly deserialises bytes to the data struct skipping unexpected tags", func(t *testing.T) {
spec := *tlvTestSpec
spec.Tag.SkipUnknownTLVTags = true
composite := NewComposite(tlvTestSpec)

// Data contains tags 9F36, 9A, 9F02 and 9F37
read, err := composite.Unpack([]byte{0x30, 0x32, 0x36, 0x9f, 0x36, 0x2, 0x1, 0x57, 0x9a, 0x3, 0x21, 0x7, 0x20,
0x9f, 0x2, 0x6, 0x0, 0x0, 0x0, 0x0, 0x5, 0x1, 0x9f, 0x37, 0x4, 0x9b, 0xad, 0xbc, 0xab})
require.NoError(t, err)
require.Equal(t, 29, read)

data := &TLVTestData{}
require.NoError(t, composite.Unmarshal(data))

require.Equal(t, "210720", data.F9A.Value())
require.Equal(t, "000000000501", data.F9F02.Value())
})

t.Run("Pack correctly serializes data to bytes (constructed ber-tlv)", func(t *testing.T) {
data := &ConstructedTLVTestData{
F82: NewStringValue("017f"),
Expand Down Expand Up @@ -399,6 +417,16 @@ func TestTLVPacking(t *testing.T) {
require.Equal(t, "027F", data.F9F36.Value())
require.Equal(t, "047F", data.F9F3B.F9F45.Value())
})

t.Run("Unpack throws an error due unexpected tags", func(t *testing.T) {
spec := *tlvTestSpec
spec.Tag.SkipUnknownTLVTags = false
composite := NewComposite(tlvTestSpec)

_, err := composite.Unpack([]byte{0x30, 0x32, 0x36, 0x9f, 0x36, 0x2, 0x1, 0x57, 0x9a, 0x3, 0x21, 0x7, 0x20,
0x9f, 0x2, 0x6, 0x0, 0x0, 0x0, 0x0, 0x5, 0x1, 0x9f, 0x37, 0x4, 0x9b, 0xad, 0xbc, 0xab})
require.EqualError(t, err, "failed to unpack subfield 9F36: field not defined in Spec")
})
}

func TestCompositePacking(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions field/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type TagSpec struct {
// spec must be packed. This ordering may also be used for unpacking
// if Spec.Tag.Enc == nil.
Sort sort.StringSlice
// SkipUnknownTLVTags is a flag which indicates whether TLV tags that are not found in
// the spec should be skipped or throw an error.
// This flag is only meant to be used in Composite fields with TLV encoding.
SkipUnknownTLVTags bool
}

// Spec defines the structure of a field.
Expand Down

0 comments on commit 8a849fd

Please sign in to comment.