Add canDecode flag to tests.
This commit adds a new flag to the tests which controls whether or not an address can be decoded. This is to support the upcoming public key address type and possible future addresses which aren't directly decodable.
This commit is contained in:
parent
8928b361d4
commit
de0c59fee1
1 changed files with 95 additions and 81 deletions
176
address_test.go
176
address_test.go
|
@ -15,18 +15,20 @@ import (
|
|||
|
||||
func TestAddresses(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr string
|
||||
valid bool
|
||||
result btcutil.Address
|
||||
f func() (btcutil.Address, error)
|
||||
net btcwire.BitcoinNet // only checked for P2PKH and P2SH
|
||||
name string
|
||||
addr string
|
||||
valid bool
|
||||
canDecode bool
|
||||
result btcutil.Address
|
||||
f func() (btcutil.Address, error)
|
||||
net btcwire.BitcoinNet
|
||||
}{
|
||||
// Positive P2PKH tests.
|
||||
{
|
||||
name: "mainnet p2pkh",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||
valid: true,
|
||||
name: "mainnet p2pkh",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
|
||||
|
@ -41,9 +43,10 @@ func TestAddresses(t *testing.T) {
|
|||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
name: "mainnet p2pkh 2",
|
||||
addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
||||
valid: true,
|
||||
name: "mainnet p2pkh 2",
|
||||
addr: "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
|
||||
|
@ -58,9 +61,10 @@ func TestAddresses(t *testing.T) {
|
|||
net: btcwire.MainNet,
|
||||
},
|
||||
{
|
||||
name: "testnet p2pkh",
|
||||
addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: true,
|
||||
name: "testnet p2pkh",
|
||||
addr: "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressPubKeyHash(
|
||||
[ripemd160.Size]byte{
|
||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||
|
@ -77,9 +81,10 @@ func TestAddresses(t *testing.T) {
|
|||
|
||||
// Negative P2PKH tests.
|
||||
{
|
||||
name: "p2pkh wrong byte identifier/net",
|
||||
addr: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: false,
|
||||
name: "p2pkh wrong byte identifier/net",
|
||||
addr: "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
|
||||
valid: false,
|
||||
canDecode: true,
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
|
||||
|
@ -88,9 +93,10 @@ func TestAddresses(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "p2pkh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
name: "p2pkh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
canDecode: true,
|
||||
f: func() (btcutil.Address, error) {
|
||||
pkHash := []byte{
|
||||
0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b,
|
||||
|
@ -100,9 +106,10 @@ func TestAddresses(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "p2pkh bad checksum",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
|
||||
valid: false,
|
||||
name: "p2pkh bad checksum",
|
||||
addr: "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
|
||||
valid: false,
|
||||
canDecode: true,
|
||||
},
|
||||
|
||||
// Positive P2SH tests.
|
||||
|
@ -110,9 +117,10 @@ func TestAddresses(t *testing.T) {
|
|||
// Taken from transactions:
|
||||
// output: 3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac
|
||||
// input: 837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba.
|
||||
name: "mainnet p2sh",
|
||||
addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
||||
valid: true,
|
||||
name: "mainnet p2sh",
|
||||
addr: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2,
|
||||
|
@ -149,9 +157,10 @@ func TestAddresses(t *testing.T) {
|
|||
// Taken from transactions:
|
||||
// output: b0539a45de13b3e0403909b8bd1a555b8cbe45fd4e3f3fda76f3a5f52835c29d
|
||||
// input: (not yet redeemed at time test was written)
|
||||
name: "mainnet p2sh 2",
|
||||
addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
||||
valid: true,
|
||||
name: "mainnet p2sh 2",
|
||||
addr: "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37,
|
||||
|
@ -167,9 +176,10 @@ func TestAddresses(t *testing.T) {
|
|||
},
|
||||
{
|
||||
// Taken from bitcoind base58_keys_valid.
|
||||
name: "testnet p2sh",
|
||||
addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: true,
|
||||
name: "testnet p2sh",
|
||||
addr: "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: true,
|
||||
canDecode: true,
|
||||
result: btcutil.TstAddressScriptHash(
|
||||
[ripemd160.Size]byte{
|
||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||
|
@ -186,9 +196,10 @@ func TestAddresses(t *testing.T) {
|
|||
|
||||
// Negative P2SH tests.
|
||||
{
|
||||
name: "p2sh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
name: "p2sh wrong hash length",
|
||||
addr: "",
|
||||
valid: false,
|
||||
canDecode: true,
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9,
|
||||
|
@ -198,9 +209,10 @@ func TestAddresses(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "p2sh wrong byte identifier/net",
|
||||
addr: "0NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: false,
|
||||
name: "p2sh wrong byte identifier/net",
|
||||
addr: "0NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n",
|
||||
valid: false,
|
||||
canDecode: true,
|
||||
f: func() (btcutil.Address, error) {
|
||||
hash := []byte{
|
||||
0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e,
|
||||
|
@ -211,56 +223,58 @@ func TestAddresses(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
// Decode addr and compare error against valid.
|
||||
decoded, err := btcutil.DecodeAddr(test.addr)
|
||||
if (err == nil) != test.valid {
|
||||
t.Errorf("%v: decoding test failed", test.name)
|
||||
return
|
||||
}
|
||||
|
||||
// If decoding succeeded, encode again and compare against the original.
|
||||
if err == nil {
|
||||
encoded := decoded.EncodeAddress()
|
||||
|
||||
// Compare encoded addr against the original encoding.
|
||||
if test.addr != encoded {
|
||||
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
|
||||
test.name, test.addr, encoded)
|
||||
if test.canDecode {
|
||||
// Decode addr and compare error against valid.
|
||||
decoded, err := btcutil.DecodeAddr(test.addr)
|
||||
if (err == nil) != test.valid {
|
||||
t.Errorf("%v: decoding test failed", test.name)
|
||||
return
|
||||
}
|
||||
|
||||
// Perform type-specific calculations.
|
||||
var saddr []byte
|
||||
var net btcwire.BitcoinNet
|
||||
switch d := decoded.(type) {
|
||||
case *btcutil.AddressPubKeyHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
// If decoding succeeded, encode again and compare against the original.
|
||||
if err == nil {
|
||||
encoded := decoded.EncodeAddress()
|
||||
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
// Compare encoded addr against the original encoding.
|
||||
if test.addr != encoded {
|
||||
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
|
||||
test.name, test.addr, encoded)
|
||||
return
|
||||
}
|
||||
|
||||
case *btcutil.AddressScriptHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
// Perform type-specific calculations.
|
||||
var saddr []byte
|
||||
var net btcwire.BitcoinNet
|
||||
switch d := decoded.(type) {
|
||||
case *btcutil.AddressPubKeyHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
}
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
|
||||
// Check script address.
|
||||
if !bytes.Equal(saddr, decoded.ScriptAddress()) {
|
||||
t.Errorf("%v: script addresses do not match:\n%v != \n%v",
|
||||
test.name, saddr, decoded.ScriptAddress())
|
||||
return
|
||||
}
|
||||
case *btcutil.AddressScriptHash:
|
||||
saddr = btcutil.TstAddressSAddr(encoded)
|
||||
|
||||
// Check networks. This check always succeeds for non-P2PKH and
|
||||
// non-P2SH addresses as both nets will be Go's default zero value.
|
||||
if net != test.net {
|
||||
t.Errorf("%v: calculated network does not match expected",
|
||||
test.name)
|
||||
return
|
||||
// Net is not part of the Address interface and
|
||||
// must be calculated here.
|
||||
net = d.Net()
|
||||
}
|
||||
|
||||
// Check script address.
|
||||
if !bytes.Equal(saddr, decoded.ScriptAddress()) {
|
||||
t.Errorf("%v: script addresses do not match:\n%v != \n%v",
|
||||
test.name, saddr, decoded.ScriptAddress())
|
||||
return
|
||||
}
|
||||
|
||||
// Check networks. This check always succeeds for non-P2PKH and
|
||||
// non-P2SH addresses as both nets will be Go's default zero value.
|
||||
if net != test.net {
|
||||
t.Errorf("%v: calculated network does not match expected",
|
||||
test.name)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue