diff --git a/algo_es_test.go b/algo_es_test.go index 959d710..5759eaf 100644 --- a/algo_es_test.go +++ b/algo_es_test.go @@ -5,6 +5,7 @@ import ( "crypto/elliptic" "crypto/rand" "errors" + "sync" "testing" ) @@ -16,25 +17,31 @@ var ( ecdsaPrivateKey256Another, ecdsaPrivateKey384Another, ecdsaPrivateKey521Another *ecdsa.PrivateKey ) -func init() { - f := func(f func() elliptic.Curve) (*ecdsa.PrivateKey, *ecdsa.PublicKey) { - privKey, err := ecdsa.GenerateKey(f(), rand.Reader) - if err != nil { - panic(err) +var initESKeysOnce sync.Once + +func initESKeys() { + initESKeysOnce.Do(func() { + f := func(f func() elliptic.Curve) (*ecdsa.PrivateKey, *ecdsa.PublicKey) { + privKey, err := ecdsa.GenerateKey(f(), rand.Reader) + if err != nil { + panic(err) + } + return privKey, &privKey.PublicKey } - return privKey, &privKey.PublicKey - } - ecdsaPrivateKey256, ecdsaPublicKey256 = f(elliptic.P256) - ecdsaPrivateKey384, ecdsaPublicKey384 = f(elliptic.P384) - ecdsaPrivateKey521, ecdsaPublicKey521 = f(elliptic.P521) + ecdsaPrivateKey256, ecdsaPublicKey256 = f(elliptic.P256) + ecdsaPrivateKey384, ecdsaPublicKey384 = f(elliptic.P384) + ecdsaPrivateKey521, ecdsaPublicKey521 = f(elliptic.P521) - ecdsaPrivateKey256Another, ecdsaPublicKey256Another = f(elliptic.P256) - ecdsaPrivateKey384Another, ecdsaPublicKey384Another = f(elliptic.P384) - ecdsaPrivateKey521Another, ecdsaPublicKey521Another = f(elliptic.P521) + ecdsaPrivateKey256Another, ecdsaPublicKey256Another = f(elliptic.P256) + ecdsaPrivateKey384Another, ecdsaPublicKey384Another = f(elliptic.P384) + ecdsaPrivateKey521Another, ecdsaPublicKey521Another = f(elliptic.P521) + }) } func TestES(t *testing.T) { + initESKeys() + f := func(alg Algorithm, privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, isCorrectSign bool) { t.Helper() @@ -44,10 +51,10 @@ func TestES(t *testing.T) { err := esVerify(t, alg, publicKey, payload, sign) if err != nil && isCorrectSign { - t.Fatal(err) + t.Error(err) } if err == nil && !isCorrectSign { - t.Fatal("must be not nil") + t.Error("must be not nil") } } @@ -65,11 +72,13 @@ func TestES(t *testing.T) { } func TestES_BadKeys(t *testing.T) { + initESKeys() + f := func(err, wantErr error) { t.Helper() if !errors.Is(err, wantErr) { - t.Fatalf("expected %v, got %v", wantErr, err) + t.Errorf("expected %v, got %v", wantErr, err) } } diff --git a/algo_ps_test.go b/algo_ps_test.go index 2645b29..4dd7988 100644 --- a/algo_ps_test.go +++ b/algo_ps_test.go @@ -4,6 +4,7 @@ import ( "crypto/rand" "crypto/rsa" "errors" + "sync" "testing" ) @@ -15,24 +16,31 @@ var ( rsapsPrivateKey256Another, rsapsPrivateKey384Another, rsapsPrivateKey512Another *rsa.PrivateKey ) -func init() { - f := func(bits int) (*rsa.PrivateKey, *rsa.PublicKey) { - privKey, err := rsa.GenerateKey(rand.Reader, bits) - if err != nil { - panic(err) +var initPSKeysOnce sync.Once + +func initPSKeys() { + initPSKeysOnce.Do(func() { + f := func(bits int) (*rsa.PrivateKey, *rsa.PublicKey) { + privKey, err := rsa.GenerateKey(rand.Reader, bits) + if err != nil { + panic(err) + } + return privKey, &privKey.PublicKey } - return privKey, &privKey.PublicKey - } - rsapsPrivateKey256, rsapsPublicKey256 = f(256 * 8) - rsapsPrivateKey384, rsapsPublicKey384 = f(384 * 8) - rsapsPrivateKey512, rsapsPublicKey512 = f(512 * 8) + rsapsPrivateKey256, rsapsPublicKey256 = f(256 * 8) + rsapsPrivateKey384, rsapsPublicKey384 = f(384 * 8) + rsapsPrivateKey512, rsapsPublicKey512 = f(512 * 8) - rsapsPrivateKey256Another, rsapsPublicKey256Another = f(256 * 8) - rsapsPrivateKey384Another, rsapsPublicKey384Another = f(384 * 8) - rsapsPrivateKey512Another, rsapsPublicKey512Another = f(512 * 8) + rsapsPrivateKey256Another, rsapsPublicKey256Another = f(256 * 8) + rsapsPrivateKey384Another, rsapsPublicKey384Another = f(384 * 8) + rsapsPrivateKey512Another, rsapsPublicKey512Another = f(512 * 8) + }) } + func TestPS(t *testing.T) { + initPSKeys() + f := func(alg Algorithm, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, isCorrectSign bool) { t.Helper() @@ -42,10 +50,10 @@ func TestPS(t *testing.T) { err := psVerify(t, alg, publicKey, payload, sign) if err != nil && isCorrectSign { - t.Fatal(err) + t.Error(err) } if err == nil && !isCorrectSign { - t.Fatal("must be not nil") + t.Error("must be not nil") } } @@ -63,11 +71,13 @@ func TestPS(t *testing.T) { } func TestPS_BadKeys(t *testing.T) { + initPSKeys() + f := func(err, wantErr error) { t.Helper() if !errors.Is(err, wantErr) { - t.Fatalf("expected %v, got %v", wantErr, err) + t.Errorf("expected %v, got %v", wantErr, err) } } diff --git a/algo_rs_test.go b/algo_rs_test.go index cdad473..191f9c8 100644 --- a/algo_rs_test.go +++ b/algo_rs_test.go @@ -4,36 +4,44 @@ import ( "crypto/rand" "crypto/rsa" "errors" + "sync" "testing" ) var ( - rsaPublicKey256, rsaPublicKey384, rsaPublicKey512 *rsa.PublicKey - rsaPrivateKey256, rsaPrivateKey384, rsaPrivateKey512 *rsa.PrivateKey + rsaPublicKey256, rsaPublicKey384, rsaPublicKey512, rsaPublicKey512Other *rsa.PublicKey + rsaPrivateKey256, rsaPrivateKey384, rsaPrivateKey512, rsaPrivateKey512Other *rsa.PrivateKey rsaPublicKey256Another, rsaPublicKey384Another, rsaPublicKey512Another *rsa.PublicKey rsaPrivateKey256Another, rsaPrivateKey384Another, rsaPrivateKey512Another *rsa.PrivateKey ) -func init() { - f := func(bits int) (*rsa.PrivateKey, *rsa.PublicKey) { - privKey, err := rsa.GenerateKey(rand.Reader, bits) - if err != nil { - panic(err) +var initRSKeysOnce sync.Once + +func initRSKeys() { + initRSKeysOnce.Do(func() { + f := func(bits int) (*rsa.PrivateKey, *rsa.PublicKey) { + privKey, err := rsa.GenerateKey(rand.Reader, bits) + if err != nil { + panic(err) + } + return privKey, &privKey.PublicKey } - return privKey, &privKey.PublicKey - } - rsaPrivateKey256, rsaPublicKey256 = f(256 * 8) - rsaPrivateKey384, rsaPublicKey384 = f(384 * 8) - rsaPrivateKey512, rsaPublicKey512 = f(256 * 8) // 256 just for the example + rsaPrivateKey256, rsaPublicKey256 = f(256 * 8) + rsaPrivateKey384, rsaPublicKey384 = f(384 * 8) + rsaPrivateKey512, rsaPublicKey512 = f(512 * 8) + rsaPrivateKey512Other, rsaPublicKey512Other = f(256 * 8) // 256 just for the example - rsaPrivateKey256Another, rsaPublicKey256Another = f(256 * 8) - rsaPrivateKey384Another, rsaPublicKey384Another = f(384 * 8) - rsaPrivateKey512Another, rsaPublicKey512Another = f(512 * 8) + rsaPrivateKey256Another, rsaPublicKey256Another = f(256 * 8) + rsaPrivateKey384Another, rsaPublicKey384Another = f(384 * 8) + rsaPrivateKey512Another, rsaPublicKey512Another = f(512 * 8) + }) } func TestRS(t *testing.T) { + initRSKeys() + f := func(alg Algorithm, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, isCorrectSign bool) { t.Helper() @@ -43,16 +51,17 @@ func TestRS(t *testing.T) { err := rsVerify(t, alg, publicKey, payload, sign) if err != nil && isCorrectSign { - t.Fatal(err) + t.Error(err) } if err == nil && !isCorrectSign { - t.Fatal("must be not nil") + t.Error("must be not nil") } } f(RS256, rsaPrivateKey256, rsaPublicKey256, true) f(RS384, rsaPrivateKey384, rsaPublicKey384, true) f(RS512, rsaPrivateKey512, rsaPublicKey512, true) + f(RS512, rsaPrivateKey512Other, rsaPublicKey512Other, true) f(RS256, rsaPrivateKey256, rsaPublicKey256Another, false) f(RS384, rsaPrivateKey384, rsaPublicKey384Another, false) @@ -61,14 +70,17 @@ func TestRS(t *testing.T) { f(RS256, rsaPrivateKey256Another, rsaPublicKey256, false) f(RS384, rsaPrivateKey384Another, rsaPublicKey384, false) f(RS512, rsaPrivateKey512Another, rsaPublicKey512, false) + f(RS512, rsaPrivateKey512Other, rsaPublicKey512, false) } func TestRS_BadKeys(t *testing.T) { + initRSKeys() + f := func(err, wantErr error) { t.Helper() if !errors.Is(err, wantErr) { - t.Fatalf("expected %v, got %v", wantErr, err) + t.Errorf("expected %v, got %v", wantErr, err) } } diff --git a/algo_test.go b/algo_test.go index 9059773..ba18407 100644 --- a/algo_test.go +++ b/algo_test.go @@ -4,7 +4,15 @@ import ( "testing" ) +func initKeys() { + initRSKeys() + initPSKeys() + initESKeys() +} + func TestSignerAlg(t *testing.T) { + initKeys() + f := func(s Signer, want Algorithm) { t.Helper() if alg := s.Algorithm(); alg != want { @@ -30,6 +38,8 @@ func TestSignerAlg(t *testing.T) { } func TestVerifierAlg(t *testing.T) { + initKeys() + f := func(v Verifier, want Algorithm) { t.Helper() if alg := v.Algorithm(); alg != want { @@ -55,6 +65,8 @@ func TestVerifierAlg(t *testing.T) { } func TestSignerBadParams(t *testing.T) { + initKeys() + f := func(_ Signer, err error) { t.Helper() if err == nil { @@ -79,6 +91,8 @@ func TestSignerBadParams(t *testing.T) { } func TestVerifierBadParams(t *testing.T) { + initKeys() + f := func(_ Verifier, err error) { t.Helper() if err == nil { diff --git a/build_test.go b/build_test.go index 0ac0861..6b29624 100644 --- a/build_test.go +++ b/build_test.go @@ -6,6 +6,8 @@ import ( ) func TestBuild(t *testing.T) { + initKeys() + f := func(signer Signer, verifier Verifier, claims interface{}) { t.Helper() @@ -92,6 +94,8 @@ func TestBuild(t *testing.T) { } func TestBuildHeader(t *testing.T) { + initKeys() + f := func(signer Signer, want string, opts ...BuilderOption) { t.Helper()