Skip to content

Commit 315fbb1

Browse files
authored
Refactor tests (cristalhq#152)
1 parent 1369dc6 commit 315fbb1

12 files changed

+777
-654
lines changed

algo_eddsa_test.go

+40-21
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,61 @@ import (
66
)
77

88
func TestEdDSA(t *testing.T) {
9-
f := func(privateKey ed25519.PrivateKey, publicKey ed25519.PublicKey, wantErr error) {
10-
t.Helper()
9+
testCases := []struct {
10+
privateKey ed25519.PrivateKey
11+
publicKey ed25519.PublicKey
12+
wantErr error
13+
}{
14+
{ed25519PrivateKey, ed25519PublicKey, nil},
15+
{ed25519PrivateKey, ed25519PublicKeyAnother, ErrInvalidSignature},
16+
{ed25519PrivateKeyAnother, ed25519PublicKey, ErrInvalidSignature},
17+
}
1118

12-
signer, err := NewSignerEdDSA(privateKey)
19+
for _, tc := range testCases {
20+
signer, err := NewSignerEdDSA(tc.privateKey)
1321
mustOk(t, err)
1422

15-
verifier, err := NewVerifierEdDSA(publicKey)
23+
verifier, err := NewVerifierEdDSA(tc.publicKey)
1624
mustOk(t, err)
1725

1826
token, err := NewBuilder(signer).Build(simplePayload)
1927
mustOk(t, err)
2028

2129
err = verifier.Verify(token)
22-
mustEqual(t, err, wantErr)
30+
mustEqual(t, err, tc.wantErr)
2331
}
24-
25-
f(ed25519PrivateKey, ed25519PublicKey, nil)
26-
f(ed25519PrivateKey, ed25519PublicKeyAnother, ErrInvalidSignature)
27-
f(ed25519PrivateKeyAnother, ed25519PublicKey, ErrInvalidSignature)
2832
}
2933

3034
func TestEdDSA_BadKeys(t *testing.T) {
31-
f := func(err, wantErr error) {
32-
t.Helper()
33-
mustEqual(t, err, wantErr)
35+
testCases := []struct {
36+
err error
37+
wantErr error
38+
}{
39+
{
40+
getErr(NewSignerEdDSA(nil)), ErrNilKey,
41+
},
42+
{
43+
getErr(NewVerifierEdDSA(nil)), ErrNilKey,
44+
},
45+
{
46+
err: func() error {
47+
key := ed25519.PrivateKey(make([]byte, 72))
48+
return getErr(NewSignerEdDSA(key))
49+
}(),
50+
wantErr: ErrInvalidKey,
51+
},
52+
{
53+
err: func() error {
54+
key := ed25519.PublicKey(make([]byte, 72))
55+
return getErr(NewVerifierEdDSA(key))
56+
}(),
57+
wantErr: ErrInvalidKey,
58+
},
3459
}
3560

36-
f(getSignerError(NewSignerEdDSA(nil)), ErrNilKey)
37-
38-
priv := ed25519.PrivateKey(make([]byte, 72))
39-
f(getSignerError(NewSignerEdDSA(priv)), ErrInvalidKey)
40-
41-
f(getVerifierError(NewVerifierEdDSA(nil)), ErrNilKey)
42-
43-
pub := ed25519.PublicKey(make([]byte, 72))
44-
f(getVerifierError(NewVerifierEdDSA(pub)), ErrInvalidKey)
61+
for _, tc := range testCases {
62+
mustEqual(t, tc.err, tc.wantErr)
63+
}
4564
}
4665

4766
var (

algo_es_test.go

+54-45
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,75 @@ import (
66
)
77

88
func TestES(t *testing.T) {
9-
f := func(alg Algorithm, privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, wantErr error) {
10-
t.Helper()
9+
testCases := []struct {
10+
alg Algorithm
11+
privateKey *ecdsa.PrivateKey
12+
publicKey *ecdsa.PublicKey
13+
wantErr error
14+
}{
15+
{ES256, ecdsaPrivateKey256, ecdsaPublicKey256, nil},
16+
{ES384, ecdsaPrivateKey384, ecdsaPublicKey384, nil},
17+
{ES512, ecdsaPrivateKey521, ecdsaPublicKey521, nil},
18+
19+
{ES256, ecdsaPrivateKey256, ecdsaPublicKey256Another, ErrInvalidSignature},
20+
{ES384, ecdsaPrivateKey384, ecdsaPublicKey384Another, ErrInvalidSignature},
21+
{ES512, ecdsaPrivateKey521, ecdsaPublicKey521Another, ErrInvalidSignature},
22+
23+
{ES256, ecdsaPrivateKey256Another, ecdsaPublicKey256, ErrInvalidSignature},
24+
{ES384, ecdsaPrivateKey384Another, ecdsaPublicKey384, ErrInvalidSignature},
25+
{ES512, ecdsaPrivateKey521Another, ecdsaPublicKey521, ErrInvalidSignature},
26+
}
1127

12-
signer, err := NewSignerES(alg, privateKey)
28+
for _, tc := range testCases {
29+
signer, err := NewSignerES(tc.alg, tc.privateKey)
1330
mustOk(t, err)
1431

15-
verifier, err := NewVerifierES(alg, publicKey)
32+
verifier, err := NewVerifierES(tc.alg, tc.publicKey)
1633
mustOk(t, err)
1734

1835
token, err := NewBuilder(signer).Build(simplePayload)
1936
mustOk(t, err)
2037

2138
err = verifier.Verify(token)
22-
mustEqual(t, err, wantErr)
39+
mustEqual(t, err, tc.wantErr)
2340
}
24-
25-
f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256, nil)
26-
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384, nil)
27-
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521, nil)
28-
29-
f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256Another, ErrInvalidSignature)
30-
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384Another, ErrInvalidSignature)
31-
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521Another, ErrInvalidSignature)
32-
33-
f(ES256, ecdsaPrivateKey256Another, ecdsaPublicKey256, ErrInvalidSignature)
34-
f(ES384, ecdsaPrivateKey384Another, ecdsaPublicKey384, ErrInvalidSignature)
35-
f(ES512, ecdsaPrivateKey521Another, ecdsaPublicKey521, ErrInvalidSignature)
3641
}
3742

3843
func TestES_BadKeys(t *testing.T) {
39-
f := func(err, wantErr error) {
40-
t.Helper()
41-
mustEqual(t, err, wantErr)
44+
testCases := []struct {
45+
err error
46+
wantErr error
47+
}{
48+
{getErr(NewSignerES(ES256, nil)), ErrNilKey},
49+
{getErr(NewSignerES(ES384, nil)), ErrNilKey},
50+
{getErr(NewSignerES(ES512, nil)), ErrNilKey},
51+
52+
{getErr(NewSignerES("foo", ecdsaPrivateKey384)), ErrUnsupportedAlg},
53+
54+
{getErr(NewSignerES(ES256, ecdsaPrivateKey384)), ErrInvalidKey},
55+
{getErr(NewSignerES(ES256, ecdsaPrivateKey521)), ErrInvalidKey},
56+
{getErr(NewSignerES(ES384, ecdsaPrivateKey256)), ErrInvalidKey},
57+
{getErr(NewSignerES(ES384, ecdsaPrivateKey521)), ErrInvalidKey},
58+
{getErr(NewSignerES(ES512, ecdsaPrivateKey256)), ErrInvalidKey},
59+
{getErr(NewSignerES(ES512, ecdsaPrivateKey384)), ErrInvalidKey},
60+
61+
{getErr(NewVerifierES(ES256, nil)), ErrNilKey},
62+
{getErr(NewVerifierES(ES384, nil)), ErrNilKey},
63+
{getErr(NewVerifierES(ES512, nil)), ErrNilKey},
64+
65+
{getErr(NewVerifierES("boo", ecdsaPublicKey384)), ErrUnsupportedAlg},
66+
67+
{getErr(NewVerifierES(ES256, ecdsaPublicKey384)), ErrInvalidKey},
68+
{getErr(NewVerifierES(ES256, ecdsaPublicKey521)), ErrInvalidKey},
69+
{getErr(NewVerifierES(ES384, ecdsaPublicKey256)), ErrInvalidKey},
70+
{getErr(NewVerifierES(ES384, ecdsaPublicKey521)), ErrInvalidKey},
71+
{getErr(NewVerifierES(ES512, ecdsaPublicKey256)), ErrInvalidKey},
72+
{getErr(NewVerifierES(ES512, ecdsaPublicKey384)), ErrInvalidKey},
4273
}
4374

44-
f(getSignerError(NewSignerES(ES256, nil)), ErrNilKey)
45-
f(getSignerError(NewSignerES(ES384, nil)), ErrNilKey)
46-
f(getSignerError(NewSignerES(ES512, nil)), ErrNilKey)
47-
48-
f(getSignerError(NewSignerES("foo", ecdsaPrivateKey384)), ErrUnsupportedAlg)
49-
50-
f(getSignerError(NewSignerES(ES256, ecdsaPrivateKey384)), ErrInvalidKey)
51-
f(getSignerError(NewSignerES(ES256, ecdsaPrivateKey521)), ErrInvalidKey)
52-
f(getSignerError(NewSignerES(ES384, ecdsaPrivateKey256)), ErrInvalidKey)
53-
f(getSignerError(NewSignerES(ES384, ecdsaPrivateKey521)), ErrInvalidKey)
54-
f(getSignerError(NewSignerES(ES512, ecdsaPrivateKey256)), ErrInvalidKey)
55-
f(getSignerError(NewSignerES(ES512, ecdsaPrivateKey384)), ErrInvalidKey)
56-
57-
f(getVerifierError(NewVerifierES(ES256, nil)), ErrNilKey)
58-
f(getVerifierError(NewVerifierES(ES384, nil)), ErrNilKey)
59-
f(getVerifierError(NewVerifierES(ES512, nil)), ErrNilKey)
60-
61-
f(getVerifierError(NewVerifierES("boo", ecdsaPublicKey384)), ErrUnsupportedAlg)
62-
63-
f(getVerifierError(NewVerifierES(ES256, ecdsaPublicKey384)), ErrInvalidKey)
64-
f(getVerifierError(NewVerifierES(ES256, ecdsaPublicKey521)), ErrInvalidKey)
65-
f(getVerifierError(NewVerifierES(ES384, ecdsaPublicKey256)), ErrInvalidKey)
66-
f(getVerifierError(NewVerifierES(ES384, ecdsaPublicKey521)), ErrInvalidKey)
67-
f(getVerifierError(NewVerifierES(ES512, ecdsaPublicKey256)), ErrInvalidKey)
68-
f(getVerifierError(NewVerifierES(ES512, ecdsaPublicKey384)), ErrInvalidKey)
75+
for _, tc := range testCases {
76+
mustEqual(t, tc.err, tc.wantErr)
77+
}
6978
}
7079

7180
var (

algo_hs_test.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@ import (
55
)
66

77
func TestHS(t *testing.T) {
8-
f := func(alg Algorithm, signKey, verifyKey []byte, wantErr error) {
9-
t.Helper()
8+
testCases := []struct {
9+
alg Algorithm
10+
signKey []byte
11+
verifyKey []byte
12+
wantErr error
13+
}{
14+
{HS256, hsKey256, hsKey256, nil},
15+
{HS384, hsKey384, hsKey384, nil},
16+
{HS512, hsKey512, hsKey512, nil},
17+
18+
{HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature},
19+
{HS384, hsKey384, hsKeyAnother384, ErrInvalidSignature},
20+
{HS512, hsKey512, hsKeyAnother512, ErrInvalidSignature},
21+
22+
{HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature},
23+
}
1024

11-
signer, err := NewSignerHS(alg, signKey)
25+
for _, tc := range testCases {
26+
signer, err := NewSignerHS(tc.alg, tc.signKey)
1227
mustOk(t, err)
1328

14-
verifier, err := NewVerifierHS(alg, verifyKey)
29+
verifier, err := NewVerifierHS(tc.alg, tc.verifyKey)
1530
mustOk(t, err)
1631

1732
token, err := NewBuilder(signer).Build(simplePayload)
1833
mustOk(t, err)
1934

2035
err = verifier.Verify(token)
21-
mustEqual(t, err, wantErr)
36+
mustEqual(t, err, tc.wantErr)
2237
}
23-
24-
f(HS256, hsKey256, hsKey256, nil)
25-
f(HS384, hsKey384, hsKey384, nil)
26-
f(HS512, hsKey512, hsKey512, nil)
27-
28-
f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
29-
f(HS384, hsKey384, hsKeyAnother384, ErrInvalidSignature)
30-
f(HS512, hsKey512, hsKeyAnother512, ErrInvalidSignature)
31-
32-
f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
3338
}
3439

3540
var (

algo_ps_test.go

+40-31
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,61 @@ import (
66
)
77

88
func TestPS(t *testing.T) {
9-
f := func(alg Algorithm, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, wantErr error) {
10-
t.Helper()
9+
testCases := []struct {
10+
alg Algorithm
11+
privateKey *rsa.PrivateKey
12+
publicKey *rsa.PublicKey
13+
wantErr error
14+
}{
15+
{PS256, rsapsPrivateKey256, rsapsPublicKey256, nil},
16+
{PS384, rsapsPrivateKey384, rsapsPublicKey384, nil},
17+
{PS512, rsapsPrivateKey512, rsapsPublicKey512, nil},
18+
{PS512, rsapsPrivateKey512Other, rsapsPublicKey512Other, nil},
19+
20+
{PS256, rsapsPrivateKey256, rsapsPublicKey256Another, ErrInvalidSignature},
21+
{PS384, rsapsPrivateKey384, rsapsPublicKey384Another, ErrInvalidSignature},
22+
{PS512, rsapsPrivateKey512, rsapsPublicKey512Another, ErrInvalidSignature},
23+
24+
{PS256, rsapsPrivateKey256Another, rsapsPublicKey256, ErrInvalidSignature},
25+
{PS384, rsapsPrivateKey384Another, rsapsPublicKey384, ErrInvalidSignature},
26+
{PS512, rsapsPrivateKey512Another, rsapsPublicKey512, ErrInvalidSignature},
27+
{PS512, rsapsPrivateKey512Another, rsapsPublicKey512Other, ErrInvalidSignature},
28+
}
1129

12-
signer, errSigner := NewSignerPS(alg, privateKey)
30+
for _, tc := range testCases {
31+
signer, errSigner := NewSignerPS(tc.alg, tc.privateKey)
1332
mustOk(t, errSigner)
1433

15-
verifier, errVerifier := NewVerifierPS(alg, publicKey)
34+
verifier, errVerifier := NewVerifierPS(tc.alg, tc.publicKey)
1635
mustOk(t, errVerifier)
1736

1837
token, err := NewBuilder(signer).Build(simplePayload)
1938
mustOk(t, err)
2039

2140
err = verifier.Verify(token)
22-
mustEqual(t, err, wantErr)
41+
mustEqual(t, err, tc.wantErr)
2342
}
24-
25-
f(PS256, rsapsPrivateKey256, rsapsPublicKey256, nil)
26-
f(PS384, rsapsPrivateKey384, rsapsPublicKey384, nil)
27-
f(PS512, rsapsPrivateKey512, rsapsPublicKey512, nil)
28-
f(PS512, rsapsPrivateKey512Other, rsapsPublicKey512Other, nil)
29-
30-
f(PS256, rsapsPrivateKey256, rsapsPublicKey256Another, ErrInvalidSignature)
31-
f(PS384, rsapsPrivateKey384, rsapsPublicKey384Another, ErrInvalidSignature)
32-
f(PS512, rsapsPrivateKey512, rsapsPublicKey512Another, ErrInvalidSignature)
33-
34-
f(PS256, rsapsPrivateKey256Another, rsapsPublicKey256, ErrInvalidSignature)
35-
f(PS384, rsapsPrivateKey384Another, rsapsPublicKey384, ErrInvalidSignature)
36-
f(PS512, rsapsPrivateKey512Another, rsapsPublicKey512, ErrInvalidSignature)
37-
f(PS512, rsapsPrivateKey512Another, rsapsPublicKey512Other, ErrInvalidSignature)
3843
}
3944

4045
func TestPS_BadKeys(t *testing.T) {
41-
f := func(err, wantErr error) {
42-
t.Helper()
43-
mustEqual(t, err, wantErr)
46+
testCases := []struct {
47+
err error
48+
wantErr error
49+
}{
50+
{getErr(NewSignerPS(PS256, nil)), ErrNilKey},
51+
{getErr(NewSignerPS(PS384, nil)), ErrNilKey},
52+
{getErr(NewSignerPS(PS512, nil)), ErrNilKey},
53+
{getErr(NewSignerPS("foo", rsapsPrivateKey384)), ErrUnsupportedAlg},
54+
55+
{getErr(NewVerifierPS(PS256, nil)), ErrNilKey},
56+
{getErr(NewVerifierPS(PS384, nil)), ErrNilKey},
57+
{getErr(NewVerifierPS(PS512, nil)), ErrNilKey},
58+
{getErr(NewVerifierPS("boo", rsapsPublicKey384)), ErrUnsupportedAlg},
4459
}
4560

46-
f(getSignerError(NewSignerPS(PS256, nil)), ErrNilKey)
47-
f(getSignerError(NewSignerPS(PS384, nil)), ErrNilKey)
48-
f(getSignerError(NewSignerPS(PS512, nil)), ErrNilKey)
49-
f(getSignerError(NewSignerPS("foo", rsapsPrivateKey384)), ErrUnsupportedAlg)
50-
51-
f(getVerifierError(NewVerifierPS(PS256, nil)), ErrNilKey)
52-
f(getVerifierError(NewVerifierPS(PS384, nil)), ErrNilKey)
53-
f(getVerifierError(NewVerifierPS(PS512, nil)), ErrNilKey)
54-
f(getVerifierError(NewVerifierPS("boo", rsapsPublicKey384)), ErrUnsupportedAlg)
61+
for _, tc := range testCases {
62+
mustEqual(t, tc.err, tc.wantErr)
63+
}
5564
}
5665

5766
var (

0 commit comments

Comments
 (0)