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

refactor crypto keys test #158

Merged
merged 4 commits into from
Jul 29, 2022
Merged
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
226 changes: 27 additions & 199 deletions crypto/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,203 +7,31 @@ import (
)

func TestKeyToBytes(t *testing.T) {
t.Run("ed25519", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(Ed25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, Ed25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, Ed25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("X25519", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(X25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, X25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, X25519)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("Secp256k1", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(Secp256k1)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, Secp256k1)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, Secp256k1)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("P224", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(P224)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, P224)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P224)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("P256", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(P256)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, P256)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P256)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("P384", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(P384)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, P384)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P384)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("P521", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(P521)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, P521)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, P521)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})

t.Run("RSA", func(tt *testing.T) {
pub, priv, err := GenerateKeyByKeyType(RSA)
assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, RSA)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, RSA)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})
for _, keyType := range GetSupportedKeyTypes() {
t.Run(string(keyType), func(tt *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table driven tests are a go convention.

What do you think about adjusting this from a loop to a table test format?

https://github.com/golang/go/wiki/TableDrivenTests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like for a table driven test it has known input and output. With this unit test it doesn't really have a known output, so the table would only have on dimension an input.

I could explicitly create an array in the test to match this basically or we can keep using GetSupportedKeyTypes

https://github.com/TBD54566975/ssi-sdk/blob/main/crypto/models.go#L45

pub, priv, err := GenerateKeyByKeyType(keyType)

assert.NoError(tt, err)
assert.NotEmpty(tt, pub)
assert.NotEmpty(tt, priv)

pubKeyBytes, err := PubKeyToBytes(pub)
assert.NoError(tt, err)
assert.NotEmpty(tt, pubKeyBytes)

reconstructedPub, err := BytesToPubKey(pubKeyBytes, keyType)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPub)
assert.EqualValues(tt, pub, reconstructedPub)

privKeyBytes, err := PrivKeyToBytes(priv)
assert.NoError(tt, err)
assert.NotEmpty(tt, privKeyBytes)

reconstructedPriv, err := BytesToPrivKey(privKeyBytes, keyType)
assert.NoError(tt, err)
assert.NotEmpty(tt, reconstructedPriv)
assert.EqualValues(tt, priv, reconstructedPriv)
})
}
}