forked from veraison/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbor_test.go
77 lines (74 loc) · 1.45 KB
/
cbor_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
package cose
import (
"bytes"
"testing"
)
func Test_byteString_UnmarshalCBOR(t *testing.T) {
// test nil pointer
t.Run("nil byteString pointer", func(t *testing.T) {
var s *byteString
data := []byte{0x40}
if err := s.UnmarshalCBOR(data); err == nil {
t.Errorf("want error on nil *byteString")
}
})
// test others
tests := []struct {
name string
data []byte
want byteString
wantErr bool
}{
{
name: "valid string",
data: []byte{0x43, 0x66, 0x6f, 0x6f},
want: []byte{0x66, 0x6f, 0x6f},
},
{
name: "empty string",
data: []byte{0x40},
want: []byte{},
},
{
name: "nil string",
data: []byte{0xf6},
want: nil,
},
{
name: "undefined string",
data: []byte{0xf7},
wantErr: true,
},
{
name: "nil CBOR data",
data: nil,
wantErr: true,
},
{
name: "empty CBOR data",
data: []byte{},
wantErr: true,
},
{
name: "tagged string",
data: []byte{0xc2, 0x40},
wantErr: true,
},
{
name: "array of bytes", // issue #46
data: []byte{0x82, 0x00, 0x1},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got byteString
if err := got.UnmarshalCBOR(tt.data); (err != nil) != tt.wantErr {
t.Errorf("byteString.UnmarshalCBOR() error = %v, wantErr %v", err, tt.wantErr)
}
if !bytes.Equal(got, tt.want) {
t.Errorf("byteString.UnmarshalCBOR() = %v, want %v", got, tt.want)
}
})
}
}