Repository navigation
Expand file tree
/
Copy pathsubmods_test.go
More file actions
194 lines (168 loc) · 5.58 KB
/
Copy pathsubmods_test.go
File metadata and controls
194 lines (168 loc) · 5.58 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
// Copyright 2020-2026 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package eat
import (
"encoding/json"
"testing"
"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/assert"
)
func TestSubmod_round_trip(t *testing.T) {
testCases := []struct {
title string
submod Submod
expectedCBOR []byte
expectedJSON string
}{
{
title: "ok nested token",
submod: Submod{&NestedToken{
Type: NestedTokenDigest,
Data: []byte(`[-16,"3q2-7w"]`),
}},
expectedCBOR: []byte{
0x6e, // tstr(14)
0x5b, 0x2d, 0x31, 0x36, 0x2c, 0x22, 0x33, 0x71, // . "[-16,\"3q"
0x32, 0x2d, 0x37, 0x77, 0x22, 0x5d, // . "2-7w\"]"
},
expectedJSON: `["DIGEST",[-16,"3q2-7w"]]`,
},
{
title: "ok claims set",
submod: Submod{&ClaimsSet{
BootCount: Ptr(uint(1)),
privateClaims: make(map[IntOrString]any),
}},
expectedCBOR: []byte{
0xa1, // map(1)
0x19, 0x01, 0x0b, // . key: 267
0x01, // . value: 1
},
expectedJSON: `{"bootcount":1}`,
},
{
title: "ok detached submod digest",
submod: Submod{MustNewDetachedSubmodDigestIntAlg(Sha256, testDigestBytes)},
expectedCBOR: []byte{
0x82, // array(2)
0x2f, // . [0]-16 [sha-256]
0x58, 0x20, // . [1]bstr(32)
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
},
expectedJSON: `["DIGEST",[-16, "AAECAwQFBgcAAQIDBAUGBwABAgMEBQYHAAECAwQFBgc"]]`,
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
encodedCBOR, err := tc.submod.MarshalCBOR()
assert.NoError(t, err)
assert.Equal(t, tc.expectedCBOR, encodedCBOR)
var decodedSubmod Submod
err = decodedSubmod.UnmarshalCBOR(encodedCBOR)
assert.NoError(t, err)
assert.EqualValues(t, tc.submod, decodedSubmod)
encodedJSON, err := tc.submod.MarshalJSON()
assert.NoError(t, err)
assert.JSONEq(t, tc.expectedJSON, string(encodedJSON))
err = decodedSubmod.UnmarshalJSON(encodedJSON)
assert.NoError(t, err)
if tc.title == "ok detached submod digest" {
// As JSON submod encoding does not allow a detanched submod digest,
// it gets wrapped in a nested token.
expectedSubmod := Submod{&NestedToken{
Type: NestedTokenDigest,
Data: []byte(`[-16,"AAECAwQFBgcAAQIDBAUGBwABAgMEBQYHAAECAwQFBgc"]`),
}}
assert.EqualValues(t, expectedSubmod, decodedSubmod)
} else {
assert.EqualValues(t, tc.submod, decodedSubmod)
}
})
}
}
func TestSubmod_UnmarshalJSON_empty(t *testing.T) {
var submod Submod
err := submod.UnmarshalJSON([]byte{})
assert.ErrorContains(t, err, "truncated input")
}
func TestSubmods_add_get(t *testing.T) {
submods := NewSubmods().
AddClaimsSet("foo", &ClaimsSet{
BootCount: Ptr(uint(1)),
privateClaims: make(map[IntOrString]any),
}).
AddDigest("bar", MustNewDetachedSubmodDigestIntAlg(Sha256, testDigestBytes)).
AddNestedToken("qux", &NestedToken{
Type: NestedTokenDigest,
Data: []byte(`[-16,"3q2-7w"]`),
})
claimsSet, err := submods.GetClaimsSet("foo")
assert.NoError(t, err)
assert.EqualValues(t, Ptr(uint(1)), claimsSet.BootCount)
_, err = submods.GetClaimsSet("bar")
assert.ErrorContains(t, err, `submod "bar" is not a claims set`)
_, err = submods.GetClaimsSet("zot")
assert.ErrorContains(t, err, `no submod named "zot"`)
digest, err := submods.GetDigest("bar")
assert.NoError(t, err)
assert.EqualValues(t, HashAlgorithmSha256, digest.Algorithm)
_, err = submods.GetDigest("foo")
assert.ErrorContains(t, err, `submod "foo" is not a detached submod digest`)
_, err = submods.GetDigest("zot")
assert.ErrorContains(t, err, `no submod named "zot"`)
token, err := submods.GetNestedToken("qux")
assert.NoError(t, err)
assert.EqualValues(t, NestedTokenDigest, token.Type)
_, err = submods.GetNestedToken("foo")
assert.ErrorContains(t, err, `submod "foo" is not a nested token`)
_, err = submods.GetNestedToken("zot")
assert.ErrorContains(t, err, `no submod named "zot"`)
assert.Panics(t, func() { submods.AddClaimsSet("zot", nil) })
assert.Panics(t, func() { submods.AddDigest("zot", nil) })
assert.Panics(t, func() { submods.AddNestedToken("zot", nil) })
}
func TestSubmods_round_trip(t *testing.T) {
testCases := []struct {
title string
submods Submods
expectedCBOR []byte
expectedJSON string
}{
{
title: "ok claims set",
submods: *NewSubmods().AddClaimsSet("foo", &ClaimsSet{
BootCount: Ptr(uint(1)),
privateClaims: make(map[IntOrString]any),
}),
expectedCBOR: []byte{
0xa1, // map(1)
0x63, // . key: tstr(3)
0x66, 0x6f, 0x6f, // . . "foo"
0xa1, // . value: map(1) [ClaimSet]
0x19, 0x01, 0x0b, // . . key: 267
0x01, // . . value: 1
},
expectedJSON: `{"foo": {"bootcount":1}}`,
},
}
for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
encodedCBOR, err := cbor.Marshal(tc.submods)
assert.NoError(t, err)
assert.Equal(t, tc.expectedCBOR, encodedCBOR)
var decodedSubmods Submods
err = cbor.Unmarshal(encodedCBOR, &decodedSubmods)
assert.NoError(t, err)
assert.EqualValues(t, tc.submods, decodedSubmods)
encodedJSON, err := json.Marshal(tc.submods)
assert.NoError(t, err)
assert.JSONEq(t, tc.expectedJSON, string(encodedJSON))
err = json.Unmarshal(encodedJSON, &decodedSubmods)
assert.NoError(t, err)
assert.EqualValues(t, tc.submods, decodedSubmods)
})
}
}