Repository: lestrrat-go/jwx
Pull request #227 (2020-11-06 02:15:54)

Add jwxtest to refactor some common test operations


Patch:
diff --git a/jwk/jwk.go b/jwk/jwk.go
index 8fa3ea01..0b7b5cd7 100644
--- a/jwk/jwk.go
+++ b/jwk/jwk.go
@@ -91,6 +91,9 @@ func New(key interface{}) (Key, error) {
 // value `v`. For example, if v is a `*rsa.PrivateKey`, then
 // `*rsa.PublicKey` is returned.
 //
+// Not to be confused with jwk.Key.PubliKey(). In hindsight, this should
+// have been named PublicRawKeyOf() or some such.
+//
 // If given a public key, then the same public key will be returned.
 // For example, if v is a `*rsa.PublicKey`, then the same value
 // is returned.


Test patch:
diff --git a/internal/jwxtest/jwxtest.go b/internal/jwxtest/jwxtest.go
new file mode 100644
index 00000000..94b83be9
--- /dev/null
+++ b/internal/jwxtest/jwxtest.go
@@ -0,0 +1,81 @@
+package jwxtest
+
+import (
+	"crypto/ecdsa"
+	"crypto/elliptic"
+	"crypto/rand"
+	"crypto/rsa"
+
+	"github.com/lestrrat-go/jwx/jwk"
+	"github.com/pkg/errors"
+)
+
+func GenerateRsaKey() (*rsa.PrivateKey, error) {
+	return rsa.GenerateKey(rand.Reader, 2048)
+}
+
+func GenerateRsaJwk() (jwk.Key, error) {
+	key, err := GenerateRsaKey()
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate RSA private key`)
+	}
+
+	k, err := jwk.New(key)
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate jwk.RSAPrivateKey`)
+	}
+
+	return k, nil
+}
+
+func GenerateRsaPublicJwk() (jwk.Key, error) {
+	key, err := GenerateRsaJwk()
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate jwk.RSAPrivateKey`)
+	}
+
+	return key.(jwk.RSAPrivateKey).PublicKey()
+}
+
+func GenerateEcdsaKey() (*ecdsa.PrivateKey, error) {
+	return ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
+}
+
+func GenerateEcdsaJwk() (jwk.Key, error) {
+	key, err := GenerateEcdsaKey()
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate ECDSA private key`)
+	}
+
+	k, err := jwk.New(key)
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate jwk.ECDSAPrivateKey`)
+	}
+
+	return k, nil
+}
+
+func GenerateEcdsaPublicJwk() (jwk.Key, error) {
+	key, err := GenerateEcdsaJwk()
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate jwk.ECDSAPrivateKey`)
+	}
+
+	return key.(jwk.ECDSAPrivateKey).PublicKey()
+}
+
+func GenerateSymmetricKey() []byte {
+	sharedKey := make([]byte, 64)
+	//nolint:errcheck
+	rand.Read(sharedKey)
+	return sharedKey
+}
+
+func GenerateSymmetricJwk() (jwk.Key, error) {
+	key, err := jwk.New(GenerateSymmetricKey())
+	if err != nil {
+		return nil, errors.Wrap(err, `failed to generate jwk.SymmetricKey`)
+	}
+
+	return key, nil
+}
diff --git a/internal/keyconv/keyconv_test.go b/internal/keyconv/keyconv_test.go
index 9691e0db..9a75d705 100644
--- a/internal/keyconv/keyconv_test.go
+++ b/internal/keyconv/keyconv_test.go
@@ -2,18 +2,17 @@ package keyconv_test
 
 import (
 	"crypto/ecdsa"
-	"crypto/elliptic"
-	"crypto/rand"
 	"crypto/rsa"
 	"testing"
 
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 	"github.com/lestrrat-go/jwx/internal/keyconv"
 	"github.com/stretchr/testify/assert"
 )
 
 func TestKeyconv(t *testing.T) {
 	t.Run("RSA", func(t *testing.T) {
-		key, err := rsa.GenerateKey(rand.Reader, 2048)
+		key, err := jwxtest.GenerateRsaKey()
 		if !assert.NoError(t, err, `rsa.GenerateKey should succeed`) {
 			return
 		}
@@ -121,7 +120,7 @@ func TestKeyconv(t *testing.T) {
 		})
 	})
 	t.Run("ECDSA", func(t *testing.T) {
-		key, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
+		key, err := jwxtest.GenerateEcdsaKey()
 		if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) {
 			return
 		}
diff --git a/jwe/headers_test.go b/jwe/headers_test.go
index 3b961a71..93f90650 100644
--- a/jwe/headers_test.go
+++ b/jwe/headers_test.go
@@ -2,12 +2,10 @@ package jwe_test
 
 import (
 	"context"
-	"crypto/ecdsa"
-	"crypto/elliptic"
-	"crypto/rand"
 	"testing"
 
 	"github.com/lestrrat-go/jwx/buffer"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 	"github.com/lestrrat-go/jwx/jwa"
 	"github.com/lestrrat-go/jwx/jwe"
 	"github.com/lestrrat-go/jwx/jwk"
@@ -15,8 +13,8 @@ import (
 )
 
 func TestHeaders(t *testing.T) {
-	rawKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
-	if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) {
+	rawKey, err := jwxtest.GenerateEcdsaKey()
+	if !assert.NoError(t, err, `jwxtest.GenerateEcdsaKey should succeed`) {
 		return
 	}
 	privKey, err := jwk.New(rawKey)
diff --git a/jwe/jwe_test.go b/jwe/jwe_test.go
index 7761770b..3ab7c89c 100644
--- a/jwe/jwe_test.go
+++ b/jwe/jwe_test.go
@@ -367,7 +367,7 @@ func TestEncode_A128KW_A128CBC_HS256(t *testing.T) {
 func TestEncode_ECDH(t *testing.T) {
 	plaintext := []byte("Lorem ipsum")
 	privkey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
-	if !assert.NoError(t, err, "ecdsa key generated") {
+	if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) {
 		return
 	}
 
diff --git a/jwk/ecdsa_test.go b/jwk/ecdsa_test.go
index 7bec8d63..380e0d3e 100644
--- a/jwk/ecdsa_test.go
+++ b/jwk/ecdsa_test.go
@@ -4,10 +4,10 @@ import (
 	"crypto"
 	"crypto/ecdsa"
 	"crypto/elliptic"
-	"crypto/rand"
 	"testing"
 
 	"github.com/lestrrat-go/jwx/internal/json"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 
 	"github.com/lestrrat-go/jwx/jwa"
 	"github.com/lestrrat-go/jwx/jwk"
@@ -118,8 +118,8 @@ func TestECDSA(t *testing.T) {
 	})
 	t.Run("Initialization", func(t *testing.T) {
 		// Generate an ECDSA P-256 test key.
-		ecPrk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
-		if !assert.NoError(t, err, "Failed to generate EC P-256 key") {
+		ecPrk, err := jwxtest.GenerateEcdsaKey()
+		if !assert.NoError(t, err, `jwxtest.GenerateEcdsaKey should succeed`) {
 			return
 		}
 		// Test initialization of a private EC JWK.
@@ -275,8 +275,8 @@ func TestECDSA(t *testing.T) {
 		for _, crv := range crvs {
 			crv := crv
 			t.Run(crv.Params().Name, func(t *testing.T) {
-				key, err := ecdsa.GenerateKey(crv, rand.Reader)
-				if !assert.NoError(t, err, `ecdsa.GenerateKey should succeed`) {
+				key, err := jwxtest.GenerateEcdsaKey()
+				if !assert.NoError(t, err, `jwxtest.GenerateEcdsaKey should succeed`) {
 					return
 				}
 
diff --git a/jwk/jwk_test.go b/jwk/jwk_test.go
index b22a7e38..36415318 100644
--- a/jwk/jwk_test.go
+++ b/jwk/jwk_test.go
@@ -4,18 +4,16 @@ import (
 	"context"
 	"crypto"
 	"crypto/ecdsa"
-	"crypto/elliptic"
-	"crypto/rand"
 	"crypto/rsa"
 	"fmt"
 	"reflect"
 	"testing"
 
 	"github.com/lestrrat-go/jwx/internal/json"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 
 	"github.com/lestrrat-go/jwx/internal/base64"
 	"github.com/lestrrat-go/jwx/jwk"
-	"github.com/pkg/errors"
 	"github.com/stretchr/testify/assert"
 )
 
@@ -170,79 +168,9 @@ func TestParse(t *testing.T) {
 	})
 }
 
-func generateRawRSAPrivateKey() (*rsa.PrivateKey, error) {
-	return rsa.GenerateKey(rand.Reader, 2048)
-}
-
-func generateRSAPrivateKey() (jwk.Key, error) {
-	key, err := generateRawRSAPrivateKey()
-
-	if err != nil {
-		return nil, errors.Wrap(err, `failed to generate RSA private key`)
-	}
-
-	k, err := jwk.New(key)
-	if err != nil {
-		return nil, errors.Wrap(err, `failed to generate jwk.RSAPrivateKey`)
-	}
-
-	return k, nil
-}
-
-func generateRSAPublicKey() (jwk.Key, error) {
-	k, err := generateRSAPrivateKey()
-	if err != nil {
-		return nil, err
-	}
-
-	return k.(jwk.RSAPrivateKey).PublicKey()
-}
-
-func generateRawECDSAPrivateKey() (*ecdsa.PrivateKey, error) {
-	return ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
-}
-
-func generateECDSAPrivateKey() (jwk.Key, error) {
-	key, err := generateRawECDSAPrivateKey()
-	if err != nil {
-		return nil, errors.Wrap(err, `failed to generate ECDSA private key`)
-	}
-
-	k, err := jwk.New(key)
-	if err != nil {
-		return nil, errors.Wrap(err, `failed to generate jwk.ECDSAPrivateKey`)
-	}
-
-	return k, nil
-}
-
-func generateECDSAPublicKey() (jwk.Key, error) {
-	k, err := generateECDSAPrivateKey()
-	if err != nil {
-		return nil, err
-	}
-
-	return k.(jwk.ECDSAPrivateKey).PublicKey()
-}
-
-func generateRawSymmetricKey() []byte {
-	sharedKey := make([]byte, 64)
-	rand.Read(sharedKey)
-	return sharedKey
-}
-
-func generateSymmetricKey() (jwk.Key, error) {
-	key, err := jwk.New(generateRawSymmetricKey())
-	if err != nil {
-		return nil, errors.Wrap(err, `failed to generate jwk.SymmetricKey`)
-	}
-
-	return key, nil
-}
-
 func TestRoundtrip(t *testing.T) {
 	generateRSA := func(use string, keyID string) (jwk.Key, error) {
-		k, err := generateRSAPrivateKey()
+		k, err := jwxtest.GenerateRsaJwk()
 		if err != nil {
 			return nil, err
 		}
@@ -253,7 +181,7 @@ func TestRoundtrip(t *testing.T) {
 	}
 
 	generateECDSA := func(use, keyID string) (jwk.Key, error) {
-		k, err := generateECDSAPrivateKey()
+		k, err := jwxtest.GenerateEcdsaJwk()
 		if err != nil {
 			return nil, err
 		}
@@ -264,7 +192,7 @@ func TestRoundtrip(t *testing.T) {
 	}
 
 	generateSymmetric := func(use, keyID string) (jwk.Key, error) {
-		k, err := generateSymmetricKey()
+		k, err := jwxtest.GenerateSymmetricJwk()
 		if err != nil {
 			return nil, err
 		}
@@ -410,11 +338,11 @@ func TestKeyOperation(t *testing.T) {
 
 func TestAssignKeyID(t *testing.T) {
 	generators := []func() (jwk.Key, error){
-		generateRSAPrivateKey,
-		generateRSAPublicKey,
-		generateECDSAPrivateKey,
-		generateECDSAPublicKey,
-		generateSymmetricKey,
+		jwxtest.GenerateRsaJwk,
+		jwxtest.GenerateRsaPublicJwk,
+		jwxtest.GenerateEcdsaJwk,
+		jwxtest.GenerateEcdsaPublicJwk,
+		jwxtest.GenerateSymmetricJwk,
 	}
 
 	for _, generator := range generators {
@@ -437,17 +365,17 @@ func TestAssignKeyID(t *testing.T) {
 }
 
 func TestPublicKeyOf(t *testing.T) {
-	rsakey, err := generateRawRSAPrivateKey()
+	rsakey, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, `generating raw RSA key should succeed`) {
 		return
 	}
 
-	ecdsakey, err := generateRawECDSAPrivateKey()
+	ecdsakey, err := jwxtest.GenerateEcdsaKey()
 	if !assert.NoError(t, err, `generating raw ECDSA key should succeed`) {
 		return
 	}
 
-	octets := generateRawSymmetricKey()
+	octets := jwxtest.GenerateSymmetricKey()
 
 	keys := []struct {
 		Key           interface{}
diff --git a/jws/jws_test.go b/jws/jws_test.go
index b21cc348..cf48a028 100644
--- a/jws/jws_test.go
+++ b/jws/jws_test.go
@@ -3,7 +3,6 @@ package jws_test
 import (
 	"bytes"
 	"crypto/ecdsa"
-	"crypto/rand"
 	"crypto/rsa"
 	"crypto/sha512"
 	"encoding/base64"
@@ -12,6 +11,7 @@ import (
 	"testing"
 
 	"github.com/lestrrat-go/jwx/internal/json"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 
 	"github.com/lestrrat-go/jwx/buffer"
 	"github.com/lestrrat-go/jwx/jwa"
@@ -137,7 +137,7 @@ func TestRoundtrip(t *testing.T) {
 
 func TestVerifyWithJWKSet(t *testing.T) {
 	payload := []byte("Hello, World!")
-	key, err := rsa.GenerateKey(rand.Reader, 2048)
+	key, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, "RSA key generated") {
 		return
 	}
@@ -173,7 +173,7 @@ func TestVerifyWithJWKSet(t *testing.T) {
 		return
 	}
 
-	key2, err := rsa.GenerateKey(rand.Reader, 2048)
+	key2, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, "RSA key generated") {
 		return
 	}
@@ -190,7 +190,7 @@ func TestVerifyWithJWKSet(t *testing.T) {
 func TestRoundtrip_RSACompact(t *testing.T) {
 	payload := []byte("Hello, World!")
 	for _, alg := range []jwa.SignatureAlgorithm{jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512} {
-		key, err := rsa.GenerateKey(rand.Reader, 2048)
+		key, err := jwxtest.GenerateRsaKey()
 		if !assert.NoError(t, err, "RSA key generated") {
 			return
 		}
@@ -861,7 +861,7 @@ func TestSign_HeaderValues(t *testing.T) {
 */
 
 func TestPublicHeaders(t *testing.T) {
-	key, err := rsa.GenerateKey(rand.Reader, 2048)
+	key, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, "GenerateKey should succeed") {
 		return
 	}
diff --git a/jws/signer_test.go b/jws/signer_test.go
index c5c825e8..be558dd2 100644
--- a/jws/signer_test.go
+++ b/jws/signer_test.go
@@ -1,13 +1,10 @@
 package jws_test
 
 import (
-	"crypto/ecdsa"
-	"crypto/elliptic"
-	"crypto/rand"
-	"crypto/rsa"
 	"strings"
 	"testing"
 
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 	"github.com/lestrrat-go/jwx/jwa"
 	"github.com/lestrrat-go/jwx/jws"
 	"github.com/lestrrat-go/jwx/jws/sign"
@@ -35,7 +32,7 @@ func TestSign(t *testing.T) {
 		}
 	})
 	t.Run("RSA roundtrip", func(t *testing.T) {
-		rsakey, err := rsa.GenerateKey(rand.Reader, 2048)
+		rsakey, err := jwxtest.GenerateRsaKey()
 		if !assert.NoError(t, err, "RSA key generated") {
 			return
 		}
@@ -63,12 +60,12 @@ func TestSign(t *testing.T) {
 	})
 }
 func TestSignMulti(t *testing.T) {
-	rsakey, err := rsa.GenerateKey(rand.Reader, 2048)
+	rsakey, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, "RSA key generated") {
 		return
 	}
 
-	dsakey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+	dsakey, err := jwxtest.GenerateEcdsaKey()
 	if !assert.NoError(t, err, "ECDSA key generated") {
 		return
 	}
diff --git a/jwt/jwt_test.go b/jwt/jwt_test.go
index f9073d82..cbea5f1b 100644
--- a/jwt/jwt_test.go
+++ b/jwt/jwt_test.go
@@ -3,23 +3,19 @@ package jwt_test
 import (
 	"bytes"
 	"context"
-	"crypto/ecdsa"
-	"crypto/elliptic"
-	"crypto/rand"
-	"crypto/rsa"
 	"encoding/base64"
 	"strings"
 	"testing"
 	"time"
 
 	"github.com/lestrrat-go/jwx/internal/json"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 
 	"github.com/lestrrat-go/jwx/jwa"
 	"github.com/lestrrat-go/jwx/jwk"
 	"github.com/lestrrat-go/jwx/jws"
 	"github.com/lestrrat-go/jwx/jwt"
 	"github.com/stretchr/testify/assert"
-	"github.com/stretchr/testify/require"
 )
 
 /* This is commented out, because it is intended to cause compilation errors */
@@ -37,9 +33,10 @@ func TestOption(t *testing.T) {
 
 func TestJWTParse(t *testing.T) {
 	alg := jwa.RS256
-	key, err := rsa.GenerateKey(rand.Reader, 2048)
-	if err != nil {
-		t.Fatal("Failed to generate RSA key")
+
+	key, err := jwxtest.GenerateRsaKey()
+	if !assert.NoError(t, err, `jwxtest.GenerateRsaKey should succeed`) {
+		return
 	}
 	t1 := jwt.New()
 	signed, err := jwt.Sign(t1, alg, key)
@@ -101,7 +98,7 @@ func TestJWTParse(t *testing.T) {
 
 func TestJWTParseVerify(t *testing.T) {
 	alg := jwa.RS256
-	key, err := rsa.GenerateKey(rand.Reader, 2048)
+	key, err := jwxtest.GenerateRsaKey()
 	if !assert.NoError(t, err, "RSA key generated") {
 		return
 	}
@@ -354,7 +351,7 @@ func TestUnmarshal(t *testing.T) {
 }
 
 func TestGH52(t *testing.T) {
-	priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+	priv, err := jwxtest.GenerateEcdsaKey()
 	if !assert.NoError(t, err) {
 		return
 	}
@@ -396,8 +393,10 @@ func TestUnmarshalJSON(t *testing.T) {
 }
 
 func TestSignErrors(t *testing.T) {
-	priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
-	require.NoError(t, err)
+	priv, err := jwxtest.GenerateEcdsaKey()
+	if !assert.NoError(t, err, `jwxtest.GenerateEcdsaKey should succeed`) {
+		return
+	}
 
 	tok := jwt.New()
 	_, err = jwt.Sign(tok, jwa.SignatureAlgorithm("BOGUS"), priv)
@@ -410,7 +409,7 @@ func TestSignErrors(t *testing.T) {
 }
 
 func TestSignJWK(t *testing.T) {
-	priv, err := rsa.GenerateKey(rand.Reader, 2048)
+	priv, err := jwxtest.GenerateRsaKey()
 	assert.Nil(t, err)
 
 	key := jwk.NewRSAPrivateKey()
diff --git a/jwt/openid/openid_test.go b/jwt/openid/openid_test.go
index 3095ceb7..00ae4857 100644
--- a/jwt/openid/openid_test.go
+++ b/jwt/openid/openid_test.go
@@ -3,13 +3,12 @@ package openid_test
 import (
 	"bytes"
 	"context"
-	"crypto/rand"
-	"crypto/rsa"
 	"fmt"
 	"testing"
 	"time"
 
 	"github.com/lestrrat-go/jwx/internal/json"
+	"github.com/lestrrat-go/jwx/internal/jwxtest"
 
 	"github.com/lestrrat-go/jwx/jwa"
 	"github.com/lestrrat-go/jwx/jwt"
@@ -448,7 +447,7 @@ func TestOpenIDClaims(t *testing.T) {
 		var token3 openid.Token
 		{
 			alg := jwa.RS256
-			key, err := rsa.GenerateKey(rand.Reader, 2048)
+			key, err := jwxtest.GenerateRsaKey()
 			if !assert.NoError(t, err, `rsa.GeneraKey should succeed`) {
 				return
 			}