forked from moov-io/iso8583
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbertlv_test.go
76 lines (65 loc) · 2.16 KB
/
bertlv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package encoding
import (
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestBerTLVTag(t *testing.T) {
tests := []struct {
desc string
numBytes int
hexTag []byte
asciiTag []byte
}{
{"PAN (single byte tag)", 1, []byte{0x5A}, []byte("5A")},
{"CVM List (single byte tag)", 1, []byte{0x8E}, []byte("8E")},
{"Acquirer Identifier (two byte tag)", 2, []byte{0x5F, 0x2A}, []byte("5F2A")},
{"BIC (two byte tag)", 2, []byte{0x5F, 0x54}, []byte("5F54")},
{"Authorized Amount", 2, []byte{0x9F, 0x02}, []byte("9F02")},
{"ATC Register (two byte tag)", 2, []byte{0x9F, 0x13}, []byte("9F13")},
{"Imaginary three byte tag", 3, []byte{0x9F, 0xA8, 0x13}, []byte("9FA813")},
}
for _, tt := range tests {
t.Run(tt.desc+"_Decode", func(t *testing.T) {
asciiTag, read, err := BerTLVTag.Decode(tt.hexTag, 0)
require.NoError(t, err)
require.Equal(t, tt.asciiTag, asciiTag)
require.Equal(t, tt.numBytes, read)
})
}
for _, tt := range tests {
t.Run(tt.desc+"_Encode", func(t *testing.T) {
hexTag, err := BerTLVTag.Encode(tt.asciiTag)
require.NoError(t, err)
require.Equal(t, tt.hexTag, hexTag)
})
}
}
func TestBerTLVTag_DecodeOnInvalidInput(t *testing.T) {
t.Run("when bytes are nil", func(t *testing.T) {
_, _, err := BerTLVTag.Decode(nil, 0)
require.EqualError(t, err, "failed to read byte")
require.ErrorIs(t, err, io.EOF)
})
t.Run("when bytes are empty", func(t *testing.T) {
_, _, err := BerTLVTag.Decode([]byte{}, 0)
require.EqualError(t, err, "failed to read byte")
require.ErrorIs(t, err, io.EOF)
})
t.Run("when bits 5-1 of first byte set but 2nd byte does not exist", func(t *testing.T) {
_, _, err := BerTLVTag.Decode([]byte{0x5F}, 0)
require.EqualError(t, err, "failed to decode TLV tag")
require.ErrorIs(t, err, io.EOF)
})
t.Run("when MSB of 2nd byte set but 3rd byte does not exist", func(t *testing.T) {
_, _, err := BerTLVTag.Decode([]byte{0x5F, 0xA8}, 0)
require.EqualError(t, err, "failed to decode TLV tag")
require.ErrorIs(t, err, io.EOF)
})
}
func FuzzDecodeBerTLV(f *testing.F) {
enc := &berTLVEncoderTag{}
f.Fuzz(func(t *testing.T, data []byte, length int) {
enc.Decode(data, length)
})
}