Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify tests with test helpers #146

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions algo_eddsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@ package jwt

import (
"crypto/ed25519"
"errors"
"testing"
)

func TestEdDSA(t *testing.T) {
f := func(privateKey ed25519.PrivateKey, publicKey ed25519.PublicKey, wantErr error) {
t.Helper()

signer, errSigner := NewSignerEdDSA(privateKey)
if errSigner != nil {
t.Fatalf("NewSignerEdDSA %v", errSigner)
}
verifier, errVerifier := NewVerifierEdDSA(publicKey)
if errVerifier != nil {
t.Fatalf("NewVerifierEdDSA %v", errVerifier)
}
signer, err := NewSignerEdDSA(privateKey)
mustOk(t, err)

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

token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}
mustOk(t, err)

errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
err = verifier.Verify(token)
mustEqual(t, err, wantErr)
}

f(ed25519PrivateKey, ed25519PublicKey, nil)
Expand All @@ -37,9 +29,8 @@ func TestEdDSA(t *testing.T) {

func TestEdDSA_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
if !errors.Is(err, wantErr) {
t.Fatalf("expected %v, got %v", wantErr, err)
}
t.Helper()
mustEqual(t, err, wantErr)
}

f(getSignerError(NewSignerEdDSA(nil)), ErrNilKey)
Expand Down
31 changes: 10 additions & 21 deletions algo_es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@ package jwt

import (
"crypto/ecdsa"
"errors"
"testing"
)

func TestES(t *testing.T) {
f := func(alg Algorithm, privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, wantErr error) {
t.Helper()

signer, errSigner := NewSignerES(alg, privateKey)
if errSigner != nil {
t.Fatalf("NewSignerES %v", errSigner)
}
verifier, errVerifier := NewVerifierES(alg, publicKey)
if errVerifier != nil {
t.Fatalf("NewVerifierES %v", errVerifier)
}
signer, err := NewSignerES(alg, privateKey)
mustOk(t, err)

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

token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}

errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
mustOk(t, err)

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

f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256, nil)
Expand All @@ -46,10 +38,7 @@ func TestES(t *testing.T) {
func TestES_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()

if !errors.Is(err, wantErr) {
t.Errorf("expected %v, got %v", wantErr, err)
}
mustEqual(t, err, wantErr)
}

f(getSignerError(NewSignerES(ES256, nil)), ErrNilKey)
Expand Down
26 changes: 9 additions & 17 deletions algo_hs_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package jwt

import (
"errors"
"testing"
)

func TestHS(t *testing.T) {
f := func(alg Algorithm, signKey, verifyKey []byte, wantErr error) {
t.Helper()

signer, errSigner := NewSignerHS(alg, signKey)
if errSigner != nil {
t.Fatalf("NewSignerHS %v", errSigner)
}
verifier, errVerifier := NewVerifierHS(alg, verifyKey)
if errVerifier != nil {
t.Fatalf("NewVerifierHS %v", errVerifier)
}
signer, err := NewSignerHS(alg, signKey)
mustOk(t, err)

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

token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}

errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
mustOk(t, err)

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

f(HS256, hsKey256, hsKey256, nil)
Expand Down
27 changes: 8 additions & 19 deletions algo_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jwt

import (
"crypto/rsa"
"errors"
"testing"
)

Expand All @@ -11,23 +10,16 @@ func TestPS(t *testing.T) {
t.Helper()

signer, errSigner := NewSignerPS(alg, privateKey)
if errSigner != nil {
t.Fatalf("NewSignerPS %v", errSigner)
}
mustOk(t, errSigner)

verifier, errVerifier := NewVerifierPS(alg, publicKey)
if errVerifier != nil {
t.Fatalf("NewVerifierPS %v", errVerifier)
}
mustOk(t, errVerifier)

token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}

errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
mustOk(t, err)

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

f(PS256, rsapsPrivateKey256, rsapsPublicKey256, nil)
Expand All @@ -48,10 +40,7 @@ func TestPS(t *testing.T) {
func TestPS_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()

if !errors.Is(err, wantErr) {
t.Errorf("expected %v, got %v", wantErr, err)
}
mustEqual(t, err, wantErr)
}

f(getSignerError(NewSignerPS(PS256, nil)), ErrNilKey)
Expand Down
25 changes: 7 additions & 18 deletions algo_rs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jwt

import (
"crypto/rsa"
"errors"
"testing"
)

Expand All @@ -11,23 +10,16 @@ func TestRS(t *testing.T) {
t.Helper()

signer, errSigner := NewSignerRS(alg, privateKey)
if errSigner != nil {
t.Fatalf("NewSignerRS %v", errSigner)
}
mustOk(t, errSigner)

verifier, errVerifier := NewVerifierRS(alg, publicKey)
if errVerifier != nil {
t.Fatalf("NewVerifierRS %v", errVerifier)
}
mustOk(t, errVerifier)

token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}
mustOk(t, err)

errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
err = verifier.Verify(token)
mustEqual(t, err, wantErr)
}

f(RS256, rsaPrivateKey256, rsaPublicKey256, nil)
Expand All @@ -48,10 +40,7 @@ func TestRS(t *testing.T) {
func TestRS_BadKeys(t *testing.T) {
f := func(err, wantErr error) {
t.Helper()

if !errors.Is(err, wantErr) {
t.Errorf("expected %v, got %v", wantErr, err)
}
mustEqual(t, err, wantErr)
}

f(getSignerError(NewSignerRS(RS256, nil)), ErrNilKey)
Expand Down
64 changes: 28 additions & 36 deletions algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,53 @@ const simplePayload = `simple-string-payload`
func TestSignerAlg(t *testing.T) {
f := func(s Signer, want Algorithm) {
t.Helper()
if alg := s.Algorithm(); alg != want {
t.Errorf("got %#v, want %#v", alg, want)
}
mustEqual(t, s.Algorithm(), want)
}

f(mustSigner(NewSignerHS(HS256, hsKey256)), HS256)
f(mustSigner(NewSignerHS(HS384, hsKey384)), HS384)
f(mustSigner(NewSignerHS(HS512, hsKey512)), HS512)
f(must(NewSignerHS(HS256, hsKey256)), HS256)
f(must(NewSignerHS(HS384, hsKey384)), HS384)
f(must(NewSignerHS(HS512, hsKey512)), HS512)

f(mustSigner(NewSignerRS(RS256, rsaPrivateKey256)), RS256)
f(mustSigner(NewSignerRS(RS384, rsaPrivateKey384)), RS384)
f(mustSigner(NewSignerRS(RS512, rsaPrivateKey512)), RS512)
f(must(NewSignerRS(RS256, rsaPrivateKey256)), RS256)
f(must(NewSignerRS(RS384, rsaPrivateKey384)), RS384)
f(must(NewSignerRS(RS512, rsaPrivateKey512)), RS512)

f(mustSigner(NewSignerPS(PS256, rsapsPrivateKey256)), PS256)
f(mustSigner(NewSignerPS(PS384, rsapsPrivateKey384)), PS384)
f(mustSigner(NewSignerPS(PS512, rsapsPrivateKey512)), PS512)
f(must(NewSignerPS(PS256, rsapsPrivateKey256)), PS256)
f(must(NewSignerPS(PS384, rsapsPrivateKey384)), PS384)
f(must(NewSignerPS(PS512, rsapsPrivateKey512)), PS512)

f(mustSigner(NewSignerES(ES256, ecdsaPrivateKey256)), ES256)
f(mustSigner(NewSignerES(ES384, ecdsaPrivateKey384)), ES384)
f(mustSigner(NewSignerES(ES512, ecdsaPrivateKey521)), ES512)
f(must(NewSignerES(ES256, ecdsaPrivateKey256)), ES256)
f(must(NewSignerES(ES384, ecdsaPrivateKey384)), ES384)
f(must(NewSignerES(ES512, ecdsaPrivateKey521)), ES512)
}

func TestVerifierAlg(t *testing.T) {
f := func(v Verifier, want Algorithm) {
t.Helper()
if alg := v.Algorithm(); alg != want {
t.Errorf("got %#v, want %#v", alg, want)
}
mustEqual(t, v.Algorithm(), want)
}

f(mustVerifier(NewVerifierHS(HS256, hsKey256)), HS256)
f(mustVerifier(NewVerifierHS(HS384, hsKey384)), HS384)
f(mustVerifier(NewVerifierHS(HS512, hsKey512)), HS512)
f(must(NewVerifierHS(HS256, hsKey256)), HS256)
f(must(NewVerifierHS(HS384, hsKey384)), HS384)
f(must(NewVerifierHS(HS512, hsKey512)), HS512)

f(mustVerifier(NewVerifierRS(RS256, rsaPublicKey256)), RS256)
f(mustVerifier(NewVerifierRS(RS384, rsaPublicKey384)), RS384)
f(mustVerifier(NewVerifierRS(RS512, rsaPublicKey512)), RS512)
f(must(NewVerifierRS(RS256, rsaPublicKey256)), RS256)
f(must(NewVerifierRS(RS384, rsaPublicKey384)), RS384)
f(must(NewVerifierRS(RS512, rsaPublicKey512)), RS512)

f(mustVerifier(NewVerifierPS(PS256, rsapsPublicKey256)), PS256)
f(mustVerifier(NewVerifierPS(PS384, rsapsPublicKey384)), PS384)
f(mustVerifier(NewVerifierPS(PS512, rsapsPublicKey512)), PS512)
f(must(NewVerifierPS(PS256, rsapsPublicKey256)), PS256)
f(must(NewVerifierPS(PS384, rsapsPublicKey384)), PS384)
f(must(NewVerifierPS(PS512, rsapsPublicKey512)), PS512)

f(mustVerifier(NewVerifierES(ES256, ecdsaPublicKey256)), ES256)
f(mustVerifier(NewVerifierES(ES384, ecdsaPublicKey384)), ES384)
f(mustVerifier(NewVerifierES(ES512, ecdsaPublicKey521)), ES512)
f(must(NewVerifierES(ES256, ecdsaPublicKey256)), ES256)
f(must(NewVerifierES(ES384, ecdsaPublicKey384)), ES384)
f(must(NewVerifierES(ES512, ecdsaPublicKey521)), ES512)
}

func TestSignerBadParams(t *testing.T) {
f := func(_ Signer, err error) {
t.Helper()
if err == nil {
t.Error("should have an error")
}
mustFail(t, err)
}

f(NewSignerEdDSA(nil))
Expand All @@ -83,9 +77,7 @@ func TestSignerBadParams(t *testing.T) {
func TestVerifierBadParams(t *testing.T) {
f := func(_ Verifier, err error) {
t.Helper()
if err == nil {
t.Error("should have an error")
}
mustFail(t, err)
}

f(NewVerifierEdDSA(nil))
Expand Down
25 changes: 6 additions & 19 deletions audience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ func TestAudienceMarshal(t *testing.T) {
t.Helper()

raw, err := got.MarshalJSON()
if err != nil {
t.Errorf("want no err, got: %#v", err)
}

if string(raw) != want {
t.Errorf("want %#v, got: %#v", want, string(raw))
}
mustOk(t, err)
mustEqual(t, string(raw), want)
}

f(nil, `""`)
Expand All @@ -30,17 +25,11 @@ func TestAudienceUnmarshal(t *testing.T) {

var a Audience
err := a.UnmarshalJSON([]byte(got))
if err != nil {
t.Errorf("want no err, got: %#v", err)
}
mustOk(t, err)
mustEqual(t, len(a), len(want))

if len(want) != len(a) {
t.Errorf("want %#v, got: %#v", len(want), len(a))
}
for i := range a {
if a[i] != want[i] {
t.Errorf("want %#v, got: %#v", want[i], a[i])
}
mustEqual(t, a[i], want[i])
}
}

Expand All @@ -56,9 +45,7 @@ func TestAudienceUnmarshalMalformed(t *testing.T) {

var a Audience
err := a.UnmarshalJSON([]byte(got))
if err == nil {
t.Error("want err")
}
mustFail(t, err)
}

f(``)
Expand Down
Loading
Loading