-
Notifications
You must be signed in to change notification settings - Fork 7
/
comid_test.go
96 lines (84 loc) · 2.46 KB
/
comid_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
// Copyright 2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/corim/extensions"
"github.com/veraison/swid"
)
func Test_Comid_Extensions(t *testing.T) {
c := NewComid()
assert.Nil(t, c.GetExtensions())
assert.Equal(t, "", c.MustGetString("field-one"))
err := c.Set("field-one", "foo")
assert.EqualError(t, err, "extension not found: field-one")
type ComidExt struct {
FieldOne string `cbor:"-1,keyasint" json:"field-one"`
}
extMap := extensions.NewMap().
Add(ExtComid, &ComidExt{}).
Add(ExtEntity, &struct{}{}).
Add(ExtReferenceValue, &struct{}{}).
Add(ExtEndorsedValueFlags, &struct{}{})
err = c.RegisterExtensions(extMap)
require.NoError(t, err)
err = c.Set("field-one", "foo")
assert.NoError(t, err)
assert.Equal(t, "foo", c.MustGetString("-1"))
}
func Test_Comid_ToJSONPretty(t *testing.T) {
c := NewComid()
_, err := c.ToJSONPretty(" ")
assert.EqualError(t, err, "tag-identity validation failed: empty tag-id")
c.TagIdentity = TagIdentity{TagID: *swid.NewTagID("test"), TagVersion: 1}
c.Triples = Triples{
ReferenceValues: NewValueTriples().Add(&ValueTriple{
Environment: Environment{
Instance: MustNewUUIDInstance(TestUUID),
},
Measurements: *NewMeasurements().Add(&Measurement{
Val: Mval{
RawValue: NewRawValue().SetBytes(MustHexDecode(t, "deadbeef")),
},
}),
}),
}
expected := `{
"tag-identity": {
"id": "test",
"version": 1
},
"triples": {
"reference-values": [
{
"environment": {
"instance": {
"type": "uuid",
"value": "31fb5abf-023e-4992-aa4e-95f9c1503bfa"
}
},
"measurements": [
{
"value": {
"raw-value": {
"type": "bytes",
"value": "3q2+7w=="
}
}
}
]
}
]
}
}`
v, err := c.ToJSONPretty(" ")
require.NoError(t, err)
assert.Equal(t, expected, string(v))
}
func Test_String2URI_nok(t *testing.T) {
s := "@@@"
_, err := String2URI(&s)
assert.EqualError(t, err, `expecting an absolute URI: "@@@" is not an absolute URI`)
}