Skip to content

Commit

Permalink
Refactor tests (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Nov 30, 2023
1 parent 1369dc6 commit 315fbb1
Show file tree
Hide file tree
Showing 12 changed files with 777 additions and 654 deletions.
61 changes: 40 additions & 21 deletions algo_eddsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,61 @@ import (
)

func TestEdDSA(t *testing.T) {
f := func(privateKey ed25519.PrivateKey, publicKey ed25519.PublicKey, wantErr error) {
t.Helper()
testCases := []struct {
privateKey ed25519.PrivateKey
publicKey ed25519.PublicKey
wantErr error
}{
{ed25519PrivateKey, ed25519PublicKey, nil},
{ed25519PrivateKey, ed25519PublicKeyAnother, ErrInvalidSignature},
{ed25519PrivateKeyAnother, ed25519PublicKey, ErrInvalidSignature},
}

signer, err := NewSignerEdDSA(privateKey)
for _, tc := range testCases {
signer, err := NewSignerEdDSA(tc.privateKey)
mustOk(t, err)

verifier, err := NewVerifierEdDSA(publicKey)
verifier, err := NewVerifierEdDSA(tc.publicKey)
mustOk(t, err)

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

err = verifier.Verify(token)
mustEqual(t, err, wantErr)
mustEqual(t, err, tc.wantErr)
}

f(ed25519PrivateKey, ed25519PublicKey, nil)
f(ed25519PrivateKey, ed25519PublicKeyAnother, ErrInvalidSignature)
f(ed25519PrivateKeyAnother, ed25519PublicKey, ErrInvalidSignature)
}

func TestEdDSA_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()
mustEqual(t, err, wantErr)
testCases := []struct {
err error
wantErr error
}{
{
getErr(NewSignerEdDSA(nil)), ErrNilKey,
},
{
getErr(NewVerifierEdDSA(nil)), ErrNilKey,
},
{
err: func() error {
key := ed25519.PrivateKey(make([]byte, 72))
return getErr(NewSignerEdDSA(key))
}(),
wantErr: ErrInvalidKey,
},
{
err: func() error {
key := ed25519.PublicKey(make([]byte, 72))
return getErr(NewVerifierEdDSA(key))
}(),
wantErr: ErrInvalidKey,
},
}

f(getSignerError(NewSignerEdDSA(nil)), ErrNilKey)

priv := ed25519.PrivateKey(make([]byte, 72))
f(getSignerError(NewSignerEdDSA(priv)), ErrInvalidKey)

f(getVerifierError(NewVerifierEdDSA(nil)), ErrNilKey)

pub := ed25519.PublicKey(make([]byte, 72))
f(getVerifierError(NewVerifierEdDSA(pub)), ErrInvalidKey)
for _, tc := range testCases {
mustEqual(t, tc.err, tc.wantErr)
}
}

var (
Expand Down
99 changes: 54 additions & 45 deletions algo_es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,75 @@ import (
)

func TestES(t *testing.T) {
f := func(alg Algorithm, privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, wantErr error) {
t.Helper()
testCases := []struct {
alg Algorithm
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
wantErr error
}{
{ES256, ecdsaPrivateKey256, ecdsaPublicKey256, nil},
{ES384, ecdsaPrivateKey384, ecdsaPublicKey384, nil},
{ES512, ecdsaPrivateKey521, ecdsaPublicKey521, nil},

{ES256, ecdsaPrivateKey256, ecdsaPublicKey256Another, ErrInvalidSignature},
{ES384, ecdsaPrivateKey384, ecdsaPublicKey384Another, ErrInvalidSignature},
{ES512, ecdsaPrivateKey521, ecdsaPublicKey521Another, ErrInvalidSignature},

{ES256, ecdsaPrivateKey256Another, ecdsaPublicKey256, ErrInvalidSignature},
{ES384, ecdsaPrivateKey384Another, ecdsaPublicKey384, ErrInvalidSignature},
{ES512, ecdsaPrivateKey521Another, ecdsaPublicKey521, ErrInvalidSignature},
}

signer, err := NewSignerES(alg, privateKey)
for _, tc := range testCases {
signer, err := NewSignerES(tc.alg, tc.privateKey)
mustOk(t, err)

verifier, err := NewVerifierES(alg, publicKey)
verifier, err := NewVerifierES(tc.alg, tc.publicKey)
mustOk(t, err)

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

err = verifier.Verify(token)
mustEqual(t, err, wantErr)
mustEqual(t, err, tc.wantErr)
}

f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256, nil)
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384, nil)
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521, nil)

f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256Another, ErrInvalidSignature)
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384Another, ErrInvalidSignature)
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521Another, ErrInvalidSignature)

f(ES256, ecdsaPrivateKey256Another, ecdsaPublicKey256, ErrInvalidSignature)
f(ES384, ecdsaPrivateKey384Another, ecdsaPublicKey384, ErrInvalidSignature)
f(ES512, ecdsaPrivateKey521Another, ecdsaPublicKey521, ErrInvalidSignature)
}

func TestES_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()
mustEqual(t, err, wantErr)
testCases := []struct {
err error
wantErr error
}{
{getErr(NewSignerES(ES256, nil)), ErrNilKey},
{getErr(NewSignerES(ES384, nil)), ErrNilKey},
{getErr(NewSignerES(ES512, nil)), ErrNilKey},

{getErr(NewSignerES("foo", ecdsaPrivateKey384)), ErrUnsupportedAlg},

{getErr(NewSignerES(ES256, ecdsaPrivateKey384)), ErrInvalidKey},
{getErr(NewSignerES(ES256, ecdsaPrivateKey521)), ErrInvalidKey},
{getErr(NewSignerES(ES384, ecdsaPrivateKey256)), ErrInvalidKey},
{getErr(NewSignerES(ES384, ecdsaPrivateKey521)), ErrInvalidKey},
{getErr(NewSignerES(ES512, ecdsaPrivateKey256)), ErrInvalidKey},
{getErr(NewSignerES(ES512, ecdsaPrivateKey384)), ErrInvalidKey},

{getErr(NewVerifierES(ES256, nil)), ErrNilKey},
{getErr(NewVerifierES(ES384, nil)), ErrNilKey},
{getErr(NewVerifierES(ES512, nil)), ErrNilKey},

{getErr(NewVerifierES("boo", ecdsaPublicKey384)), ErrUnsupportedAlg},

{getErr(NewVerifierES(ES256, ecdsaPublicKey384)), ErrInvalidKey},
{getErr(NewVerifierES(ES256, ecdsaPublicKey521)), ErrInvalidKey},
{getErr(NewVerifierES(ES384, ecdsaPublicKey256)), ErrInvalidKey},
{getErr(NewVerifierES(ES384, ecdsaPublicKey521)), ErrInvalidKey},
{getErr(NewVerifierES(ES512, ecdsaPublicKey256)), ErrInvalidKey},
{getErr(NewVerifierES(ES512, ecdsaPublicKey384)), ErrInvalidKey},
}

f(getSignerError(NewSignerES(ES256, nil)), ErrNilKey)
f(getSignerError(NewSignerES(ES384, nil)), ErrNilKey)
f(getSignerError(NewSignerES(ES512, nil)), ErrNilKey)

f(getSignerError(NewSignerES("foo", ecdsaPrivateKey384)), ErrUnsupportedAlg)

f(getSignerError(NewSignerES(ES256, ecdsaPrivateKey384)), ErrInvalidKey)
f(getSignerError(NewSignerES(ES256, ecdsaPrivateKey521)), ErrInvalidKey)
f(getSignerError(NewSignerES(ES384, ecdsaPrivateKey256)), ErrInvalidKey)
f(getSignerError(NewSignerES(ES384, ecdsaPrivateKey521)), ErrInvalidKey)
f(getSignerError(NewSignerES(ES512, ecdsaPrivateKey256)), ErrInvalidKey)
f(getSignerError(NewSignerES(ES512, ecdsaPrivateKey384)), ErrInvalidKey)

f(getVerifierError(NewVerifierES(ES256, nil)), ErrNilKey)
f(getVerifierError(NewVerifierES(ES384, nil)), ErrNilKey)
f(getVerifierError(NewVerifierES(ES512, nil)), ErrNilKey)

f(getVerifierError(NewVerifierES("boo", ecdsaPublicKey384)), ErrUnsupportedAlg)

f(getVerifierError(NewVerifierES(ES256, ecdsaPublicKey384)), ErrInvalidKey)
f(getVerifierError(NewVerifierES(ES256, ecdsaPublicKey521)), ErrInvalidKey)
f(getVerifierError(NewVerifierES(ES384, ecdsaPublicKey256)), ErrInvalidKey)
f(getVerifierError(NewVerifierES(ES384, ecdsaPublicKey521)), ErrInvalidKey)
f(getVerifierError(NewVerifierES(ES512, ecdsaPublicKey256)), ErrInvalidKey)
f(getVerifierError(NewVerifierES(ES512, ecdsaPublicKey384)), ErrInvalidKey)
for _, tc := range testCases {
mustEqual(t, tc.err, tc.wantErr)
}
}

var (
Expand Down
35 changes: 20 additions & 15 deletions algo_hs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ import (
)

func TestHS(t *testing.T) {
f := func(alg Algorithm, signKey, verifyKey []byte, wantErr error) {
t.Helper()
testCases := []struct {
alg Algorithm
signKey []byte
verifyKey []byte
wantErr error
}{
{HS256, hsKey256, hsKey256, nil},
{HS384, hsKey384, hsKey384, nil},
{HS512, hsKey512, hsKey512, nil},

{HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature},
{HS384, hsKey384, hsKeyAnother384, ErrInvalidSignature},
{HS512, hsKey512, hsKeyAnother512, ErrInvalidSignature},

{HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature},
}

signer, err := NewSignerHS(alg, signKey)
for _, tc := range testCases {
signer, err := NewSignerHS(tc.alg, tc.signKey)
mustOk(t, err)

verifier, err := NewVerifierHS(alg, verifyKey)
verifier, err := NewVerifierHS(tc.alg, tc.verifyKey)
mustOk(t, err)

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

err = verifier.Verify(token)
mustEqual(t, err, wantErr)
mustEqual(t, err, tc.wantErr)
}

f(HS256, hsKey256, hsKey256, nil)
f(HS384, hsKey384, hsKey384, nil)
f(HS512, hsKey512, hsKey512, nil)

f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
f(HS384, hsKey384, hsKeyAnother384, ErrInvalidSignature)
f(HS512, hsKey512, hsKeyAnother512, ErrInvalidSignature)

f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
}

var (
Expand Down
71 changes: 40 additions & 31 deletions algo_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,61 @@ import (
)

func TestPS(t *testing.T) {
f := func(alg Algorithm, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, wantErr error) {
t.Helper()
testCases := []struct {
alg Algorithm
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
wantErr error
}{
{PS256, rsapsPrivateKey256, rsapsPublicKey256, nil},
{PS384, rsapsPrivateKey384, rsapsPublicKey384, nil},
{PS512, rsapsPrivateKey512, rsapsPublicKey512, nil},
{PS512, rsapsPrivateKey512Other, rsapsPublicKey512Other, nil},

{PS256, rsapsPrivateKey256, rsapsPublicKey256Another, ErrInvalidSignature},
{PS384, rsapsPrivateKey384, rsapsPublicKey384Another, ErrInvalidSignature},
{PS512, rsapsPrivateKey512, rsapsPublicKey512Another, ErrInvalidSignature},

{PS256, rsapsPrivateKey256Another, rsapsPublicKey256, ErrInvalidSignature},
{PS384, rsapsPrivateKey384Another, rsapsPublicKey384, ErrInvalidSignature},
{PS512, rsapsPrivateKey512Another, rsapsPublicKey512, ErrInvalidSignature},
{PS512, rsapsPrivateKey512Another, rsapsPublicKey512Other, ErrInvalidSignature},
}

signer, errSigner := NewSignerPS(alg, privateKey)
for _, tc := range testCases {
signer, errSigner := NewSignerPS(tc.alg, tc.privateKey)
mustOk(t, errSigner)

verifier, errVerifier := NewVerifierPS(alg, publicKey)
verifier, errVerifier := NewVerifierPS(tc.alg, tc.publicKey)
mustOk(t, errVerifier)

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

err = verifier.Verify(token)
mustEqual(t, err, wantErr)
mustEqual(t, err, tc.wantErr)
}

f(PS256, rsapsPrivateKey256, rsapsPublicKey256, nil)
f(PS384, rsapsPrivateKey384, rsapsPublicKey384, nil)
f(PS512, rsapsPrivateKey512, rsapsPublicKey512, nil)
f(PS512, rsapsPrivateKey512Other, rsapsPublicKey512Other, nil)

f(PS256, rsapsPrivateKey256, rsapsPublicKey256Another, ErrInvalidSignature)
f(PS384, rsapsPrivateKey384, rsapsPublicKey384Another, ErrInvalidSignature)
f(PS512, rsapsPrivateKey512, rsapsPublicKey512Another, ErrInvalidSignature)

f(PS256, rsapsPrivateKey256Another, rsapsPublicKey256, ErrInvalidSignature)
f(PS384, rsapsPrivateKey384Another, rsapsPublicKey384, ErrInvalidSignature)
f(PS512, rsapsPrivateKey512Another, rsapsPublicKey512, ErrInvalidSignature)
f(PS512, rsapsPrivateKey512Another, rsapsPublicKey512Other, ErrInvalidSignature)
}

func TestPS_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()
mustEqual(t, err, wantErr)
testCases := []struct {
err error
wantErr error
}{
{getErr(NewSignerPS(PS256, nil)), ErrNilKey},
{getErr(NewSignerPS(PS384, nil)), ErrNilKey},
{getErr(NewSignerPS(PS512, nil)), ErrNilKey},
{getErr(NewSignerPS("foo", rsapsPrivateKey384)), ErrUnsupportedAlg},

{getErr(NewVerifierPS(PS256, nil)), ErrNilKey},
{getErr(NewVerifierPS(PS384, nil)), ErrNilKey},
{getErr(NewVerifierPS(PS512, nil)), ErrNilKey},
{getErr(NewVerifierPS("boo", rsapsPublicKey384)), ErrUnsupportedAlg},
}

f(getSignerError(NewSignerPS(PS256, nil)), ErrNilKey)
f(getSignerError(NewSignerPS(PS384, nil)), ErrNilKey)
f(getSignerError(NewSignerPS(PS512, nil)), ErrNilKey)
f(getSignerError(NewSignerPS("foo", rsapsPrivateKey384)), ErrUnsupportedAlg)

f(getVerifierError(NewVerifierPS(PS256, nil)), ErrNilKey)
f(getVerifierError(NewVerifierPS(PS384, nil)), ErrNilKey)
f(getVerifierError(NewVerifierPS(PS512, nil)), ErrNilKey)
f(getVerifierError(NewVerifierPS("boo", rsapsPublicKey384)), ErrUnsupportedAlg)
for _, tc := range testCases {
mustEqual(t, tc.err, tc.wantErr)
}
}

var (
Expand Down
Loading

0 comments on commit 315fbb1

Please sign in to comment.