Skip to content

Commit

Permalink
🩹 [wallet/test,backend/.../wallet] Test setup: Explicit zero address
Browse files Browse the repository at this point in the history
Before, the wallet address test was generating a zero address assuming a specific address encoding.
The assumption was that address encodings always have the same length and that decoding a zero bytes array of that length gives the zero address.
However, while this was true for the existing backends, it may not be true for all future backends.
Hence, we now provide the zero address specifically with the test setup.

Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs committed Oct 5, 2021
1 parent 72a972d commit 0dbb06a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions backend/ethereum/wallet/hd/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, accounts.Wallet
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: sampleBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, rawHDWallet, hdWallet
}
1 change: 1 addition & 0 deletions backend/ethereum/wallet/keystore/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func newSetup(t require.TestingT) *test.Setup {
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: validAddrBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/ethereum/wallet/simple/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func newSetup(t require.TestingT, prng *rand.Rand) (*test.Setup, *simple.Wallet)
AddressInWallet: acc.Address(),
Backend: new(ethwallet.Backend),
AddressEncoded: validAddrBytes,
ZeroAddress: ethwallet.AsWalletAddr(common.Address{}),
DataToSign: dataToSign,
}, simpleWallet
}
14 changes: 12 additions & 2 deletions backend/sim/wallet/wallet_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package wallet

import (
"bytes"
"math/big"
"math/rand"
"testing"
Expand Down Expand Up @@ -99,11 +100,20 @@ func newWalletSetup(rng *rand.Rand) *test.Setup {
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),
Backend: backend,
Wallet: w,
AddressInWallet: acc.Address(),
AddressEncoded: accountB.Address().Bytes(),
AddressEncoded: addrEncoded,
ZeroAddress: zeroAddr,
DataToSign: data,
}
}
9 changes: 6 additions & 3 deletions wallet/test/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
// TestAddress runs a test suite designed to test the general functionality of
// an address implementation.
func TestAddress(t *testing.T, s *Setup) {
addrLen := len(s.AddressEncoded)
null, err := s.Backend.DecodeAddress(bytes.NewReader(make([]byte, addrLen)))
assert.NoError(t, err, "Byte deserialization of zero address should work")
null := s.ZeroAddress
addr, err := s.Backend.DecodeAddress(bytes.NewReader(s.AddressEncoded))
assert.NoError(t, err, "Byte deserialization of address should work")

Expand All @@ -42,6 +40,11 @@ func TestAddress(t *testing.T, s *Setup) {
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()
Expand Down
1 change: 1 addition & 0 deletions wallet/test/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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
}
Expand Down

0 comments on commit 0dbb06a

Please sign in to comment.