-
Notifications
You must be signed in to change notification settings - Fork 3
/
tagid_test.go
238 lines (160 loc) · 4.64 KB
/
tagid_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
// Copyright 2020 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package swid
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTagID_NewFromUUIDString(t *testing.T) {
tv := "00010001-0001-0001-0001-000100010001"
expected := "00010001-0001-0001-0001-000100010001"
actual := NewTagID(tv)
assert.NotNil(t, actual)
assert.Equal(t, expected, actual.String())
}
func TestTagID_NewTagID_empty(t *testing.T) {
tv := ""
actual := NewTagID(tv)
assert.Nil(t, actual)
}
func TestTagID_16Bytes(t *testing.T) {
tv := []byte{
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
}
expected := "00010001-0001-0001-0001-000100010001"
actual := NewTagID(tv)
assert.NotNil(t, actual)
assert.Equal(t, expected, actual.String())
}
func TestTagID_15Bytes(t *testing.T) {
tv := []byte{
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
}
_, err := NewTagIDFromUUIDBytes(tv)
assert.EqualError(t, err, "invalid UUID (got 15 bytes)")
}
func TestTagID_17Bytes(t *testing.T) {
tv := []byte{
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00,
}
_, err := NewTagIDFromUUIDBytes(tv)
assert.EqualError(t, err, "invalid UUID (got 17 bytes)")
}
func TestTagID_String(t *testing.T) {
tv := "example.acme.roadrunner-sw-v1-0-0"
actual := NewTagID(tv)
assert.NotNil(t, actual)
assert.Equal(t, tv, actual.String())
}
func TestTagID_UnhandledType(t *testing.T) {
tv := struct {
a int
b string
}{
a: 1,
b: "one",
}
actual := NewTagID(tv)
assert.Nil(t, actual)
}
func TestTagID_UnmarshalXMLAttrString_empty(t *testing.T) {
v := ""
tv := xml.Attr{
Name: xml.Name{Local: "tagId"},
Value: v,
}
expectedErr := `error unmarshaling tag-id "": tag-id is neither a UUID nor a valid string`
var actual TagID
err := actual.UnmarshalXMLAttr(tv)
assert.EqualError(t, err, expectedErr)
}
func TestTagID_UnmarshalXMLAttrString(t *testing.T) {
v := "example.acme.roadrunner-sw-v1-0-0"
tv := xml.Attr{
Name: xml.Name{Local: "tagId"},
Value: v,
}
expected := NewTagID(v)
require.NotNil(t, expected)
var actual TagID
err := actual.UnmarshalXMLAttr(tv)
assert.Nil(t, err)
assert.Equal(t, *expected, actual)
}
func TestTagID_MarshalXMLAttrString(t *testing.T) {
v := "example.acme.roadrunner-sw-v1-0-0"
tv := NewTagID(v)
require.NotNil(t, tv)
expected := xml.Attr{
Name: xml.Name{Local: "tagId"},
Value: v,
}
actual, err := tv.MarshalXMLAttr(xml.Name{Local: "tagId"})
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestTagID_MarshalXMLAttrBytes(t *testing.T) {
v := []byte{
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
}
tv := NewTagID(v)
require.NotNil(t, tv)
expected := "00010001-0001-0001-0001-000100010001"
actual, err := tv.MarshalXMLAttr(xml.Name{Local: "tagId"})
assert.Nil(t, err)
assert.Equal(t, expected, actual.Value)
}
func TestTagID_MarshalJSONBytes(t *testing.T) {
v := []byte{
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
}
tv := NewTagID(v)
require.NotNil(t, tv)
expected := `"00010001-0001-0001-0001-000100010001"`
actual, err := tv.MarshalJSON()
assert.Nil(t, err)
assert.Equal(t, expected, string(actual))
}
func TestTagID_UnmarshalJSONUnhandled(t *testing.T) {
tv := []byte(`{ "k": "0" }`)
var actual TagID
expectedErr := "error unmarshaling tag-id: json: cannot unmarshal object into Go value of type string"
err := actual.UnmarshalJSON(tv)
assert.EqualError(t, err, expectedErr)
}
func TestTagID_UnmarshalJSON_empty(t *testing.T) {
tv := []byte(`""`)
expectedErr := `error unmarshaling tag-id "": tag-id is neither a UUID nor a valid string`
var actual TagID
err := actual.UnmarshalJSON(tv)
assert.EqualError(t, err, expectedErr)
}
func TestTagID_UnmarshalCBOR_EOF(t *testing.T) {
tv := []byte{}
expectedErr := `EOF`
var actual TagID
err := actual.UnmarshalCBOR(tv)
assert.EqualError(t, err, expectedErr)
}
func TestTagID_UnmarshalCBOR_unhandled_type(t *testing.T) {
tv := []byte{0xf6} // null
expectedErr := `error unmarshaling tag-id: tag-id MUST be []byte or string; got <nil>`
var actual TagID
err := actual.UnmarshalCBOR(tv)
assert.EqualError(t, err, expectedErr)
}
func TestTagID_UnmarshalCBOR_empty_bytes(t *testing.T) {
tv := []byte{0x40} // bytes(0)
expectedErr := `error unmarshaling tag-id: invalid UUID (got 0 bytes)`
var actual TagID
err := actual.UnmarshalCBOR(tv)
assert.EqualError(t, err, expectedErr)
}