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

@ -18,15 +18,17 @@ func TestAddresses(t *testing.T) {
name string name string
addr string addr string
valid bool valid bool
canDecode bool
result btcutil.Address result btcutil.Address
f func() (btcutil.Address, error) f func() (btcutil.Address, error)
net btcwire.BitcoinNet // only checked for P2PKH and P2SH 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,
@ -44,6 +46,7 @@ func TestAddresses(t *testing.T) {
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,
@ -61,6 +64,7 @@ func TestAddresses(t *testing.T) {
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,
@ -80,6 +84,7 @@ func TestAddresses(t *testing.T) {
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,
@ -91,6 +96,7 @@ 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,
@ -103,6 +109,7 @@ 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.
@ -113,6 +120,7 @@ func TestAddresses(t *testing.T) {
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,
@ -152,6 +160,7 @@ func TestAddresses(t *testing.T) {
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,
@ -170,6 +179,7 @@ func TestAddresses(t *testing.T) {
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,
@ -189,6 +199,7 @@ func TestAddresses(t *testing.T) {
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,
@ -201,6 +212,7 @@ 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,6 +223,7 @@ func TestAddresses(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
if test.canDecode {
// Decode addr and compare error against valid. // Decode addr and compare error against valid.
decoded, err := btcutil.DecodeAddr(test.addr) decoded, err := btcutil.DecodeAddr(test.addr)
if (err == nil) != test.valid { if (err == nil) != test.valid {
@ -263,6 +276,7 @@ func TestAddresses(t *testing.T) {
return return
} }
} }
}
if !test.valid { if !test.valid {
// If address is invalid, but a creation function exists, // If address is invalid, but a creation function exists,