Skip to content

Commit

Permalink
Init keys in tests once (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jul 9, 2021
1 parent 2a10343 commit 8c2d44d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 50 deletions.
41 changes: 25 additions & 16 deletions algo_es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"errors"
"sync"
"testing"
)

Expand All @@ -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()

Expand All @@ -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")
}
}

Expand All @@ -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)
}
}

Expand Down
42 changes: 26 additions & 16 deletions algo_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"crypto/rsa"
"errors"
"sync"
"testing"
)

Expand All @@ -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()

Expand All @@ -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")
}
}

Expand All @@ -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)
}
}

Expand Down
48 changes: 30 additions & 18 deletions algo_rs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -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)
}
}

Expand Down
14 changes: 14 additions & 0 deletions algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestBuild(t *testing.T) {
initKeys()

f := func(signer Signer, verifier Verifier, claims interface{}) {
t.Helper()

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 8c2d44d

Please sign in to comment.