forked from mozilla-services/go-cose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_headers_test.go
232 lines (207 loc) · 4.8 KB
/
common_headers_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
package cose
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
var CompressionTestCases = []struct {
name string
input map[interface{}]interface{}
intermediate map[interface{}]interface{}
roundtrip map[interface{}]interface{}
}{
{
"all empty",
map[interface{}]interface{}{},
map[interface{}]interface{}{},
map[interface{}]interface{}{},
},
{
"all keys",
map[interface{}]interface{}{
"counter signature": []int{1, 2, -3},
"Partial IV": "foo",
"alg": true,
"IV": nil,
"content type": false,
"kid": -1,
"crit": true,
},
map[interface{}]interface{}{
3: false,
1: true,
2: true,
4: -1,
5: nil,
6: "foo",
7: []int{1, 2, -3},
},
map[interface{}]interface{}{
"counter signature": []int{1, 2, -3},
"Partial IV": "foo",
"alg": true,
"IV": nil,
"content type": false,
"kid": -1,
"crit": true,
},
},
{
"unknown key",
map[interface{}]interface{}{
"unknown": -1,
},
map[interface{}]interface{}{
"unknown": -1,
},
map[interface{}]interface{}{
"unknown": -1,
},
},
{
"known key wrong case \"ALG\"",
map[interface{}]interface{}{
"ALG": 1,
},
map[interface{}]interface{}{
"ALG": 1,
},
map[interface{}]interface{}{
"ALG": 1,
},
},
{
"supported alg value \"ES256\" compressed",
map[interface{}]interface{}{
"alg": "ES256",
},
map[interface{}]interface{}{
1: -7,
},
map[interface{}]interface{}{
"alg": "ES256",
},
},
{
"supported alg value \"PS256\" compressed",
map[interface{}]interface{}{
"alg": "PS256",
},
map[interface{}]interface{}{
1: -37,
},
map[interface{}]interface{}{
"alg": "PS256",
},
},
{
"converts int64 to int",
map[interface{}]interface{}{
int64(1): int64(-37),
},
map[interface{}]interface{}{
1: -37,
},
map[interface{}]interface{}{
"alg": "PS256",
},
},
}
func TestHeaderCompressionRoundTrip(t *testing.T) {
for _, testCase := range CompressionTestCases {
assert := assert.New(t)
compressed := CompressHeaders(testCase.input)
assert.Equal(
testCase.intermediate,
compressed,
fmt.Sprintf("%s: header compression failed", testCase.name))
assert.Equal(
testCase.roundtrip,
DecompressHeaders(compressed),
fmt.Sprintf("%s: header compression-decompression roundtrip failed", testCase.name))
}
}
func TestHeaderCompressionDoesNotDecompressUnknownTag(t *testing.T) {
assert := assert.New(t)
compressed := map[interface{}]interface{}{
777: 1,
}
assert.Equal(
compressed,
DecompressHeaders(compressed),
"header decompression modifies unknown tag")
}
func TestGetAlgPanics(t *testing.T) {
assert := assert.New(t)
var algName = "FOOOO"
assert.Panics(func () { getAlgByNameOrPanic(algName) })
}
func TestGetCommonHeaderTagOrPanicPanics(t *testing.T) {
assert := assert.New(t)
var label = "FOOOO"
assert.Panics(func () { GetCommonHeaderTagOrPanic(label) })
}
func TestGetAlgWithString(t *testing.T) {
assert := assert.New(t)
var h *Headers = nil
alg, err := getAlg(h)
assert.Nil(alg)
assert.NotNil(err)
assert.Equal("Cannot getAlg on nil Headers", err.Error())
h = &Headers{}
h.Protected = map[interface{}]interface{}{
"alg": "ROT13",
}
alg, err = getAlg(h)
assert.Nil(alg)
assert.NotNil(err)
assert.Equal(err.Error(), "Algorithm named ROT13 not found")
h.Protected["alg"] = "ES256"
alg, err = getAlg(h)
assert.NotNil(alg)
assert.Nil(err)
assert.Equal(alg.Name, "ES256")
}
func TestFindDuplicateHeaderWithNilHeaders(t *testing.T) {
assert := assert.New(t)
var h *Headers = nil
assert.Nil(FindDuplicateHeader(h))
}
func TestHeaderEncodeErrors(t *testing.T) {
assert := assert.New(t)
var h *Headers = nil
assert.Panics(func () { h.EncodeProtected() })
h = &Headers{
Protected: map[interface{}]interface{}{
"alg": -3,
1: -7,
},
}
assert.Panics(func () { h.EncodeProtected() })
}
func TestHeaderDecodeErrors(t *testing.T) {
assert := assert.New(t)
var (
h *Headers = &Headers{
Protected: nil,
Unprotected: nil,
}
v []interface{}
err error
)
err = h.Decode(v)
assert.NotNil(err)
assert.Equal(err.Error(), "can only decode headers from 2-item array; got 0")
v = []interface{}{[]byte("\x90"), map[interface{}]interface{}{}}
err = h.Decode(v)
assert.NotNil(err)
assert.Equal(err.Error(), "error CBOR decoding protected header bytes; got <nil>")
v = []interface{}{[]byte("\x60"), map[interface{}]interface{}{}}
err = h.Decode(v)
assert.NotNil(err)
assert.Equal(err.Error(), "error casting protected to map; got string")
v = []interface{}{[]byte("\xA1\x02\x26"), -1}
err = h.Decode(v)
assert.NotNil(err)
assert.Equal(err.Error(), "error decoding unprotected header as map[interface {}]interface {}; got int")
}