Repository navigation
Expand file tree
/
Copy pathinstance_test.go
More file actions
293 lines (244 loc) · 6.77 KB
/
Copy pathinstance_test.go
File metadata and controls
293 lines (244 loc) · 6.77 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
// Copyright 2024-2026 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInstance_SetUUID_OK(t *testing.T) {
inst := &Instance{}
testUUID, err := uuid.Parse(TestUUIDString)
require.NoError(t, err)
i := inst.SetUUID(testUUID)
require.NotNil(t, i)
}
func TestInstance_GetUUID_OK(t *testing.T) {
inst := MustNewUUIDInstance(TestUUID)
require.NotNil(t, inst)
u, err := inst.GetUUID()
assert.Nil(t, err)
assert.Equal(t, u, TestUUID)
}
func TestInstance_GetUUID_NOK(t *testing.T) {
inst := &Instance{}
expectedErr := "instance-id type is: <nil>"
_, err := inst.GetUUID()
assert.EqualError(t, err, expectedErr)
}
func TestInstance_SetGetUEID_OK(t *testing.T) {
inst := &Instance{}
inst = inst.SetUEID(TestUEID)
require.NotNil(t, inst)
expectedUEID, err := inst.GetUEID()
require.NoError(t, err)
assert.Equal(t, TestUEID, expectedUEID)
}
type testInstance string
func newTestInstance(_ any) (*Instance, error) {
ret := testInstance("test")
return &Instance{&ret}, nil
}
func (o testInstance) Bytes() []byte {
return []byte(o)
}
func (o testInstance) Type() string {
return "test-instance"
}
func (o testInstance) String() string {
return string(o)
}
func (o testInstance) Valid() error {
return nil
}
func Test_RegisterInstanceType(t *testing.T) {
err := RegisterInstanceType(99997, newTestInstance)
require.NoError(t, err)
instance, err := newTestInstance(nil)
require.NoError(t, err)
data, err := json.Marshal(instance)
require.NoError(t, err)
assert.Equal(t, string(data), `{"type":"test-instance","value":"test"}`)
var out Instance
err = json.Unmarshal(data, &out)
require.NoError(t, err)
assert.Equal(t, instance.Bytes(), out.Bytes())
data, err = em.Marshal(instance)
require.NoError(t, err)
assert.Equal(t, data, []byte{
0xda, 0x0, 0x1, 0x86, 0x9d, // tag 99997
0x64, // tstr(4)
0x74, 0x65, 0x73, 0x74, // "test"
})
var out2 Instance
err = dm.Unmarshal(data, &out2)
require.NoError(t, err)
assert.Equal(t, instance.Bytes(), out2.Bytes())
}
func Test_NewBytesInstance_OK(t *testing.T) {
var testBytes = []byte{0x01, 0x02, 0x03, 0x04}
for _, v := range []any{
testBytes,
&testBytes,
string(testBytes),
} {
instance, err := NewBytesInstance(v)
require.NoError(t, err)
got := instance.Bytes()
assert.Equal(t, testBytes, got)
}
}
func Test_NewBytesInstance_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 := NewBytesInstance(tv.Input)
assert.EqualError(t, err, tv.Err)
})
}
}
func Test_MustNewBytesInstance(t *testing.T) {
MustNewBytesInstance([]byte{0x01, 0x02, 0x03, 0x04})
assert.Panics(t, func() { MustNewBytesInstance(7) })
}
func TestInstance_MarshalCBOR_Bytes(t *testing.T) {
tv, err := NewBytesInstance(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 TestInstance_UnmarshalCBOR_Bytes_OK(t *testing.T) {
tv := MustHexDecode(t, "d90230458999786556")
var actual Instance
err := actual.UnmarshalCBOR(tv)
assert.Nil(t, err)
assert.Equal(t, "bytes", actual.Type())
assert.Equal(t, TestBytes, actual.Bytes())
}
func TestInstance_MarshalJSONBytes_OK(t *testing.T) {
testBytes := []byte{0x01, 0x02, 0x03}
tb := TaggedBytes(testBytes)
tv := Instance{&tb}
jsonBytes, err := tv.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `{"type":"bytes","value":"AQID"}`, string(jsonBytes))
}
func TestInstance_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": "CgsMDQ4="}`,
},
} {
t.Run(tv.Name, func(t *testing.T) {
var actual Instance
err := actual.UnmarshalJSON([]byte(tv.Input))
require.NoError(t, err)
})
}
}
func TestInstance_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 instance: illegal base64 data at input byte 0",
},
{
Name: "invalid input",
Input: `{ "type": "bytes", "value": 10 }`,
Err: "cannot unmarshal instance: json: cannot unmarshal number into Go value of type comid.TaggedBytes",
},
} {
t.Run(tv.Name, func(t *testing.T) {
var actual Instance
err := actual.UnmarshalJSON([]byte(tv.Input))
assert.EqualError(t, err, tv.Err)
})
}
}
func TestNewPKIXBase64KeyInstance(t *testing.T) {
_, err := NewPKIXBase64KeyInstance(TestECPubKey)
assert.NoError(t, err)
MustNewPKIXBase64KeyInstance(TestECPubKey)
_, err = NewPKIXBase64KeyInstance("foo")
assert.ErrorContains(t, err, "could not decode PEM block")
assert.Panics(t, func() { MustNewPKIXBase64KeyInstance("foo") })
}
func TestNewPKIXBase64CertInstance(t *testing.T) {
_, err := NewPKIXBase64CertInstance(TestCert)
assert.NoError(t, err)
MustNewPKIXBase64CertInstance(TestCert)
_, err = NewPKIXBase64CertInstance("foo")
assert.ErrorContains(t, err, "could not decode PEM block")
assert.Panics(t, func() { MustNewPKIXBase64CertInstance("foo") })
}
func TestNewCOSEKeyInstance(t *testing.T) {
_, err := NewCOSEKeyInstance(TestCOSEKey)
assert.NoError(t, err)
MustNewCOSEKeyInstance(TestCOSEKey)
_, err = NewCOSEKeyInstance("foo")
assert.ErrorContains(t, err, "base64 decode error")
assert.Panics(t, func() { MustNewCOSEKeyInstance("foo") })
}
func TestNewThumbprintInstance(t *testing.T) {
testCases := []struct {
title string
newFunc func(any) (*Instance, error)
mustNewFunc func(any) *Instance
}{
{
title: "thumbprint",
newFunc: NewThumbprintInstance,
mustNewFunc: MustNewThumbprintInstance,
},
{
title: "cert thumbprint",
newFunc: NewCertThumbprintInstance,
mustNewFunc: MustNewCertThumbprintInstance,
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
_, err := tc.newFunc(TestThumbprint)
assert.NoError(t, err)
tc.mustNewFunc(TestThumbprint)
_, err = tc.newFunc(7)
assert.ErrorContains(t, err, "must be a Digest or a string")
assert.Panics(t, func() { tc.mustNewFunc("foo") })
})
}
}