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 TestSignatureAlgorithm(t *testing.T) {
13 t.Parallel()
14 t.Run(`accept jwa constant ES256`, func(t *testing.T) {
15 t.Parallel()
16 var dst jwa.SignatureAlgorithm
17 if !assert.NoError(t, dst.Accept(jwa.ES256), `accept is successful`) {
18 return
19 }
20 if !assert.Equal(t, jwa.ES256, dst, `accepted value should be equal to constant`) {
21 return
22 }
23 })
24 t.Run(`accept the string ES256`, func(t *testing.T) {
25 t.Parallel()
26 var dst jwa.SignatureAlgorithm
27 if !assert.NoError(t, dst.Accept("ES256"), `accept is successful`) {
28 return
29 }
30 if !assert.Equal(t, jwa.ES256, dst, `accepted value should be equal to constant`) {
31 return
32 }
33 })
34 t.Run(`accept fmt.Stringer for ES256`, func(t *testing.T) {
35 t.Parallel()
36 var dst jwa.SignatureAlgorithm
37 if !assert.NoError(t, dst.Accept(stringer{src: "ES256"}), `accept is successful`) {
38 return
39 }
40 if !assert.Equal(t, jwa.ES256, dst, `accepted value should be equal to constant`) {
41 return
42 }
43 })
44 t.Run(`stringification for ES256`, func(t *testing.T) {
45 t.Parallel()
46 if !assert.Equal(t, "ES256", jwa.ES256.String(), `stringified value matches`) {
47 return
48 }
49 })
50 t.Run(`accept jwa constant ES256K`, func(t *testing.T) {
51 t.Parallel()
52 var dst jwa.SignatureAlgorithm
53 if !assert.NoError(t, dst.Accept(jwa.ES256K), `accept is successful`) {
54 return
55 }
56 if !assert.Equal(t, jwa.ES256K, dst, `accepted value should be equal to constant`) {
57 return
58 }
59 })
60 t.Run(`accept the string ES256K`, func(t *testing.T) {
61 t.Parallel()
62 var dst jwa.SignatureAlgorithm
63 if !assert.NoError(t, dst.Accept("ES256K"), `accept is successful`) {
64 return
65 }
66 if !assert.Equal(t, jwa.ES256K, dst, `accepted value should be equal to constant`) {
67 return
68 }
69 })
70 t.Run(`accept fmt.Stringer for ES256K`, func(t *testing.T) {
71 t.Parallel()
72 var dst jwa.SignatureAlgorithm
73 if !assert.NoError(t, dst.Accept(stringer{src: "ES256K"}), `accept is successful`) {
74 return
75 }
76 if !assert.Equal(t, jwa.ES256K, dst, `accepted value should be equal to constant`) {
77 return
78 }
79 })
80 t.Run(`stringification for ES256K`, func(t *testing.T) {
81 t.Parallel()
82 if !assert.Equal(t, "ES256K", jwa.ES256K.String(), `stringified value matches`) {
83 return
84 }
85 })
86 t.Run(`accept jwa constant ES384`, func(t *testing.T) {
87 t.Parallel()
88 var dst jwa.SignatureAlgorithm
89 if !assert.NoError(t, dst.Accept(jwa.ES384), `accept is successful`) {
90 return
91 }
92 if !assert.Equal(t, jwa.ES384, dst, `accepted value should be equal to constant`) {
93 return
94 }
95 })
96 t.Run(`accept the string ES384`, func(t *testing.T) {
97 t.Parallel()
98 var dst jwa.SignatureAlgorithm
99 if !assert.NoError(t, dst.Accept("ES384"), `accept is successful`) {
100 return
101 }
102 if !assert.Equal(t, jwa.ES384, dst, `accepted value should be equal to constant`) {
103 return
104 }
105 })
106 t.Run(`accept fmt.Stringer for ES384`, func(t *testing.T) {
107 t.Parallel()
108 var dst jwa.SignatureAlgorithm
109 if !assert.NoError(t, dst.Accept(stringer{src: "ES384"}), `accept is successful`) {
110 return
111 }
112 if !assert.Equal(t, jwa.ES384, dst, `accepted value should be equal to constant`) {
113 return
114 }
115 })
116 t.Run(`stringification for ES384`, func(t *testing.T) {
117 t.Parallel()
118 if !assert.Equal(t, "ES384", jwa.ES384.String(), `stringified value matches`) {
119 return
120 }
121 })
122 t.Run(`accept jwa constant ES512`, func(t *testing.T) {
123 t.Parallel()
124 var dst jwa.SignatureAlgorithm
125 if !assert.NoError(t, dst.Accept(jwa.ES512), `accept is successful`) {
126 return
127 }
128 if !assert.Equal(t, jwa.ES512, dst, `accepted value should be equal to constant`) {
129 return
130 }
131 })
132 t.Run(`accept the string ES512`, func(t *testing.T) {
133 t.Parallel()
134 var dst jwa.SignatureAlgorithm
135 if !assert.NoError(t, dst.Accept("ES512"), `accept is successful`) {
136 return
137 }
138 if !assert.Equal(t, jwa.ES512, dst, `accepted value should be equal to constant`) {
139 return
140 }
141 })
142 t.Run(`accept fmt.Stringer for ES512`, func(t *testing.T) {
143 t.Parallel()
144 var dst jwa.SignatureAlgorithm
145 if !assert.NoError(t, dst.Accept(stringer{src: "ES512"}), `accept is successful`) {
146 return
147 }
148 if !assert.Equal(t, jwa.ES512, dst, `accepted value should be equal to constant`) {
149 return
150 }
151 })
152 t.Run(`stringification for ES512`, func(t *testing.T) {
153 t.Parallel()
154 if !assert.Equal(t, "ES512", jwa.ES512.String(), `stringified value matches`) {
155 return
156 }
157 })
158 t.Run(`accept jwa constant EdDSA`, func(t *testing.T) {
159 t.Parallel()
160 var dst jwa.SignatureAlgorithm
161 if !assert.NoError(t, dst.Accept(jwa.EdDSA), `accept is successful`) {
162 return
163 }
164 if !assert.Equal(t, jwa.EdDSA, dst, `accepted value should be equal to constant`) {
165 return
166 }
167 })
168 t.Run(`accept the string EdDSA`, func(t *testing.T) {
169 t.Parallel()
170 var dst jwa.SignatureAlgorithm
171 if !assert.NoError(t, dst.Accept("EdDSA"), `accept is successful`) {
172 return
173 }
174 if !assert.Equal(t, jwa.EdDSA, dst, `accepted value should be equal to constant`) {
175 return
176 }
177 })
178 t.Run(`accept fmt.Stringer for EdDSA`, func(t *testing.T) {
179 t.Parallel()
180 var dst jwa.SignatureAlgorithm
181 if !assert.NoError(t, dst.Accept(stringer{src: "EdDSA"}), `accept is successful`) {
182 return
183 }
184 if !assert.Equal(t, jwa.EdDSA, dst, `accepted value should be equal to constant`) {
185 return
186 }
187 })
188 t.Run(`stringification for EdDSA`, func(t *testing.T) {
189 t.Parallel()
190 if !assert.Equal(t, "EdDSA", jwa.EdDSA.String(), `stringified value matches`) {
191 return
192 }
193 })
194 t.Run(`accept jwa constant HS256`, func(t *testing.T) {
195 t.Parallel()
196 var dst jwa.SignatureAlgorithm
197 if !assert.NoError(t, dst.Accept(jwa.HS256), `accept is successful`) {
198 return
199 }
200 if !assert.Equal(t, jwa.HS256, dst, `accepted value should be equal to constant`) {
201 return
202 }
203 })
204 t.Run(`accept the string HS256`, func(t *testing.T) {
205 t.Parallel()
206 var dst jwa.SignatureAlgorithm
207 if !assert.NoError(t, dst.Accept("HS256"), `accept is successful`) {
208 return
209 }
210 if !assert.Equal(t, jwa.HS256, dst, `accepted value should be equal to constant`) {
211 return
212 }
213 })
214 t.Run(`accept fmt.Stringer for HS256`, func(t *testing.T) {
215 t.Parallel()
216 var dst jwa.SignatureAlgorithm
217 if !assert.NoError(t, dst.Accept(stringer{src: "HS256"}), `accept is successful`) {
218 return
219 }
220 if !assert.Equal(t, jwa.HS256, dst, `accepted value should be equal to constant`) {
221 return
222 }
223 })
224 t.Run(`stringification for HS256`, func(t *testing.T) {
225 t.Parallel()
226 if !assert.Equal(t, "HS256", jwa.HS256.String(), `stringified value matches`) {
227 return
228 }
229 })
230 t.Run(`accept jwa constant HS384`, func(t *testing.T) {
231 t.Parallel()
232 var dst jwa.SignatureAlgorithm
233 if !assert.NoError(t, dst.Accept(jwa.HS384), `accept is successful`) {
234 return
235 }
236 if !assert.Equal(t, jwa.HS384, dst, `accepted value should be equal to constant`) {
237 return
238 }
239 })
240 t.Run(`accept the string HS384`, func(t *testing.T) {
241 t.Parallel()
242 var dst jwa.SignatureAlgorithm
243 if !assert.NoError(t, dst.Accept("HS384"), `accept is successful`) {
244 return
245 }
246 if !assert.Equal(t, jwa.HS384, dst, `accepted value should be equal to constant`) {
247 return
248 }
249 })
250 t.Run(`accept fmt.Stringer for HS384`, func(t *testing.T) {
251 t.Parallel()
252 var dst jwa.SignatureAlgorithm
253 if !assert.NoError(t, dst.Accept(stringer{src: "HS384"}), `accept is successful`) {
254 return
255 }
256 if !assert.Equal(t, jwa.HS384, dst, `accepted value should be equal to constant`) {
257 return
258 }
259 })
260 t.Run(`stringification for HS384`, func(t *testing.T) {
261 t.Parallel()
262 if !assert.Equal(t, "HS384", jwa.HS384.String(), `stringified value matches`) {
263 return
264 }
265 })
266 t.Run(`accept jwa constant HS512`, func(t *testing.T) {
267 t.Parallel()
268 var dst jwa.SignatureAlgorithm
269 if !assert.NoError(t, dst.Accept(jwa.HS512), `accept is successful`) {
270 return
271 }
272 if !assert.Equal(t, jwa.HS512, dst, `accepted value should be equal to constant`) {
273 return
274 }
275 })
276 t.Run(`accept the string HS512`, func(t *testing.T) {
277 t.Parallel()
278 var dst jwa.SignatureAlgorithm
279 if !assert.NoError(t, dst.Accept("HS512"), `accept is successful`) {
280 return
281 }
282 if !assert.Equal(t, jwa.HS512, dst, `accepted value should be equal to constant`) {
283 return
284 }
285 })
286 t.Run(`accept fmt.Stringer for HS512`, func(t *testing.T) {
287 t.Parallel()
288 var dst jwa.SignatureAlgorithm
289 if !assert.NoError(t, dst.Accept(stringer{src: "HS512"}), `accept is successful`) {
290 return
291 }
292 if !assert.Equal(t, jwa.HS512, dst, `accepted value should be equal to constant`) {
293 return
294 }
295 })
296 t.Run(`stringification for HS512`, func(t *testing.T) {
297 t.Parallel()
298 if !assert.Equal(t, "HS512", jwa.HS512.String(), `stringified value matches`) {
299 return
300 }
301 })
302 t.Run(`accept jwa constant NoSignature`, func(t *testing.T) {
303 t.Parallel()
304 var dst jwa.SignatureAlgorithm
305 if !assert.NoError(t, dst.Accept(jwa.NoSignature), `accept is successful`) {
306 return
307 }
308 if !assert.Equal(t, jwa.NoSignature, dst, `accepted value should be equal to constant`) {
309 return
310 }
311 })
312 t.Run(`accept the string none`, func(t *testing.T) {
313 t.Parallel()
314 var dst jwa.SignatureAlgorithm
315 if !assert.NoError(t, dst.Accept("none"), `accept is successful`) {
316 return
317 }
318 if !assert.Equal(t, jwa.NoSignature, dst, `accepted value should be equal to constant`) {
319 return
320 }
321 })
322 t.Run(`accept fmt.Stringer for none`, func(t *testing.T) {
323 t.Parallel()
324 var dst jwa.SignatureAlgorithm
325 if !assert.NoError(t, dst.Accept(stringer{src: "none"}), `accept is successful`) {
326 return
327 }
328 if !assert.Equal(t, jwa.NoSignature, dst, `accepted value should be equal to constant`) {
329 return
330 }
331 })
332 t.Run(`stringification for none`, func(t *testing.T) {
333 t.Parallel()
334 if !assert.Equal(t, "none", jwa.NoSignature.String(), `stringified value matches`) {
335 return
336 }
337 })
338 t.Run(`accept jwa constant PS256`, func(t *testing.T) {
339 t.Parallel()
340 var dst jwa.SignatureAlgorithm
341 if !assert.NoError(t, dst.Accept(jwa.PS256), `accept is successful`) {
342 return
343 }
344 if !assert.Equal(t, jwa.PS256, dst, `accepted value should be equal to constant`) {
345 return
346 }
347 })
348 t.Run(`accept the string PS256`, func(t *testing.T) {
349 t.Parallel()
350 var dst jwa.SignatureAlgorithm
351 if !assert.NoError(t, dst.Accept("PS256"), `accept is successful`) {
352 return
353 }
354 if !assert.Equal(t, jwa.PS256, dst, `accepted value should be equal to constant`) {
355 return
356 }
357 })
358 t.Run(`accept fmt.Stringer for PS256`, func(t *testing.T) {
359 t.Parallel()
360 var dst jwa.SignatureAlgorithm
361 if !assert.NoError(t, dst.Accept(stringer{src: "PS256"}), `accept is successful`) {
362 return
363 }
364 if !assert.Equal(t, jwa.PS256, dst, `accepted value should be equal to constant`) {
365 return
366 }
367 })
368 t.Run(`stringification for PS256`, func(t *testing.T) {
369 t.Parallel()
370 if !assert.Equal(t, "PS256", jwa.PS256.String(), `stringified value matches`) {
371 return
372 }
373 })
374 t.Run(`accept jwa constant PS384`, func(t *testing.T) {
375 t.Parallel()
376 var dst jwa.SignatureAlgorithm
377 if !assert.NoError(t, dst.Accept(jwa.PS384), `accept is successful`) {
378 return
379 }
380 if !assert.Equal(t, jwa.PS384, dst, `accepted value should be equal to constant`) {
381 return
382 }
383 })
384 t.Run(`accept the string PS384`, func(t *testing.T) {
385 t.Parallel()
386 var dst jwa.SignatureAlgorithm
387 if !assert.NoError(t, dst.Accept("PS384"), `accept is successful`) {
388 return
389 }
390 if !assert.Equal(t, jwa.PS384, dst, `accepted value should be equal to constant`) {
391 return
392 }
393 })
394 t.Run(`accept fmt.Stringer for PS384`, func(t *testing.T) {
395 t.Parallel()
396 var dst jwa.SignatureAlgorithm
397 if !assert.NoError(t, dst.Accept(stringer{src: "PS384"}), `accept is successful`) {
398 return
399 }
400 if !assert.Equal(t, jwa.PS384, dst, `accepted value should be equal to constant`) {
401 return
402 }
403 })
404 t.Run(`stringification for PS384`, func(t *testing.T) {
405 t.Parallel()
406 if !assert.Equal(t, "PS384", jwa.PS384.String(), `stringified value matches`) {
407 return
408 }
409 })
410 t.Run(`accept jwa constant PS512`, func(t *testing.T) {
411 t.Parallel()
412 var dst jwa.SignatureAlgorithm
413 if !assert.NoError(t, dst.Accept(jwa.PS512), `accept is successful`) {
414 return
415 }
416 if !assert.Equal(t, jwa.PS512, dst, `accepted value should be equal to constant`) {
417 return
418 }
419 })
420 t.Run(`accept the string PS512`, func(t *testing.T) {
421 t.Parallel()
422 var dst jwa.SignatureAlgorithm
423 if !assert.NoError(t, dst.Accept("PS512"), `accept is successful`) {
424 return
425 }
426 if !assert.Equal(t, jwa.PS512, dst, `accepted value should be equal to constant`) {
427 return
428 }
429 })
430 t.Run(`accept fmt.Stringer for PS512`, func(t *testing.T) {
431 t.Parallel()
432 var dst jwa.SignatureAlgorithm
433 if !assert.NoError(t, dst.Accept(stringer{src: "PS512"}), `accept is successful`) {
434 return
435 }
436 if !assert.Equal(t, jwa.PS512, dst, `accepted value should be equal to constant`) {
437 return
438 }
439 })
440 t.Run(`stringification for PS512`, func(t *testing.T) {
441 t.Parallel()
442 if !assert.Equal(t, "PS512", jwa.PS512.String(), `stringified value matches`) {
443 return
444 }
445 })
446 t.Run(`accept jwa constant RS256`, func(t *testing.T) {
447 t.Parallel()
448 var dst jwa.SignatureAlgorithm
449 if !assert.NoError(t, dst.Accept(jwa.RS256), `accept is successful`) {
450 return
451 }
452 if !assert.Equal(t, jwa.RS256, dst, `accepted value should be equal to constant`) {
453 return
454 }
455 })
456 t.Run(`accept the string RS256`, func(t *testing.T) {
457 t.Parallel()
458 var dst jwa.SignatureAlgorithm
459 if !assert.NoError(t, dst.Accept("RS256"), `accept is successful`) {
460 return
461 }
462 if !assert.Equal(t, jwa.RS256, dst, `accepted value should be equal to constant`) {
463 return
464 }
465 })
466 t.Run(`accept fmt.Stringer for RS256`, func(t *testing.T) {
467 t.Parallel()
468 var dst jwa.SignatureAlgorithm
469 if !assert.NoError(t, dst.Accept(stringer{src: "RS256"}), `accept is successful`) {
470 return
471 }
472 if !assert.Equal(t, jwa.RS256, dst, `accepted value should be equal to constant`) {
473 return
474 }
475 })
476 t.Run(`stringification for RS256`, func(t *testing.T) {
477 t.Parallel()
478 if !assert.Equal(t, "RS256", jwa.RS256.String(), `stringified value matches`) {
479 return
480 }
481 })
482 t.Run(`accept jwa constant RS384`, func(t *testing.T) {
483 t.Parallel()
484 var dst jwa.SignatureAlgorithm
485 if !assert.NoError(t, dst.Accept(jwa.RS384), `accept is successful`) {
486 return
487 }
488 if !assert.Equal(t, jwa.RS384, dst, `accepted value should be equal to constant`) {
489 return
490 }
491 })
492 t.Run(`accept the string RS384`, func(t *testing.T) {
493 t.Parallel()
494 var dst jwa.SignatureAlgorithm
495 if !assert.NoError(t, dst.Accept("RS384"), `accept is successful`) {
496 return
497 }
498 if !assert.Equal(t, jwa.RS384, dst, `accepted value should be equal to constant`) {
499 return
500 }
501 })
502 t.Run(`accept fmt.Stringer for RS384`, func(t *testing.T) {
503 t.Parallel()
504 var dst jwa.SignatureAlgorithm
505 if !assert.NoError(t, dst.Accept(stringer{src: "RS384"}), `accept is successful`) {
506 return
507 }
508 if !assert.Equal(t, jwa.RS384, dst, `accepted value should be equal to constant`) {
509 return
510 }
511 })
512 t.Run(`stringification for RS384`, func(t *testing.T) {
513 t.Parallel()
514 if !assert.Equal(t, "RS384", jwa.RS384.String(), `stringified value matches`) {
515 return
516 }
517 })
518 t.Run(`accept jwa constant RS512`, func(t *testing.T) {
519 t.Parallel()
520 var dst jwa.SignatureAlgorithm
521 if !assert.NoError(t, dst.Accept(jwa.RS512), `accept is successful`) {
522 return
523 }
524 if !assert.Equal(t, jwa.RS512, dst, `accepted value should be equal to constant`) {
525 return
526 }
527 })
528 t.Run(`accept the string RS512`, func(t *testing.T) {
529 t.Parallel()
530 var dst jwa.SignatureAlgorithm
531 if !assert.NoError(t, dst.Accept("RS512"), `accept is successful`) {
532 return
533 }
534 if !assert.Equal(t, jwa.RS512, dst, `accepted value should be equal to constant`) {
535 return
536 }
537 })
538 t.Run(`accept fmt.Stringer for RS512`, func(t *testing.T) {
539 t.Parallel()
540 var dst jwa.SignatureAlgorithm
541 if !assert.NoError(t, dst.Accept(stringer{src: "RS512"}), `accept is successful`) {
542 return
543 }
544 if !assert.Equal(t, jwa.RS512, dst, `accepted value should be equal to constant`) {
545 return
546 }
547 })
548 t.Run(`stringification for RS512`, func(t *testing.T) {
549 t.Parallel()
550 if !assert.Equal(t, "RS512", jwa.RS512.String(), `stringified value matches`) {
551 return
552 }
553 })
554 t.Run(`bail out on random integer value`, func(t *testing.T) {
555 t.Parallel()
556 var dst jwa.SignatureAlgorithm
557 if !assert.Error(t, dst.Accept(1), `accept should fail`) {
558 return
559 }
560 })
561 t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
562 t.Parallel()
563 var dst jwa.SignatureAlgorithm
564 if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
565 return
566 }
567 })
568 t.Run(`check list of elements`, func(t *testing.T) {
569 t.Parallel()
570 var expected = map[jwa.SignatureAlgorithm]struct{}{
571 jwa.ES256: {},
572 jwa.ES256K: {},
573 jwa.ES384: {},
574 jwa.ES512: {},
575 jwa.EdDSA: {},
576 jwa.HS256: {},
577 jwa.HS384: {},
578 jwa.HS512: {},
579 jwa.NoSignature: {},
580 jwa.PS256: {},
581 jwa.PS384: {},
582 jwa.PS512: {},
583 jwa.RS256: {},
584 jwa.RS384: {},
585 jwa.RS512: {},
586 }
587 for _, v := range jwa.SignatureAlgorithms() {
588 if _, ok := expected[v]; !assert.True(t, ok, `%s should be in the expected list`, v) {
589 return
590 }
591 delete(expected, v)
592 }
593 if !assert.Len(t, expected, 0) {
594 return
595 }
596 })
597 }
598
View as plain text