1
2
3 package jwa_test
4
5 import (
6 "testing"
7
8 "github.com/lestrrat-go/jwx/jwa"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestEllipticCurveAlgorithm(t *testing.T) {
13 t.Parallel()
14 t.Run(`accept jwa constant Ed25519`, func(t *testing.T) {
15 t.Parallel()
16 var dst jwa.EllipticCurveAlgorithm
17 if !assert.NoError(t, dst.Accept(jwa.Ed25519), `accept is successful`) {
18 return
19 }
20 if !assert.Equal(t, jwa.Ed25519, dst, `accepted value should be equal to constant`) {
21 return
22 }
23 })
24 t.Run(`accept the string Ed25519`, func(t *testing.T) {
25 t.Parallel()
26 var dst jwa.EllipticCurveAlgorithm
27 if !assert.NoError(t, dst.Accept("Ed25519"), `accept is successful`) {
28 return
29 }
30 if !assert.Equal(t, jwa.Ed25519, dst, `accepted value should be equal to constant`) {
31 return
32 }
33 })
34 t.Run(`accept fmt.Stringer for Ed25519`, func(t *testing.T) {
35 t.Parallel()
36 var dst jwa.EllipticCurveAlgorithm
37 if !assert.NoError(t, dst.Accept(stringer{src: "Ed25519"}), `accept is successful`) {
38 return
39 }
40 if !assert.Equal(t, jwa.Ed25519, dst, `accepted value should be equal to constant`) {
41 return
42 }
43 })
44 t.Run(`stringification for Ed25519`, func(t *testing.T) {
45 t.Parallel()
46 if !assert.Equal(t, "Ed25519", jwa.Ed25519.String(), `stringified value matches`) {
47 return
48 }
49 })
50 t.Run(`accept jwa constant Ed448`, func(t *testing.T) {
51 t.Parallel()
52 var dst jwa.EllipticCurveAlgorithm
53 if !assert.NoError(t, dst.Accept(jwa.Ed448), `accept is successful`) {
54 return
55 }
56 if !assert.Equal(t, jwa.Ed448, dst, `accepted value should be equal to constant`) {
57 return
58 }
59 })
60 t.Run(`accept the string Ed448`, func(t *testing.T) {
61 t.Parallel()
62 var dst jwa.EllipticCurveAlgorithm
63 if !assert.NoError(t, dst.Accept("Ed448"), `accept is successful`) {
64 return
65 }
66 if !assert.Equal(t, jwa.Ed448, dst, `accepted value should be equal to constant`) {
67 return
68 }
69 })
70 t.Run(`accept fmt.Stringer for Ed448`, func(t *testing.T) {
71 t.Parallel()
72 var dst jwa.EllipticCurveAlgorithm
73 if !assert.NoError(t, dst.Accept(stringer{src: "Ed448"}), `accept is successful`) {
74 return
75 }
76 if !assert.Equal(t, jwa.Ed448, dst, `accepted value should be equal to constant`) {
77 return
78 }
79 })
80 t.Run(`stringification for Ed448`, func(t *testing.T) {
81 t.Parallel()
82 if !assert.Equal(t, "Ed448", jwa.Ed448.String(), `stringified value matches`) {
83 return
84 }
85 })
86 t.Run(`accept jwa constant P256`, func(t *testing.T) {
87 t.Parallel()
88 var dst jwa.EllipticCurveAlgorithm
89 if !assert.NoError(t, dst.Accept(jwa.P256), `accept is successful`) {
90 return
91 }
92 if !assert.Equal(t, jwa.P256, dst, `accepted value should be equal to constant`) {
93 return
94 }
95 })
96 t.Run(`accept the string P-256`, func(t *testing.T) {
97 t.Parallel()
98 var dst jwa.EllipticCurveAlgorithm
99 if !assert.NoError(t, dst.Accept("P-256"), `accept is successful`) {
100 return
101 }
102 if !assert.Equal(t, jwa.P256, dst, `accepted value should be equal to constant`) {
103 return
104 }
105 })
106 t.Run(`accept fmt.Stringer for P-256`, func(t *testing.T) {
107 t.Parallel()
108 var dst jwa.EllipticCurveAlgorithm
109 if !assert.NoError(t, dst.Accept(stringer{src: "P-256"}), `accept is successful`) {
110 return
111 }
112 if !assert.Equal(t, jwa.P256, dst, `accepted value should be equal to constant`) {
113 return
114 }
115 })
116 t.Run(`stringification for P-256`, func(t *testing.T) {
117 t.Parallel()
118 if !assert.Equal(t, "P-256", jwa.P256.String(), `stringified value matches`) {
119 return
120 }
121 })
122 t.Run(`accept jwa constant P384`, func(t *testing.T) {
123 t.Parallel()
124 var dst jwa.EllipticCurveAlgorithm
125 if !assert.NoError(t, dst.Accept(jwa.P384), `accept is successful`) {
126 return
127 }
128 if !assert.Equal(t, jwa.P384, dst, `accepted value should be equal to constant`) {
129 return
130 }
131 })
132 t.Run(`accept the string P-384`, func(t *testing.T) {
133 t.Parallel()
134 var dst jwa.EllipticCurveAlgorithm
135 if !assert.NoError(t, dst.Accept("P-384"), `accept is successful`) {
136 return
137 }
138 if !assert.Equal(t, jwa.P384, dst, `accepted value should be equal to constant`) {
139 return
140 }
141 })
142 t.Run(`accept fmt.Stringer for P-384`, func(t *testing.T) {
143 t.Parallel()
144 var dst jwa.EllipticCurveAlgorithm
145 if !assert.NoError(t, dst.Accept(stringer{src: "P-384"}), `accept is successful`) {
146 return
147 }
148 if !assert.Equal(t, jwa.P384, dst, `accepted value should be equal to constant`) {
149 return
150 }
151 })
152 t.Run(`stringification for P-384`, func(t *testing.T) {
153 t.Parallel()
154 if !assert.Equal(t, "P-384", jwa.P384.String(), `stringified value matches`) {
155 return
156 }
157 })
158 t.Run(`accept jwa constant P521`, func(t *testing.T) {
159 t.Parallel()
160 var dst jwa.EllipticCurveAlgorithm
161 if !assert.NoError(t, dst.Accept(jwa.P521), `accept is successful`) {
162 return
163 }
164 if !assert.Equal(t, jwa.P521, dst, `accepted value should be equal to constant`) {
165 return
166 }
167 })
168 t.Run(`accept the string P-521`, func(t *testing.T) {
169 t.Parallel()
170 var dst jwa.EllipticCurveAlgorithm
171 if !assert.NoError(t, dst.Accept("P-521"), `accept is successful`) {
172 return
173 }
174 if !assert.Equal(t, jwa.P521, dst, `accepted value should be equal to constant`) {
175 return
176 }
177 })
178 t.Run(`accept fmt.Stringer for P-521`, func(t *testing.T) {
179 t.Parallel()
180 var dst jwa.EllipticCurveAlgorithm
181 if !assert.NoError(t, dst.Accept(stringer{src: "P-521"}), `accept is successful`) {
182 return
183 }
184 if !assert.Equal(t, jwa.P521, dst, `accepted value should be equal to constant`) {
185 return
186 }
187 })
188 t.Run(`stringification for P-521`, func(t *testing.T) {
189 t.Parallel()
190 if !assert.Equal(t, "P-521", jwa.P521.String(), `stringified value matches`) {
191 return
192 }
193 })
194 t.Run(`accept jwa constant X25519`, func(t *testing.T) {
195 t.Parallel()
196 var dst jwa.EllipticCurveAlgorithm
197 if !assert.NoError(t, dst.Accept(jwa.X25519), `accept is successful`) {
198 return
199 }
200 if !assert.Equal(t, jwa.X25519, dst, `accepted value should be equal to constant`) {
201 return
202 }
203 })
204 t.Run(`accept the string X25519`, func(t *testing.T) {
205 t.Parallel()
206 var dst jwa.EllipticCurveAlgorithm
207 if !assert.NoError(t, dst.Accept("X25519"), `accept is successful`) {
208 return
209 }
210 if !assert.Equal(t, jwa.X25519, dst, `accepted value should be equal to constant`) {
211 return
212 }
213 })
214 t.Run(`accept fmt.Stringer for X25519`, func(t *testing.T) {
215 t.Parallel()
216 var dst jwa.EllipticCurveAlgorithm
217 if !assert.NoError(t, dst.Accept(stringer{src: "X25519"}), `accept is successful`) {
218 return
219 }
220 if !assert.Equal(t, jwa.X25519, dst, `accepted value should be equal to constant`) {
221 return
222 }
223 })
224 t.Run(`stringification for X25519`, func(t *testing.T) {
225 t.Parallel()
226 if !assert.Equal(t, "X25519", jwa.X25519.String(), `stringified value matches`) {
227 return
228 }
229 })
230 t.Run(`accept jwa constant X448`, func(t *testing.T) {
231 t.Parallel()
232 var dst jwa.EllipticCurveAlgorithm
233 if !assert.NoError(t, dst.Accept(jwa.X448), `accept is successful`) {
234 return
235 }
236 if !assert.Equal(t, jwa.X448, dst, `accepted value should be equal to constant`) {
237 return
238 }
239 })
240 t.Run(`accept the string X448`, func(t *testing.T) {
241 t.Parallel()
242 var dst jwa.EllipticCurveAlgorithm
243 if !assert.NoError(t, dst.Accept("X448"), `accept is successful`) {
244 return
245 }
246 if !assert.Equal(t, jwa.X448, dst, `accepted value should be equal to constant`) {
247 return
248 }
249 })
250 t.Run(`accept fmt.Stringer for X448`, func(t *testing.T) {
251 t.Parallel()
252 var dst jwa.EllipticCurveAlgorithm
253 if !assert.NoError(t, dst.Accept(stringer{src: "X448"}), `accept is successful`) {
254 return
255 }
256 if !assert.Equal(t, jwa.X448, dst, `accepted value should be equal to constant`) {
257 return
258 }
259 })
260 t.Run(`stringification for X448`, func(t *testing.T) {
261 t.Parallel()
262 if !assert.Equal(t, "X448", jwa.X448.String(), `stringified value matches`) {
263 return
264 }
265 })
266 t.Run(`do not accept invalid constant InvalidEllipticCurve`, func(t *testing.T) {
267 t.Parallel()
268 var dst jwa.EllipticCurveAlgorithm
269 if !assert.Error(t, dst.Accept(jwa.InvalidEllipticCurve), `accept should fail`) {
270 return
271 }
272 })
273 t.Run(`bail out on random integer value`, func(t *testing.T) {
274 t.Parallel()
275 var dst jwa.EllipticCurveAlgorithm
276 if !assert.Error(t, dst.Accept(1), `accept should fail`) {
277 return
278 }
279 })
280 t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
281 t.Parallel()
282 var dst jwa.EllipticCurveAlgorithm
283 if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
284 return
285 }
286 })
287 t.Run(`check list of elements`, func(t *testing.T) {
288 t.Parallel()
289 var expected = map[jwa.EllipticCurveAlgorithm]struct{}{
290 jwa.Ed25519: {},
291 jwa.Ed448: {},
292 jwa.P256: {},
293 jwa.P384: {},
294 jwa.P521: {},
295 jwa.X25519: {},
296 jwa.X448: {},
297 }
298 for _, v := range jwa.EllipticCurveAlgorithms() {
299
300
301 if v.String() == `secp256k1` {
302 continue
303 }
304 if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) {
305 return
306 }
307 delete(expected, v)
308 }
309 if !assert.Len(t, expected, 0) {
310 return
311 }
312 })
313 }
314
View as plain text