-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtlv_test.go
More file actions
444 lines (404 loc) · 14.9 KB
/
Copy pathtlv_test.go
File metadata and controls
444 lines (404 loc) · 14.9 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package bertlv_test
import (
"encoding/hex"
"fmt"
"testing"
"github.com/moov-io/bertlv"
"github.com/stretchr/testify/require"
)
func TestEncodeDecode(t *testing.T) {
// FCI template
data := []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewComposite("BF0C", // FCI Issuer Discretionary Data
bertlv.NewComposite("61", // Application Template
bertlv.NewTag("4F", []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}),
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
bertlv.NewTag("87", []byte{0x01}), // Application Priority Indicator
),
),
),
),
}
encoded, err := bertlv.Encode(data)
require.NoError(t, err)
expectedData := "6F2F840E325041592E5359532E4444463031A51DBF0C1A61184F07A0000000041010500A4D617374657263617264870101"
require.Equal(t, expectedData, fmt.Sprintf("%X", encoded))
decoded, err := bertlv.Decode(encoded)
require.NoError(t, err)
require.Equal(t, data, decoded)
}
func TestFindTag(t *testing.T) {
_, found := bertlv.FindTagByPath([]bertlv.TLV{}, "00")
require.False(t, found)
data := []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewComposite("BF0C", // FCI Issuer Discretionary Data
bertlv.NewComposite("61", // Application Template
bertlv.NewTag("4F", []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}),
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
bertlv.NewTag("87", []byte{0x01}), // Application Priority Indicator
),
),
),
),
}
_, found = bertlv.FindTagByPath(data, "6F")
require.True(t, found)
tag, found := bertlv.FindTagByPath(data, "6F.A5.BF0C.61.4F")
require.True(t, found)
require.Equal(t, []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}, tag.Value)
_, found = bertlv.FindFirstTag(data, "4F")
require.True(t, found)
require.Equal(t, []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}, tag.Value)
}
func TestUnmarshalSuccess(t *testing.T) {
data := []bertlv.TLV{
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("61", // Application Template
bertlv.NewTag("4F", []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}),
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
bertlv.NewTag("87", []byte{0x01}), // Application Priority Indicator
),
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x00, 0x12, 0x34}), // Amount, Authorised (Numeric)
bertlv.NewTag("9F03", []byte("5678")), // Amount, Other (Numeric) in ASCII
}
type EMVData struct {
DedicatedFileName []byte `bertlv:"84"`
ApplicationTemplate struct {
ApplicationID string `bertlv:"4F"`
ApplicationLabel string `bertlv:"50,ascii"`
ApplicationPriorityIndicator []byte `bertlv:"87"`
} `bertlv:"61"`
AmountAuthorized int64 `bertlv:"9F02"`
AmountOther int64 `bertlv:"9F03,ascii"`
}
emvData := &EMVData{}
err := bertlv.Unmarshal(data, emvData)
require.NoError(t, err)
require.Equal(t, []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}, emvData.DedicatedFileName)
require.Equal(t, "A0000000041010", emvData.ApplicationTemplate.ApplicationID)
require.Equal(t, "Mastercard", emvData.ApplicationTemplate.ApplicationLabel)
require.Equal(t, []byte{0x01}, emvData.ApplicationTemplate.ApplicationPriorityIndicator)
require.Equal(t, int64(1234), emvData.AmountAuthorized)
require.Equal(t, int64(5678), emvData.AmountOther)
}
func TestUnmarshalEdgeCases(t *testing.T) {
data := []bertlv.TLV{
bertlv.NewComposite("61"), // empty composite
}
type Nested struct {
Template struct {
Field []byte `bertlv:"4F"`
} `bertlv:"61"`
}
var n Nested
err := bertlv.Unmarshal(data, &n)
require.NoError(t, err)
// Missing tag in data
data = []bertlv.TLV{
bertlv.NewTag("84", []byte{1}),
bertlv.NewComposite("61",
bertlv.NewTag("4F", []byte{2}),
),
}
type MissingTag struct {
Field []byte `bertlv:"99"` // non-existent tag
Field2 []byte // no mapping to tag
}
err = bertlv.Unmarshal(data, &MissingTag{})
require.NoError(t, err) // should skip missing tags
// Nil pointer
var nilPtr *struct {
Field []byte `bertlv:"84"`
}
err = bertlv.Unmarshal(data, nilPtr)
require.Error(t, err)
}
func TestCopyTags(t *testing.T) {
tests := []struct {
name string
input []bertlv.TLV
tags []string
expected []bertlv.TLV
}{
{
name: "Empty input",
input: []bertlv.TLV{},
tags: []string{"9F02"},
expected: nil,
},
{
name: "Single flat tag match",
input: []bertlv.TLV{
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("5A", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}),
},
tags: []string{"9F02"},
expected: []bertlv.TLV{
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
},
},
{
name: "Multiple flat tag matches",
input: []bertlv.TLV{
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("5A", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}),
bertlv.NewTag("57", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xD2, 0x30, 0x12}),
},
tags: []string{"9F02", "57"},
expected: []bertlv.TLV{
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("57", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xD2, 0x30, 0x12}),
},
},
{
name: "No matches",
input: []bertlv.TLV{
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("5A", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}),
},
tags: []string{"9F03", "9F06"},
expected: nil,
},
{
name: "Nested structures - parent tag match",
input: []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
),
),
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
},
tags: []string{"6F"},
expected: []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
),
),
},
},
{
name: "Multiple nested and flat matches",
input: []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
),
),
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("5A", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}),
},
tags: []string{"6F", "9F02"},
expected: []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
),
),
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := bertlv.CopyTags(tc.input, tc.tags...)
require.Equal(t, tc.expected, result)
// Verify it's a deep copy by modifying the original
if len(tc.input) > 0 && len(tc.input[0].Value) > 0 {
// Modify first tag's value in original
tc.input[0].Value[0] = 0xFF
// Result should remain unchanged
if len(result) > 0 && len(result[0].Value) > 0 && result[0].Tag == tc.input[0].Tag {
require.NotEqual(t, tc.input[0].Value[0], result[0].Value[0])
}
}
})
}
}
func BenchmarkCopyTags(b *testing.B) {
input := []bertlv.TLV{
bertlv.NewComposite("6F", // File Control Information (FCI) Template
bertlv.NewTag("84", []byte{0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}),
bertlv.NewComposite("A5", // FCI Proprietary Template
bertlv.NewComposite("BF0C", // FCI Issuer Discretionary Data
bertlv.NewComposite("61", // Application Template
bertlv.NewTag("4F", []byte{0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10}),
bertlv.NewTag("50", []byte{0x4D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x63, 0x61, 0x72, 0x64}),
bertlv.NewTag("87", []byte{0x01}),
),
),
),
),
bertlv.NewTag("9F02", []byte{0x00, 0x00, 0x00, 0x01, 0x23, 0x45}),
bertlv.NewTag("5A", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}),
bertlv.NewTag("57", []byte{0x41, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xD2, 0x30, 0x12}),
bertlv.NewTag("5F20", []byte{0x4A, 0x4F, 0x48, 0x4E, 0x20, 0x44, 0x4F, 0x45}),
bertlv.NewTag("9F1A", []byte{0x08, 0x40}),
bertlv.NewTag("9F1B", []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00}),
bertlv.NewTag("9F1C", []byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38}),
bertlv.NewTag("9F35", []byte{0x22}),
bertlv.NewTag("9F36", []byte{0x00, 0x01}),
}
benchmarks := []struct {
name string
tags []string
matches int // expected number of matches for context
}{
{
name: "No tags matched",
tags: []string{"9F99", "9F98"},
matches: 0,
},
{
name: "One flat tag matched",
tags: []string{"9F02"},
matches: 1,
},
{
name: "Multiple flat tags matched",
tags: []string{"9F02", "5A", "57", "5F20"},
matches: 4,
},
{
name: "Nested composite tag matched",
tags: []string{"6F"},
matches: 1, // But contains a complex nested structure
},
{
name: "Mix of flat and nested tags",
tags: []string{"6F", "9F02", "5A", "57"},
matches: 4,
},
{
name: "All tags matched",
tags: []string{"6F", "9F02", "5A", "57", "5F20", "9F1A", "9F1B", "9F1C", "9F35", "9F36"},
matches: 10,
},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs() // Track allocations
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := bertlv.CopyTags(input, bm.tags...)
// Force compiler to evaluate result to prevent optimization
if len(result) > 100000 {
b.Fatalf("unexpected result length")
}
}
})
}
}
// TestDecodeMalformedLengthDoesNotPanic verifies that Decode returns an error
// (never panics) on inputs whose long-form length field overflows an int.
//
// Regression for a slice-bounds-out-of-range panic: a long-form BER length of up
// to 127 bytes was accumulated into an int without an overflow guard, so a large
// length wrapped to a negative value. The caller's "len(data) < length" bounds
// check then passed (a small non-negative len is not < a negative length) and
// the subsequent data[:length] slice expression panicked.
func TestDecodeMalformedLengthDoesNotPanic(t *testing.T) {
tests := []struct {
name string
data []byte
}{
{
// tag 5A; length byte 0x88 = long form, 8 length-bytes; then 8x0xFF.
// The 8 bytes accumulate to int64(-1), bypassing the bounds check.
name: "8-byte length overflows to negative",
data: []byte{0x5A, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
// Fuzz corpus: nested long-form lengths that previously overflowed.
name: "nested long-form overflow corpus",
data: []byte{0x30, 0x89, 0x30, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
// length byte 0x9C = long form, 0x1C (28) length-bytes: more than 8,
// so it must be rejected outright.
name: "28-byte length field",
data: []byte("0\x9c00000000000000000000\x9c0000000"),
},
{
// Positive-but-too-large length (claims 0xFFFF bytes of value).
name: "length exceeds remaining data",
data: []byte{0x5A, 0x82, 0xFF, 0xFF},
},
{
// High bit set in a 4-byte length: negative on both 32- and 64-bit int.
name: "4-byte length with sign bit set",
data: []byte{0x5A, 0x84, 0x80, 0x00, 0x00, 0x00},
},
{
// BER indefinite length (0x80) is not supported.
name: "indefinite length",
data: []byte{0x5A, 0x80},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NotPanics(t, func() {
_, err := bertlv.Decode(tt.data)
require.Error(t, err)
})
})
}
}
// FuzzDecode ensures Decode never panics on arbitrary input; returning an error
// is the only acceptable failure mode for malformed data. Successful decodes
// are round-tripped through Encode when possible.
func FuzzDecode(f *testing.F) {
// Minimal / edge seeds
f.Add([]byte{})
f.Add([]byte{0x5A, 0x08, 0x41, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11})
f.Add([]byte{0x6F, 0x03, 0x84, 0x01, 0x00}) // composite (recurses)
// Overflow / malformed length seeds (regression)
f.Add([]byte{0x5A, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
f.Add([]byte{0x30, 0x89, 0x30, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00})
f.Add([]byte("0\x9c00000000000000000000\x9c0000000"))
f.Add([]byte{0x5A, 0x80}) // indefinite length
f.Add([]byte{0x5A, 0x84, 0x80, 0x00, 0x00, 0x00})
// Real-world EMV samples (from library tests / payment processing)
for _, h := range []string{
// EMV FCI with AID, label, PDOL (optimization_test.go simpleEMVData)
"6F468407A0000000031010A53B500B56495341204352454449548701015F2D02656E9F38189F66049F02069F03069F1A0295055F2A029A039C019F3704BF0C089F5A051108400840",
// PSE FCI with Mastercard AID
"6F2F840E325041592E5359532E4444463031A51DBF0C1A61184F07A0000000041010500A4D617374657263617264870101",
} {
if raw, err := hex.DecodeString(h); err == nil {
f.Add(raw)
}
}
// Common EMV tags
f.Add([]byte{0x9F, 0x02, 0x06, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34})
f.Add([]byte{0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x04, 0x10, 0x10})
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) > 16*1024 {
t.Skip()
}
tlvs, err := bertlv.Decode(data)
if err != nil {
return
}
// Round-trip: Encode then Decode again must not panic.
encoded, err := bertlv.Encode(tlvs)
if err != nil {
return
}
_, _ = bertlv.Decode(encoded)
_ = bertlv.BuildTagMap(tlvs)
_, _ = bertlv.FindFirstTag(tlvs, "4F")
_, _ = bertlv.FindTagByPath(tlvs, "6F")
})
}