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

Revise generic wallet tests #225

Merged
16 changes: 7 additions & 9 deletions backend/ethereum/wallet/hd/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
"perun.network/go-perun/backend/ethereum/wallet/hd"
pkgtest "perun.network/go-perun/pkg/test"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wallet/test"
)

Expand All @@ -37,9 +36,9 @@ var sampleAddr = "1234560000000000000000000000000000000000"

func TestGenericSignatureTests(t *testing.T) {
s, _, _ := newSetup(t, pkgtest.Prng(t))
test.GenericSignatureTest(t, s)
test.TestAccountWithWalletAndBackend(t, s)
test.GenericSignatureSizeTest(t, s)
test.GenericAddressTest(t, s)
test.TestAddress(t, s)
}

func TestNewWallet(t *testing.T) {
Expand Down Expand Up @@ -87,8 +86,7 @@ func TestUnlock(t *testing.T) {
_, err := hdWallet.Unlock(ethwallet.AsWalletAddr(missingAddr))
assert.Error(t, err, "should error on unlocking missing address")

validAcc, _ := setup.UnlockedAccount()
acc, err := hdWallet.Unlock(validAcc.Address())
acc, err := hdWallet.Unlock(setup.AddressInWallet)
assert.NoError(t, err, "should not error on unlocking valid address")
assert.NotNil(t, acc, "account should be non nil when error is nil")
}
Expand All @@ -101,9 +99,7 @@ func TestContains(t *testing.T) {
missingAddr := common.BytesToAddress(setup.AddressEncoded)
assert.False(t, hdWallet.Contains(missingAddr), "should not contain address of the missing account")

validAcc, err := setup.UnlockedAccount()
require.NoError(t, err)
assert.True(t, hdWallet.Contains(ethwallet.AsEthAddr(validAcc.Address())), "should contain valid account")
assert.True(t, hdWallet.Contains(ethwallet.AsEthAddr(setup.AddressInWallet)), "should contain valid account")
}

// nolint:interfacer // rand.Rand is preferred over io.Reader here.
Expand All @@ -129,9 +125,11 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, accounts.Wallet
require.NoError(t, err, "invalid sample address")

return &test.Setup{
UnlockedAccount: func() (wallet.Account, error) { return acc, nil },
Wallet: hdWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: sampleBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, rawHDWallet, hdWallet
}
12 changes: 7 additions & 5 deletions backend/ethereum/wallet/keystore/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
ethwallettest "perun.network/go-perun/backend/ethereum/wallet/test"
pkgtest "perun.network/go-perun/pkg/test"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wallet/test"
)

Expand All @@ -39,12 +38,12 @@ const (

func TestGenericSignatureTests(t *testing.T) {
setup := newSetup(t)
test.GenericSignatureTest(t, setup)
test.TestAccountWithWalletAndBackend(t, setup)
test.GenericSignatureSizeTest(t, setup)
}

func TestGenericAddressTests(t *testing.T) {
test.GenericAddressTest(t, newSetup(t))
test.TestAddress(t, newSetup(t))
}

func TestWallet_Contains(t *testing.T) {
Expand Down Expand Up @@ -85,14 +84,17 @@ func TestBackend(t *testing.T) {
}

func newSetup(t require.TestingT) *test.Setup {
acc := ethwallettest.NewTmpWallet().NewAccount()
w := ethwallettest.NewTmpWallet()
acc := w.NewAccount()
validAddrBytes, err := hex.DecodeString(validAddr)
require.NoError(t, err, "decoding valid address should not fail")

return &test.Setup{
UnlockedAccount: func() (wallet.Account, error) { return acc, nil },
Wallet: w,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: validAddrBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}
}
Expand Down
16 changes: 7 additions & 9 deletions backend/ethereum/wallet/simple/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
"perun.network/go-perun/backend/ethereum/wallet/simple"
pkgtest "perun.network/go-perun/pkg/test"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wallet/test"
)

Expand All @@ -39,9 +38,9 @@ const sampleAddr = "1234560000000000000000000000000000000000"

func TestGenericSignatureTests(t *testing.T) {
setup, _ := newSetup(t, pkgtest.Prng(t))
test.GenericSignatureTest(t, setup)
test.TestAccountWithWalletAndBackend(t, setup)
test.GenericSignatureSizeTest(t, setup)
test.GenericAddressTest(t, setup)
test.TestAddress(t, setup)
}

func TestNewWallet(t *testing.T) {
Expand All @@ -59,8 +58,7 @@ func TestUnlock(t *testing.T) {
_, err := simpleWallet.Unlock(ethwallet.AsWalletAddr(missingAddr))
assert.Error(t, err, "should error on unlocking missing address")

validAcc, _ := setup.UnlockedAccount()
acc, err := simpleWallet.Unlock(validAcc.Address())
acc, err := simpleWallet.Unlock(setup.AddressInWallet)
assert.NoError(t, err, "should not error on unlocking missing address")
assert.NotNil(t, acc, "account should be non nil when error is nil")
}
Expand All @@ -71,9 +69,7 @@ func TestWallet_Contains(t *testing.T) {
missingAddr := common.BytesToAddress(setup.AddressEncoded)
assert.False(t, simpleWallet.Contains(missingAddr))

validAcc, err := setup.UnlockedAccount()
require.NoError(t, err)
assert.True(t, simpleWallet.Contains(ethwallet.AsEthAddr(validAcc.Address())))
assert.True(t, simpleWallet.Contains(ethwallet.AsEthAddr(setup.AddressInWallet)))
}

func TestSignatures(t *testing.T) {
Expand Down Expand Up @@ -110,9 +106,11 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, *simple.Wallet)
require.NoError(t, err, "invalid sample address")

return &test.Setup{
UnlockedAccount: func() (wallet.Account, error) { return acc, nil },
Wallet: simpleWallet,
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: validAddrBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, simpleWallet
}
27 changes: 0 additions & 27 deletions backend/sim/wallet/init_test.go

This file was deleted.

34 changes: 25 additions & 9 deletions backend/sim/wallet/wallet_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package wallet

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

"github.com/stretchr/testify/assert"

pkgtest "perun.network/go-perun/pkg/test"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wallet/test"
)

Expand Down Expand Up @@ -60,12 +60,12 @@ func TestGenericTests(t *testing.T) {
t.Run("Generic Address Test", func(t *testing.T) {
t.Parallel()
rng := pkgtest.Prng(t, "address")
test.GenericAddressTest(t, newWalletSetup(rng))
test.TestAddress(t, newWalletSetup(rng))
})
t.Run("Generic Signature Test", func(t *testing.T) {
t.Parallel()
rng := pkgtest.Prng(t, "signature")
test.GenericSignatureTest(t, newWalletSetup(rng))
test.TestAccountWithWalletAndBackend(t, newWalletSetup(rng))
test.GenericSignatureSizeTest(t, newWalletSetup(rng))
})

Expand All @@ -89,15 +89,31 @@ func TestGenericTests(t *testing.T) {
}
}

// nolint: interfacer
func newWalletSetup(rng *rand.Rand) *test.Setup {
accountA := NewRandomAccount(rng)
w := NewWallet()
acc := w.NewRandomAccount(rng)
accountB := NewRandomAccount(rng)
unlockedAccount := func() (wallet.Account, error) { return accountA, nil }

data := make([]byte, 128)
_, err := rng.Read(data)
if err != nil {
panic(err)
}

backend := new(Backend)
addrEncoded := accountB.Address().Bytes()
addrLen := len(addrEncoded)
zeroAddr, err := backend.DecodeAddress(bytes.NewReader(make([]byte, addrLen)))
if err != nil {
panic(err)
}

return &test.Setup{
Backend: new(Backend),
UnlockedAccount: unlockedAccount,
AddressEncoded: accountB.Address().Bytes(),
Backend: backend,
Wallet: w,
AddressInWallet: acc.Address(),
AddressEncoded: addrEncoded,
ZeroAddress: zeroAddr,
DataToSign: data,
}
}
29 changes: 0 additions & 29 deletions wallet/backendtest.go

This file was deleted.

63 changes: 63 additions & 0 deletions wallet/test/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2019 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestAddress runs a test suite designed to test the general functionality of
// an address implementation.
func TestAddress(t *testing.T, s *Setup) {
null := s.ZeroAddress
addr, err := s.Backend.DecodeAddress(bytes.NewReader(s.AddressEncoded))
assert.NoError(t, err, "Byte deserialization of address should work")

// Test Address.String.
nullString := null.String()
addrString := addr.String()
assert.Greater(t, len(nullString), 0)
assert.Greater(t, len(addrString), 0)
assert.NotEqual(t, addrString, nullString)

// Test Address.Equals.
assert.False(t, addr.Equals(null), "Expected inequality of zero, nonzero address")
assert.True(t, null.Equals(null), "Expected equality of zero address to itself")

// Test Address.Cmp.
assert.Positive(t, addr.Cmp(null), "Expected addr > zero")
assert.Zero(t, null.Cmp(null), "Expected zero = zero")
assert.Negative(t, null.Cmp(addr), "Expected null < addr")

// Test Address.Bytes.
addrBytes := addr.Bytes()
nullBytes := null.Bytes()
assert.False(t, bytes.Equal(addrBytes, nullBytes), "Expected inequality of byte representations of nonzero and zero address")
assert.True(t, bytes.Equal(addrBytes, addr.Bytes()), "Expected that byte representations do not change")

// a.Equals(Decode(Encode(a)))
t.Run("Serialize Equals Test", func(t *testing.T) {
buff := new(bytes.Buffer)
require.NoError(t, addr.Encode(buff))
addr2, err := s.Backend.DecodeAddress(buff)
require.NoError(t, err)

assert.True(t, addr.Equals(addr2))
})
}
29 changes: 0 additions & 29 deletions wallet/test/randomizertest.go

This file was deleted.

Loading