Skip to content

Commit

Permalink
rename key vars in tests (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 14, 2021
1 parent d02bb92 commit b938517
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
10 changes: 5 additions & 5 deletions algo_eddsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var (
ed25519PrivateKey ed25519.PrivateKey
ed25519PublicKey ed25519.PublicKey

ed25519OtherPrivateKey ed25519.PrivateKey
ed25519OtherPublicKey ed25519.PublicKey
ed25519PrivateKeyAnother ed25519.PrivateKey
ed25519PublicKeyAnother ed25519.PublicKey
)

func init() {
Expand All @@ -25,7 +25,7 @@ func init() {
}

ed25519PrivateKey, ed25519PublicKey = f()
ed25519OtherPrivateKey, ed25519OtherPublicKey = f()
ed25519PrivateKeyAnother, ed25519PublicKeyAnother = f()
}

func TestEdDSA(t *testing.T) {
Expand All @@ -46,8 +46,8 @@ func TestEdDSA(t *testing.T) {
}

f(ed25519PrivateKey, ed25519PublicKey, true)
f(ed25519PrivateKey, ed25519OtherPublicKey, false)
f(ed25519OtherPrivateKey, ed25519PublicKey, false)
f(ed25519PrivateKey, ed25519PublicKeyAnother, false)
f(ed25519PrivateKeyAnother, ed25519PublicKey, false)
}

func TestEdDSA_BadKeys(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions algo_es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var (
ecdsaPublicKey256, ecdsaPublicKey384, ecdsaPublicKey521 *ecdsa.PublicKey
ecdsaPrivateKey256, ecdsaPrivateKey384, ecdsaPrivateKey521 *ecdsa.PrivateKey

ecdsaOtherPublicKey256, ecdsaOtherPublicKey384, ecdsaOtherPublicKey521 *ecdsa.PublicKey
ecdsaOtherPrivateKey256, ecdsaOtherPrivateKey384, ecdsaOtherPrivateKey521 *ecdsa.PrivateKey
ecdsaPublicKey256Another, ecdsaPublicKey384Another, ecdsaPublicKey521Another *ecdsa.PublicKey
ecdsaPrivateKey256Another, ecdsaPrivateKey384Another, ecdsaPrivateKey521Another *ecdsa.PrivateKey
)

func init() {
Expand All @@ -29,9 +29,9 @@ func init() {
ecdsaPrivateKey384, ecdsaPublicKey384 = f(elliptic.P384)
ecdsaPrivateKey521, ecdsaPublicKey521 = f(elliptic.P521)

ecdsaOtherPrivateKey256, ecdsaOtherPublicKey256 = f(elliptic.P256)
ecdsaOtherPrivateKey384, ecdsaOtherPublicKey384 = f(elliptic.P384)
ecdsaOtherPrivateKey521, ecdsaOtherPublicKey521 = f(elliptic.P521)
ecdsaPrivateKey256Another, ecdsaPublicKey256Another = f(elliptic.P256)
ecdsaPrivateKey384Another, ecdsaPublicKey384Another = f(elliptic.P384)
ecdsaPrivateKey521Another, ecdsaPublicKey521Another = f(elliptic.P521)
}

func TestES(t *testing.T) {
Expand All @@ -55,13 +55,13 @@ func TestES(t *testing.T) {
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384, true)
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521, true)

f(ES256, ecdsaPrivateKey256, ecdsaOtherPublicKey256, false)
f(ES384, ecdsaPrivateKey384, ecdsaOtherPublicKey384, false)
f(ES512, ecdsaPrivateKey521, ecdsaOtherPublicKey521, false)
f(ES256, ecdsaPrivateKey256, ecdsaPublicKey256Another, false)
f(ES384, ecdsaPrivateKey384, ecdsaPublicKey384Another, false)
f(ES512, ecdsaPrivateKey521, ecdsaPublicKey521Another, false)

f(ES256, ecdsaOtherPrivateKey256, ecdsaPublicKey256, false)
f(ES384, ecdsaOtherPrivateKey384, ecdsaPublicKey384, false)
f(ES512, ecdsaOtherPrivateKey521, ecdsaPublicKey521, false)
f(ES256, ecdsaPrivateKey256Another, ecdsaPublicKey256, false)
f(ES384, ecdsaPrivateKey384Another, ecdsaPublicKey384, false)
f(ES512, ecdsaPrivateKey521Another, ecdsaPublicKey521, false)
}

func TestES_BadKeys(t *testing.T) {
Expand Down
34 changes: 22 additions & 12 deletions algo_hs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import (
"testing"
)

var (
hsKey256 = []byte("hmac-secret-key-256")
hsKey384 = []byte("hmac-secret-key-384")
hsKey512 = []byte("hmac-secret-key-512")

hsKeyAnother256 = []byte("hmac-secret-key-256-another")
hsKeyAnother384 = []byte("hmac-secret-key-384-another")
hsKeyAnother512 = []byte("hmac-secret-key-512-another")
)

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

const payload = `simple-string-payload`
Expand All @@ -21,21 +31,21 @@ func TestHS(t *testing.T) {
}
}

f(HS256, `hmac-secret-key`, `hmac-secret-key`, true)
f(HS384, `hmac-secret-key`, `hmac-secret-key`, true)
f(HS512, `hmac-secret-key`, `hmac-secret-key`, true)
f(HS256, hsKey256, hsKey256, true)
f(HS384, hsKey384, hsKey384, true)
f(HS512, hsKey512, hsKey512, true)

f(HS256, `key_1`, `1_key`, false)
f(HS384, `key_1`, `1_key`, false)
f(HS512, `key_1`, `1_key`, false)
f(HS256, hsKey256, hsKeyAnother256, false)
f(HS384, hsKey384, hsKeyAnother384, false)
f(HS512, hsKey512, hsKeyAnother512, false)

f(HS256, `hmac-secret-key`, `key_1`, false)
f(HS256, hsKey256, hsKeyAnother256, false)
}

func hsSign(t *testing.T, alg Algorithm, key, payload string) []byte {
func hsSign(t *testing.T, alg Algorithm, key []byte, payload string) []byte {
t.Helper()

signer, errSigner := NewSignerHS(alg, []byte(key))
signer, errSigner := NewSignerHS(alg, key)
if errSigner != nil {
t.Fatalf("NewSignerHS %v", errSigner)
}
Expand All @@ -47,10 +57,10 @@ func hsSign(t *testing.T, alg Algorithm, key, payload string) []byte {
return sign
}

func hsVerify(t *testing.T, alg Algorithm, key, payload string, sign []byte) error {
func hsVerify(t *testing.T, alg Algorithm, key []byte, payload string, sign []byte) error {
t.Helper()

verifier, errVerifier := NewVerifierHS(alg, []byte(key))
verifier, errVerifier := NewVerifierHS(alg, key)
if errVerifier != nil {
t.Fatalf("NewVerifierHS %v", errVerifier)
}
Expand Down
12 changes: 6 additions & 6 deletions algo_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func TestPS(t *testing.T) {
f(PS384, rsaPrivateKey384, rsaPublicKey384, true)
f(PS512, rsaPrivateKey512, rsaPublicKey512, true)

f(PS256, rsaPrivateKey256, rsaOtherPublicKey256, false)
f(PS384, rsaPrivateKey384, rsaOtherPublicKey384, false)
f(PS512, rsaPrivateKey512, rsaOtherPublicKey512, false)
f(PS256, rsaPrivateKey256, rsaPublicKey256Another, false)
f(PS384, rsaPrivateKey384, rsaPublicKey384Another, false)
f(PS512, rsaPrivateKey512, rsaPublicKey512Another, false)

f(PS256, rsaOtherPrivateKey256, rsaPublicKey256, false)
f(PS384, rsaOtherPrivateKey384, rsaPublicKey384, false)
f(PS512, rsaOtherPrivateKey512, rsaPublicKey512, false)
f(PS256, rsaPrivateKey256Another, rsaPublicKey256, false)
f(PS384, rsaPrivateKey384Another, rsaPublicKey384, false)
f(PS512, rsaPrivateKey512Another, rsaPublicKey512, false)
}

func TestPS_BadKeys(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions algo_rs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var (
rsaPublicKey256, rsaPublicKey384, rsaPublicKey512 *rsa.PublicKey
rsaPrivateKey256, rsaPrivateKey384, rsaPrivateKey512 *rsa.PrivateKey

rsaOtherPublicKey256, rsaOtherPublicKey384, rsaOtherPublicKey512 *rsa.PublicKey
rsaOtherPrivateKey256, rsaOtherPrivateKey384, rsaOtherPrivateKey512 *rsa.PrivateKey
rsaPublicKey256Another, rsaPublicKey384Another, rsaPublicKey512Another *rsa.PublicKey
rsaPrivateKey256Another, rsaPrivateKey384Another, rsaPrivateKey512Another *rsa.PrivateKey
)

func init() {
Expand All @@ -28,9 +28,9 @@ func init() {
rsaPrivateKey384, rsaPublicKey384 = f(384 * 8)
rsaPrivateKey512, rsaPublicKey512 = f(512 * 8)

rsaOtherPrivateKey256, rsaOtherPublicKey256 = f(256 * 8)
rsaOtherPrivateKey384, rsaOtherPublicKey384 = f(384 * 8)
rsaOtherPrivateKey512, rsaOtherPublicKey512 = f(512 * 8)
rsaPrivateKey256Another, rsaPublicKey256Another = f(256 * 8)
rsaPrivateKey384Another, rsaPublicKey384Another = f(384 * 8)
rsaPrivateKey512Another, rsaPublicKey512Another = f(512 * 8)
}

func TestRS(t *testing.T) {
Expand All @@ -54,13 +54,13 @@ func TestRS(t *testing.T) {
f(RS384, rsaPrivateKey384, rsaPublicKey384, true)
f(RS512, rsaPrivateKey512, rsaPublicKey512, true)

f(RS256, rsaPrivateKey256, rsaOtherPublicKey256, false)
f(RS384, rsaPrivateKey384, rsaOtherPublicKey384, false)
f(RS512, rsaPrivateKey512, rsaOtherPublicKey512, false)
f(RS256, rsaPrivateKey256, rsaPublicKey256Another, false)
f(RS384, rsaPrivateKey384, rsaPublicKey384Another, false)
f(RS512, rsaPrivateKey512, rsaPublicKey512Another, false)

f(RS256, rsaOtherPrivateKey256, rsaPublicKey256, false)
f(RS384, rsaOtherPrivateKey384, rsaPublicKey384, false)
f(RS512, rsaOtherPrivateKey512, rsaPublicKey512, false)
f(RS256, rsaPrivateKey256Another, rsaPublicKey256, false)
f(RS384, rsaPrivateKey384Another, rsaPublicKey384, false)
f(RS512, rsaPrivateKey512Another, rsaPublicKey512, false)
}

func TestRS_BadKeys(t *testing.T) {
Expand Down
14 changes: 6 additions & 8 deletions algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ func TestSignerAlg(t *testing.T) {
}
}

hmacKey := []byte("key")
f(mustSigner(NewSignerHS(HS256, hmacKey)), HS256)
f(mustSigner(NewSignerHS(HS384, hmacKey)), HS384)
f(mustSigner(NewSignerHS(HS512, hmacKey)), HS512)
f(mustSigner(NewSignerHS(HS256, hsKey256)), HS256)
f(mustSigner(NewSignerHS(HS384, hsKey384)), HS384)
f(mustSigner(NewSignerHS(HS512, hsKey512)), HS512)

f(mustSigner(NewSignerRS(RS256, rsaPrivateKey256)), RS256)
f(mustSigner(NewSignerRS(RS384, rsaPrivateKey384)), RS384)
Expand All @@ -38,10 +37,9 @@ func TestVerifierAlg(t *testing.T) {
}
}

hmacKey := []byte("key")
f(mustVerifier(NewVerifierHS(HS256, hmacKey)), HS256)
f(mustVerifier(NewVerifierHS(HS384, hmacKey)), HS384)
f(mustVerifier(NewVerifierHS(HS512, hmacKey)), HS512)
f(mustVerifier(NewVerifierHS(HS256, hsKey256)), HS256)
f(mustVerifier(NewVerifierHS(HS384, hsKey384)), HS384)
f(mustVerifier(NewVerifierHS(HS512, hsKey512)), HS512)

f(mustVerifier(NewVerifierRS(RS256, rsaPublicKey256)), RS256)
f(mustVerifier(NewVerifierRS(RS384, rsaPublicKey384)), RS384)
Expand Down

0 comments on commit b938517

Please sign in to comment.