-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
ed25519_test.go
98 lines (93 loc) · 2.54 KB
/
ed25519_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
package jwt_test
import (
"testing"
"github.com/gbrlsnchs/jwt/v3"
"github.com/gbrlsnchs/jwt/v3/internal"
"github.com/google/go-cmp/cmp"
)
var (
ed25519PrivateKey1, ed25519PublicKey1 = internal.GenerateEd25519Keys()
ed25519PrivateKey2, ed25519PublicKey2 = internal.GenerateEd25519Keys()
ed25519TestCases = []testCase{
{
alg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey1)),
payload: tp,
verifyAlg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey1)),
wantHeader: jwt.Header{
Algorithm: "EdDSA",
Type: "JWT",
},
wantPayload: tp,
signErr: nil,
verifyErr: nil,
},
{
alg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey1)),
payload: tp,
verifyAlg: jwt.NewEd25519(jwt.Ed25519PublicKey(ed25519PublicKey1)),
wantHeader: jwt.Header{
Algorithm: "EdDSA",
Type: "JWT",
},
wantPayload: tp,
signErr: nil,
verifyErr: nil,
},
{
alg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey1)),
payload: tp,
verifyAlg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey2)),
wantHeader: jwt.Header{
Algorithm: "EdDSA",
Type: "JWT",
},
wantPayload: testPayload{},
signErr: nil,
verifyErr: jwt.ErrEd25519Verification,
},
{
alg: jwt.NewEd25519(jwt.Ed25519PrivateKey(ed25519PrivateKey1)),
payload: tp,
verifyAlg: jwt.NewEd25519(jwt.Ed25519PublicKey(ed25519PublicKey2)),
wantHeader: jwt.Header{
Algorithm: "EdDSA",
Type: "JWT",
},
wantPayload: testPayload{},
signErr: nil,
verifyErr: jwt.ErrEd25519Verification,
},
}
)
func TestNewEd25519(t *testing.T) {
testCases := []struct {
builder func(...func(*jwt.Ed25519)) *jwt.Ed25519
opts func(*jwt.Ed25519)
err error
}{
{jwt.NewEd25519, nil, jwt.ErrEd25519NilPrivKey},
{jwt.NewEd25519, jwt.Ed25519PrivateKey(nil), jwt.ErrEd25519NilPrivKey},
{jwt.NewEd25519, jwt.Ed25519PrivateKey(ed25519PrivateKey1), nil},
{jwt.NewEd25519, jwt.Ed25519PublicKey(ed25519PublicKey1), nil},
}
for _, tc := range testCases {
funcName := funcName(tc.builder)
t.Run(funcName, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
t.Fatal("r is not an error")
}
if want, got := tc.err, err; !internal.ErrorIs(got, want) {
t.Fatalf("jwt.%s err mismatch (-want +got):\n%s", funcName, cmp.Diff(want, got))
}
}
}()
_ = tc.builder(tc.opts)
if tc.err != nil {
t.Fatalf("jwt.%s didn't panicked", funcName)
}
})
}
}