Skip to content

Commit

Permalink
wallet-test: Use marshalling instead of encoding
Browse files Browse the repository at this point in the history
- Because encoding functions convert data to/from formats that can be
  send/received over the wire.

- But, here, we only want to convert it to binary form.

- Also, rename the variables in test setup to convey that these are
  marshalled values and not encoded ones.

Signed-off-by: Manoranjith <[email protected]>
  • Loading branch information
Manoranjith committed Jan 19, 2022
1 parent 0e6b47e commit 5f49e74
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 85 deletions.
28 changes: 11 additions & 17 deletions backend/ethereum/wallet/hd/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package hd_test

import (
"bytes"
"math/rand"
"testing"

Expand All @@ -29,7 +28,6 @@ import (
"perun.network/go-perun/backend/ethereum/wallet/hd"
ethwallettest "perun.network/go-perun/backend/ethereum/wallet/test"
"perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire/perunio"
pkgtest "polycry.pt/poly-go/test"
)

Expand Down Expand Up @@ -73,7 +71,7 @@ func TestNewWallet(t *testing.T) {
func TestSignWithMissingKey(t *testing.T) {
setup, accsWallet, _ := newSetup(t, pkgtest.Prng(t))

missingAddr := common.BytesToAddress(setup.AddressEncoded)
missingAddr := common.BytesToAddress(setup.AddressMarshalled)
acc := hd.NewAccountFromEth(accsWallet, accounts.Account{Address: missingAddr})
require.NotNil(t, acc)
_, err := acc.SignData(setup.DataToSign)
Expand All @@ -83,7 +81,7 @@ func TestSignWithMissingKey(t *testing.T) {
func TestUnlock(t *testing.T) {
setup, _, hdWallet := newSetup(t, pkgtest.Prng(t))

missingAddr := common.BytesToAddress(setup.AddressEncoded)
missingAddr := common.BytesToAddress(setup.AddressMarshalled)
_, err := hdWallet.Unlock(ethwallet.AsWalletAddr(missingAddr))
assert.Error(t, err, "should error on unlocking missing address")

Expand All @@ -97,7 +95,7 @@ func TestContains(t *testing.T) {

assert.False(t, hdWallet.Contains(common.Address{}), "should not contain nil account")

missingAddr := common.BytesToAddress(setup.AddressEncoded)
missingAddr := common.BytesToAddress(setup.AddressMarshalled)
assert.False(t, hdWallet.Contains(missingAddr), "should not contain address of the missing account")

assert.True(t, hdWallet.Contains(ethwallet.AsEthAddr(setup.AddressInWallet)), "should contain valid account")
Expand All @@ -122,19 +120,15 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, accounts.Wallet
require.NotNil(t, acc)

addressNotInWallet := ethwallettest.NewRandomAddress(prng)
var buff bytes.Buffer
err = perunio.Encode(&buff, &addressNotInWallet)
if err != nil {
panic(err)
}
addrEncoded := buff.Bytes()
addrMarshalled, err := addressNotInWallet.MarshalBinary()
require.NoError(t, err)

return &test.Setup{
Wallet: hdWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: addrEncoded,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
Wallet: hdWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressMarshalled: addrMarshalled,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, rawHDWallet, hdWallet
}
36 changes: 12 additions & 24 deletions backend/ethereum/wallet/keystore/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package keystore_test

import (
"bytes"
"encoding/hex"
"math/rand"
"testing"
Expand All @@ -27,7 +26,6 @@ import (
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
ethwallettest "perun.network/go-perun/backend/ethereum/wallet/test"
"perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire/perunio"
pkgtest "polycry.pt/poly-go/test"
)

Expand Down Expand Up @@ -72,38 +70,28 @@ func TestBackend(t *testing.T) {
backend := new(ethwallet.Backend)

s := newSetup(t, pkgtest.Prng(t))

buff := bytes.NewReader(s.AddressEncoded)
addr := backend.NewAddress()
err := perunio.Decode(buff, addr)
assert.NoError(t, err, "NewAddress from Bytes should work")
err := addr.UnmarshalBinary(s.AddressMarshalled)
assert.NoError(t, err, "UnmarshalBinary should work")

buff = bytes.NewReader([]byte(invalidAddr))
addr = backend.NewAddress()
err = perunio.Decode(buff, addr)
assert.Error(t, err, "Conversion from wrong address should fail")
err = addr.UnmarshalBinary([]byte(invalidAddr))
assert.Error(t, err, "UnmarshalBinary for an invalid address should fail")
}

func newSetup(t require.TestingT, prng *rand.Rand) *test.Setup {
w := ethwallettest.NewTmpWallet()

addressNotInWallet := ethwallettest.NewRandomAddress(prng)
var buff bytes.Buffer
err := perunio.Encode(&buff, &addressNotInWallet)
if err != nil {
panic(err)
}
addrEncoded := buff.Bytes()

require.NoError(t, err, "decoding valid address should not fail")
addrMarshalled, err := addressNotInWallet.MarshalBinary()
require.NoError(t, err)

return &test.Setup{
Wallet: w,
AddressInWallet: w.NewAccount().Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: addrEncoded,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
Wallet: w,
AddressInWallet: w.NewAccount().Address(),
Backend: new(ethwallet.Backend),
AddressMarshalled: addrMarshalled,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}
}

Expand Down
26 changes: 10 additions & 16 deletions backend/ethereum/wallet/simple/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package simple_test

import (
"bytes"
"crypto/ecdsa"
"math/rand"
"testing"
Expand All @@ -30,7 +29,6 @@ import (
"perun.network/go-perun/backend/ethereum/wallet/simple"
ethwallettest "perun.network/go-perun/backend/ethereum/wallet/test"
"perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire/perunio"
pkgtest "polycry.pt/poly-go/test"
)

Expand All @@ -54,7 +52,7 @@ func TestNewWallet(t *testing.T) {
func TestUnlock(t *testing.T) {
setup, simpleWallet := newSetup(t, pkgtest.Prng(t))

missingAddr := common.BytesToAddress(setup.AddressEncoded)
missingAddr := common.BytesToAddress(setup.AddressMarshalled)
_, err := simpleWallet.Unlock(ethwallet.AsWalletAddr(missingAddr))
assert.Error(t, err, "should error on unlocking missing address")

Expand All @@ -66,7 +64,7 @@ func TestUnlock(t *testing.T) {
func TestWallet_Contains(t *testing.T) {
setup, simpleWallet := newSetup(t, pkgtest.Prng(t))

missingAddr := common.BytesToAddress(setup.AddressEncoded)
missingAddr := common.BytesToAddress(setup.AddressMarshalled)
assert.False(t, simpleWallet.Contains(missingAddr))

assert.True(t, simpleWallet.Contains(ethwallet.AsEthAddr(setup.AddressInWallet)))
Expand Down Expand Up @@ -103,19 +101,15 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, *simple.Wallet)
require.NotNil(t, acc)

addressNotInWallet := ethwallettest.NewRandomAddress(prng)
var buff bytes.Buffer
err = perunio.Encode(&buff, &addressNotInWallet)
if err != nil {
panic(err)
}
addrEncoded := buff.Bytes()
addrMarshalled, err := addressNotInWallet.MarshalBinary()
require.NoError(t, err)

return &test.Setup{
Wallet: simpleWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: addrEncoded,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
Wallet: simpleWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressMarshalled: addrMarshalled,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, simpleWallet
}
18 changes: 7 additions & 11 deletions backend/sim/wallet/wallet_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package wallet

import (
"bytes"
"math/big"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"

"perun.network/go-perun/wallet/test"
"perun.network/go-perun/wire/perunio"
pkgtest "polycry.pt/poly-go/test"
)

Expand Down Expand Up @@ -101,12 +99,10 @@ func newWalletSetup(rng *rand.Rand) *test.Setup {
}

addressNotInWallet := NewRandomAccount(rng).Address()
var buff bytes.Buffer
err = perunio.Encode(&buff, addressNotInWallet)
addrMarshalled, err := addressNotInWallet.MarshalBinary()
if err != nil {
panic(err)
}
addrEncoded := buff.Bytes()

zeroAddr := &Address{
Curve: curve,
Expand All @@ -115,11 +111,11 @@ func newWalletSetup(rng *rand.Rand) *test.Setup {
}

return &test.Setup{
Backend: new(Backend),
Wallet: w,
AddressInWallet: w.NewRandomAccount(rng).Address(),
AddressEncoded: addrEncoded,
ZeroAddress: zeroAddr,
DataToSign: data,
Backend: new(Backend),
Wallet: w,
AddressInWallet: w.NewRandomAccount(rng).Address(),
AddressMarshalled: addrMarshalled,
ZeroAddress: zeroAddr,
DataToSign: data,
}
}
3 changes: 1 addition & 2 deletions wallet/test/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import (
func TestAddress(t *testing.T, s *Setup) { //nolint:revive // `test.Test...` stutters, but we accept that here.
null := s.ZeroAddress
addr := s.Backend.NewAddress()
err := perunio.Decode(bytes.NewReader(s.AddressEncoded), addr)
assert.NoError(t, err, "Byte deserialization of address should work")
assert.NoError(t, addr.UnmarshalBinary(s.AddressMarshalled), "Byte deserialization of address should work")

// Test Address.String.
nullString := null.String()
Expand Down
16 changes: 8 additions & 8 deletions wallet/test/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ type UnlockedAccount func() (wallet.Account, error)

// Setup provides all objects needed for the generic tests.
type Setup struct {
Backend wallet.Backend // backend implementation
Wallet wallet.Wallet // the wallet instance used for testing
AddressInWallet wallet.Address // an address of an account in the test wallet
ZeroAddress wallet.Address // an address that is less or equal to any other address
DataToSign []byte // some data to sign
AddressEncoded []byte // a valid nonzero address not in the wallet
Backend wallet.Backend // backend implementation
Wallet wallet.Wallet // the wallet instance used for testing
AddressInWallet wallet.Address // an address of an account in the test wallet
ZeroAddress wallet.Address // an address that is less or equal to any other address
DataToSign []byte // some data to sign
AddressMarshalled []byte // a valid nonzero address not in the wallet
}

// TestAccountWithWalletAndBackend tests an account implementation together with
Expand All @@ -54,8 +54,8 @@ func TestAccountWithWalletAndBackend(t *testing.T, s *Setup) { //nolint:revive /
assert.NoError(t, err, "Verification should not produce error")

addr := s.Backend.NewAddress()
err = perunio.Decode(bytes.NewReader(s.AddressEncoded), addr)
assert.NoError(t, err, "Byte deserialization of address should work")
err = addr.UnmarshalBinary(s.AddressMarshalled)
assert.NoError(t, err, "Binary unmarshalling of address should work")
valid, err = s.Backend.VerifySignature(s.DataToSign, sig, addr)
assert.False(t, valid, "Verification with wrong address should fail")
assert.NoError(t, err, "Verification of valid signature should not produce error")
Expand Down
16 changes: 9 additions & 7 deletions wallet/test/walletbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
package test

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
"perun.network/go-perun/wire/perunio"
)

// GenericAccountBenchmark runs a suite designed to benchmark the general speed of an implementation of an Account.
Expand All @@ -42,12 +40,15 @@ func benchAccountSign(b *testing.B, s *Setup) {
}
}

// GenericBackendBenchmark runs a suite designed to benchmark the general speed of an implementation of a Backend.
// This function should be called by every implementation of the Backend interface.
// GenericBackendBenchmark runs a suite designed to benchmark the general speed
// of an implementation of a Backend.
//
// This function should be called by every implementation of the Backend
// interface.
func GenericBackendBenchmark(b *testing.B, s *Setup) {
b.Helper()
b.Run("VerifySig", func(b *testing.B) { benchBackendVerifySig(b, s) })
b.Run("DecodeAddress", func(b *testing.B) { benchBackendDecodeAddress(b, s) })
b.Run("UnmarshalAddress", func(b *testing.B) { benchUnmarshalAddress(b, s) })
}

func benchBackendVerifySig(b *testing.B, s *Setup) {
Expand All @@ -69,10 +70,11 @@ func benchBackendVerifySig(b *testing.B, s *Setup) {
}
}

func benchBackendDecodeAddress(b *testing.B, s *Setup) {
func benchUnmarshalAddress(b *testing.B, s *Setup) {
b.Helper()
for n := 0; n < b.N; n++ {
err := perunio.Decode(bytes.NewReader(s.AddressEncoded), s.Backend.NewAddress())
addr := s.Backend.NewAddress()
err := addr.UnmarshalBinary(s.AddressMarshalled)
if err != nil {
b.Fatal(err)
}
Expand Down

0 comments on commit 5f49e74

Please sign in to comment.