forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassid_test.go
673 lines (555 loc) · 16 KB
/
classid_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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/binary"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClassID_MarshalCBOR_UUID(t *testing.T) {
tv := MustNewUUIDClassID(TestUUID)
// 37(h'31FB5ABF023E4992AA4E95F9C1503BFA')
// tag(37): d8 25
expected := MustHexDecode(t, "d8255031fb5abf023e4992aa4e95f9c1503bfa")
actual, err := tv.MarshalCBOR()
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestClassID_MarshalCBOR_ImplID(t *testing.T) {
tv := MustNewImplIDClassID(TestImplID)
// 600 (h'61636D652D696D706C656D656E746174696F6E2D69642D303030303030303031')
// tag(600): d9 0258
expected := MustHexDecode(t, "d90258582061636d652d696d706c656d656e746174696f6e2d69642d303030303030303031")
actual, err := tv.MarshalCBOR()
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestClassID_MarshalCBOR_Empty(t *testing.T) {
var tv ClassID
// null (primitive 22)
expected := MustHexDecode(t, "f6")
actual, err := tv.MarshalCBOR()
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestClassID_UnmarshalCBOR_UUID_OK(t *testing.T) {
tv := MustHexDecode(t, "d8255031fb5abf023e4992aa4e95f9c1503bfa")
var actual ClassID
err := actual.UnmarshalCBOR(tv)
assert.Nil(t, err)
assert.Equal(t, "uuid", actual.Type())
assert.Equal(t, TestUUIDString, actual.String())
}
func TestClassID_UnmarshalCBOR_ImplID_OK(t *testing.T) {
tv := MustHexDecode(t, "d90258582061636d652d696d706c656d656e746174696f6e2d69642d303030303030303031")
expected := b64TestImplID()
var actual ClassID
err := actual.UnmarshalCBOR(tv)
assert.Nil(t, err)
assert.Equal(t, "psa.impl-id", actual.Type())
assert.Equal(t, expected, actual.String())
}
func TestClassID_UnmarshalCBOR_badInput(t *testing.T) {
// raw, no tag
hex := "582061636d652d696d706c656d656e746174696f6e2d69642d303030303030303031"
tv := MustHexDecode(t, hex)
var actual ClassID
err := actual.UnmarshalCBOR(tv)
assert.EqualError(t, err, "cbor: cannot unmarshal byte string into Go value of type comid.IClassIDValue")
}
func TestClassID_UnmarshalJSON_UUID(t *testing.T) {
tvFmt := `{ "type": "uuid", "value": "%s" }`
tv := fmt.Sprintf(tvFmt, TestUUIDString)
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.Nil(t, err)
assert.Equal(t, "uuid", actual.Type())
assert.Equal(t, TestUUIDString, actual.String())
}
func TestClassID_UnmarshalJSON_ImplID(t *testing.T) {
b64ImplID := b64TestImplID()
tvFmt := `{ "type": "psa.impl-id", "value": "%s" }`
// in JSON, impl-id is base64 encoded
tv := fmt.Sprintf(tvFmt, b64ImplID)
expected := b64ImplID
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.Nil(t, err)
assert.Equal(t, "psa.impl-id", actual.Type())
// the returned string is the base64 encoding of the stored binary
assert.Equal(t, expected, actual.String())
}
func TestClassID_UnmarshalJSON_badInput_unknown_type(t *testing.T) {
tv := `{ "type": "FOOBAR", "value": "1234567890" }`
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.EqualError(t, err, "unknown class id type: FOOBAR")
assert.Equal(t, "", actual.Type())
}
func TestClassID_UnmarshalJSON_badInput_missing_value(t *testing.T) {
tv := `{ "type": "psa.impl-id" }`
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.EqualError(t, err, "class id decoding failure: no value provided for psa.impl-id")
assert.Equal(t, "", actual.Type())
}
func TestClassID_UnmarshalJSON_badInput_empty_value(t *testing.T) {
tv := `{ "type": "psa.impl-id", "value": "" }`
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.EqualError(t, err, "cannot unmarshal class id: bad psa.impl-id: decoded 0 bytes, want 32")
assert.Equal(t, "", actual.Type())
}
func TestClassID_UnmarshalJSON_badInput_badly_encoded_ImplID_value(t *testing.T) {
tv := `{ "type": "psa.impl-id", "value": ";" }`
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.EqualError(t, err, "cannot unmarshal class id: illegal base64 data at input byte 0")
assert.Equal(t, "", actual.Type())
}
func TestClassID_UnmarshalJSON_badInput_badly_encoded_UUID_value(t *testing.T) {
tv := `{ "type": "uuid", "value": "abcd-1234" }`
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv))
assert.EqualError(t, err, "cannot unmarshal class id: bad UUID: invalid UUID length: 9")
assert.Equal(t, "", actual.Type())
}
func TestClassID_SetOID_ok(t *testing.T) {
tvs := []string{
"1.2.3",
"1.2.3.4",
"1.2.3.4.5",
"1.2.3.4.5.6",
"1.2.3.4.5.6.7",
"1.2.3.4.5.6.7.8",
"1.2.3.4.5.6.7.8.9",
"1.2.3.4.5.6.7.8.9.10",
"1.2.3.4.5.6.7.8.9.10.11",
"1.2.3.4.5.6.7.8.9.10.11.12",
"1.2.3.4.5.6.7.8.9.10.11.12.13",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20",
}
for _, tv := range tvs {
c := MustNewOIDClassID(tv)
assert.Equal(t, tv, c.String())
}
}
func TestClassID_SetOID_GetOID_ok(t *testing.T) {
tvs := []string{
"1.2.3",
"1.2.3.4",
"1.2.3.4.5",
"1.2.3.4.5.6",
"1.2.3.4.5.6.7",
"1.2.3.4.5.6.7.8",
"1.2.3.4.5.6.7.8.9",
"1.2.3.4.5.6.7.8.9.10",
"1.2.3.4.5.6.7.8.9.10.11",
"1.2.3.4.5.6.7.8.9.10.11.12",
"1.2.3.4.5.6.7.8.9.10.11.12.13",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19",
"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20",
}
for _, tv := range tvs {
c := &ClassID{}
err := c.SetOID(tv)
require.Nil(t, err)
out, err := c.GetOID()
require.NoError(t, err)
assert.Equal(t, tv, out)
}
}
func TestClassID_SetOID_NOK(t *testing.T) {
tvs := []struct {
desc string
input string
expectedErr string
}{
{
desc: "empyt OID",
input: "",
expectedErr: "empty OID",
},
{
desc: "too little OID",
input: "1",
expectedErr: "invalid OID: got 1 arcs, expecting at least 3",
},
{
desc: "still too little OID",
input: "1.2",
expectedErr: "invalid OID: got 2 arcs, expecting at least 3",
},
{
desc: "negative arc",
input: "1.2.-3",
expectedErr: "invalid OID: negative arc -3 not allowed",
},
{
desc: "not absolute",
input: ".1.2.3",
expectedErr: "OID must be absolute",
},
{
desc: "not dotted decimal",
input: "iso(1) org(3) dod(6) iana(1)",
expectedErr: `invalid OID: strconv.Atoi: parsing "iso(1) org(3) dod(6) iana(1)": invalid syntax`,
},
{
desc: "invalid format",
input: "1...",
expectedErr: `invalid OID: strconv.Atoi: parsing "": invalid syntax`,
},
{
desc: "invalid format, no digits",
input: "1...",
expectedErr: `invalid OID: strconv.Atoi: parsing "": invalid syntax`,
},
}
for _, tv := range tvs {
c := &ClassID{}
err := c.SetOID(tv.input)
assert.NotNil(t, err)
assert.EqualError(t, err, tv.expectedErr)
}
}
func TestClassID_SetOID_bad(t *testing.T) {
tvs := []string{
"", // empty
"1", // too little
"1.2", // still too little
"1.2.-3", // negative arc
".1.2.3", // not absolute
"iso(1) org(3) dod(6) iana(1)", // not dotted decimal
"1...",
"a.b.c",
}
for _, tv := range tvs {
c, err := NewOIDClassID(tv)
assert.NotNil(t, err)
assert.Nil(t, c)
}
}
func TestClassID_SetUUID_GetUUID_OK(t *testing.T) {
class := &ClassID{}
class = class.SetUUID(TestUUID)
require.NotNil(t, class)
uuid, err := class.GetUUID()
require.NoError(t, err)
assert.Equal(t, TestUUID, uuid)
}
func Test_NewImplIDClassID(t *testing.T) {
classID, err := NewImplIDClassID(nil)
expected := [32]byte{}
require.NoError(t, err)
assert.Equal(t, expected[:], classID.Bytes())
taggedImplID := TaggedImplID(TestImplID)
for _, v := range []any{
TestImplID,
&TestImplID,
taggedImplID,
&taggedImplID,
taggedImplID.Bytes(),
} {
classID, err = NewImplIDClassID(v)
require.NoError(t, err)
assert.Equal(t, taggedImplID.Bytes(), classID.Bytes())
}
expected = [32]byte{
0x61, 0x63, 0x6d, 0x65, 0x2d, 0x69, 0x6d, 0x70,
0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2d, 0x69, 0x64, 0x2d, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31,
}
classID, err = NewImplIDClassID("YWNtZS1pbXBsZW1lbnRhdGlvbi1pZC0wMDAwMDAwMDE=")
require.NoError(t, err)
assert.Equal(t, expected[:], classID.Bytes())
_, err = NewImplIDClassID(7)
assert.EqualError(t, err, "unexpected type for psa.impl-id: int")
}
func Test_NewUUIDClassID(t *testing.T) {
classID, err := NewUUIDClassID(nil)
expected := [16]byte{}
require.NoError(t, err)
assert.Equal(t, expected[:], classID.Bytes())
taggedUUID := TaggedUUID(TestUUID)
for _, v := range []any{
TestUUID,
&TestUUID,
taggedUUID,
&taggedUUID,
taggedUUID.Bytes(),
} {
classID, err = NewUUIDClassID(v)
require.NoError(t, err)
assert.Equal(t, taggedUUID.Bytes(), classID.Bytes())
}
classID, err = NewUUIDClassID(taggedUUID.String())
require.NoError(t, err)
assert.Equal(t, taggedUUID.Bytes(), classID.Bytes())
}
func Test_NewOIDClassID(t *testing.T) {
classID, err := NewOIDClassID(nil)
expected := []byte{}
require.NoError(t, err)
assert.Equal(t, expected, classID.Bytes())
var oid OID
require.NoError(t, oid.FromString(TestOID))
taggedOID := TaggedOID(oid)
for _, v := range []any{
TestOID,
oid,
&oid,
taggedOID,
&taggedOID,
taggedOID.Bytes(),
} {
classID, err = NewOIDClassID(v)
require.NoError(t, err)
expected := taggedOID.Bytes()
got := classID.Bytes()
assert.Equal(t, expected, got)
}
classID, err = NewOIDClassID(taggedOID.String())
require.NoError(t, err)
assert.Equal(t, taggedOID.Bytes(), classID.Bytes())
}
func Test_NewIntClassID(t *testing.T) {
classID, err := NewIntClassID(nil)
require.NoError(t, err)
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, classID.Bytes())
testInt := 7
testInt64 := int64(7)
testUint64 := uint64(7)
var testBytes [8]byte
binary.BigEndian.PutUint64(testBytes[:], testUint64)
for _, v := range []any{
testInt,
&testInt,
testInt64,
&testInt64,
testUint64,
&testUint64,
"7",
testBytes[:],
} {
classID, err = NewIntClassID(v)
require.NoError(t, err)
got := classID.Bytes()
assert.Equal(t, testBytes[:], got)
}
}
func Test_TaggedInt(t *testing.T) {
val := TaggedInt(7)
assert.Equal(t, "7", val.String())
assert.Equal(t, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07}, val.Bytes())
assert.Equal(t, "int", val.Type())
assert.NoError(t, val.Valid())
classID := ClassID{&val}
bytes, err := em.Marshal(classID)
require.NoError(t, err)
assert.Equal(t, []byte{
0xd9, 0x02, 0x27, // tag 551
0x07, // int 7
}, bytes)
var out ClassID
err = dm.Unmarshal(bytes, &out)
require.NoError(t, err)
assert.Equal(t, classID, out)
jsonBytes, err := json.Marshal(classID)
require.NoError(t, err)
assert.Equal(t, `{"type":"int","value":7}`, string(jsonBytes))
out = ClassID{}
err = json.Unmarshal(jsonBytes, &out)
require.NoError(t, err)
assert.Equal(t, classID, out)
}
type testClassID [4]byte
func newTestClassID(_ any) (*ClassID, error) {
return &ClassID{&testClassID{0x74, 0x65, 0x73, 0x74}}, nil
}
func (o testClassID) Bytes() []byte {
return o[:]
}
func (o testClassID) Type() string {
return "test-class-id"
}
func (o testClassID) String() string {
return "test"
}
func (o testClassID) Valid() error {
return nil
}
func (o testClassID) MarshalJSON() ([]byte, error) {
return json.Marshal(o.String())
}
func (o *testClassID) UnmarshalJSON(data []byte) error {
var out string
if err := json.Unmarshal(data, &out); err != nil {
return err
}
if len(out) != 4 {
return fmt.Errorf("bad testClassID: decoded %d bytes, want 4", len(out))
}
copy((*o)[:], out)
return nil
}
func Test_RegisterClassIDType(t *testing.T) {
err := RegisterClassIDType(99999, newTestClassID)
require.NoError(t, err)
classID, err := newTestClassID(nil)
require.NoError(t, err)
data, err := json.Marshal(classID)
require.NoError(t, err)
assert.Equal(t, string(data), `{"type":"test-class-id","value":"test"}`)
var out ClassID
err = json.Unmarshal(data, &out)
require.NoError(t, err)
assert.Equal(t, classID.Bytes(), out.Bytes())
data, err = em.Marshal(classID)
require.NoError(t, err)
assert.Equal(t, data, []byte{
0xda, 0x0, 0x1, 0x86, 0x9f, // tag 99999
0x44, // bstr(4)
0x74, 0x65, 0x73, 0x74, // "test"
})
var out2 ClassID
err = dm.Unmarshal(data, &out2)
require.NoError(t, err)
assert.Equal(t, classID.Bytes(), out2.Bytes())
}
func Test_NewBytesClassID_OK(t *testing.T) {
var testBytes = []byte{0x01, 0x02, 0x03, 0x04}
for _, v := range []any{
testBytes,
&testBytes,
string(testBytes),
} {
classID, err := NewBytesClassID(v)
require.NoError(t, err)
got := classID.Bytes()
assert.Equal(t, testBytes, got)
}
}
func Test_NewBytesClassID_NOK(t *testing.T) {
for _, tv := range []struct {
Name string
Input any
Err string
}{
{
Name: "invalid input integer",
Input: 7,
Err: "unexpected type for bytes: int",
},
{
Name: "invalid input fixed array",
Input: [3]byte{0x01, 0x02, 0x03},
Err: "unexpected type for bytes: [3]uint8",
},
} {
t.Run(tv.Name, func(t *testing.T) {
_, err := NewBytesClassID(tv.Input)
assert.EqualError(t, err, tv.Err)
})
}
}
func TestClassID_MarshalCBOR_Bytes(t *testing.T) {
tv, err := NewBytesClassID(TestBytes)
require.NoError(t, err)
// 560 (h'458999786556')
// tag(560): d9 0230
expected := MustHexDecode(t, "d90230458999786556")
actual, err := tv.MarshalCBOR()
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestClassID_UnmarshalCBOR_Bytes_OK(t *testing.T) {
tv := MustHexDecode(t, "d90230458999786556")
var actual ClassID
err := actual.UnmarshalCBOR(tv)
assert.Nil(t, err)
assert.Equal(t, "bytes", actual.Type())
assert.Equal(t, TestBytes, actual.Bytes())
}
func TestClassID_MarshalJSONBytes_OK(t *testing.T) {
testBytes := []byte{0x01, 0x02, 0x03}
tb := TaggedBytes(testBytes)
tv := ClassID{&tb}
jsonBytes, err := tv.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `{"type":"bytes","value":"AQID"}`, string(jsonBytes))
}
func TestClassID_UnmarshalJSON_Bytes_OK(t *testing.T) {
for _, tv := range []struct {
Name string
Input string
}{
{
Name: "valid input test 1",
Input: `{ "type": "bytes", "value": "MTIzNDU2Nzg5" }`,
},
{
Name: "valid input test 2",
Input: `{ "type": "bytes", "value": "deadbeef"}`,
},
} {
t.Run(tv.Name, func(t *testing.T) {
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv.Input))
require.NoError(t, err)
})
}
}
func TestClassID_UnmarshalJSON_Bytes_NOK(t *testing.T) {
for _, tv := range []struct {
Name string
Input string
Err string
}{
{
Name: "invalid value",
Input: `{ "type": "bytes", "value": "/0" }`,
Err: "cannot unmarshal class id: illegal base64 data at input byte 0",
},
{
Name: "invalid input",
Input: `{ "type": "bytes", "value": 10 }`,
Err: "cannot unmarshal class id: json: cannot unmarshal number into Go value of type comid.TaggedBytes",
},
} {
t.Run(tv.Name, func(t *testing.T) {
var actual ClassID
err := actual.UnmarshalJSON([]byte(tv.Input))
assert.EqualError(t, err, tv.Err)
})
}
}
func TestClassID_ImplID(t *testing.T) {
var implID ImplID
copy(implID[:], MustHexDecode(t,
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"))
var classID ClassID
classID.SetImplID(implID)
other, err := classID.GetImplID()
assert.NoError(t, err)
assert.Equal(t, implID, other)
}