2015-01-31 19:32:55 +01:00
|
|
|
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire_test
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-07-03 02:43:33 +02:00
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2014-07-03 02:43:33 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestInvVectStringer tests the stringized output for inventory vector types.
|
|
|
|
func TestInvTypeStringer(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-01-31 19:32:55 +01:00
|
|
|
in wire.InvType
|
2013-05-08 21:31:00 +02:00
|
|
|
want string
|
|
|
|
}{
|
2015-01-31 19:32:55 +01:00
|
|
|
{wire.InvTypeError, "ERROR"},
|
|
|
|
{wire.InvTypeTx, "MSG_TX"},
|
|
|
|
{wire.InvTypeBlock, "MSG_BLOCK"},
|
2013-05-08 21:31:00 +02:00
|
|
|
{0xffffffff, "Unknown InvType (4294967295)"},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
result := test.in.String()
|
|
|
|
if result != test.want {
|
|
|
|
t.Errorf("String #%d\n got: %s want: %s", i, result,
|
|
|
|
test.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestInvVect tests the InvVect API.
|
|
|
|
func TestInvVect(t *testing.T) {
|
2015-01-31 19:32:55 +01:00
|
|
|
ivType := wire.InvTypeBlock
|
|
|
|
hash := wire.ShaHash{}
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// Ensure we get the same payload and signature back out.
|
2015-01-31 19:32:55 +01:00
|
|
|
iv := wire.NewInvVect(ivType, &hash)
|
2013-05-08 21:31:00 +02:00
|
|
|
if iv.Type != ivType {
|
|
|
|
t.Errorf("NewInvVect: wrong type - got %v, want %v",
|
|
|
|
iv.Type, ivType)
|
|
|
|
}
|
|
|
|
if !iv.Hash.IsEqual(&hash) {
|
|
|
|
t.Errorf("NewInvVect: wrong hash - got %v, want %v",
|
|
|
|
spew.Sdump(iv.Hash), spew.Sdump(hash))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestInvVectWire tests the InvVect wire encode and decode for various
|
|
|
|
// protocol versions and supported inventory vector types.
|
|
|
|
func TestInvVectWire(t *testing.T) {
|
|
|
|
// Block 203707 hash.
|
|
|
|
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
2015-01-31 19:32:55 +01:00
|
|
|
baseHash, err := wire.NewShaHashFromStr(hashStr)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("NewShaHashFromStr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// errInvVect is an inventory vector with an error.
|
2015-01-31 19:32:55 +01:00
|
|
|
errInvVect := wire.InvVect{
|
|
|
|
Type: wire.InvTypeError,
|
|
|
|
Hash: wire.ShaHash{},
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// errInvVectEncoded is the wire encoded bytes of errInvVect.
|
|
|
|
errInvVectEncoded := []byte{
|
2013-10-08 22:36:20 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, // InvTypeError
|
2013-05-08 21:31:00 +02:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // No hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// txInvVect is an inventory vector representing a transaction.
|
2015-01-31 19:32:55 +01:00
|
|
|
txInvVect := wire.InvVect{
|
|
|
|
Type: wire.InvTypeTx,
|
2013-05-08 21:31:00 +02:00
|
|
|
Hash: *baseHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// txInvVectEncoded is the wire encoded bytes of txInvVect.
|
|
|
|
txInvVectEncoded := []byte{
|
2013-10-08 22:36:20 +02:00
|
|
|
0x01, 0x00, 0x00, 0x00, // InvTypeTx
|
2013-05-08 21:31:00 +02:00
|
|
|
0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
|
|
|
|
0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
|
|
|
|
0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
|
|
|
|
0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockInvVect is an inventory vector representing a block.
|
2015-01-31 19:32:55 +01:00
|
|
|
blockInvVect := wire.InvVect{
|
|
|
|
Type: wire.InvTypeBlock,
|
2013-05-08 21:31:00 +02:00
|
|
|
Hash: *baseHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockInvVectEncoded is the wire encoded bytes of blockInvVect.
|
|
|
|
blockInvVectEncoded := []byte{
|
2013-10-08 22:36:20 +02:00
|
|
|
0x02, 0x00, 0x00, 0x00, // InvTypeBlock
|
2013-05-08 21:31:00 +02:00
|
|
|
0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
|
|
|
|
0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
|
|
|
|
0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
|
|
|
|
0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-01-31 19:32:55 +01:00
|
|
|
in wire.InvVect // NetAddress to encode
|
|
|
|
out wire.InvVect // Expected decoded NetAddress
|
|
|
|
buf []byte // Wire encoding
|
|
|
|
pver uint32 // Protocol version for wire encoding
|
2013-05-08 21:31:00 +02:00
|
|
|
}{
|
|
|
|
// Latest protocol version error inventory vector.
|
|
|
|
{
|
|
|
|
errInvVect,
|
|
|
|
errInvVect,
|
|
|
|
errInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.ProtocolVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Latest protocol version tx inventory vector.
|
|
|
|
{
|
|
|
|
txInvVect,
|
|
|
|
txInvVect,
|
|
|
|
txInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.ProtocolVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Latest protocol version block inventory vector.
|
|
|
|
{
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.ProtocolVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0035Version error inventory vector.
|
|
|
|
{
|
|
|
|
errInvVect,
|
|
|
|
errInvVect,
|
|
|
|
errInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0035Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0035Version tx inventory vector.
|
|
|
|
{
|
|
|
|
txInvVect,
|
|
|
|
txInvVect,
|
|
|
|
txInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0035Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0035Version block inventory vector.
|
|
|
|
{
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0035Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0031Version error inventory vector.
|
|
|
|
{
|
|
|
|
errInvVect,
|
|
|
|
errInvVect,
|
|
|
|
errInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0031Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0031Version tx inventory vector.
|
|
|
|
{
|
|
|
|
txInvVect,
|
|
|
|
txInvVect,
|
|
|
|
txInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0031Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0031Version block inventory vector.
|
|
|
|
{
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.BIP0031Version,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version NetAddressTimeVersion error inventory vector.
|
|
|
|
{
|
|
|
|
errInvVect,
|
|
|
|
errInvVect,
|
|
|
|
errInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.NetAddressTimeVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version NetAddressTimeVersion tx inventory vector.
|
|
|
|
{
|
|
|
|
txInvVect,
|
|
|
|
txInvVect,
|
|
|
|
txInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.NetAddressTimeVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version NetAddressTimeVersion block inventory vector.
|
|
|
|
{
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.NetAddressTimeVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version MultipleAddressVersion error inventory vector.
|
|
|
|
{
|
|
|
|
errInvVect,
|
|
|
|
errInvVect,
|
|
|
|
errInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.MultipleAddressVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version MultipleAddressVersion tx inventory vector.
|
|
|
|
{
|
|
|
|
txInvVect,
|
|
|
|
txInvVect,
|
|
|
|
txInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.MultipleAddressVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version MultipleAddressVersion block inventory vector.
|
|
|
|
{
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVect,
|
|
|
|
blockInvVectEncoded,
|
2015-01-31 19:32:55 +01:00
|
|
|
wire.MultipleAddressVersion,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
// Encode to wire format.
|
|
|
|
var buf bytes.Buffer
|
2015-01-31 19:32:55 +01:00
|
|
|
err := wire.TstWriteInvVect(&buf, test.pver, &test.in)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("writeInvVect #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
|
|
t.Errorf("writeInvVect #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the message from wire format.
|
2015-01-31 19:32:55 +01:00
|
|
|
var iv wire.InvVect
|
2014-06-05 06:39:03 +02:00
|
|
|
rbuf := bytes.NewReader(test.buf)
|
2015-01-31 19:32:55 +01:00
|
|
|
err = wire.TstReadInvVect(rbuf, test.pver, &iv)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("readInvVect #%d error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(iv, test.out) {
|
|
|
|
t.Errorf("readInvVect #%d\n got: %s want: %s", i,
|
|
|
|
spew.Sdump(iv), spew.Sdump(test.out))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|