Skip to content

Commit

Permalink
Merge pull request #8 from joeabbey/increasing-coverage-while-reading…
Browse files Browse the repository at this point in the history
…-repo

Increasing code coverage
  • Loading branch information
gsgalloway authored Oct 2, 2019
2 parents 221f22c + 6fb2f7a commit 4a2ee1d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191001103751-88343688bb37
golang.org/x/sys v0.0.0-20190927073244-c990c680b611 // indirect
golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191001103751-88343688bb37 h1:gOdmyiD3mSKehzalEgl5w2v7b/99fwe3lDj+Rrt/PJk=
golang.org/x/crypto v0.0.0-20191001103751-88343688bb37/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc h1:KyTYo8xkh/2WdbFLUyQwBS0Jfn3qfZ9QmuPbok2oENE=
golang.org/x/crypto v0.0.0-20191001170739-f9e2070545dc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190927073244-c990c680b611 h1:q9u40nxWT5zRClI/uU9dHCiYGottAg6Nzz4YUQyHxdA=
golang.org/x/sys v0.0.0-20190927073244-c990c680b611/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed h1:5TJcLJn2a55mJjzYk0yOoqN8X1OdvBDUnaZaKKyQtkY=
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
86 changes: 69 additions & 17 deletions keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/hex"
"testing"

Expand All @@ -30,6 +32,8 @@ type keyTest struct {
ExpectedPrivateKeyBytes []byte
ExpectedPublicKey tezosprotocol.PublicKey
ExpectedPublicKeyBytes []byte
SupportedKeyType bool
CanDeserializePublicKey bool
}

var keysTestCases = []keyTest{
Expand All @@ -39,18 +43,36 @@ var keysTestCases = []keyTest{
ExpectedPrivateKeyBytes: fromHex("01010101010101010101010101010101010101010101010101010101010101018a88e3dd7409f195fd52db2d3cba5d72ca6709bf1d94121bf3748801b40f6f5c"),
ExpectedPublicKey: tezosprotocol.PublicKey("edpkuhEcwoLysLvodRxQLzuM3AVZvCuT6koVkUahS53mNBdE8LbuGo"),
ExpectedPublicKeyBytes: fromHex("008a88e3dd7409f195fd52db2d3cba5d72ca6709bf1d94121bf3748801b40f6f5c"),
SupportedKeyType: true,
CanDeserializePublicKey: true,
}, {
KeyType: "secp256k1",
ExpectedPrivateKey: tezosprotocol.PrivateKey("spsk1S1KpLsBEXYYw3nQEGHdNQDTjpBsJH9Y86XZVJNobHFkxezaPv"),
ExpectedPrivateKeyBytes: fromHex("0101010101010101024798bbd525dd3cfffad755af8ea0fffbbb8dec79497fc2"),
ExpectedPublicKey: tezosprotocol.PublicKey("sppk7czDjVPj1o3hVLeErZTi6brjZNYGc6jFWzFVvW3oRnki3XB58Yq"),
ExpectedPublicKeyBytes: fromHex("0103e4f8056521e0da9cfbb85bf7023d45089588c143e7cf4f784ff319cdc9c42385"),
SupportedKeyType: true,
CanDeserializePublicKey: true,
}, {
KeyType: "P256",
ExpectedPrivateKey: tezosprotocol.PrivateKey("p2sk2Mg6PgZcQ3hvj3SV6CXZvSGthGM9T91YENMMAwemHKx2AJRxU6"),
ExpectedPrivateKeyBytes: fromHex("02020201fefefeff01445d62b55152b9866561ee015f71beb5a0b12157501662"),
ExpectedPublicKey: tezosprotocol.PublicKey("p2pk653txU6DqbwmfVrpRjs3kWsMfFZD2bZxuDoMbNbu3FQ4s557mHT"),
ExpectedPublicKeyBytes: fromHex("02023ef92fb44bb6d204854a511f775947ff762d493357c1b91205ba173171f61a2c"),
SupportedKeyType: true,
CanDeserializePublicKey: false,
}, {
KeyType: "P224",
SupportedKeyType: false,
}, {
KeyType: "P384",
SupportedKeyType: false,
}, {
KeyType: "P521",
SupportedKeyType: false,
}, {
KeyType: "RSA4096",
SupportedKeyType: false,
},
}

Expand All @@ -74,28 +96,58 @@ func TestKeys(t *testing.T) {
require.NoError(err)
cryptoPrivateKey = ecdsaPrivKey
cryptoPublicKey = ecdsaPrivKey.PublicKey
case "P224":
ecdsaPrivKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
require.NoError(err)
cryptoPrivateKey = ecdsaPrivKey
cryptoPublicKey = ecdsaPrivKey.PublicKey
case "P384":
ecdsaPrivKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
require.NoError(err)
cryptoPrivateKey = ecdsaPrivKey
cryptoPublicKey = ecdsaPrivKey.PublicKey
case "P521":
ecdsaPrivKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
require.NoError(err)
cryptoPrivateKey = ecdsaPrivKey
cryptoPublicKey = ecdsaPrivKey.PublicKey
case "RSA4096":
rsaPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
require.NoError(err)
cryptoPrivateKey = rsaPrivKey
cryptoPublicKey = rsaPrivKey.PublicKey
}

// private key
privateKey, err := tezosprotocol.NewPrivateKeyFromCryptoPrivateKey(cryptoPrivateKey)
require.NoError(err)
require.Equal(testCase.ExpectedPrivateKey, privateKey)
cryptoPrivateKey2, err := privateKey.CryptoPrivateKey()
require.NoError(err)
require.Equal(cryptoPrivateKey, cryptoPrivateKey2)
privateKeyBytes, err := privateKey.MarshalBinary()
require.NoError(err)
require.Equal(testCase.ExpectedPrivateKeyBytes, privateKeyBytes, hex.EncodeToString(privateKeyBytes))

require.Equal(err == nil, testCase.SupportedKeyType)
// public key
publicKey, err := tezosprotocol.NewPublicKeyFromCryptoPublicKey(cryptoPublicKey)
require.NoError(err)
require.Equal(testCase.ExpectedPublicKey, publicKey)
publicKeyBytes, err := publicKey.MarshalBinary()
require.NoError(err)
require.Equal(testCase.ExpectedPublicKeyBytes, publicKeyBytes, hex.EncodeToString(publicKeyBytes))
var publicKey2 tezosprotocol.PublicKey
require.NoError(publicKey2.UnmarshalBinary(publicKeyBytes))
require.Equal(publicKey, publicKey2)
require.Equal(err == nil, testCase.SupportedKeyType)

if privateKey != "" {
require.Equal(testCase.ExpectedPrivateKey, privateKey)
cryptoPrivateKey2, err := privateKey.CryptoPrivateKey()
require.NoError(err)
require.Equal(cryptoPrivateKey, cryptoPrivateKey2)
privateKeyBytes, err := privateKey.MarshalBinary()
require.NoError(err)
require.Equal(testCase.ExpectedPrivateKeyBytes, privateKeyBytes, hex.EncodeToString(privateKeyBytes))
}
if publicKey != "" {
require.Equal(testCase.ExpectedPublicKey, publicKey)
publicKeyBytes, err := publicKey.MarshalBinary()
require.NoError(err)
require.Equal(testCase.ExpectedPublicKeyBytes, publicKeyBytes, hex.EncodeToString(publicKeyBytes))
var publicKey2 tezosprotocol.PublicKey
require.NoError(publicKey2.UnmarshalBinary(publicKeyBytes))
require.Equal(publicKey, publicKey2)
_, err = publicKey2.CryptoPublicKey()
if testCase.CanDeserializePublicKey {
require.NoError(err)
} else {
require.Error(err)
}
}
}
}
25 changes: 25 additions & 0 deletions p2p_messages_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tezosprotocol_test

import (
"bytes"
"encoding/hex"
"math/big"
"testing"

"github.com/anchorageoss/tezosprotocol"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ed25519"
)

type encodeDecodeTestCase struct {
Expand Down Expand Up @@ -319,3 +321,26 @@ func TestDeriveOriginatedAddress(t *testing.T) {
require.NoError(err)
require.Equal(tezosprotocol.ContractID("KT1MXc7s1ZtoVZvbws7vrmz1oLeVGPFoBqpL"), originatedAddr1)
}

func TestNewContractIDGeneration(t *testing.T) {
require := require.New(t)
cryptoPublicKey, _, err := ed25519.GenerateKey(bytes.NewReader(randSeed))
require.NoError(err)
publicKey, err := tezosprotocol.NewPublicKeyFromCryptoPublicKey(cryptoPublicKey)
require.NoError(err)
_, err = tezosprotocol.NewContractIDFromPublicKey(publicKey)
require.NoError(err)
}

func TestMessageSignatureVerification(t *testing.T) {
require := require.New(t)
msg := "Hi, my name is Werner Brandes. My voice is my passport. Verify Me."
cryptoPublicKey, cryptoPrivateKey, err := ed25519.GenerateKey(bytes.NewReader(randSeed))
require.NoError(err)
privateKey, err := tezosprotocol.NewPrivateKeyFromCryptoPrivateKey(cryptoPrivateKey)
require.NoError(err)
sig, err := tezosprotocol.SignMessage(msg, privateKey)
require.NoError(err)
err = tezosprotocol.VerifyMessage(msg, sig, cryptoPublicKey)
require.NoError(err)
}

0 comments on commit 4a2ee1d

Please sign in to comment.