Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion field/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func TestBinaryField(t *testing.T) {
bin := NewBinary(spec)
_, err := bin.Pack()

require.EqualError(t, err, "failed to encode length: field length: 0 should be fixed: 10")
require.EqualError(t, err, "failed to encode length: data length: 0 should be fixed: 10")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})
}

Expand Down
19 changes: 13 additions & 6 deletions field/composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,8 @@ func TestCompositePacking(t *testing.T) {
require.NoError(t, err)

_, err = composite.Pack()
require.EqualError(t, err, "failed to pack subfield 1: failed to encode length: field length: 4 should be fixed: 2")
require.EqualError(t, err, "failed to pack subfield 1: failed to encode length: data length: 4 should be fixed: 2")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack returns error when encoded data length is larger than specified fixed max length", func(t *testing.T) {
Expand Down Expand Up @@ -798,7 +799,8 @@ func TestCompositePacking(t *testing.T) {
require.NoError(t, err)

_, err = composite.Pack()
require.EqualError(t, err, "failed to encode length: field length: 6 should be fixed: 4")
require.EqualError(t, err, "failed to encode length: data length: 6 should be fixed: 4")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack correctly serializes data with padded tags to bytes", func(t *testing.T) {
Expand Down Expand Up @@ -948,6 +950,7 @@ func TestCompositePacking(t *testing.T) {
require.Equal(t, 0, read)
require.Error(t, err)
require.EqualError(t, err, "failed to decode length: data length: 7 is larger than maximum 4")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Unpack without error when not all subfields are set", func(t *testing.T) {
Expand Down Expand Up @@ -1100,7 +1103,8 @@ func TestCompositePackingWithTags(t *testing.T) {
b, err := composite.Pack()
require.Nil(t, b)
require.Error(t, err)
require.EqualError(t, err, "failed to encode length: field length: 12 should be fixed: 6")
require.EqualError(t, err, "failed to encode length: data length: 12 should be fixed: 6")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack returns error when encoded data length is larger than specified variable max length", func(t *testing.T) {
Expand Down Expand Up @@ -1143,7 +1147,8 @@ func TestCompositePackingWithTags(t *testing.T) {

b, err := composite.Pack()
require.Nil(t, b)
require.EqualError(t, err, "failed to encode length: field length: 12 is larger than maximum: 8")
require.EqualError(t, err, "failed to encode length: data length: 12 is larger than maximum: 8")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack correctly serializes fully populated data to bytes", func(t *testing.T) {
Expand Down Expand Up @@ -1352,7 +1357,8 @@ func TestCompositePackingWithBitmap(t *testing.T) {
b, err := composite.Pack()
require.Nil(t, b)
require.Error(t, err)
require.EqualError(t, err, "failed to encode length: field length: 14 should be fixed: 20")
require.EqualError(t, err, "failed to encode length: data length: 14 should be fixed: 20")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack returns error when encoded data length is larger than specified variable max length", func(t *testing.T) {
Expand Down Expand Up @@ -1395,7 +1401,8 @@ func TestCompositePackingWithBitmap(t *testing.T) {

b, err := composite.Pack()
require.Nil(t, b)
require.EqualError(t, err, "failed to encode length: field length: 14 is larger than maximum: 5")
require.EqualError(t, err, "failed to encode length: data length: 14 is larger than maximum: 5")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})

t.Run("Pack correctly serializes fully populated data to bytes with default bitmap", func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion field/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func TestHexPack(t *testing.T) {
str := NewHex(spec)
_, err := str.Pack()

require.EqualError(t, err, "failed to encode length: field length: 0 should be fixed: 10")
require.EqualError(t, err, "failed to encode length: data length: 0 should be fixed: 10")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})
}

Expand Down
3 changes: 2 additions & 1 deletion field/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func TestNumericPack(t *testing.T) {
_, err := numeric.Pack()

// zero value for Numeric is 0, so we have default field length 1
require.EqualError(t, err, "failed to encode length: field length: 1 should be fixed: 10")
require.EqualError(t, err, "failed to encode length: data length: 1 should be fixed: 10")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})
}

Expand Down
3 changes: 2 additions & 1 deletion field/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func TestStringPack(t *testing.T) {
str := NewString(spec)
_, err := str.Pack()

require.EqualError(t, err, "failed to encode length: field length: 0 should be fixed: 10")
require.EqualError(t, err, "failed to encode length: data length: 0 should be fixed: 10")
require.True(t, prefix.IsLengthError(err), "error should be a length error")
})
}

Expand Down
24 changes: 18 additions & 6 deletions prefix/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ var ASCII = Prefixers{

func (p *asciiVarPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {
if dataLen > maxLen {
return nil, fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen)
return nil, &LengthError{
fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen),
}
}

if len(strconv.Itoa(dataLen)) > p.Digits {
return nil, fmt.Errorf(numberOfDigitsInLengthExceeds, dataLen, p.Digits)
return nil, &LengthError{
fmt.Errorf(numberOfDigitsInLengthExceeds, dataLen, p.Digits),
}
}

res := fmt.Sprintf("%0*d", p.Digits, dataLen)
Expand All @@ -36,7 +40,9 @@ func (p *asciiVarPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {

func (p *asciiVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error) {
if len(data) < p.Digits {
return 0, 0, fmt.Errorf(notEnoughDataToRead, len(data), p.Digits)
return 0, 0, &LengthError{
fmt.Errorf(notEnoughDataToRead, len(data), p.Digits),
}
}

dataLen, err := strconv.Atoi(string(data[:p.Digits]))
Expand All @@ -46,11 +52,15 @@ func (p *asciiVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, erro

// length should be positive
if dataLen < 0 {
return 0, 0, fmt.Errorf(invalidLength, dataLen)
return 0, 0, &LengthError{
fmt.Errorf(invalidLength, dataLen),
}
}

if dataLen > maxLen {
return 0, 0, fmt.Errorf(dataLengthIsLargerThanMax, dataLen, maxLen)
return 0, 0, &LengthError{
fmt.Errorf(dataLengthIsLargerThanMax, dataLen, maxLen),
}
}

return dataLen, p.Digits, nil
Expand All @@ -65,7 +75,9 @@ type asciiFixedPrefixer struct {

func (p *asciiFixedPrefixer) EncodeLength(fixLen, dataLen int) ([]byte, error) {
if dataLen != fixLen {
return nil, fmt.Errorf(fieldLengthShouldBeFixed, dataLen, fixLen)
return nil, &LengthError{
fmt.Errorf(fieldLengthShouldBeFixed, dataLen, fixLen),
}
}

return []byte{}, nil
Expand Down
8 changes: 6 additions & 2 deletions prefix/ascii_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestAsciiVarPrefixer_EncodeLengthDigitsValidation(t *testing.T) {
_, err := pref.EncodeLength(999, 123)

require.Contains(t, err.Error(), "number of digits in length: 123 exceeds: 2")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestAsciiVarPrefixer_EncodeLengthMaxLengthValidation(t *testing.T) {
Expand All @@ -23,7 +24,8 @@ func TestAsciiVarPrefixer_EncodeLengthMaxLengthValidation(t *testing.T) {

_, err := pref.EncodeLength(20, 22)

require.Contains(t, err.Error(), "field length: 22 is larger than maximum: 20")
require.Contains(t, err.Error(), "data length: 22 is larger than maximum: 20")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestAsciiVarPrefixer_DecodeLengthMaxLengthValidation(t *testing.T) {
Expand All @@ -34,6 +36,7 @@ func TestAsciiVarPrefixer_DecodeLengthMaxLengthValidation(t *testing.T) {
_, _, err := pref.DecodeLength(20, []byte("22"))

require.Contains(t, err.Error(), "not enough data length: 2 to read: 3 byte digits")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestAsciiVarPrefixer_LHelpers(t *testing.T) {
Expand Down Expand Up @@ -96,5 +99,6 @@ func TestAsciiFixedPrefixer_EncodeLengthValidation(t *testing.T) {

_, err := pref.EncodeLength(8, 12)

require.Contains(t, err.Error(), "field length: 12 should be fixed: 8")
require.Contains(t, err.Error(), "data length: 12 should be fixed: 8")
require.True(t, IsLengthError(err), "error should be a length error")
}
20 changes: 15 additions & 5 deletions prefix/bcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ var BCD = Prefixers{

func (p *bcdVarPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {
if dataLen > maxLen {
return nil, fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen)
return nil, &LengthError{
fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen),
}
}

if len(strconv.Itoa(dataLen)) > p.Digits {
return nil, fmt.Errorf(numberOfDigitsInLengthExceeds, dataLen, p.Digits)
return nil, &LengthError{
fmt.Errorf(numberOfDigitsInLengthExceeds, dataLen, p.Digits),
}
}

strLen := fmt.Sprintf("%0*d", p.Digits, dataLen)
Expand All @@ -44,7 +48,9 @@ func (p *bcdVarPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {
func (p *bcdVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error) {
length := bcd.EncodedLen(p.Digits)
if len(data) < length {
return 0, 0, fmt.Errorf(notEnoughDataToRead, length, len(data))
return 0, 0, &LengthError{
fmt.Errorf(notEnoughDataToRead, length, len(data)),
}
}

bDigits, _, err := encoding.BCD.Decode(data[:length], p.Digits)
Expand All @@ -58,7 +64,9 @@ func (p *bcdVarPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error)
}

if dataLen > maxLen {
return 0, 0, fmt.Errorf(dataLengthIsLargerThanMax, dataLen, maxLen)
return 0, 0, &LengthError{
fmt.Errorf(dataLengthIsLargerThanMax, dataLen, maxLen),
}
}

return dataLen, length, nil
Expand All @@ -73,7 +81,9 @@ type bcdFixedPrefixer struct {

func (p *bcdFixedPrefixer) EncodeLength(fixLen, dataLen int) ([]byte, error) {
if dataLen != fixLen {
return nil, fmt.Errorf(fieldLengthShouldBeFixed, dataLen, fixLen)
return nil, &LengthError{
fmt.Errorf(fieldLengthShouldBeFixed, dataLen, fixLen),
}
}

return []byte{}, nil
Expand Down
11 changes: 8 additions & 3 deletions prefix/bcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ func TestBCDVarPrefixer_EncodeLengthDigitsValidation(t *testing.T) {
_, err := BCD.LL.EncodeLength(999, 123)

require.Contains(t, err.Error(), "number of digits in length: 123 exceeds: 2")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestBCDVarPrefixer_EncodeLengthMaxLengthValidation(t *testing.T) {
_, err := BCD.LL.EncodeLength(20, 22)

require.Contains(t, err.Error(), "field length: 22 is larger than maximum: 20")
require.Contains(t, err.Error(), "data length: 22 is larger than maximum: 20")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestBCDVarPrefixer_DecodeLengthMaxLengthValidation(t *testing.T) {
_, _, err := BCD.LLL.DecodeLength(20, []byte{0x22})

require.Contains(t, err.Error(), "not enough data length: 2 to read: 1 byte digits")
require.True(t, IsLengthError(err), "error should be a length error")
}

func TestBCDVarPrefixer_LHelpers(t *testing.T) {
Expand Down Expand Up @@ -85,10 +88,12 @@ func TestBCDFixedPrefixer_EncodeLengthValidation(t *testing.T) {
_, err := pref.EncodeLength(8, 12)

require.Error(t, err)
require.Contains(t, err.Error(), "field length: 12 should be fixed: 8")
require.Contains(t, err.Error(), "data length: 12 should be fixed: 8")
require.True(t, IsLengthError(err), "error should be a length error")

_, err = pref.EncodeLength(8, 6)

require.Error(t, err)
require.Contains(t, err.Error(), "field length: 6 should be fixed: 8")
require.Contains(t, err.Error(), "data length: 6 should be fixed: 8")
require.True(t, IsLengthError(err), "error should be a length error")
}
12 changes: 9 additions & 3 deletions prefix/bertlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (p *berTLVPrefixer) EncodeLength(maxLen, dataLen int) ([]byte, error) {
// checking maxLen for a 0 is a way to disable check and also support
// backwards compatibility with the old contract that didn't have maxLen
if maxLen != 0 && dataLen > maxLen {
return nil, fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen)
return nil, &LengthError{
fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen),
}
}

buf := big.NewInt(int64(dataLen)).Bytes()
Expand Down Expand Up @@ -74,7 +76,9 @@ func (p *berTLVPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error)
// checking maxLen for a 0 is a way to disable check and also support
// backwards compatibility with the old contract that didn't have maxLen
if maxLen != 0 && dataLen > maxLen {
return 0, read, fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen)
return 0, read, &LengthError{
fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen),
}
}

return dataLen, read, nil
Expand All @@ -92,7 +96,9 @@ func (p *berTLVPrefixer) DecodeLength(maxLen int, data []byte) (int, int, error)
// checking maxLen for a 0 is a way to disable check and also support
// backwards compatibility with the old contract that didn't have maxLen
if maxLen != 0 && dataLen > maxLen {
return 0, read, fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen)
return 0, read, &LengthError{
fmt.Errorf(fieldLengthIsLargerThanMax, dataLen, maxLen),
}
}

return dataLen, read, nil
Expand Down
6 changes: 4 additions & 2 deletions prefix/bertlv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ func TestBerTLVPrefixer(t *testing.T) {

t.Run("when maxLen is set EncodeLength returns error if length is larger than maxLen", func(t *testing.T) {
_, err := BerTLV.EncodeLength(2, 3)
require.EqualError(t, err, "field length: 3 is larger than maximum: 2")
require.EqualError(t, err, "data length: 3 is larger than maximum: 2")
require.True(t, IsLengthError(err), "error should be a length error")
})

t.Run("when maxLen is set DecodeLength returns error if length is larger than maxLen", func(t *testing.T) {
_, _, err := BerTLV.DecodeLength(2, []byte{0b10000010, 0b11111110, 0b00001111})
require.EqualError(t, err, "field length: 65039 is larger than maximum: 2")
require.EqualError(t, err, "data length: 65039 is larger than maximum: 2")
require.True(t, IsLengthError(err), "error should be a length error")
})
}

Expand Down
Loading
Loading