-
Notifications
You must be signed in to change notification settings - Fork 7
/
role_test.go
234 lines (196 loc) · 4.61 KB
/
role_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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoles_NewRoles_empty(t *testing.T) {
tv := NewRoles()
require.NotNil(t, tv)
expected := "empty roles"
assert.EqualError(t, tv.Valid(), expected)
}
func TestRoles_NewRoles_set_multi(t *testing.T) {
actual := NewRoles().
Add(RoleCreator).
Add(RoleMaintainer, RoleTagCreator)
require.NotNil(t, actual)
expected := &Roles{
RoleCreator,
RoleMaintainer,
RoleTagCreator,
}
assert.Nil(t, actual.Valid())
assert.Equal(t, expected, actual)
}
func TestRoles_FromCBOR_ok(t *testing.T) {
tvs := []struct {
cbor []byte
expected Roles
}{
{
// 83020100 = [2, 1, 0]
cbor: MustHexDecode(t, "83020100"),
expected: Roles{RoleMaintainer, RoleCreator, RoleTagCreator},
},
{
// use non-registered value -123
// 8202387a = [2, -123]
cbor: MustHexDecode(t, "8202387a"),
expected: Roles{RoleMaintainer, Role(-123)},
},
}
for _, tv := range tvs {
var actual Roles
err := actual.FromCBOR(tv.cbor)
assert.Nil(t, err)
assert.Equal(t, tv.expected, actual)
}
}
func TestRoles_FromCBOR_fail(t *testing.T) {
tvs := []struct {
cbor []byte
expectedErr string
}{
{
// 80 = []
cbor: MustHexDecode(t, "80"),
expectedErr: "empty roles",
},
{
// 8163626c61 = ["bla"]
cbor: MustHexDecode(t, "8163626c61"),
expectedErr: "cbor: cannot unmarshal UTF-8 text string into Go value of type comid.Role",
},
/*
{
// XXX(tho) - the cbor library treats null as 0
// 81f6 = [null]
cbor: mustHexDecode(t, "81f6"),
expectedErr: "..."
},
*/
}
for _, tv := range tvs {
var actual Roles
err := actual.FromCBOR(tv.cbor)
assert.EqualError(t, err, tv.expectedErr)
}
}
func TestRoles_ToCBOR_ok(t *testing.T) {
tvs := []struct {
roles *Roles
expected []byte
}{
{
// [0, 1, 2]
roles: NewRoles().Add(RoleTagCreator, RoleCreator, RoleMaintainer),
expected: MustHexDecode(t, "83000102"),
},
{
// [0]
roles: NewRoles().Add(RoleTagCreator),
expected: MustHexDecode(t, "8100"),
},
{
// [1]
roles: NewRoles().Add(RoleCreator),
expected: MustHexDecode(t, "8101"),
},
{
// [2]
roles: NewRoles().Add(RoleMaintainer),
expected: MustHexDecode(t, "8102"),
},
{
// it is possible to force non-registered values
// [1, 123]
roles: NewRoles().Add(RoleCreator, Role(123)),
expected: MustHexDecode(t, "8201187b"),
},
}
for _, tv := range tvs {
actual, err := tv.roles.ToCBOR()
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, tv.expected, actual)
}
}
func TestRoles_ToCBOR_fail_unset(t *testing.T) {
r := NewRoles()
require.NotNil(t, r)
_, err := r.ToCBOR()
assert.EqualError(t, err, "empty roles")
}
func TestRoles_UnmarshalJSON_ok(t *testing.T) {
tvs := []struct {
json string
expected Roles
}{
{
json: `[ "tagCreator", "creator", "maintainer" ]`,
expected: Roles{RoleTagCreator, RoleCreator, RoleMaintainer},
},
{
json: `[ "creator" ]`,
expected: Roles{RoleCreator},
},
{
json: `[ "maintainer" ]`,
expected: Roles{RoleMaintainer},
},
{
json: `[ "tagCreator" ]`,
expected: Roles{RoleTagCreator},
},
}
for _, tv := range tvs {
var actual Roles
err := actual.UnmarshalJSON([]byte(tv.json))
assert.Nil(t, err)
assert.Equal(t, tv.expected, actual)
}
}
func TestRoles_UnmarshalJSON_fail(t *testing.T) {
tvs := []struct {
json string
expectedErr string
}{
{
json: `[]`,
expectedErr: "no roles found",
},
{
json: `["blabla"]`,
expectedErr: `unknown role "blabla"`,
},
{
json: `"tagCreator"`,
expectedErr: "json: cannot unmarshal string into Go value of type []string",
},
}
for _, tv := range tvs {
var actual Roles
err := actual.UnmarshalJSON([]byte(tv.json))
assert.EqualError(t, err, tv.expectedErr)
}
}
func Test_Role_String(t *testing.T) {
assert.Equal(t, "maintainer", RoleMaintainer.String())
assert.Equal(t, "Role(9999)", Role(9999).String())
}
func Test_RegisterRole(t *testing.T) {
err := RegisterRole(1, "owner")
assert.EqualError(t, err, "role with value 1 already exists")
err = RegisterRole(3, "maintainer")
assert.EqualError(t, err, `role with name "maintainer" already exists`)
err = RegisterRole(3, "owner")
assert.NoError(t, err)
roles := NewRoles().Add(Role(3))
out, err := roles.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `["owner"]`, string(out))
}