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:
Dave Collins 2014-01-07 22:09:30 -06:00
parent 8928b361d4
commit de0c59fee1

View file

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