wire: Consolidate tests into the wire pkg. (#728)

Putting the test code in the same package makes it easier for forks
since they don't have to change the import paths as much and it also
gets rid of the need for internal_test.go to bridge.

This same thing should probably be done for the majority of the code
base.
This commit is contained in:
Dave Collins 2016-08-08 11:42:54 -05:00 committed by GitHub
parent 6e644855f5
commit d406d9e52b
34 changed files with 1220 additions and 1440 deletions

View file

@ -12,7 +12,6 @@ import (
"net"
"os"
"testing"
"time"
)
// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
@ -59,65 +58,6 @@ var genesisCoinbaseTx = MsgTx{
LockTime: 0,
}
// blockOne is the first block in the mainnet block chain.
var blockOne = MsgBlock{
Header: BlockHeader{
Version: 1,
PrevBlock: ShaHash([HashSize]byte{ // Make go vet happy.
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
}),
MerkleRoot: ShaHash([HashSize]byte{ // Make go vet happy.
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
0xcd, 0xb6, 0x06, 0xe8, 0x57, 0x23, 0x3e, 0x0e,
}),
Timestamp: time.Unix(0x4966bc61, 0), // 2009-01-08 20:54:25 -0600 CST
Bits: 0x1d00ffff, // 486604799
Nonce: 0x9962e301, // 2573394689
},
Transactions: []*MsgTx{
{
Version: 1,
TxIn: []*TxIn{
{
PreviousOutPoint: OutPoint{
Hash: ShaHash{},
Index: 0xffffffff,
},
SignatureScript: []byte{
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04,
},
Sequence: 0xffffffff,
},
},
TxOut: []*TxOut{
{
Value: 0x12a05f200,
PkScript: []byte{
0x41, // OP_DATA_65
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
0xee, // 65-byte signature
0xac, // OP_CHECKSIG
},
},
},
LockTime: 0,
},
},
}
// BenchmarkWriteVarInt1 performs a benchmark on how long it takes to write
// a single byte variable length integer.
func BenchmarkWriteVarInt1(b *testing.B) {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,13 +10,12 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestBlockHeader tests the BlockHeader API.
func TestBlockHeader(t *testing.T) {
nonce64, err := wire.RandomUint64()
nonce64, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: Error generating nonce: %v", err)
}
@ -25,7 +24,7 @@ func TestBlockHeader(t *testing.T) {
hash := mainNetGenesisHash
merkleHash := mainNetGenesisMerkleRoot
bits := uint32(0x1d00ffff)
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
bh := NewBlockHeader(&hash, &merkleHash, bits, nonce)
// Ensure we get the same data back out.
if !bh.PrevBlock.IsEqual(&hash) {
@ -54,7 +53,7 @@ func TestBlockHeaderWire(t *testing.T) {
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
bits := uint32(0x1d00ffff)
baseBlockHdr := &wire.BlockHeader{
baseBlockHdr := &BlockHeader{
Version: 1,
PrevBlock: mainNetGenesisHash,
MerkleRoot: mainNetGenesisMerkleRoot,
@ -80,8 +79,8 @@ func TestBlockHeaderWire(t *testing.T) {
}
tests := []struct {
in *wire.BlockHeader // Data to encode
out *wire.BlockHeader // Expected decoded data
in *BlockHeader // Data to encode
out *BlockHeader // Expected decoded data
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -90,7 +89,7 @@ func TestBlockHeaderWire(t *testing.T) {
baseBlockHdr,
baseBlockHdr,
baseBlockHdrEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version.
@ -98,7 +97,7 @@ func TestBlockHeaderWire(t *testing.T) {
baseBlockHdr,
baseBlockHdr,
baseBlockHdrEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -106,7 +105,7 @@ func TestBlockHeaderWire(t *testing.T) {
baseBlockHdr,
baseBlockHdr,
baseBlockHdrEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -114,7 +113,7 @@ func TestBlockHeaderWire(t *testing.T) {
baseBlockHdr,
baseBlockHdr,
baseBlockHdrEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -122,7 +121,7 @@ func TestBlockHeaderWire(t *testing.T) {
baseBlockHdr,
baseBlockHdr,
baseBlockHdrEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -130,7 +129,7 @@ func TestBlockHeaderWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.TstWriteBlockHeader(&buf, test.pver, test.in)
err := writeBlockHeader(&buf, test.pver, test.in)
if err != nil {
t.Errorf("writeBlockHeader #%d error %v", i, err)
continue
@ -154,9 +153,9 @@ func TestBlockHeaderWire(t *testing.T) {
}
// Decode the block header from wire format.
var bh wire.BlockHeader
var bh BlockHeader
rbuf := bytes.NewReader(test.buf)
err = wire.TstReadBlockHeader(rbuf, test.pver, &bh)
err = readBlockHeader(rbuf, test.pver, &bh)
if err != nil {
t.Errorf("readBlockHeader #%d error %v", i, err)
continue
@ -187,7 +186,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
bits := uint32(0x1d00ffff)
baseBlockHdr := &wire.BlockHeader{
baseBlockHdr := &BlockHeader{
Version: 1,
PrevBlock: mainNetGenesisHash,
MerkleRoot: mainNetGenesisMerkleRoot,
@ -213,8 +212,8 @@ func TestBlockHeaderSerialize(t *testing.T) {
}
tests := []struct {
in *wire.BlockHeader // Data to encode
out *wire.BlockHeader // Expected decoded data
in *BlockHeader // Data to encode
out *BlockHeader // Expected decoded data
buf []byte // Serialized data
}{
{
@ -240,7 +239,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
}
// Deserialize the block header.
var bh wire.BlockHeader
var bh BlockHeader
rbuf := bytes.NewReader(test.buf)
err = bh.Deserialize(rbuf)
if err != nil {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -12,13 +12,12 @@ import (
"strings"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// mainNetGenesisHash is the hash of the first block in the block chain for the
// main network (genesis block).
var mainNetGenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
var mainNetGenesisHash = ShaHash([HashSize]byte{ // Make go vet happy.
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
@ -27,7 +26,7 @@ var mainNetGenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
// mainNetGenesisMerkleRoot is the hash of the first transaction in the genesis
// block for the main network.
var mainNetGenesisMerkleRoot = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
var mainNetGenesisMerkleRoot = ShaHash([HashSize]byte{ // Make go vet happy.
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
@ -84,7 +83,7 @@ func TestElementWire(t *testing.T) {
[]byte{0x01, 0x02, 0x03, 0x04},
},
{
[wire.CommandSize]byte{
[CommandSize]byte{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c,
},
@ -104,7 +103,7 @@ func TestElementWire(t *testing.T) {
},
},
{
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
(*ShaHash)(&[HashSize]byte{ // Make go vet happy.
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
@ -118,15 +117,15 @@ func TestElementWire(t *testing.T) {
},
},
{
wire.ServiceFlag(wire.SFNodeNetwork),
ServiceFlag(SFNodeNetwork),
[]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
wire.InvType(wire.InvTypeTx),
InvType(InvTypeTx),
[]byte{0x01, 0x00, 0x00, 0x00},
},
{
wire.BitcoinNet(wire.MainNet),
BitcoinNet(MainNet),
[]byte{0xf9, 0xbe, 0xb4, 0xd9},
},
// Type not supported by the "fast" path and requires reflection.
@ -140,7 +139,7 @@ func TestElementWire(t *testing.T) {
for i, test := range tests {
// Write to wire format.
var buf bytes.Buffer
err := wire.TstWriteElement(&buf, test.in)
err := writeElement(&buf, test.in)
if err != nil {
t.Errorf("writeElement #%d error %v", i, err)
continue
@ -157,7 +156,7 @@ func TestElementWire(t *testing.T) {
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
val = reflect.New(reflect.TypeOf(test.in)).Interface()
}
err = wire.TstReadElement(rbuf, val)
err = readElement(rbuf, val)
if err != nil {
t.Errorf("readElement #%d error %v", i, err)
continue
@ -189,7 +188,7 @@ func TestElementWireErrors(t *testing.T) {
{true, 0, io.ErrShortWrite, io.EOF},
{[4]byte{0x01, 0x02, 0x03, 0x04}, 0, io.ErrShortWrite, io.EOF},
{
[wire.CommandSize]byte{
[CommandSize]byte{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c,
},
@ -203,7 +202,7 @@ func TestElementWireErrors(t *testing.T) {
0, io.ErrShortWrite, io.EOF,
},
{
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
(*ShaHash)(&[HashSize]byte{ // Make go vet happy.
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
@ -211,16 +210,16 @@ func TestElementWireErrors(t *testing.T) {
}),
0, io.ErrShortWrite, io.EOF,
},
{wire.ServiceFlag(wire.SFNodeNetwork), 0, io.ErrShortWrite, io.EOF},
{wire.InvType(wire.InvTypeTx), 0, io.ErrShortWrite, io.EOF},
{wire.BitcoinNet(wire.MainNet), 0, io.ErrShortWrite, io.EOF},
{ServiceFlag(SFNodeNetwork), 0, io.ErrShortWrite, io.EOF},
{InvType(InvTypeTx), 0, io.ErrShortWrite, io.EOF},
{BitcoinNet(MainNet), 0, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := wire.TstWriteElement(w, test.in)
err := writeElement(w, test.in)
if err != test.writeErr {
t.Errorf("writeElement #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@ -233,7 +232,7 @@ func TestElementWireErrors(t *testing.T) {
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
val = reflect.New(reflect.TypeOf(test.in)).Interface()
}
err = wire.TstReadElement(r, val)
err = readElement(r, val)
if err != test.readErr {
t.Errorf("readElement #%d wrong error got: %v, want: %v",
i, err, test.readErr)
@ -244,7 +243,7 @@ func TestElementWireErrors(t *testing.T) {
// TestVarIntWire tests wire encode and decode for variable length integers.
func TestVarIntWire(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
in uint64 // Value to encode
@ -283,7 +282,7 @@ func TestVarIntWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.TstWriteVarInt(&buf, test.pver, test.in)
err := WriteVarInt(&buf, test.pver, test.in)
if err != nil {
t.Errorf("WriteVarInt #%d error %v", i, err)
continue
@ -296,7 +295,7 @@ func TestVarIntWire(t *testing.T) {
// Decode from wire format.
rbuf := bytes.NewReader(test.buf)
val, err := wire.TstReadVarInt(rbuf, test.pver)
val, err := ReadVarInt(rbuf, test.pver)
if err != nil {
t.Errorf("ReadVarInt #%d error %v", i, err)
continue
@ -312,7 +311,7 @@ func TestVarIntWire(t *testing.T) {
// TestVarIntWireErrors performs negative tests against wire encode and decode
// of variable length integers to confirm error paths work correctly.
func TestVarIntWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
in uint64 // Value to encode
@ -336,7 +335,7 @@ func TestVarIntWireErrors(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := wire.TstWriteVarInt(w, test.pver, test.in)
err := WriteVarInt(w, test.pver, test.in)
if err != test.writeErr {
t.Errorf("WriteVarInt #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@ -345,7 +344,7 @@ func TestVarIntWireErrors(t *testing.T) {
// Decode from wire format.
r := newFixedReader(test.max, test.buf)
_, err = wire.TstReadVarInt(r, test.pver)
_, err = ReadVarInt(r, test.pver)
if err != test.readErr {
t.Errorf("ReadVarInt #%d wrong error got: %v, want: %v",
i, err, test.readErr)
@ -357,7 +356,7 @@ func TestVarIntWireErrors(t *testing.T) {
// TestVarIntNonCanonical ensures variable length integers that are not encoded
// canonically return the expected error.
func TestVarIntNonCanonical(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
name string // Test name for easier identification
@ -396,8 +395,8 @@ func TestVarIntNonCanonical(t *testing.T) {
for i, test := range tests {
// Decode from wire format.
rbuf := bytes.NewReader(test.in)
val, err := wire.TstReadVarInt(rbuf, test.pver)
if _, ok := err.(*wire.MessageError); !ok {
val, err := ReadVarInt(rbuf, test.pver)
if _, ok := err.(*MessageError); !ok {
t.Errorf("ReadVarInt #%d (%s) unexpected error %v", i,
test.name, err)
continue
@ -436,7 +435,7 @@ func TestVarIntSerializeSize(t *testing.T) {
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
serializedSize := wire.VarIntSerializeSize(test.val)
serializedSize := VarIntSerializeSize(test.val)
if serializedSize != test.size {
t.Errorf("VarIntSerializeSize #%d got: %d, want: %d", i,
serializedSize, test.size)
@ -447,7 +446,7 @@ func TestVarIntSerializeSize(t *testing.T) {
// TestVarStringWire tests wire encode and decode for variable length strings.
func TestVarStringWire(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// str256 is a string that takes a 2-byte varint to encode.
str256 := strings.Repeat("test", 64)
@ -471,7 +470,7 @@ func TestVarStringWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.WriteVarString(&buf, test.pver, test.in)
err := WriteVarString(&buf, test.pver, test.in)
if err != nil {
t.Errorf("WriteVarString #%d error %v", i, err)
continue
@ -484,7 +483,7 @@ func TestVarStringWire(t *testing.T) {
// Decode from wire format.
rbuf := bytes.NewReader(test.buf)
val, err := wire.ReadVarString(rbuf, test.pver)
val, err := ReadVarString(rbuf, test.pver)
if err != nil {
t.Errorf("ReadVarString #%d error %v", i, err)
continue
@ -500,7 +499,7 @@ func TestVarStringWire(t *testing.T) {
// TestVarStringWireErrors performs negative tests against wire encode and
// decode of variable length strings to confirm error paths work correctly.
func TestVarStringWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// str256 is a string that takes a 2-byte varint to encode.
str256 := strings.Repeat("test", 64)
@ -526,7 +525,7 @@ func TestVarStringWireErrors(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := wire.WriteVarString(w, test.pver, test.in)
err := WriteVarString(w, test.pver, test.in)
if err != test.writeErr {
t.Errorf("WriteVarString #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@ -535,7 +534,7 @@ func TestVarStringWireErrors(t *testing.T) {
// Decode from wire format.
r := newFixedReader(test.max, test.buf)
_, err = wire.ReadVarString(r, test.pver)
_, err = ReadVarString(r, test.pver)
if err != test.readErr {
t.Errorf("ReadVarString #%d wrong error got: %v, want: %v",
i, err, test.readErr)
@ -549,7 +548,7 @@ func TestVarStringWireErrors(t *testing.T) {
// length are handled properly. This could otherwise potentially be used as an
// attack vector.
func TestVarStringOverflowErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
buf []byte // Wire encoding
@ -557,16 +556,16 @@ func TestVarStringOverflowErrors(t *testing.T) {
err error // Expected error
}{
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
pver, &wire.MessageError{}},
pver, &MessageError{}},
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
pver, &wire.MessageError{}},
pver, &MessageError{}},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
rbuf := bytes.NewReader(test.buf)
_, err := wire.ReadVarString(rbuf, test.pver)
_, err := ReadVarString(rbuf, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("ReadVarString #%d wrong error got: %v, "+
"want: %v", i, err, reflect.TypeOf(test.err))
@ -578,7 +577,7 @@ func TestVarStringOverflowErrors(t *testing.T) {
// TestVarBytesWire tests wire encode and decode for variable length byte array.
func TestVarBytesWire(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// bytes256 is a byte array that takes a 2-byte varint to encode.
bytes256 := bytes.Repeat([]byte{0x01}, 256)
@ -601,7 +600,7 @@ func TestVarBytesWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.TstWriteVarBytes(&buf, test.pver, test.in)
err := WriteVarBytes(&buf, test.pver, test.in)
if err != nil {
t.Errorf("WriteVarBytes #%d error %v", i, err)
continue
@ -614,8 +613,8 @@ func TestVarBytesWire(t *testing.T) {
// Decode from wire format.
rbuf := bytes.NewReader(test.buf)
val, err := wire.TstReadVarBytes(rbuf, test.pver,
wire.MaxMessagePayload, "test payload")
val, err := ReadVarBytes(rbuf, test.pver, MaxMessagePayload,
"test payload")
if err != nil {
t.Errorf("ReadVarBytes #%d error %v", i, err)
continue
@ -631,7 +630,7 @@ func TestVarBytesWire(t *testing.T) {
// TestVarBytesWireErrors performs negative tests against wire encode and
// decode of variable length byte arrays to confirm error paths work correctly.
func TestVarBytesWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// bytes256 is a byte array that takes a 2-byte varint to encode.
bytes256 := bytes.Repeat([]byte{0x01}, 256)
@ -657,7 +656,7 @@ func TestVarBytesWireErrors(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := wire.TstWriteVarBytes(w, test.pver, test.in)
err := WriteVarBytes(w, test.pver, test.in)
if err != test.writeErr {
t.Errorf("WriteVarBytes #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@ -666,8 +665,8 @@ func TestVarBytesWireErrors(t *testing.T) {
// Decode from wire format.
r := newFixedReader(test.max, test.buf)
_, err = wire.TstReadVarBytes(r, test.pver,
wire.MaxMessagePayload, "test payload")
_, err = ReadVarBytes(r, test.pver, MaxMessagePayload,
"test payload")
if err != test.readErr {
t.Errorf("ReadVarBytes #%d wrong error got: %v, want: %v",
i, err, test.readErr)
@ -681,7 +680,7 @@ func TestVarBytesWireErrors(t *testing.T) {
// length are handled properly. This could otherwise potentially be used as an
// attack vector.
func TestVarBytesOverflowErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
buf []byte // Wire encoding
@ -689,17 +688,17 @@ func TestVarBytesOverflowErrors(t *testing.T) {
err error // Expected error
}{
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
pver, &wire.MessageError{}},
pver, &MessageError{}},
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
pver, &wire.MessageError{}},
pver, &MessageError{}},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
rbuf := bytes.NewReader(test.buf)
_, err := wire.TstReadVarBytes(rbuf, test.pver,
wire.MaxMessagePayload, "test payload")
_, err := ReadVarBytes(rbuf, test.pver, MaxMessagePayload,
"test payload")
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("ReadVarBytes #%d wrong error got: %v, "+
"want: %v", i, err, reflect.TypeOf(test.err))
@ -725,7 +724,7 @@ func TestRandomUint64(t *testing.T) {
numHits := 0
for i := 0; i < tries; i++ {
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64 iteration %d failed - err %v",
i, err)
@ -748,7 +747,7 @@ func TestRandomUint64(t *testing.T) {
func TestRandomUint64Errors(t *testing.T) {
// Test short reads.
fr := &fakeRandReader{n: 2, err: io.EOF}
nonce, err := wire.TstRandomUint64(fr)
nonce, err := randomUint64(fr)
if err != io.ErrUnexpectedEOF {
t.Errorf("Error not expected value of %v [%v]",
io.ErrUnexpectedEOF, err)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"net"

View file

@ -1,17 +1,13 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"io"
import "io"
"github.com/btcsuite/btcd/wire"
)
// fakeMessage implements the wire.Message interface and is used to force
// encode errors in messages.
// fakeMessage implements the Message interface and is used to force encode
// errors in messages.
type fakeMessage struct {
command string
payload []byte
@ -19,18 +15,17 @@ type fakeMessage struct {
forceLenErr bool
}
// BtcDecode doesn't do anything. It just satisfies the wire.Message
// interface.
// BtcDecode doesn't do anything. It just satisfies the Message interface.
func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error {
return nil
}
// BtcEncode writes the payload field of the fake message or forces an error
// if the forceEncodeErr flag of the fake message is set. It also satisfies the
// wire.Message interface.
// Message interface.
func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
if msg.forceEncodeErr {
err := &wire.MessageError{
err := &MessageError{
Func: "fakeMessage.BtcEncode",
Description: "intentional error",
}
@ -42,14 +37,14 @@ func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
}
// Command returns the command field of the fake message and satisfies the
// wire.Message interface.
// Message interface.
func (msg *fakeMessage) Command() string {
return msg.command
}
// MaxPayloadLength returns the length of the payload field of fake message
// or a smaller value if the forceLenErr flag of the fake message is set. It
// satisfies the wire.Message interface.
// satisfies the Message interface.
func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
lenp := uint32(len(msg.payload))
if msg.forceLenErr {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"

View file

@ -1,118 +0,0 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
/*
This test file is part of the wire package rather than than the wire_test
package so it can bridge access to the internals to properly test cases which
are either not possible or can't reliably be tested via the public interface.
The functions are only exported while the tests are being run.
*/
package wire
import (
"io"
)
const (
// MaxTxPerBlock makes the internal maxTxPerBlock constant available to
// the test package.
MaxTxPerBlock = maxTxPerBlock
// MaxFlagsPerMerkleBlock makes the internal maxFlagsPerMerkleBlock
// constant available to the test package.
MaxFlagsPerMerkleBlock = maxFlagsPerMerkleBlock
// MaxCountSetCancel makes the internal maxCountSetCancel constant
// available to the test package.
MaxCountSetCancel = maxCountSetCancel
// MaxCountSetSubVer makes the internal maxCountSetSubVer constant
// available to the test package.
MaxCountSetSubVer = maxCountSetSubVer
)
// TstRandomUint64 makes the internal randomUint64 function available to the
// test package.
func TstRandomUint64(r io.Reader) (uint64, error) {
return randomUint64(r)
}
// TstReadElement makes the internal readElement function available to the
// test package.
func TstReadElement(r io.Reader, element interface{}) error {
return readElement(r, element)
}
// TstWriteElement makes the internal writeElement function available to the
// test package.
func TstWriteElement(w io.Writer, element interface{}) error {
return writeElement(w, element)
}
// TstReadVarInt makes the internal ReadVarInt function available to the
// test package.
func TstReadVarInt(r io.Reader, pver uint32) (uint64, error) {
return ReadVarInt(r, pver)
}
// TstWriteVarInt makes the internal WriteVarInt function available to the
// test package.
func TstWriteVarInt(w io.Writer, pver uint32, val uint64) error {
return WriteVarInt(w, pver, val)
}
// TstReadVarBytes makes the internal ReadVarBytes function available to the
// test package.
func TstReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32, fieldName string) ([]byte, error) {
return ReadVarBytes(r, pver, maxAllowed, fieldName)
}
// TstWriteVarBytes makes the internal WriteVarBytes function available to the
// test package.
func TstWriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
return WriteVarBytes(w, pver, bytes)
}
// TstReadNetAddress makes the internal readNetAddress function available to
// the test package.
func TstReadNetAddress(r io.Reader, pver uint32, na *NetAddress, ts bool) error {
return readNetAddress(r, pver, na, ts)
}
// TstWriteNetAddress makes the internal writeNetAddress function available to
// the test package.
func TstWriteNetAddress(w io.Writer, pver uint32, na *NetAddress, ts bool) error {
return writeNetAddress(w, pver, na, ts)
}
// TstMaxNetAddressPayload makes the internal maxNetAddressPayload function
// available to the test package.
func TstMaxNetAddressPayload(pver uint32) uint32 {
return maxNetAddressPayload(pver)
}
// TstReadInvVect makes the internal readInvVect function available to the test
// package.
func TstReadInvVect(r io.Reader, pver uint32, iv *InvVect) error {
return readInvVect(r, pver, iv)
}
// TstWriteInvVect makes the internal writeInvVect function available to the
// test package.
func TstWriteInvVect(w io.Writer, pver uint32, iv *InvVect) error {
return writeInvVect(w, pver, iv)
}
// TstReadBlockHeader makes the internal readBlockHeader function available to
// the test package.
func TstReadBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
return readBlockHeader(r, pver, bh)
}
// TstWriteBlockHeader makes the internal writeBlockHeader function available to
// the test package.
func TstWriteBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
return writeBlockHeader(w, pver, bh)
}

View file

@ -1,27 +1,26 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestInvVectStringer tests the stringized output for inventory vector types.
func TestInvTypeStringer(t *testing.T) {
tests := []struct {
in wire.InvType
in InvType
want string
}{
{wire.InvTypeError, "ERROR"},
{wire.InvTypeTx, "MSG_TX"},
{wire.InvTypeBlock, "MSG_BLOCK"},
{InvTypeError, "ERROR"},
{InvTypeTx, "MSG_TX"},
{InvTypeBlock, "MSG_BLOCK"},
{0xffffffff, "Unknown InvType (4294967295)"},
}
@ -39,11 +38,11 @@ func TestInvTypeStringer(t *testing.T) {
// TestInvVect tests the InvVect API.
func TestInvVect(t *testing.T) {
ivType := wire.InvTypeBlock
hash := wire.ShaHash{}
ivType := InvTypeBlock
hash := ShaHash{}
// Ensure we get the same payload and signature back out.
iv := wire.NewInvVect(ivType, &hash)
iv := NewInvVect(ivType, &hash)
if iv.Type != ivType {
t.Errorf("NewInvVect: wrong type - got %v, want %v",
iv.Type, ivType)
@ -60,15 +59,15 @@ func TestInvVect(t *testing.T) {
func TestInvVectWire(t *testing.T) {
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
baseHash, err := wire.NewShaHashFromStr(hashStr)
baseHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// errInvVect is an inventory vector with an error.
errInvVect := wire.InvVect{
Type: wire.InvTypeError,
Hash: wire.ShaHash{},
errInvVect := InvVect{
Type: InvTypeError,
Hash: ShaHash{},
}
// errInvVectEncoded is the wire encoded bytes of errInvVect.
@ -81,8 +80,8 @@ func TestInvVectWire(t *testing.T) {
}
// txInvVect is an inventory vector representing a transaction.
txInvVect := wire.InvVect{
Type: wire.InvTypeTx,
txInvVect := InvVect{
Type: InvTypeTx,
Hash: *baseHash,
}
@ -96,8 +95,8 @@ func TestInvVectWire(t *testing.T) {
}
// blockInvVect is an inventory vector representing a block.
blockInvVect := wire.InvVect{
Type: wire.InvTypeBlock,
blockInvVect := InvVect{
Type: InvTypeBlock,
Hash: *baseHash,
}
@ -111,8 +110,8 @@ func TestInvVectWire(t *testing.T) {
}
tests := []struct {
in wire.InvVect // NetAddress to encode
out wire.InvVect // Expected decoded NetAddress
in InvVect // NetAddress to encode
out InvVect // Expected decoded NetAddress
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -121,7 +120,7 @@ func TestInvVectWire(t *testing.T) {
errInvVect,
errInvVect,
errInvVectEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version tx inventory vector.
@ -129,7 +128,7 @@ func TestInvVectWire(t *testing.T) {
txInvVect,
txInvVect,
txInvVectEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version block inventory vector.
@ -137,7 +136,7 @@ func TestInvVectWire(t *testing.T) {
blockInvVect,
blockInvVect,
blockInvVectEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version error inventory vector.
@ -145,7 +144,7 @@ func TestInvVectWire(t *testing.T) {
errInvVect,
errInvVect,
errInvVectEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version tx inventory vector.
@ -153,7 +152,7 @@ func TestInvVectWire(t *testing.T) {
txInvVect,
txInvVect,
txInvVectEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version block inventory vector.
@ -161,7 +160,7 @@ func TestInvVectWire(t *testing.T) {
blockInvVect,
blockInvVect,
blockInvVectEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version error inventory vector.
@ -169,7 +168,7 @@ func TestInvVectWire(t *testing.T) {
errInvVect,
errInvVect,
errInvVectEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version tx inventory vector.
@ -177,7 +176,7 @@ func TestInvVectWire(t *testing.T) {
txInvVect,
txInvVect,
txInvVectEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version block inventory vector.
@ -185,7 +184,7 @@ func TestInvVectWire(t *testing.T) {
blockInvVect,
blockInvVect,
blockInvVectEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion error inventory vector.
@ -193,7 +192,7 @@ func TestInvVectWire(t *testing.T) {
errInvVect,
errInvVect,
errInvVectEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion tx inventory vector.
@ -201,7 +200,7 @@ func TestInvVectWire(t *testing.T) {
txInvVect,
txInvVect,
txInvVectEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion block inventory vector.
@ -209,7 +208,7 @@ func TestInvVectWire(t *testing.T) {
blockInvVect,
blockInvVect,
blockInvVectEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion error inventory vector.
@ -217,7 +216,7 @@ func TestInvVectWire(t *testing.T) {
errInvVect,
errInvVect,
errInvVectEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion tx inventory vector.
@ -225,7 +224,7 @@ func TestInvVectWire(t *testing.T) {
txInvVect,
txInvVect,
txInvVectEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion block inventory vector.
@ -233,7 +232,7 @@ func TestInvVectWire(t *testing.T) {
blockInvVect,
blockInvVect,
blockInvVectEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -241,7 +240,7 @@ func TestInvVectWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.TstWriteInvVect(&buf, test.pver, &test.in)
err := writeInvVect(&buf, test.pver, &test.in)
if err != nil {
t.Errorf("writeInvVect #%d error %v", i, err)
continue
@ -253,9 +252,9 @@ func TestInvVectWire(t *testing.T) {
}
// Decode the message from wire format.
var iv wire.InvVect
var iv InvVect
rbuf := bytes.NewReader(test.buf)
err = wire.TstReadInvVect(rbuf, test.pver, &iv)
err = readInvVect(rbuf, test.pver, &iv)
if err != nil {
t.Errorf("readInvVect #%d error %v", i, err)
continue

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -13,13 +13,12 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// makeHeader is a convenience function to make a message header in the form of
// a byte slice. It is used to force errors when reading messages.
func makeHeader(btcnet wire.BitcoinNet, command string,
func makeHeader(btcnet BitcoinNet, command string,
payloadLen uint32, checksum uint32) []byte {
// The length of a bitcoin message header is 24 bytes.
@ -35,82 +34,82 @@ func makeHeader(btcnet wire.BitcoinNet, command string,
// TestMessage tests the Read/WriteMessage and Read/WriteMessageN API.
func TestMessage(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Create the various types of messages to test.
// MsgVersion.
addrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
you, err := wire.NewNetAddress(addrYou, wire.SFNodeNetwork)
you, err := NewNetAddress(addrYou, SFNodeNetwork)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
you.Timestamp = time.Time{} // Version message has zero value timestamp.
addrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
me, err := wire.NewNetAddress(addrMe, wire.SFNodeNetwork)
me, err := NewNetAddress(addrMe, SFNodeNetwork)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
me.Timestamp = time.Time{} // Version message has zero value timestamp.
msgVersion := wire.NewMsgVersion(me, you, 123123, 0)
msgVersion := NewMsgVersion(me, you, 123123, 0)
msgVerack := wire.NewMsgVerAck()
msgGetAddr := wire.NewMsgGetAddr()
msgAddr := wire.NewMsgAddr()
msgGetBlocks := wire.NewMsgGetBlocks(&wire.ShaHash{})
msgVerack := NewMsgVerAck()
msgGetAddr := NewMsgGetAddr()
msgAddr := NewMsgAddr()
msgGetBlocks := NewMsgGetBlocks(&ShaHash{})
msgBlock := &blockOne
msgInv := wire.NewMsgInv()
msgGetData := wire.NewMsgGetData()
msgNotFound := wire.NewMsgNotFound()
msgTx := wire.NewMsgTx()
msgPing := wire.NewMsgPing(123123)
msgPong := wire.NewMsgPong(123123)
msgGetHeaders := wire.NewMsgGetHeaders()
msgHeaders := wire.NewMsgHeaders()
msgAlert := wire.NewMsgAlert([]byte("payload"), []byte("signature"))
msgMemPool := wire.NewMsgMemPool()
msgFilterAdd := wire.NewMsgFilterAdd([]byte{0x01})
msgFilterClear := wire.NewMsgFilterClear()
msgFilterLoad := wire.NewMsgFilterLoad([]byte{0x01}, 10, 0, wire.BloomUpdateNone)
bh := wire.NewBlockHeader(&wire.ShaHash{}, &wire.ShaHash{}, 0, 0)
msgMerkleBlock := wire.NewMsgMerkleBlock(bh)
msgReject := wire.NewMsgReject("block", wire.RejectDuplicate, "duplicate block")
msgInv := NewMsgInv()
msgGetData := NewMsgGetData()
msgNotFound := NewMsgNotFound()
msgTx := NewMsgTx()
msgPing := NewMsgPing(123123)
msgPong := NewMsgPong(123123)
msgGetHeaders := NewMsgGetHeaders()
msgHeaders := NewMsgHeaders()
msgAlert := NewMsgAlert([]byte("payload"), []byte("signature"))
msgMemPool := NewMsgMemPool()
msgFilterAdd := NewMsgFilterAdd([]byte{0x01})
msgFilterClear := NewMsgFilterClear()
msgFilterLoad := NewMsgFilterLoad([]byte{0x01}, 10, 0, BloomUpdateNone)
bh := NewBlockHeader(&ShaHash{}, &ShaHash{}, 0, 0)
msgMerkleBlock := NewMsgMerkleBlock(bh)
msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
tests := []struct {
in wire.Message // Value to encode
out wire.Message // Expected decoded value
in Message // Value to encode
out Message // Expected decoded value
pver uint32 // Protocol version for wire encoding
btcnet wire.BitcoinNet // Network to use for wire encoding
btcnet BitcoinNet // Network to use for wire encoding
bytes int // Expected num bytes read/written
}{
{msgVersion, msgVersion, pver, wire.MainNet, 125},
{msgVerack, msgVerack, pver, wire.MainNet, 24},
{msgGetAddr, msgGetAddr, pver, wire.MainNet, 24},
{msgAddr, msgAddr, pver, wire.MainNet, 25},
{msgGetBlocks, msgGetBlocks, pver, wire.MainNet, 61},
{msgBlock, msgBlock, pver, wire.MainNet, 239},
{msgInv, msgInv, pver, wire.MainNet, 25},
{msgGetData, msgGetData, pver, wire.MainNet, 25},
{msgNotFound, msgNotFound, pver, wire.MainNet, 25},
{msgTx, msgTx, pver, wire.MainNet, 34},
{msgPing, msgPing, pver, wire.MainNet, 32},
{msgPong, msgPong, pver, wire.MainNet, 32},
{msgGetHeaders, msgGetHeaders, pver, wire.MainNet, 61},
{msgHeaders, msgHeaders, pver, wire.MainNet, 25},
{msgAlert, msgAlert, pver, wire.MainNet, 42},
{msgMemPool, msgMemPool, pver, wire.MainNet, 24},
{msgFilterAdd, msgFilterAdd, pver, wire.MainNet, 26},
{msgFilterClear, msgFilterClear, pver, wire.MainNet, 24},
{msgFilterLoad, msgFilterLoad, pver, wire.MainNet, 35},
{msgMerkleBlock, msgMerkleBlock, pver, wire.MainNet, 110},
{msgReject, msgReject, pver, wire.MainNet, 79},
{msgVersion, msgVersion, pver, MainNet, 125},
{msgVerack, msgVerack, pver, MainNet, 24},
{msgGetAddr, msgGetAddr, pver, MainNet, 24},
{msgAddr, msgAddr, pver, MainNet, 25},
{msgGetBlocks, msgGetBlocks, pver, MainNet, 61},
{msgBlock, msgBlock, pver, MainNet, 239},
{msgInv, msgInv, pver, MainNet, 25},
{msgGetData, msgGetData, pver, MainNet, 25},
{msgNotFound, msgNotFound, pver, MainNet, 25},
{msgTx, msgTx, pver, MainNet, 34},
{msgPing, msgPing, pver, MainNet, 32},
{msgPong, msgPong, pver, MainNet, 32},
{msgGetHeaders, msgGetHeaders, pver, MainNet, 61},
{msgHeaders, msgHeaders, pver, MainNet, 25},
{msgAlert, msgAlert, pver, MainNet, 42},
{msgMemPool, msgMemPool, pver, MainNet, 24},
{msgFilterAdd, msgFilterAdd, pver, MainNet, 26},
{msgFilterClear, msgFilterClear, pver, MainNet, 24},
{msgFilterLoad, msgFilterLoad, pver, MainNet, 35},
{msgMerkleBlock, msgMerkleBlock, pver, MainNet, 110},
{msgReject, msgReject, pver, MainNet, 79},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
nw, err := wire.WriteMessageN(&buf, test.in, test.pver, test.btcnet)
nw, err := WriteMessageN(&buf, test.in, test.pver, test.btcnet)
if err != nil {
t.Errorf("WriteMessage #%d error %v", i, err)
continue
@ -124,7 +123,7 @@ func TestMessage(t *testing.T) {
// Decode from wire format.
rbuf := bytes.NewReader(buf.Bytes())
nr, msg, _, err := wire.ReadMessageN(rbuf, test.pver, test.btcnet)
nr, msg, _, err := ReadMessageN(rbuf, test.pver, test.btcnet)
if err != nil {
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,
spew.Sdump(msg))
@ -149,7 +148,7 @@ func TestMessage(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.WriteMessage(&buf, test.in, test.pver, test.btcnet)
err := WriteMessage(&buf, test.in, test.pver, test.btcnet)
if err != nil {
t.Errorf("WriteMessage #%d error %v", i, err)
continue
@ -157,7 +156,7 @@ func TestMessage(t *testing.T) {
// Decode from wire format.
rbuf := bytes.NewReader(buf.Bytes())
msg, _, err := wire.ReadMessage(rbuf, test.pver, test.btcnet)
msg, _, err := ReadMessage(rbuf, test.pver, test.btcnet)
if err != nil {
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,
spew.Sdump(msg))
@ -174,12 +173,12 @@ func TestMessage(t *testing.T) {
// TestReadMessageWireErrors performs negative tests against wire decoding into
// concrete messages to confirm error paths work correctly.
func TestReadMessageWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
btcnet := wire.MainNet
pver := ProtocolVersion
btcnet := MainNet
// Ensure message errors are as expected with no function specified.
wantErr := "something bad happened"
testErr := wire.MessageError{Description: wantErr}
testErr := MessageError{Description: wantErr}
if testErr.Error() != wantErr {
t.Errorf("MessageError: wrong error - got %v, want %v",
testErr.Error(), wantErr)
@ -187,18 +186,18 @@ func TestReadMessageWireErrors(t *testing.T) {
// Ensure message errors are as expected with a function specified.
wantFunc := "foo"
testErr = wire.MessageError{Func: wantFunc, Description: wantErr}
testErr = MessageError{Func: wantFunc, Description: wantErr}
if testErr.Error() != wantFunc+": "+wantErr {
t.Errorf("MessageError: wrong error - got %v, want %v",
testErr.Error(), wantErr)
}
// Wire encoded bytes for main and testnet3 networks magic identifiers.
testNet3Bytes := makeHeader(wire.TestNet3, "", 0, 0)
testNet3Bytes := makeHeader(TestNet3, "", 0, 0)
// Wire encoded bytes for a message that exceeds max overall message
// length.
mpl := uint32(wire.MaxMessagePayload)
mpl := uint32(MaxMessagePayload)
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)
// Wire encoded bytes for a command which is invalid utf-8.
@ -235,7 +234,7 @@ func TestReadMessageWireErrors(t *testing.T) {
tests := []struct {
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
btcnet wire.BitcoinNet // Bitcoin network for wire encoding
btcnet BitcoinNet // Bitcoin network for wire encoding
max int // Max size of fixed buffer to induce errors
readErr error // Expected read error
bytes int // Expected num bytes read
@ -258,7 +257,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(testNet3Bytes),
&wire.MessageError{},
&MessageError{},
24,
},
@ -268,7 +267,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(exceedMaxPayloadBytes),
&wire.MessageError{},
&MessageError{},
24,
},
@ -278,7 +277,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(badCommandBytes),
&wire.MessageError{},
&MessageError{},
24,
},
@ -288,7 +287,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(unsupportedCommandBytes),
&wire.MessageError{},
&MessageError{},
24,
},
@ -298,7 +297,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(exceedTypePayloadBytes),
&wire.MessageError{},
&MessageError{},
24,
},
@ -318,7 +317,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(badChecksumBytes),
&wire.MessageError{},
&MessageError{},
26,
},
@ -338,7 +337,7 @@ func TestReadMessageWireErrors(t *testing.T) {
pver,
btcnet,
len(discardBytes),
&wire.MessageError{},
&MessageError{},
24,
},
}
@ -347,7 +346,7 @@ func TestReadMessageWireErrors(t *testing.T) {
for i, test := range tests {
// Decode from wire format.
r := newFixedReader(test.max, test.buf)
nr, _, _, err := wire.ReadMessageN(r, test.pver, test.btcnet)
nr, _, _, err := ReadMessageN(r, test.pver, test.btcnet)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
"want: %T", i, err, err, test.readErr)
@ -360,9 +359,9 @@ func TestReadMessageWireErrors(t *testing.T) {
"got %d, want %d", i, nr, test.bytes)
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
"want: %v <%T>", i, err, err,
@ -376,9 +375,9 @@ func TestReadMessageWireErrors(t *testing.T) {
// TestWriteMessageWireErrors performs negative tests against wire encoding from
// concrete messages to confirm error paths work correctly.
func TestWriteMessageWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
btcnet := wire.MainNet
wireErr := &wire.MessageError{}
pver := ProtocolVersion
btcnet := MainNet
wireErr := &MessageError{}
// Fake message with a command that is too long.
badCommandMsg := &fakeMessage{command: "somethingtoolong"}
@ -387,7 +386,7 @@ func TestWriteMessageWireErrors(t *testing.T) {
encodeErrMsg := &fakeMessage{forceEncodeErr: true}
// Fake message that has payload which exceeds max overall message size.
exceedOverallPayload := make([]byte, wire.MaxMessagePayload+1)
exceedOverallPayload := make([]byte, MaxMessagePayload+1)
exceedOverallPayloadErrMsg := &fakeMessage{payload: exceedOverallPayload}
// Fake message that has payload which exceeds max allowed per message.
@ -400,9 +399,9 @@ func TestWriteMessageWireErrors(t *testing.T) {
bogusMsg := &fakeMessage{command: "bogus", payload: bogusPayload}
tests := []struct {
msg wire.Message // Message to encode
msg Message // Message to encode
pver uint32 // Protocol version for wire encoding
btcnet wire.BitcoinNet // Bitcoin network for wire encoding
btcnet BitcoinNet // Bitcoin network for wire encoding
max int // Max size of fixed buffer to induce errors
err error // Expected error
bytes int // Expected num bytes written
@ -425,7 +424,7 @@ func TestWriteMessageWireErrors(t *testing.T) {
for i, test := range tests {
// Encode wire format.
w := newFixedWriter(test.max)
nw, err := wire.WriteMessageN(w, test.msg, test.pver, test.btcnet)
nw, err := WriteMessageN(w, test.msg, test.pver, test.btcnet)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("WriteMessage #%d wrong error got: %v <%T>, "+
"want: %T", i, err, err, test.err)
@ -438,9 +437,9 @@ func TestWriteMessageWireErrors(t *testing.T) {
"written - got %d, want %d", i, nw, test.bytes)
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.err {
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
"want: %v <%T>", i, err, err,

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -12,17 +12,16 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestAddr tests the MsgAddr API.
func TestAddr(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "addr"
msg := wire.NewMsgAddr()
msg := NewMsgAddr()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgAddr: wrong command - got %v want %v",
cmd, wantCmd)
@ -40,7 +39,7 @@ func TestAddr(t *testing.T) {
// Ensure NetAddresses are added properly.
tcpAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
na, err := wire.NewNetAddress(tcpAddr, wire.SFNodeNetwork)
na, err := NewNetAddress(tcpAddr, SFNodeNetwork)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
@ -63,7 +62,7 @@ func TestAddr(t *testing.T) {
// Ensure adding more than the max allowed addresses per message returns
// error.
for i := 0; i < wire.MaxAddrPerMsg+1; i++ {
for i := 0; i < MaxAddrPerMsg+1; i++ {
err = msg.AddAddress(na)
}
if err == nil {
@ -79,7 +78,7 @@ func TestAddr(t *testing.T) {
// Ensure max payload is expected value for protocol versions before
// timestamp was added to NetAddress.
// Num addresses (varInt) + max allowed addresses.
pver = wire.NetAddressTimeVersion - 1
pver = NetAddressTimeVersion - 1
wantPayload = uint32(26009)
maxPayload = msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
@ -91,7 +90,7 @@ func TestAddr(t *testing.T) {
// Ensure max payload is expected value for protocol versions before
// multiple addresses were allowed.
// Num addresses (varInt) + a single net addresses.
pver = wire.MultipleAddressVersion - 1
pver = MultipleAddressVersion - 1
wantPayload = uint32(35)
maxPayload = msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
@ -107,27 +106,27 @@ func TestAddr(t *testing.T) {
// of addreses and protocol versions.
func TestAddrWire(t *testing.T) {
// A couple of NetAddresses to use for testing.
na := &wire.NetAddress{
na := &NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
}
na2 := &wire.NetAddress{
na2 := &NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("192.168.0.1"),
Port: 8334,
}
// Empty address message.
noAddr := wire.NewMsgAddr()
noAddr := NewMsgAddr()
noAddrEncoded := []byte{
0x00, // Varint for number of addresses
}
// Address message with multiple addresses.
multiAddr := wire.NewMsgAddr()
multiAddr := NewMsgAddr()
multiAddr.AddAddresses(na, na2)
multiAddrEncoded := []byte{
0x02, // Varint for number of addresses
@ -145,8 +144,8 @@ func TestAddrWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgAddr // Message to encode
out *wire.MsgAddr // Expected decoded message
in *MsgAddr // Message to encode
out *MsgAddr // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -155,7 +154,7 @@ func TestAddrWire(t *testing.T) {
noAddr,
noAddr,
noAddrEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple addresses.
@ -163,7 +162,7 @@ func TestAddrWire(t *testing.T) {
multiAddr,
multiAddr,
multiAddrEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version MultipleAddressVersion-1 with no addresses.
@ -171,7 +170,7 @@ func TestAddrWire(t *testing.T) {
noAddr,
noAddr,
noAddrEncoded,
wire.MultipleAddressVersion - 1,
MultipleAddressVersion - 1,
},
}
@ -191,7 +190,7 @@ func TestAddrWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgAddr
var msg MsgAddr
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -209,26 +208,26 @@ func TestAddrWire(t *testing.T) {
// TestAddrWireErrors performs negative tests against wire encode and decode
// of MsgAddr to confirm error paths work correctly.
func TestAddrWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverMA := wire.MultipleAddressVersion
wireErr := &wire.MessageError{}
pver := ProtocolVersion
pverMA := MultipleAddressVersion
wireErr := &MessageError{}
// A couple of NetAddresses to use for testing.
na := &wire.NetAddress{
na := &NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
}
na2 := &wire.NetAddress{
na2 := &NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("192.168.0.1"),
Port: 8334,
}
// Address message with multiple addresses.
baseAddr := wire.NewMsgAddr()
baseAddr := NewMsgAddr()
baseAddr.AddAddresses(na, na2)
baseAddrEncoded := []byte{
0x02, // Varint for number of addresses
@ -247,8 +246,8 @@ func TestAddrWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed
// addresses.
maxAddr := wire.NewMsgAddr()
for i := 0; i < wire.MaxAddrPerMsg; i++ {
maxAddr := NewMsgAddr()
for i := 0; i < MaxAddrPerMsg; i++ {
maxAddr.AddAddress(na)
}
maxAddr.AddrList = append(maxAddr.AddrList, na)
@ -257,7 +256,7 @@ func TestAddrWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgAddr // Value to encode
in *MsgAddr // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -287,9 +286,9 @@ func TestAddrWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -298,7 +297,7 @@ func TestAddrWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgAddr
var msg MsgAddr
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -307,9 +306,9 @@ func TestAddrWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,18 +10,17 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestMsgAlert tests the MsgAlert API.
func TestMsgAlert(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
serializedpayload := []byte("some message")
signature := []byte("some sig")
// Ensure we get the same payload and signature back out.
msg := wire.NewMsgAlert(serializedpayload, signature)
msg := NewMsgAlert(serializedpayload, signature)
if !reflect.DeepEqual(msg.SerializedPayload, serializedpayload) {
t.Errorf("NewMsgAlert: wrong serializedpayload - got %v, want %v",
msg.SerializedPayload, serializedpayload)
@ -64,7 +63,7 @@ func TestMsgAlert(t *testing.T) {
// Test BtcEncode with Payload != nil
// note: Payload is an empty Alert but not nil
msg.Payload = new(wire.Alert)
msg.Payload = new(Alert)
buf = *new(bytes.Buffer)
err = msg.BtcEncode(&buf, pver)
if err != nil {
@ -85,7 +84,7 @@ func TestMsgAlert(t *testing.T) {
// TestMsgAlertWire tests the MsgAlert wire encode and decode for various protocol
// versions.
func TestMsgAlertWire(t *testing.T) {
baseMsgAlert := wire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
baseMsgAlert := NewMsgAlert([]byte("some payload"), []byte("somesig"))
baseMsgAlertEncoded := []byte{
0x0c, // Varint for payload length
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x61, 0x79,
@ -95,8 +94,8 @@ func TestMsgAlertWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgAlert // Message to encode
out *wire.MsgAlert // Expected decoded message
in *MsgAlert // Message to encode
out *MsgAlert // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -105,7 +104,7 @@ func TestMsgAlertWire(t *testing.T) {
baseMsgAlert,
baseMsgAlert,
baseMsgAlertEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version.
@ -113,7 +112,7 @@ func TestMsgAlertWire(t *testing.T) {
baseMsgAlert,
baseMsgAlert,
baseMsgAlertEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -121,7 +120,7 @@ func TestMsgAlertWire(t *testing.T) {
baseMsgAlert,
baseMsgAlert,
baseMsgAlertEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -129,7 +128,7 @@ func TestMsgAlertWire(t *testing.T) {
baseMsgAlert,
baseMsgAlert,
baseMsgAlertEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -137,7 +136,7 @@ func TestMsgAlertWire(t *testing.T) {
baseMsgAlert,
baseMsgAlert,
baseMsgAlertEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -157,7 +156,7 @@ func TestMsgAlertWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgAlert
var msg MsgAlert
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -175,9 +174,9 @@ func TestMsgAlertWire(t *testing.T) {
// TestMsgAlertWireErrors performs negative tests against wire encode and decode
// of MsgAlert to confirm error paths work correctly.
func TestMsgAlertWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
baseMsgAlert := wire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
baseMsgAlert := NewMsgAlert([]byte("some payload"), []byte("somesig"))
baseMsgAlertEncoded := []byte{
0x0c, // Varint for payload length
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x61, 0x79,
@ -187,7 +186,7 @@ func TestMsgAlertWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgAlert // Value to encode
in *MsgAlert // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -215,9 +214,9 @@ func TestMsgAlertWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -226,7 +225,7 @@ func TestMsgAlertWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgAlert
var msg MsgAlert
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -235,9 +234,9 @@ func TestMsgAlertWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)
@ -250,38 +249,38 @@ func TestMsgAlertWireErrors(t *testing.T) {
baseMsgAlert.SerializedPayload = []byte{}
w := new(bytes.Buffer)
err := baseMsgAlert.BtcEncode(w, pver)
if _, ok := err.(*wire.MessageError); !ok {
if _, ok := err.(*MessageError); !ok {
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
err, wire.MessageError{})
err, MessageError{})
}
// Test Payload Serialize error
// overflow the max number of elements in SetCancel
baseMsgAlert.Payload = new(wire.Alert)
baseMsgAlert.Payload.SetCancel = make([]int32, wire.MaxCountSetCancel+1)
baseMsgAlert.Payload = new(Alert)
baseMsgAlert.Payload.SetCancel = make([]int32, maxCountSetCancel+1)
buf := *new(bytes.Buffer)
err = baseMsgAlert.BtcEncode(&buf, pver)
if _, ok := err.(*wire.MessageError); !ok {
if _, ok := err.(*MessageError); !ok {
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
err, wire.MessageError{})
err, MessageError{})
}
// overflow the max number of elements in SetSubVer
baseMsgAlert.Payload = new(wire.Alert)
baseMsgAlert.Payload.SetSubVer = make([]string, wire.MaxCountSetSubVer+1)
baseMsgAlert.Payload = new(Alert)
baseMsgAlert.Payload.SetSubVer = make([]string, maxCountSetSubVer+1)
buf = *new(bytes.Buffer)
err = baseMsgAlert.BtcEncode(&buf, pver)
if _, ok := err.(*wire.MessageError); !ok {
if _, ok := err.(*MessageError); !ok {
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
err, wire.MessageError{})
err, MessageError{})
}
}
// TestAlert tests serialization and deserialization
// of the payload to Alert
func TestAlert(t *testing.T) {
pver := wire.ProtocolVersion
alert := wire.NewAlert(
pver := ProtocolVersion
alert := NewAlert(
1, 1337093712, 1368628812, 1015,
1013, []int32{1014}, 0, 40599, []string{"/Satoshi:0.7.2/"}, 5000, "",
"URGENT: upgrade required, see http://bitcoin.org/dos for details",
@ -292,7 +291,7 @@ func TestAlert(t *testing.T) {
t.Error(err.Error())
}
serializedpayload := w.Bytes()
newAlert, err := wire.NewAlertFromPayload(serializedpayload, pver)
newAlert, err := NewAlertFromPayload(serializedpayload, pver)
if err != nil {
t.Error(err.Error())
}
@ -366,9 +365,9 @@ func TestAlert(t *testing.T) {
// TestAlertErrors performs negative tests against payload serialization,
// deserialization of Alert to confirm error paths work correctly.
func TestAlertErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
baseAlert := wire.NewAlert(
baseAlert := NewAlert(
1, 1337093712, 1368628812, 1015,
1013, []int32{1014}, 0, 40599, []string{"/Satoshi:0.7.2/"}, 5000, "",
"URGENT",
@ -381,7 +380,7 @@ func TestAlertErrors(t *testing.T) {
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
}
tests := []struct {
in *wire.Alert // Value to encode
in *Alert // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -420,7 +419,7 @@ func TestAlertErrors(t *testing.T) {
continue
}
var alert wire.Alert
var alert Alert
r := newFixedReader(test.max, test.buf)
err = alert.Deserialize(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -440,12 +439,12 @@ func TestAlertErrors(t *testing.T) {
0x73, 0x68, 0x69, 0x3a, 0x30, 0x2e, 0x37, 0x2e, 0x32, 0x2f, 0x88, 0x13, 0x00, 0x00, 0x00, 0x06, //|shi:0.7.2/......|
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
}
var alert wire.Alert
var alert Alert
r := bytes.NewReader(badAlertEncoded)
err := alert.Deserialize(r, pver)
if _, ok := err.(*wire.MessageError); !ok {
if _, ok := err.(*MessageError); !ok {
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",
err, wire.MessageError{})
err, MessageError{})
}
// overflow the max number of elements in SetSubVer
@ -460,8 +459,8 @@ func TestAlertErrors(t *testing.T) {
}
r = bytes.NewReader(badAlertEncoded)
err = alert.Deserialize(r, pver)
if _, ok := err.(*wire.MessageError); !ok {
if _, ok := err.(*MessageError); !ok {
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",
err, wire.MessageError{})
err, MessageError{})
}
}

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -11,24 +11,23 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestBlock tests the MsgBlock API.
func TestBlock(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Block 1 header.
prevHash := &blockOne.Header.PrevBlock
merkleHash := &blockOne.Header.MerkleRoot
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
bh := NewBlockHeader(prevHash, merkleHash, bits, nonce)
// Ensure the command is expected value.
wantCmd := "block"
msg := wire.NewMsgBlock(bh)
msg := NewMsgBlock(bh)
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
cmd, wantCmd)
@ -74,13 +73,13 @@ func TestBlock(t *testing.T) {
func TestBlockTxShas(t *testing.T) {
// Block 1, transaction 1 hash.
hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
wantHash, err := wire.NewShaHashFromStr(hashStr)
wantHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
return
}
wantShas := []wire.ShaHash{*wantHash}
wantShas := []ShaHash{*wantHash}
shas, err := blockOne.TxShas()
if err != nil {
t.Errorf("TxShas: %v", err)
@ -95,7 +94,7 @@ func TestBlockTxShas(t *testing.T) {
func TestBlockSha(t *testing.T) {
// Block 1 hash.
hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
wantHash, err := wire.NewShaHashFromStr(hashStr)
wantHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
@ -112,10 +111,10 @@ func TestBlockSha(t *testing.T) {
// of transaction inputs and outputs and protocol versions.
func TestBlockWire(t *testing.T) {
tests := []struct {
in *wire.MsgBlock // Message to encode
out *wire.MsgBlock // Expected decoded message
in *MsgBlock // Message to encode
out *MsgBlock // Expected decoded message
buf []byte // Wire encoding
txLocs []wire.TxLoc // Expected transaction locations
txLocs []TxLoc // Expected transaction locations
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
@ -124,7 +123,7 @@ func TestBlockWire(t *testing.T) {
&blockOne,
blockOneBytes,
blockOneTxLocs,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version.
@ -133,7 +132,7 @@ func TestBlockWire(t *testing.T) {
&blockOne,
blockOneBytes,
blockOneTxLocs,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -142,7 +141,7 @@ func TestBlockWire(t *testing.T) {
&blockOne,
blockOneBytes,
blockOneTxLocs,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -151,7 +150,7 @@ func TestBlockWire(t *testing.T) {
&blockOne,
blockOneBytes,
blockOneTxLocs,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -160,7 +159,7 @@ func TestBlockWire(t *testing.T) {
&blockOne,
blockOneBytes,
blockOneTxLocs,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -180,7 +179,7 @@ func TestBlockWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgBlock
var msg MsgBlock
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -204,7 +203,7 @@ func TestBlockWireErrors(t *testing.T) {
pver := uint32(60002)
tests := []struct {
in *wire.MsgBlock // Value to encode
in *MsgBlock // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -241,7 +240,7 @@ func TestBlockWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgBlock
var msg MsgBlock
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if err != test.readErr {
@ -255,10 +254,10 @@ func TestBlockWireErrors(t *testing.T) {
// TestBlockSerialize tests MsgBlock serialize and deserialize.
func TestBlockSerialize(t *testing.T) {
tests := []struct {
in *wire.MsgBlock // Message to encode
out *wire.MsgBlock // Expected decoded message
in *MsgBlock // Message to encode
out *MsgBlock // Expected decoded message
buf []byte // Serialized data
txLocs []wire.TxLoc // Expected transaction locations
txLocs []TxLoc // Expected transaction locations
}{
{
&blockOne,
@ -284,7 +283,7 @@ func TestBlockSerialize(t *testing.T) {
}
// Deserialize the block.
var block wire.MsgBlock
var block MsgBlock
rbuf := bytes.NewReader(test.buf)
err = block.Deserialize(rbuf)
if err != nil {
@ -299,7 +298,7 @@ func TestBlockSerialize(t *testing.T) {
// Deserialize the block while gathering transaction location
// information.
var txLocBlock wire.MsgBlock
var txLocBlock MsgBlock
br := bytes.NewBuffer(test.buf)
txLocs, err := txLocBlock.DeserializeTxLoc(br)
if err != nil {
@ -323,7 +322,7 @@ func TestBlockSerialize(t *testing.T) {
// decode of MsgBlock to confirm error paths work correctly.
func TestBlockSerializeErrors(t *testing.T) {
tests := []struct {
in *wire.MsgBlock // Value to encode
in *MsgBlock // Value to encode
buf []byte // Serialized data
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
@ -359,7 +358,7 @@ func TestBlockSerializeErrors(t *testing.T) {
}
// Deserialize the block.
var block wire.MsgBlock
var block MsgBlock
r := newFixedReader(test.max, test.buf)
err = block.Deserialize(r)
if err != test.readErr {
@ -368,7 +367,7 @@ func TestBlockSerializeErrors(t *testing.T) {
continue
}
var txLocBlock wire.MsgBlock
var txLocBlock MsgBlock
br := bytes.NewBuffer(test.buf[0:test.max])
_, err = txLocBlock.DeserializeTxLoc(br)
if err != test.readErr {
@ -411,14 +410,14 @@ func TestBlockOverflowErrors(t *testing.T) {
0x01, 0xe3, 0x62, 0x99, // Nonce
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, // TxnCount
}, pver, &wire.MessageError{},
}, pver, &MessageError{},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
var msg wire.MsgBlock
var msg MsgBlock
r := bytes.NewReader(test.buf)
err := msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
@ -451,10 +450,10 @@ func TestBlockOverflowErrors(t *testing.T) {
// various blocks is accurate.
func TestBlockSerializeSize(t *testing.T) {
// Block with no transactions.
noTxBlock := wire.NewMsgBlock(&blockOne.Header)
noTxBlock := NewMsgBlock(&blockOne.Header)
tests := []struct {
in *wire.MsgBlock // Block to encode
in *MsgBlock // Block to encode
size int // Expected serialized size
}{
// Block with no transactions.
@ -475,16 +474,17 @@ func TestBlockSerializeSize(t *testing.T) {
}
}
var blockOne = wire.MsgBlock{
Header: wire.BlockHeader{
// blockOne is the first block in the mainnet block chain.
var blockOne = MsgBlock{
Header: BlockHeader{
Version: 1,
PrevBlock: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
PrevBlock: ShaHash([HashSize]byte{ // Make go vet happy.
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
}),
MerkleRoot: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
MerkleRoot: ShaHash([HashSize]byte{ // Make go vet happy.
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
@ -495,13 +495,13 @@ var blockOne = wire.MsgBlock{
Bits: 0x1d00ffff, // 486604799
Nonce: 0x9962e301, // 2573394689
},
Transactions: []*wire.MsgTx{
Transactions: []*MsgTx{
{
Version: 1,
TxIn: []*wire.TxIn{
TxIn: []*TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash{},
PreviousOutPoint: OutPoint{
Hash: ShaHash{},
Index: 0xffffffff,
},
SignatureScript: []byte{
@ -510,7 +510,7 @@ var blockOne = wire.MsgBlock{
Sequence: 0xffffffff,
},
},
TxOut: []*wire.TxOut{
TxOut: []*TxOut{
{
Value: 0x12a05f200,
PkScript: []byte{
@ -576,6 +576,6 @@ var blockOneBytes = []byte{
}
// Transaction location information for block one transactions.
var blockOneTxLocs = []wire.TxLoc{
var blockOneTxLocs = []TxLoc{
{TxStart: 81, TxLen: 134},
}

View file

@ -1,25 +1,23 @@
// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"io"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
)
// TestFilterAddLatest tests the MsgFilterAdd API against the latest protocol
// version.
func TestFilterAddLatest(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
data := []byte{0x01, 0x02}
msg := wire.NewMsgFilterAdd(data)
msg := NewMsgFilterAdd(data)
// Ensure the command is expected value.
wantCmd := "filteradd"
@ -45,7 +43,7 @@ func TestFilterAddLatest(t *testing.T) {
}
// Test decode with latest protocol version.
var readmsg wire.MsgFilterAdd
var readmsg MsgFilterAdd
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgFilterAdd failed [%v] err <%v>", buf, err)
@ -58,21 +56,21 @@ func TestFilterAddLatest(t *testing.T) {
// latest protocol version and decoding with BIP0031Version.
func TestFilterAddCrossProtocol(t *testing.T) {
data := []byte{0x01, 0x02}
msg := wire.NewMsgFilterAdd(data)
msg := NewMsgFilterAdd(data)
if !bytes.Equal(msg.Data, data) {
t.Errorf("should get same data back out")
}
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgFilterAdd failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
var readmsg wire.MsgFilterAdd
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
var readmsg MsgFilterAdd
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err == nil {
t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
"have %v", msg)
@ -89,11 +87,11 @@ func TestFilterAddCrossProtocol(t *testing.T) {
// TestFilterAddMaxDataSize tests the MsgFilterAdd API maximum data size.
func TestFilterAddMaxDataSize(t *testing.T) {
data := bytes.Repeat([]byte{0xff}, 521)
msg := wire.NewMsgFilterAdd(data)
msg := NewMsgFilterAdd(data)
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err == nil {
t.Errorf("encode of MsgFilterAdd succeeded when it shouldn't "+
"have %v", msg)
@ -101,7 +99,7 @@ func TestFilterAddMaxDataSize(t *testing.T) {
// Decode with latest protocol version.
readbuf := bytes.NewReader(data)
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
err = msg.BtcDecode(readbuf, ProtocolVersion)
if err == nil {
t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
"have %v", msg)
@ -111,16 +109,16 @@ func TestFilterAddMaxDataSize(t *testing.T) {
// TestFilterAddWireErrors performs negative tests against wire encode and decode
// of MsgFilterAdd to confirm error paths work correctly.
func TestFilterAddWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverNoFilterAdd := wire.BIP0037Version - 1
wireErr := &wire.MessageError{}
pver := ProtocolVersion
pverNoFilterAdd := BIP0037Version - 1
wireErr := &MessageError{}
baseData := []byte{0x01, 0x02, 0x03, 0x04}
baseFilterAdd := wire.NewMsgFilterAdd(baseData)
baseFilterAdd := NewMsgFilterAdd(baseData)
baseFilterAddEncoded := append([]byte{0x04}, baseData...)
tests := []struct {
in *wire.MsgFilterAdd // Value to encode
in *MsgFilterAdd // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -156,9 +154,9 @@ func TestFilterAddWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -167,7 +165,7 @@ func TestFilterAddWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgFilterAdd
var msg MsgFilterAdd
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -176,9 +174,9 @@ func TestFilterAddWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,24 +1,23 @@
// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestFilterCLearLatest tests the MsgFilterClear API against the latest
// protocol version.
func TestFilterClearLatest(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
msg := wire.NewMsgFilterClear()
msg := NewMsgFilterClear()
// Ensure the command is expected value.
wantCmd := "filterclear"
@ -42,18 +41,18 @@ func TestFilterClearLatest(t *testing.T) {
// TestFilterClearCrossProtocol tests the MsgFilterClear API when encoding with
// the latest protocol version and decoding with BIP0031Version.
func TestFilterClearCrossProtocol(t *testing.T) {
msg := wire.NewMsgFilterClear()
msg := NewMsgFilterClear()
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgFilterClear failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
var readmsg wire.MsgFilterClear
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
var readmsg MsgFilterClear
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err == nil {
t.Errorf("decode of MsgFilterClear succeeded when it "+
"shouldn't have %v", msg)
@ -63,12 +62,12 @@ func TestFilterClearCrossProtocol(t *testing.T) {
// TestFilterClearWire tests the MsgFilterClear wire encode and decode for
// various protocol versions.
func TestFilterClearWire(t *testing.T) {
msgFilterClear := wire.NewMsgFilterClear()
msgFilterClear := NewMsgFilterClear()
msgFilterClearEncoded := []byte{}
tests := []struct {
in *wire.MsgFilterClear // Message to encode
out *wire.MsgFilterClear // Expected decoded message
in *MsgFilterClear // Message to encode
out *MsgFilterClear // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -77,7 +76,7 @@ func TestFilterClearWire(t *testing.T) {
msgFilterClear,
msgFilterClear,
msgFilterClearEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0037Version + 1.
@ -85,7 +84,7 @@ func TestFilterClearWire(t *testing.T) {
msgFilterClear,
msgFilterClear,
msgFilterClearEncoded,
wire.BIP0037Version + 1,
BIP0037Version + 1,
},
// Protocol version BIP0037Version.
@ -93,7 +92,7 @@ func TestFilterClearWire(t *testing.T) {
msgFilterClear,
msgFilterClear,
msgFilterClearEncoded,
wire.BIP0037Version,
BIP0037Version,
},
}
@ -113,7 +112,7 @@ func TestFilterClearWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgFilterClear
var msg MsgFilterClear
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -131,14 +130,14 @@ func TestFilterClearWire(t *testing.T) {
// TestFilterClearWireErrors performs negative tests against wire encode and
// decode of MsgFilterClear to confirm error paths work correctly.
func TestFilterClearWireErrors(t *testing.T) {
pverNoFilterClear := wire.BIP0037Version - 1
wireErr := &wire.MessageError{}
pverNoFilterClear := BIP0037Version - 1
wireErr := &MessageError{}
baseFilterClear := wire.NewMsgFilterClear()
baseFilterClear := NewMsgFilterClear()
baseFilterClearEncoded := []byte{}
tests := []struct {
in *wire.MsgFilterClear // Value to encode
in *MsgFilterClear // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -163,9 +162,9 @@ func TestFilterClearWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -174,7 +173,7 @@ func TestFilterClearWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgFilterClear
var msg MsgFilterClear
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -183,9 +182,9 @@ func TestFilterClearWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,25 +1,23 @@
// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"io"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
)
// TestFilterCLearLatest tests the MsgFilterLoad API against the latest protocol
// version.
func TestFilterLoadLatest(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
data := []byte{0x01, 0x02}
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
msg := NewMsgFilterLoad(data, 10, 0, 0)
// Ensure the command is expected value.
wantCmd := "filterload"
@ -45,7 +43,7 @@ func TestFilterLoadLatest(t *testing.T) {
}
// Test decode with latest protocol version.
readmsg := wire.MsgFilterLoad{}
readmsg := MsgFilterLoad{}
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgFilterLoad failed [%v] err <%v>", buf, err)
@ -58,19 +56,19 @@ func TestFilterLoadLatest(t *testing.T) {
// the latest protocol version and decoding with BIP0031Version.
func TestFilterLoadCrossProtocol(t *testing.T) {
data := []byte{0x01, 0x02}
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
msg := NewMsgFilterLoad(data, 10, 0, 0)
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
err)
}
// Decode with old protocol version.
var readmsg wire.MsgFilterLoad
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
var readmsg MsgFilterLoad
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err == nil {
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
msg)
@ -80,11 +78,11 @@ func TestFilterLoadCrossProtocol(t *testing.T) {
// TestFilterLoadMaxFilterSize tests the MsgFilterLoad API maximum filter size.
func TestFilterLoadMaxFilterSize(t *testing.T) {
data := bytes.Repeat([]byte{0xff}, 36001)
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
msg := NewMsgFilterLoad(data, 10, 0, 0)
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err == nil {
t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't "+
"have %v", msg)
@ -92,7 +90,7 @@ func TestFilterLoadMaxFilterSize(t *testing.T) {
// Decode with latest protocol version.
readbuf := bytes.NewReader(data)
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
err = msg.BtcDecode(readbuf, ProtocolVersion)
if err == nil {
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't "+
"have %v", msg)
@ -102,11 +100,11 @@ func TestFilterLoadMaxFilterSize(t *testing.T) {
// TestFilterLoadMaxHashFuncsSize tests the MsgFilterLoad API maximum hash functions.
func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
data := bytes.Repeat([]byte{0xff}, 10)
msg := wire.NewMsgFilterLoad(data, 61, 0, 0)
msg := NewMsgFilterLoad(data, 61, 0, 0)
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err == nil {
t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't have %v",
msg)
@ -121,7 +119,7 @@ func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
}
// Decode with latest protocol version.
readbuf := bytes.NewReader(newBuf)
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
err = msg.BtcDecode(readbuf, ProtocolVersion)
if err == nil {
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
msg)
@ -131,13 +129,12 @@ func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
// TestFilterLoadWireErrors performs negative tests against wire encode and decode
// of MsgFilterLoad to confirm error paths work correctly.
func TestFilterLoadWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverNoFilterLoad := wire.BIP0037Version - 1
wireErr := &wire.MessageError{}
pver := ProtocolVersion
pverNoFilterLoad := BIP0037Version - 1
wireErr := &MessageError{}
baseFilter := []byte{0x01, 0x02, 0x03, 0x04}
baseFilterLoad := wire.NewMsgFilterLoad(baseFilter, 10, 0,
wire.BloomUpdateNone)
baseFilterLoad := NewMsgFilterLoad(baseFilter, 10, 0, BloomUpdateNone)
baseFilterLoadEncoded := append([]byte{0x04}, baseFilter...)
baseFilterLoadEncoded = append(baseFilterLoadEncoded,
0x00, 0x00, 0x00, 0x0a, // HashFuncs
@ -145,7 +142,7 @@ func TestFilterLoadWireErrors(t *testing.T) {
0x00) // Flags
tests := []struct {
in *wire.MsgFilterLoad // Value to encode
in *MsgFilterLoad // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -196,9 +193,9 @@ func TestFilterLoadWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -207,7 +204,7 @@ func TestFilterLoadWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgFilterLoad
var msg MsgFilterLoad
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -216,9 +213,9 @@ func TestFilterLoadWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,25 +1,24 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestGetAddr tests the MsgGetAddr API.
func TestGetAddr(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "getaddr"
msg := wire.NewMsgGetAddr()
msg := NewMsgGetAddr()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgGetAddr: wrong command - got %v want %v",
cmd, wantCmd)
@ -41,12 +40,12 @@ func TestGetAddr(t *testing.T) {
// TestGetAddrWire tests the MsgGetAddr wire encode and decode for various
// protocol versions.
func TestGetAddrWire(t *testing.T) {
msgGetAddr := wire.NewMsgGetAddr()
msgGetAddr := NewMsgGetAddr()
msgGetAddrEncoded := []byte{}
tests := []struct {
in *wire.MsgGetAddr // Message to encode
out *wire.MsgGetAddr // Expected decoded message
in *MsgGetAddr // Message to encode
out *MsgGetAddr // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -55,7 +54,7 @@ func TestGetAddrWire(t *testing.T) {
msgGetAddr,
msgGetAddr,
msgGetAddrEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version.
@ -63,7 +62,7 @@ func TestGetAddrWire(t *testing.T) {
msgGetAddr,
msgGetAddr,
msgGetAddrEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -71,7 +70,7 @@ func TestGetAddrWire(t *testing.T) {
msgGetAddr,
msgGetAddr,
msgGetAddrEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -79,7 +78,7 @@ func TestGetAddrWire(t *testing.T) {
msgGetAddr,
msgGetAddr,
msgGetAddrEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -87,7 +86,7 @@ func TestGetAddrWire(t *testing.T) {
msgGetAddr,
msgGetAddr,
msgGetAddrEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -107,7 +106,7 @@ func TestGetAddrWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgGetAddr
var msg MsgGetAddr
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,30 +10,29 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestGetBlocks tests the MsgGetBlocks API.
func TestGetBlocks(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Block 99500 hash.
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
locatorHash, err := wire.NewShaHashFromStr(hashStr)
locatorHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := wire.NewShaHashFromStr(hashStr)
hashStop, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Ensure we get the same data back out.
msg := wire.NewMsgGetBlocks(hashStop)
msg := NewMsgGetBlocks(hashStop)
if !msg.HashStop.IsEqual(hashStop) {
t.Errorf("NewMsgGetBlocks: wrong stop hash - got %v, want %v",
msg.HashStop, hashStop)
@ -71,7 +70,7 @@ func TestGetBlocks(t *testing.T) {
// Ensure adding more than the max allowed block locator hashes per
// message returns an error.
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
err = msg.AddBlockLocatorHash(locatorHash)
}
if err == nil {
@ -90,27 +89,27 @@ func TestGetBlocksWire(t *testing.T) {
// Block 99499 hash.
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
hashLocator, err := wire.NewShaHashFromStr(hashStr)
hashLocator, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 99500 hash.
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
hashLocator2, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := wire.NewShaHashFromStr(hashStr)
hashStop, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// MsgGetBlocks message with no block locators or stop hash.
noLocators := wire.NewMsgGetBlocks(&wire.ShaHash{})
noLocators := NewMsgGetBlocks(&ShaHash{})
noLocators.ProtocolVersion = pver
noLocatorsEncoded := []byte{
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
@ -122,7 +121,7 @@ func TestGetBlocksWire(t *testing.T) {
}
// MsgGetBlocks message with multiple block locators and a stop hash.
multiLocators := wire.NewMsgGetBlocks(hashStop)
multiLocators := NewMsgGetBlocks(hashStop)
multiLocators.AddBlockLocatorHash(hashLocator2)
multiLocators.AddBlockLocatorHash(hashLocator)
multiLocators.ProtocolVersion = pver
@ -144,8 +143,8 @@ func TestGetBlocksWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetBlocks // Message to encode
out *wire.MsgGetBlocks // Expected decoded message
in *MsgGetBlocks // Message to encode
out *MsgGetBlocks // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -154,7 +153,7 @@ func TestGetBlocksWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple block locators.
@ -162,7 +161,7 @@ func TestGetBlocksWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version with no block locators.
@ -170,7 +169,7 @@ func TestGetBlocksWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple block locators.
@ -178,7 +177,7 @@ func TestGetBlocksWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version with no block locators.
@ -186,7 +185,7 @@ func TestGetBlocksWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Versionwith multiple block locators.
@ -194,7 +193,7 @@ func TestGetBlocksWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion with no block locators.
@ -202,7 +201,7 @@ func TestGetBlocksWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion multiple block locators.
@ -210,7 +209,7 @@ func TestGetBlocksWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion with no block locators.
@ -218,7 +217,7 @@ func TestGetBlocksWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion multiple block locators.
@ -226,7 +225,7 @@ func TestGetBlocksWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -246,7 +245,7 @@ func TestGetBlocksWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgGetBlocks
var msg MsgGetBlocks
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -268,31 +267,31 @@ func TestGetBlocksWireErrors(t *testing.T) {
// specifically here instead of the latest because the test data is
// using bytes encoded with that protocol version.
pver := uint32(60002)
wireErr := &wire.MessageError{}
wireErr := &MessageError{}
// Block 99499 hash.
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
hashLocator, err := wire.NewShaHashFromStr(hashStr)
hashLocator, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 99500 hash.
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
hashLocator2, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := wire.NewShaHashFromStr(hashStr)
hashStop, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// MsgGetBlocks message with multiple block locators and a stop hash.
baseGetBlocks := wire.NewMsgGetBlocks(hashStop)
baseGetBlocks := NewMsgGetBlocks(hashStop)
baseGetBlocks.ProtocolVersion = pver
baseGetBlocks.AddBlockLocatorHash(hashLocator2)
baseGetBlocks.AddBlockLocatorHash(hashLocator)
@ -315,8 +314,8 @@ func TestGetBlocksWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed
// block locator hashes.
maxGetBlocks := wire.NewMsgGetBlocks(hashStop)
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
maxGetBlocks := NewMsgGetBlocks(hashStop)
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
maxGetBlocks.AddBlockLocatorHash(&mainNetGenesisHash)
}
maxGetBlocks.BlockLocatorHashes = append(maxGetBlocks.BlockLocatorHashes,
@ -327,7 +326,7 @@ func TestGetBlocksWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetBlocks // Value to encode
in *MsgGetBlocks // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -357,9 +356,9 @@ func TestGetBlocksWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -368,7 +367,7 @@ func TestGetBlocksWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgGetBlocks
var msg MsgGetBlocks
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -377,9 +376,9 @@ func TestGetBlocksWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,17 +10,16 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestGetData tests the MsgGetData API.
func TestGetData(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "getdata"
msg := wire.NewMsgGetData()
msg := NewMsgGetData()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgGetData: wrong command - got %v want %v",
cmd, wantCmd)
@ -37,8 +36,8 @@ func TestGetData(t *testing.T) {
}
// Ensure inventory vectors are added properly.
hash := wire.ShaHash{}
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
hash := ShaHash{}
iv := NewInvVect(InvTypeBlock, &hash)
err := msg.AddInvVect(iv)
if err != nil {
t.Errorf("AddInvVect: %v", err)
@ -50,7 +49,7 @@ func TestGetData(t *testing.T) {
// Ensure adding more than the max allowed inventory vectors per
// message returns an error.
for i := 0; i < wire.MaxInvPerMsg; i++ {
for i := 0; i < MaxInvPerMsg; i++ {
err = msg.AddInvVect(iv)
}
if err == nil {
@ -60,8 +59,8 @@ func TestGetData(t *testing.T) {
// Ensure creating the message with a size hint larger than the max
// works as expected.
msg = wire.NewMsgGetDataSizeHint(wire.MaxInvPerMsg + 1)
wantCap := wire.MaxInvPerMsg
msg = NewMsgGetDataSizeHint(MaxInvPerMsg + 1)
wantCap := MaxInvPerMsg
if cap(msg.InvList) != wantCap {
t.Errorf("NewMsgGetDataSizeHint: wrong cap for size hint - "+
"got %v, want %v", cap(msg.InvList), wantCap)
@ -75,29 +74,29 @@ func TestGetData(t *testing.T) {
func TestGetDataWire(t *testing.T) {
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Transation 1 of Block 203707 hash.
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
txHash, err := wire.NewShaHashFromStr(hashStr)
txHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
iv := NewInvVect(InvTypeBlock, blockHash)
iv2 := NewInvVect(InvTypeTx, txHash)
// Empty MsgGetData message.
NoInv := wire.NewMsgGetData()
NoInv := NewMsgGetData()
NoInvEncoded := []byte{
0x00, // Varint for number of inventory vectors
}
// MsgGetData message with multiple inventory vectors.
MultiInv := wire.NewMsgGetData()
MultiInv := NewMsgGetData()
MultiInv.AddInvVect(iv)
MultiInv.AddInvVect(iv2)
MultiInvEncoded := []byte{
@ -115,8 +114,8 @@ func TestGetDataWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetData // Message to encode
out *wire.MsgGetData // Expected decoded message
in *MsgGetData // Message to encode
out *MsgGetData // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -125,7 +124,7 @@ func TestGetDataWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple inv vectors.
@ -133,7 +132,7 @@ func TestGetDataWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version no inv vectors.
@ -141,7 +140,7 @@ func TestGetDataWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple inv vectors.
@ -149,7 +148,7 @@ func TestGetDataWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version no inv vectors.
@ -157,7 +156,7 @@ func TestGetDataWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version with multiple inv vectors.
@ -165,7 +164,7 @@ func TestGetDataWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion no inv vectors.
@ -173,7 +172,7 @@ func TestGetDataWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with multiple inv vectors.
@ -181,7 +180,7 @@ func TestGetDataWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion no inv vectors.
@ -189,7 +188,7 @@ func TestGetDataWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion with multiple inv vectors.
@ -197,7 +196,7 @@ func TestGetDataWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -217,7 +216,7 @@ func TestGetDataWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgGetData
var msg MsgGetData
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -235,20 +234,20 @@ func TestGetDataWire(t *testing.T) {
// TestGetDataWireErrors performs negative tests against wire encode and decode
// of MsgGetData to confirm error paths work correctly.
func TestGetDataWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
wireErr := &wire.MessageError{}
pver := ProtocolVersion
wireErr := &MessageError{}
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv := NewInvVect(InvTypeBlock, blockHash)
// Base message used to induce errors.
baseGetData := wire.NewMsgGetData()
baseGetData := NewMsgGetData()
baseGetData.AddInvVect(iv)
baseGetDataEncoded := []byte{
0x02, // Varint for number of inv vectors
@ -261,8 +260,8 @@ func TestGetDataWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed inv
// vectors.
maxGetData := wire.NewMsgGetData()
for i := 0; i < wire.MaxInvPerMsg; i++ {
maxGetData := NewMsgGetData()
for i := 0; i < MaxInvPerMsg; i++ {
maxGetData.AddInvVect(iv)
}
maxGetData.InvList = append(maxGetData.InvList, iv)
@ -271,7 +270,7 @@ func TestGetDataWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetData // Value to encode
in *MsgGetData // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -298,9 +297,9 @@ func TestGetDataWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -309,7 +308,7 @@ func TestGetDataWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgGetData
var msg MsgGetData
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -318,9 +317,9 @@ func TestGetDataWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,24 +10,23 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestGetHeaders tests the MsgGetHeader API.
func TestGetHeaders(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Block 99500 hash.
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
locatorHash, err := wire.NewShaHashFromStr(hashStr)
locatorHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Ensure the command is expected value.
wantCmd := "getheaders"
msg := wire.NewMsgGetHeaders()
msg := NewMsgGetHeaders()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgGetHeaders: wrong command - got %v want %v",
cmd, wantCmd)
@ -58,7 +57,7 @@ func TestGetHeaders(t *testing.T) {
// Ensure adding more than the max allowed block locator hashes per
// message returns an error.
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
err = msg.AddBlockLocatorHash(locatorHash)
}
if err == nil {
@ -79,27 +78,27 @@ func TestGetHeadersWire(t *testing.T) {
// Block 99499 hash.
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
hashLocator, err := wire.NewShaHashFromStr(hashStr)
hashLocator, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 99500 hash.
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
hashLocator2, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := wire.NewShaHashFromStr(hashStr)
hashStop, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// MsgGetHeaders message with no block locators or stop hash.
noLocators := wire.NewMsgGetHeaders()
noLocators := NewMsgGetHeaders()
noLocators.ProtocolVersion = pver
noLocatorsEncoded := []byte{
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
@ -111,7 +110,7 @@ func TestGetHeadersWire(t *testing.T) {
}
// MsgGetHeaders message with multiple block locators and a stop hash.
multiLocators := wire.NewMsgGetHeaders()
multiLocators := NewMsgGetHeaders()
multiLocators.ProtocolVersion = pver
multiLocators.HashStop = *hashStop
multiLocators.AddBlockLocatorHash(hashLocator2)
@ -134,8 +133,8 @@ func TestGetHeadersWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetHeaders // Message to encode
out *wire.MsgGetHeaders // Expected decoded message
in *MsgGetHeaders // Message to encode
out *MsgGetHeaders // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -144,7 +143,7 @@ func TestGetHeadersWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple block locators.
@ -152,7 +151,7 @@ func TestGetHeadersWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version with no block locators.
@ -160,7 +159,7 @@ func TestGetHeadersWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple block locators.
@ -168,7 +167,7 @@ func TestGetHeadersWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version with no block locators.
@ -176,7 +175,7 @@ func TestGetHeadersWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Versionwith multiple block locators.
@ -184,7 +183,7 @@ func TestGetHeadersWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion with no block locators.
@ -192,7 +191,7 @@ func TestGetHeadersWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion multiple block locators.
@ -200,7 +199,7 @@ func TestGetHeadersWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion with no block locators.
@ -208,7 +207,7 @@ func TestGetHeadersWire(t *testing.T) {
noLocators,
noLocators,
noLocatorsEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion multiple block locators.
@ -216,7 +215,7 @@ func TestGetHeadersWire(t *testing.T) {
multiLocators,
multiLocators,
multiLocatorsEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -236,7 +235,7 @@ func TestGetHeadersWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgGetHeaders
var msg MsgGetHeaders
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -258,31 +257,31 @@ func TestGetHeadersWireErrors(t *testing.T) {
// specifically here instead of the latest because the test data is
// using bytes encoded with that protocol version.
pver := uint32(60002)
wireErr := &wire.MessageError{}
wireErr := &MessageError{}
// Block 99499 hash.
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
hashLocator, err := wire.NewShaHashFromStr(hashStr)
hashLocator, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 99500 hash.
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
hashLocator2, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Block 100000 hash.
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hashStop, err := wire.NewShaHashFromStr(hashStr)
hashStop, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// MsgGetHeaders message with multiple block locators and a stop hash.
baseGetHeaders := wire.NewMsgGetHeaders()
baseGetHeaders := NewMsgGetHeaders()
baseGetHeaders.ProtocolVersion = pver
baseGetHeaders.HashStop = *hashStop
baseGetHeaders.AddBlockLocatorHash(hashLocator2)
@ -306,8 +305,8 @@ func TestGetHeadersWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed
// block locator hashes.
maxGetHeaders := wire.NewMsgGetHeaders()
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
maxGetHeaders := NewMsgGetHeaders()
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
maxGetHeaders.AddBlockLocatorHash(&mainNetGenesisHash)
}
maxGetHeaders.BlockLocatorHashes = append(maxGetHeaders.BlockLocatorHashes,
@ -318,7 +317,7 @@ func TestGetHeadersWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgGetHeaders // Value to encode
in *MsgGetHeaders // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -348,9 +347,9 @@ func TestGetHeadersWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -359,7 +358,7 @@ func TestGetHeadersWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgGetHeaders
var msg MsgGetHeaders
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -368,9 +367,9 @@ func TestGetHeadersWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,7 +10,6 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
@ -20,7 +19,7 @@ func TestHeaders(t *testing.T) {
// Ensure the command is expected value.
wantCmd := "headers"
msg := wire.NewMsgHeaders()
msg := NewMsgHeaders()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgHeaders: wrong command - got %v want %v",
cmd, wantCmd)
@ -49,10 +48,10 @@ func TestHeaders(t *testing.T) {
// Ensure adding more than the max allowed headers per message returns
// error.
var err error
for i := 0; i < wire.MaxBlockHeadersPerMsg+1; i++ {
for i := 0; i < MaxBlockHeadersPerMsg+1; i++ {
err = msg.AddBlockHeader(bh)
}
if reflect.TypeOf(err) != reflect.TypeOf(&wire.MessageError{}) {
if reflect.TypeOf(err) != reflect.TypeOf(&MessageError{}) {
t.Errorf("AddBlockHeader: expected error on too many headers " +
"not received")
}
@ -67,18 +66,18 @@ func TestHeadersWire(t *testing.T) {
merkleHash := blockOne.Header.MerkleRoot
bits := uint32(0x1d00ffff)
nonce := uint32(0x9962e301)
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
bh := NewBlockHeader(&hash, &merkleHash, bits, nonce)
bh.Version = blockOne.Header.Version
bh.Timestamp = blockOne.Header.Timestamp
// Empty headers message.
noHeaders := wire.NewMsgHeaders()
noHeaders := NewMsgHeaders()
noHeadersEncoded := []byte{
0x00, // Varint for number of headers
}
// Headers message with one header.
oneHeader := wire.NewMsgHeaders()
oneHeader := NewMsgHeaders()
oneHeader.AddBlockHeader(bh)
oneHeaderEncoded := []byte{
0x01, // VarInt for number of headers.
@ -98,8 +97,8 @@ func TestHeadersWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgHeaders // Message to encode
out *wire.MsgHeaders // Expected decoded message
in *MsgHeaders // Message to encode
out *MsgHeaders // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -108,7 +107,7 @@ func TestHeadersWire(t *testing.T) {
noHeaders,
noHeaders,
noHeadersEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with one header.
@ -116,7 +115,7 @@ func TestHeadersWire(t *testing.T) {
oneHeader,
oneHeader,
oneHeaderEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version with no headers.
@ -124,7 +123,7 @@ func TestHeadersWire(t *testing.T) {
noHeaders,
noHeaders,
noHeadersEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with one header.
@ -132,7 +131,7 @@ func TestHeadersWire(t *testing.T) {
oneHeader,
oneHeader,
oneHeaderEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version with no headers.
@ -140,7 +139,7 @@ func TestHeadersWire(t *testing.T) {
noHeaders,
noHeaders,
noHeadersEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version with one header.
@ -148,14 +147,14 @@ func TestHeadersWire(t *testing.T) {
oneHeader,
oneHeader,
oneHeaderEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion with no headers.
{
noHeaders,
noHeaders,
noHeadersEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with one header.
@ -163,7 +162,7 @@ func TestHeadersWire(t *testing.T) {
oneHeader,
oneHeader,
oneHeaderEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion with no headers.
@ -171,7 +170,7 @@ func TestHeadersWire(t *testing.T) {
noHeaders,
noHeaders,
noHeadersEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion with one header.
@ -179,7 +178,7 @@ func TestHeadersWire(t *testing.T) {
oneHeader,
oneHeader,
oneHeaderEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -199,7 +198,7 @@ func TestHeadersWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgHeaders
var msg MsgHeaders
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -217,19 +216,19 @@ func TestHeadersWire(t *testing.T) {
// TestHeadersWireErrors performs negative tests against wire encode and decode
// of MsgHeaders to confirm error paths work correctly.
func TestHeadersWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
wireErr := &wire.MessageError{}
pver := ProtocolVersion
wireErr := &MessageError{}
hash := mainNetGenesisHash
merkleHash := blockOne.Header.MerkleRoot
bits := uint32(0x1d00ffff)
nonce := uint32(0x9962e301)
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
bh := NewBlockHeader(&hash, &merkleHash, bits, nonce)
bh.Version = blockOne.Header.Version
bh.Timestamp = blockOne.Header.Timestamp
// Headers message with one header.
oneHeader := wire.NewMsgHeaders()
oneHeader := NewMsgHeaders()
oneHeader.AddBlockHeader(bh)
oneHeaderEncoded := []byte{
0x01, // VarInt for number of headers.
@ -250,8 +249,8 @@ func TestHeadersWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed
// headers.
maxHeaders := wire.NewMsgHeaders()
for i := 0; i < wire.MaxBlockHeadersPerMsg; i++ {
maxHeaders := NewMsgHeaders()
for i := 0; i < MaxBlockHeadersPerMsg; i++ {
maxHeaders.AddBlockHeader(bh)
}
maxHeaders.Headers = append(maxHeaders.Headers, bh)
@ -261,11 +260,11 @@ func TestHeadersWireErrors(t *testing.T) {
// Intentionally invalid block header that has a transaction count used
// to force errors.
bhTrans := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
bhTrans := NewBlockHeader(&hash, &merkleHash, bits, nonce)
bhTrans.Version = blockOne.Header.Version
bhTrans.Timestamp = blockOne.Header.Timestamp
transHeader := wire.NewMsgHeaders()
transHeader := NewMsgHeaders()
transHeader.AddBlockHeader(bhTrans)
transHeaderEncoded := []byte{
0x01, // VarInt for number of headers.
@ -285,7 +284,7 @@ func TestHeadersWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgHeaders // Value to encode
in *MsgHeaders // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -316,9 +315,9 @@ func TestHeadersWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -327,7 +326,7 @@ func TestHeadersWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgHeaders
var msg MsgHeaders
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -336,9 +335,9 @@ func TestHeadersWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,17 +10,16 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestInv tests the MsgInv API.
func TestInv(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "inv"
msg := wire.NewMsgInv()
msg := NewMsgInv()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgInv: wrong command - got %v want %v",
cmd, wantCmd)
@ -37,8 +36,8 @@ func TestInv(t *testing.T) {
}
// Ensure inventory vectors are added properly.
hash := wire.ShaHash{}
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
hash := ShaHash{}
iv := NewInvVect(InvTypeBlock, &hash)
err := msg.AddInvVect(iv)
if err != nil {
t.Errorf("AddInvVect: %v", err)
@ -50,7 +49,7 @@ func TestInv(t *testing.T) {
// Ensure adding more than the max allowed inventory vectors per
// message returns an error.
for i := 0; i < wire.MaxInvPerMsg; i++ {
for i := 0; i < MaxInvPerMsg; i++ {
err = msg.AddInvVect(iv)
}
if err == nil {
@ -60,8 +59,8 @@ func TestInv(t *testing.T) {
// Ensure creating the message with a size hint larger than the max
// works as expected.
msg = wire.NewMsgInvSizeHint(wire.MaxInvPerMsg + 1)
wantCap := wire.MaxInvPerMsg
msg = NewMsgInvSizeHint(MaxInvPerMsg + 1)
wantCap := MaxInvPerMsg
if cap(msg.InvList) != wantCap {
t.Errorf("NewMsgInvSizeHint: wrong cap for size hint - "+
"got %v, want %v", cap(msg.InvList), wantCap)
@ -75,29 +74,29 @@ func TestInv(t *testing.T) {
func TestInvWire(t *testing.T) {
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Transation 1 of Block 203707 hash.
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
txHash, err := wire.NewShaHashFromStr(hashStr)
txHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
iv := NewInvVect(InvTypeBlock, blockHash)
iv2 := NewInvVect(InvTypeTx, txHash)
// Empty inv message.
NoInv := wire.NewMsgInv()
NoInv := NewMsgInv()
NoInvEncoded := []byte{
0x00, // Varint for number of inventory vectors
}
// Inv message with multiple inventory vectors.
MultiInv := wire.NewMsgInv()
MultiInv := NewMsgInv()
MultiInv.AddInvVect(iv)
MultiInv.AddInvVect(iv2)
MultiInvEncoded := []byte{
@ -115,8 +114,8 @@ func TestInvWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgInv // Message to encode
out *wire.MsgInv // Expected decoded message
in *MsgInv // Message to encode
out *MsgInv // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -125,7 +124,7 @@ func TestInvWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple inv vectors.
@ -133,7 +132,7 @@ func TestInvWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version no inv vectors.
@ -141,7 +140,7 @@ func TestInvWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple inv vectors.
@ -149,7 +148,7 @@ func TestInvWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version no inv vectors.
@ -157,7 +156,7 @@ func TestInvWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version with multiple inv vectors.
@ -165,7 +164,7 @@ func TestInvWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion no inv vectors.
@ -173,7 +172,7 @@ func TestInvWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with multiple inv vectors.
@ -181,7 +180,7 @@ func TestInvWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion no inv vectors.
@ -189,7 +188,7 @@ func TestInvWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion with multiple inv vectors.
@ -197,7 +196,7 @@ func TestInvWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -217,7 +216,7 @@ func TestInvWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgInv
var msg MsgInv
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -235,20 +234,20 @@ func TestInvWire(t *testing.T) {
// TestInvWireErrors performs negative tests against wire encode and decode
// of MsgInv to confirm error paths work correctly.
func TestInvWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
wireErr := &wire.MessageError{}
pver := ProtocolVersion
wireErr := &MessageError{}
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv := NewInvVect(InvTypeBlock, blockHash)
// Base inv message used to induce errors.
baseInv := wire.NewMsgInv()
baseInv := NewMsgInv()
baseInv.AddInvVect(iv)
baseInvEncoded := []byte{
0x02, // Varint for number of inv vectors
@ -261,8 +260,8 @@ func TestInvWireErrors(t *testing.T) {
// Inv message that forces an error by having more than the max allowed
// inv vectors.
maxInv := wire.NewMsgInv()
for i := 0; i < wire.MaxInvPerMsg; i++ {
maxInv := NewMsgInv()
for i := 0; i < MaxInvPerMsg; i++ {
maxInv.AddInvVect(iv)
}
maxInv.InvList = append(maxInv.InvList, iv)
@ -271,7 +270,7 @@ func TestInvWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgInv // Value to encode
in *MsgInv // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -298,9 +297,9 @@ func TestInvWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -309,7 +308,7 @@ func TestInvWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgInv
var msg MsgInv
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -318,9 +317,9 @@ func TestInvWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,22 +1,20 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"testing"
"github.com/btcsuite/btcd/wire"
)
func TestMemPool(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "mempool"
msg := wire.NewMsgMemPool()
msg := NewMsgMemPool()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgMemPool: wrong command - got %v want %v",
cmd, wantCmd)
@ -40,7 +38,7 @@ func TestMemPool(t *testing.T) {
// Older protocol versions should fail encode since message didn't
// exist yet.
oldPver := wire.BIP0035Version - 1
oldPver := BIP0035Version - 1
err = msg.BtcEncode(&buf, oldPver)
if err == nil {
s := "encode of MsgMemPool passed for old protocol version %v err <%v>"
@ -48,7 +46,7 @@ func TestMemPool(t *testing.T) {
}
// Test decode with latest protocol version.
readmsg := wire.NewMsgMemPool()
readmsg := NewMsgMemPool()
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgMemPool failed [%v] err <%v>", buf, err)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -12,24 +12,23 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestMerkleBlock tests the MsgMerkleBlock API.
func TestMerkleBlock(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Block 1 header.
prevHash := &blockOne.Header.PrevBlock
merkleHash := &blockOne.Header.MerkleRoot
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
bh := NewBlockHeader(prevHash, merkleHash, bits, nonce)
// Ensure the command is expected value.
wantCmd := "merkleblock"
msg := wire.NewMsgMerkleBlock(bh)
msg := NewMsgMerkleBlock(bh)
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
cmd, wantCmd)
@ -47,9 +46,9 @@ func TestMerkleBlock(t *testing.T) {
// Load maxTxPerBlock hashes
data := make([]byte, 32)
for i := 0; i < wire.MaxTxPerBlock; i++ {
for i := 0; i < maxTxPerBlock; i++ {
rand.Read(data)
hash, err := wire.NewShaHash(data)
hash, err := NewShaHash(data)
if err != nil {
t.Errorf("NewShaHash failed: %v\n", err)
return
@ -63,7 +62,7 @@ func TestMerkleBlock(t *testing.T) {
// Add one more Tx to test failure.
rand.Read(data)
hash, err := wire.NewShaHash(data)
hash, err := NewShaHash(data)
if err != nil {
t.Errorf("NewShaHash failed: %v\n", err)
return
@ -82,7 +81,7 @@ func TestMerkleBlock(t *testing.T) {
}
// Test decode with latest protocol version.
readmsg := wire.MsgMerkleBlock{}
readmsg := MsgMerkleBlock{}
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgMerkleBlock failed [%v] err <%v>", buf, err)
@ -100,7 +99,7 @@ func TestMerkleBlock(t *testing.T) {
// Force too many flag bytes to test maxFlagsPerMerkleBlock.
// Reset the number of hashes back to a valid value.
msg.Hashes = msg.Hashes[len(msg.Hashes)-1:]
msg.Flags = make([]byte, wire.MaxFlagsPerMerkleBlock+1)
msg.Flags = make([]byte, maxFlagsPerMerkleBlock+1)
err = msg.BtcEncode(&buf, pver)
if err == nil {
t.Errorf("encode of MsgMerkleBlock succeeded with too many " +
@ -117,21 +116,21 @@ func TestMerkleBlockCrossProtocol(t *testing.T) {
merkleHash := &blockOne.Header.MerkleRoot
bits := blockOne.Header.Bits
nonce := blockOne.Header.Nonce
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
bh := NewBlockHeader(prevHash, merkleHash, bits, nonce)
msg := wire.NewMsgMerkleBlock(bh)
msg := NewMsgMerkleBlock(bh)
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
err)
}
// Decode with old protocol version.
var readmsg wire.MsgFilterLoad
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
var readmsg MsgFilterLoad
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err == nil {
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
msg)
@ -142,21 +141,21 @@ func TestMerkleBlockCrossProtocol(t *testing.T) {
// various numbers of transaction hashes and protocol versions.
func TestMerkleBlockWire(t *testing.T) {
tests := []struct {
in *wire.MsgMerkleBlock // Message to encode
out *wire.MsgMerkleBlock // Expected decoded message
in *MsgMerkleBlock // Message to encode
out *MsgMerkleBlock // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
{
&merkleBlockOne, &merkleBlockOne, merkleBlockOneBytes,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0037Version.
{
&merkleBlockOne, &merkleBlockOne, merkleBlockOneBytes,
wire.BIP0037Version,
BIP0037Version,
},
}
@ -176,7 +175,7 @@ func TestMerkleBlockWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgMerkleBlock
var msg MsgMerkleBlock
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -198,11 +197,11 @@ func TestMerkleBlockWireErrors(t *testing.T) {
// because the test data is using bytes encoded with that protocol
// version.
pver := uint32(70001)
pverNoMerkleBlock := wire.BIP0037Version - 1
wireErr := &wire.MessageError{}
pverNoMerkleBlock := BIP0037Version - 1
wireErr := &MessageError{}
tests := []struct {
in *wire.MsgMerkleBlock // Value to encode
in *MsgMerkleBlock // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -282,9 +281,9 @@ func TestMerkleBlockWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -293,7 +292,7 @@ func TestMerkleBlockWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgMerkleBlock
var msg MsgMerkleBlock
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -302,9 +301,9 @@ func TestMerkleBlockWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)
@ -327,7 +326,7 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
// Create bytes for a merkle block that claims to have more than the max
// allowed tx hashes.
var buf bytes.Buffer
wire.TstWriteVarInt(&buf, pver, wire.MaxTxPerBlock+1)
WriteVarInt(&buf, pver, maxTxPerBlock+1)
numHashesOffset := 84
exceedMaxHashes := make([]byte, numHashesOffset)
copy(exceedMaxHashes, merkleBlockOneBytes[:numHashesOffset])
@ -336,7 +335,7 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
// Create bytes for a merkle block that claims to have more than the max
// allowed flag bytes.
buf.Reset()
wire.TstWriteVarInt(&buf, pver, wire.MaxFlagsPerMerkleBlock+1)
WriteVarInt(&buf, pver, maxFlagsPerMerkleBlock+1)
numFlagBytesOffset := 117
exceedMaxFlagBytes := make([]byte, numFlagBytesOffset)
copy(exceedMaxFlagBytes, merkleBlockOneBytes[:numFlagBytesOffset])
@ -348,15 +347,15 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
err error // Expected error
}{
// Block that claims to have more than max allowed hashes.
{exceedMaxHashes, pver, &wire.MessageError{}},
{exceedMaxHashes, pver, &MessageError{}},
// Block that claims to have more than max allowed flag bytes.
{exceedMaxFlagBytes, pver, &wire.MessageError{}},
{exceedMaxFlagBytes, pver, &MessageError{}},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
var msg wire.MsgMerkleBlock
var msg MsgMerkleBlock
r := bytes.NewReader(test.buf)
err := msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
@ -369,16 +368,16 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
// merkleBlockOne is a merkle block created from block one of the block chain
// where the first transaction matches.
var merkleBlockOne = wire.MsgMerkleBlock{
Header: wire.BlockHeader{
var merkleBlockOne = MsgMerkleBlock{
Header: BlockHeader{
Version: 1,
PrevBlock: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
PrevBlock: ShaHash([HashSize]byte{ // Make go vet happy.
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
}),
MerkleRoot: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
MerkleRoot: ShaHash([HashSize]byte{ // Make go vet happy.
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
@ -389,8 +388,8 @@ var merkleBlockOne = wire.MsgMerkleBlock{
Nonce: 0x9962e301, // 2573394689
},
Transactions: 1,
Hashes: []*wire.ShaHash{
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
Hashes: []*ShaHash{
(*ShaHash)(&[HashSize]byte{ // Make go vet happy.
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,17 +10,16 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestNotFound tests the MsgNotFound API.
func TestNotFound(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "notfound"
msg := wire.NewMsgNotFound()
msg := NewMsgNotFound()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgNotFound: wrong command - got %v want %v",
cmd, wantCmd)
@ -37,8 +36,8 @@ func TestNotFound(t *testing.T) {
}
// Ensure inventory vectors are added properly.
hash := wire.ShaHash{}
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
hash := ShaHash{}
iv := NewInvVect(InvTypeBlock, &hash)
err := msg.AddInvVect(iv)
if err != nil {
t.Errorf("AddInvVect: %v", err)
@ -50,7 +49,7 @@ func TestNotFound(t *testing.T) {
// Ensure adding more than the max allowed inventory vectors per
// message returns an error.
for i := 0; i < wire.MaxInvPerMsg; i++ {
for i := 0; i < MaxInvPerMsg; i++ {
err = msg.AddInvVect(iv)
}
if err == nil {
@ -66,29 +65,29 @@ func TestNotFound(t *testing.T) {
func TestNotFoundWire(t *testing.T) {
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Transation 1 of Block 203707 hash.
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
txHash, err := wire.NewShaHashFromStr(hashStr)
txHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
iv := NewInvVect(InvTypeBlock, blockHash)
iv2 := NewInvVect(InvTypeTx, txHash)
// Empty notfound message.
NoInv := wire.NewMsgNotFound()
NoInv := NewMsgNotFound()
NoInvEncoded := []byte{
0x00, // Varint for number of inventory vectors
}
// NotFound message with multiple inventory vectors.
MultiInv := wire.NewMsgNotFound()
MultiInv := NewMsgNotFound()
MultiInv.AddInvVect(iv)
MultiInv.AddInvVect(iv2)
MultiInvEncoded := []byte{
@ -106,8 +105,8 @@ func TestNotFoundWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgNotFound // Message to encode
out *wire.MsgNotFound // Expected decoded message
in *MsgNotFound // Message to encode
out *MsgNotFound // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -116,7 +115,7 @@ func TestNotFoundWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple inv vectors.
@ -124,7 +123,7 @@ func TestNotFoundWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version no inv vectors.
@ -132,7 +131,7 @@ func TestNotFoundWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple inv vectors.
@ -140,7 +139,7 @@ func TestNotFoundWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version no inv vectors.
@ -148,7 +147,7 @@ func TestNotFoundWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version with multiple inv vectors.
@ -156,7 +155,7 @@ func TestNotFoundWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion no inv vectors.
@ -164,7 +163,7 @@ func TestNotFoundWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with multiple inv vectors.
@ -172,7 +171,7 @@ func TestNotFoundWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion no inv vectors.
@ -180,7 +179,7 @@ func TestNotFoundWire(t *testing.T) {
NoInv,
NoInv,
NoInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion with multiple inv vectors.
@ -188,7 +187,7 @@ func TestNotFoundWire(t *testing.T) {
MultiInv,
MultiInv,
MultiInvEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -208,7 +207,7 @@ func TestNotFoundWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgNotFound
var msg MsgNotFound
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -226,20 +225,20 @@ func TestNotFoundWire(t *testing.T) {
// TestNotFoundWireErrors performs negative tests against wire encode and decode
// of MsgNotFound to confirm error paths work correctly.
func TestNotFoundWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
wireErr := &wire.MessageError{}
pver := ProtocolVersion
wireErr := &MessageError{}
// Block 203707 hash.
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
blockHash, err := wire.NewShaHashFromStr(hashStr)
blockHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
iv := NewInvVect(InvTypeBlock, blockHash)
// Base message used to induce errors.
baseNotFound := wire.NewMsgNotFound()
baseNotFound := NewMsgNotFound()
baseNotFound.AddInvVect(iv)
baseNotFoundEncoded := []byte{
0x02, // Varint for number of inv vectors
@ -252,8 +251,8 @@ func TestNotFoundWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed inv
// vectors.
maxNotFound := wire.NewMsgNotFound()
for i := 0; i < wire.MaxInvPerMsg; i++ {
maxNotFound := NewMsgNotFound()
for i := 0; i < MaxInvPerMsg; i++ {
maxNotFound.AddInvVect(iv)
}
maxNotFound.InvList = append(maxNotFound.InvList, iv)
@ -262,7 +261,7 @@ func TestNotFoundWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgNotFound // Value to encode
in *MsgNotFound // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -288,9 +287,9 @@ func TestNotFoundWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -299,7 +298,7 @@ func TestNotFoundWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgNotFound
var msg MsgNotFound
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -308,9 +307,9 @@ func TestNotFoundWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,20 +10,19 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestPing tests the MsgPing API against the latest protocol version.
func TestPing(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure we get the same nonce back out.
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: Error generating nonce: %v", err)
}
msg := wire.NewMsgPing(nonce)
msg := NewMsgPing(nonce)
if msg.Nonce != nonce {
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
msg.Nonce, nonce)
@ -52,13 +51,13 @@ func TestPing(t *testing.T) {
// BIP0031Version.
func TestPingBIP0031(t *testing.T) {
// Use the protocol version just prior to BIP0031Version changes.
pver := wire.BIP0031Version
pver := BIP0031Version
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: Error generating nonce: %v", err)
}
msg := wire.NewMsgPing(nonce)
msg := NewMsgPing(nonce)
if msg.Nonce != nonce {
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
msg.Nonce, nonce)
@ -81,7 +80,7 @@ func TestPingBIP0031(t *testing.T) {
}
// Test decode with old protocol version.
readmsg := wire.NewMsgPing(0)
readmsg := NewMsgPing(0)
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
@ -99,11 +98,11 @@ func TestPingBIP0031(t *testing.T) {
// TestPingCrossProtocol tests the MsgPing API when encoding with the latest
// protocol version and decoding with BIP0031Version.
func TestPingCrossProtocol(t *testing.T) {
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: Error generating nonce: %v", err)
}
msg := wire.NewMsgPing(nonce)
msg := NewMsgPing(nonce)
if msg.Nonce != nonce {
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
msg.Nonce, nonce)
@ -111,14 +110,14 @@ func TestPingCrossProtocol(t *testing.T) {
// Encode with latest protocol version.
var buf bytes.Buffer
err = msg.BtcEncode(&buf, wire.ProtocolVersion)
err = msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
readmsg := wire.NewMsgPing(0)
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
readmsg := NewMsgPing(0)
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err != nil {
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
}
@ -134,33 +133,33 @@ func TestPingCrossProtocol(t *testing.T) {
// versions.
func TestPingWire(t *testing.T) {
tests := []struct {
in wire.MsgPing // Message to encode
out wire.MsgPing // Expected decoded message
in MsgPing // Message to encode
out MsgPing // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
{
wire.MsgPing{Nonce: 123123}, // 0x1e0f3
wire.MsgPing{Nonce: 123123}, // 0x1e0f3
MsgPing{Nonce: 123123}, // 0x1e0f3
MsgPing{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0031Version+1
{
wire.MsgPing{Nonce: 456456}, // 0x6f708
wire.MsgPing{Nonce: 456456}, // 0x6f708
MsgPing{Nonce: 456456}, // 0x6f708
MsgPing{Nonce: 456456}, // 0x6f708
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
wire.BIP0031Version + 1,
BIP0031Version + 1,
},
// Protocol version BIP0031Version
{
wire.MsgPing{Nonce: 789789}, // 0xc0d1d
wire.MsgPing{Nonce: 0}, // No nonce for pver
MsgPing{Nonce: 789789}, // 0xc0d1d
MsgPing{Nonce: 0}, // No nonce for pver
[]byte{}, // No nonce for pver
wire.BIP0031Version,
BIP0031Version,
},
}
@ -180,7 +179,7 @@ func TestPingWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgPing
var msg MsgPing
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -198,10 +197,10 @@ func TestPingWire(t *testing.T) {
// TestPingWireErrors performs negative tests against wire encode and decode
// of MsgPing to confirm error paths work correctly.
func TestPingWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
tests := []struct {
in *wire.MsgPing // Value to encode
in *MsgPing // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -210,7 +209,7 @@ func TestPingWireErrors(t *testing.T) {
}{
// Latest protocol version with intentional read/write errors.
{
&wire.MsgPing{Nonce: 123123}, // 0x1e0f3
&MsgPing{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00},
pver,
2,
@ -231,7 +230,7 @@ func TestPingWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgPing
var msg MsgPing
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if err != test.readErr {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,19 +10,18 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestPongLatest tests the MsgPong API against the latest protocol version.
func TestPongLatest(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: error generating nonce: %v", err)
}
msg := wire.NewMsgPong(nonce)
msg := NewMsgPong(nonce)
if msg.Nonce != nonce {
t.Errorf("NewMsgPong: wrong nonce - got %v, want %v",
msg.Nonce, nonce)
@ -52,7 +51,7 @@ func TestPongLatest(t *testing.T) {
}
// Test decode with latest protocol version.
readmsg := wire.NewMsgPong(0)
readmsg := NewMsgPong(0)
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgPong failed [%v] err <%v>", buf, err)
@ -70,13 +69,13 @@ func TestPongLatest(t *testing.T) {
// BIP0031Version.
func TestPongBIP0031(t *testing.T) {
// Use the protocol version just prior to BIP0031Version changes.
pver := wire.BIP0031Version
pver := BIP0031Version
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("Error generating nonce: %v", err)
}
msg := wire.NewMsgPong(nonce)
msg := NewMsgPong(nonce)
if msg.Nonce != nonce {
t.Errorf("Should get same nonce back out.")
}
@ -97,7 +96,7 @@ func TestPongBIP0031(t *testing.T) {
}
// Test decode with old protocol version.
readmsg := wire.NewMsgPong(0)
readmsg := NewMsgPong(0)
err = readmsg.BtcDecode(&buf, pver)
if err == nil {
t.Errorf("decode of MsgPong succeeded when it shouldn't have %v",
@ -116,25 +115,25 @@ func TestPongBIP0031(t *testing.T) {
// TestPongCrossProtocol tests the MsgPong API when encoding with the latest
// protocol version and decoding with BIP0031Version.
func TestPongCrossProtocol(t *testing.T) {
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("Error generating nonce: %v", err)
}
msg := wire.NewMsgPong(nonce)
msg := NewMsgPong(nonce)
if msg.Nonce != nonce {
t.Errorf("Should get same nonce back out.")
}
// Encode with latest protocol version.
var buf bytes.Buffer
err = msg.BtcEncode(&buf, wire.ProtocolVersion)
err = msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgPong failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
readmsg := wire.NewMsgPong(0)
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
readmsg := NewMsgPong(0)
err = readmsg.BtcDecode(&buf, BIP0031Version)
if err == nil {
t.Errorf("encode of MsgPong succeeded when it shouldn't have %v",
msg)
@ -151,25 +150,25 @@ func TestPongCrossProtocol(t *testing.T) {
// versions.
func TestPongWire(t *testing.T) {
tests := []struct {
in wire.MsgPong // Message to encode
out wire.MsgPong // Expected decoded message
in MsgPong // Message to encode
out MsgPong // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
{
wire.MsgPong{Nonce: 123123}, // 0x1e0f3
wire.MsgPong{Nonce: 123123}, // 0x1e0f3
MsgPong{Nonce: 123123}, // 0x1e0f3
MsgPong{Nonce: 123123}, // 0x1e0f3
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0031Version+1
{
wire.MsgPong{Nonce: 456456}, // 0x6f708
wire.MsgPong{Nonce: 456456}, // 0x6f708
MsgPong{Nonce: 456456}, // 0x6f708
MsgPong{Nonce: 456456}, // 0x6f708
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
wire.BIP0031Version + 1,
BIP0031Version + 1,
},
}
@ -189,7 +188,7 @@ func TestPongWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgPong
var msg MsgPong
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -207,17 +206,17 @@ func TestPongWire(t *testing.T) {
// TestPongWireErrors performs negative tests against wire encode and decode
// of MsgPong to confirm error paths work correctly.
func TestPongWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverNoPong := wire.BIP0031Version
wireErr := &wire.MessageError{}
pver := ProtocolVersion
pverNoPong := BIP0031Version
wireErr := &MessageError{}
basePong := wire.NewMsgPong(123123) // 0x1e0f3
basePong := NewMsgPong(123123) // 0x1e0f3
basePongEncoded := []byte{
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
}
tests := []struct {
in *wire.MsgPong // Value to encode
in *MsgPong // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -242,9 +241,9 @@ func TestPongWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -253,7 +252,7 @@ func TestPongWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgPong
var msg MsgPong
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -262,9 +261,9 @@ func TestPongWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -1,8 +1,8 @@
// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -10,24 +10,23 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestRejectCodeStringer tests the stringized output for the reject code type.
func TestRejectCodeStringer(t *testing.T) {
tests := []struct {
in wire.RejectCode
in RejectCode
want string
}{
{wire.RejectMalformed, "REJECT_MALFORMED"},
{wire.RejectInvalid, "REJECT_INVALID"},
{wire.RejectObsolete, "REJECT_OBSOLETE"},
{wire.RejectDuplicate, "REJECT_DUPLICATE"},
{wire.RejectNonstandard, "REJECT_NONSTANDARD"},
{wire.RejectDust, "REJECT_DUST"},
{wire.RejectInsufficientFee, "REJECT_INSUFFICIENTFEE"},
{wire.RejectCheckpoint, "REJECT_CHECKPOINT"},
{RejectMalformed, "REJECT_MALFORMED"},
{RejectInvalid, "REJECT_INVALID"},
{RejectObsolete, "REJECT_OBSOLETE"},
{RejectDuplicate, "REJECT_DUPLICATE"},
{RejectNonstandard, "REJECT_NONSTANDARD"},
{RejectDust, "REJECT_DUST"},
{RejectInsufficientFee, "REJECT_INSUFFICIENTFEE"},
{RejectCheckpoint, "REJECT_CHECKPOINT"},
{0xff, "Unknown RejectCode (255)"},
}
@ -45,16 +44,16 @@ func TestRejectCodeStringer(t *testing.T) {
// TestRejectLatest tests the MsgPong API against the latest protocol version.
func TestRejectLatest(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Create reject message data.
rejCommand := (&wire.MsgBlock{}).Command()
rejCode := wire.RejectDuplicate
rejCommand := (&MsgBlock{}).Command()
rejCode := RejectDuplicate
rejReason := "duplicate block"
rejHash := mainNetGenesisHash
// Ensure we get the correct data back out.
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
msg := NewMsgReject(rejCommand, rejCode, rejReason)
msg.Hash = rejHash
if msg.Cmd != rejCommand {
t.Errorf("NewMsgReject: wrong rejected command - got %v, "+
@ -77,7 +76,7 @@ func TestRejectLatest(t *testing.T) {
}
// Ensure max payload is expected value for latest protocol version.
wantPayload := uint32(wire.MaxMessagePayload)
wantPayload := uint32(MaxMessagePayload)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
@ -93,7 +92,7 @@ func TestRejectLatest(t *testing.T) {
}
// Test decode with latest protocol version.
readMsg := wire.MsgReject{}
readMsg := MsgReject{}
err = readMsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgReject failed %v err <%v>", buf.Bytes(),
@ -123,15 +122,15 @@ func TestRejectLatest(t *testing.T) {
// before the version which introduced it (RejectVersion).
func TestRejectBeforeAdded(t *testing.T) {
// Use the protocol version just prior to RejectVersion.
pver := wire.RejectVersion - 1
pver := RejectVersion - 1
// Create reject message data.
rejCommand := (&wire.MsgBlock{}).Command()
rejCode := wire.RejectDuplicate
rejCommand := (&MsgBlock{}).Command()
rejCode := RejectDuplicate
rejReason := "duplicate block"
rejHash := mainNetGenesisHash
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
msg := NewMsgReject(rejCommand, rejCode, rejReason)
msg.Hash = rejHash
// Ensure max payload is expected value for old protocol version.
@ -150,7 +149,7 @@ func TestRejectBeforeAdded(t *testing.T) {
}
// // Test decode with old protocol version.
readMsg := wire.MsgReject{}
readMsg := MsgReject{}
err = readMsg.BtcDecode(&buf, pver)
if err == nil {
t.Errorf("decode of MsgReject succeeded when it shouldn't "+
@ -182,24 +181,24 @@ func TestRejectBeforeAdded(t *testing.T) {
// introduced it (RejectVersion).
func TestRejectCrossProtocol(t *testing.T) {
// Create reject message data.
rejCommand := (&wire.MsgBlock{}).Command()
rejCode := wire.RejectDuplicate
rejCommand := (&MsgBlock{}).Command()
rejCode := RejectDuplicate
rejReason := "duplicate block"
rejHash := mainNetGenesisHash
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
msg := NewMsgReject(rejCommand, rejCode, rejReason)
msg.Hash = rejHash
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgReject failed %v err <%v>", msg, err)
}
// Decode with old protocol version.
readMsg := wire.MsgReject{}
err = readMsg.BtcDecode(&buf, wire.RejectVersion-1)
readMsg := MsgReject{}
err = readMsg.BtcDecode(&buf, RejectVersion-1)
if err == nil {
t.Errorf("encode of MsgReject succeeded when it shouldn't "+
"have %v", msg)
@ -226,37 +225,37 @@ func TestRejectCrossProtocol(t *testing.T) {
// protocol versions.
func TestRejectWire(t *testing.T) {
tests := []struct {
msg wire.MsgReject // Message to encode
msg MsgReject // Message to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version rejected command version (no hash).
{
wire.MsgReject{
MsgReject{
Cmd: "version",
Code: wire.RejectDuplicate,
Code: RejectDuplicate,
Reason: "duplicate version",
},
[]byte{
0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // "version"
0x12, // wire.RejectDuplicate
0x12, // RejectDuplicate
0x11, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
0x74, 0x65, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, // "duplicate version"
},
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version rejected command block (has hash).
{
wire.MsgReject{
MsgReject{
Cmd: "block",
Code: wire.RejectDuplicate,
Code: RejectDuplicate,
Reason: "duplicate block",
Hash: mainNetGenesisHash,
},
[]byte{
0x05, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "block"
0x12, // wire.RejectDuplicate
0x12, // RejectDuplicate
0x0f, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
0x74, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "duplicate block"
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
@ -264,7 +263,7 @@ func TestRejectWire(t *testing.T) {
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // mainNetGenesisHash
},
wire.ProtocolVersion,
ProtocolVersion,
},
}
@ -284,7 +283,7 @@ func TestRejectWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgReject
var msg MsgReject
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -302,16 +301,15 @@ func TestRejectWire(t *testing.T) {
// TestRejectWireErrors performs negative tests against wire encode and decode
// of MsgReject to confirm error paths work correctly.
func TestRejectWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverNoReject := wire.RejectVersion - 1
wireErr := &wire.MessageError{}
pver := ProtocolVersion
pverNoReject := RejectVersion - 1
wireErr := &MessageError{}
baseReject := wire.NewMsgReject("block", wire.RejectDuplicate,
"duplicate block")
baseReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
baseReject.Hash = mainNetGenesisHash
baseRejectEncoded := []byte{
0x05, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "block"
0x12, // wire.RejectDuplicate
0x12, // RejectDuplicate
0x0f, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
0x74, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "duplicate block"
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
@ -321,7 +319,7 @@ func TestRejectWireErrors(t *testing.T) {
}
tests := []struct {
in *wire.MsgReject // Value to encode
in *MsgReject // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -352,9 +350,9 @@ func TestRejectWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -363,7 +361,7 @@ func TestRejectWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgReject
var msg MsgReject
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -372,9 +370,9 @@ func TestRejectWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)

View file

@ -2,25 +2,24 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestSendHeaders tests the MsgSendHeaders API against the latest protocol
// version.
func TestSendHeaders(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "sendheaders"
msg := wire.NewMsgSendHeaders()
msg := NewMsgSendHeaders()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgSendHeaders: wrong command - got %v want %v",
cmd, wantCmd)
@ -45,7 +44,7 @@ func TestSendHeaders(t *testing.T) {
// Older protocol versions should fail encode since message didn't
// exist yet.
oldPver := wire.SendHeadersVersion - 1
oldPver := SendHeadersVersion - 1
err = msg.BtcEncode(&buf, oldPver)
if err == nil {
s := "encode of MsgSendHeaders passed for old protocol " +
@ -54,7 +53,7 @@ func TestSendHeaders(t *testing.T) {
}
// Test decode with latest protocol version.
readmsg := wire.NewMsgSendHeaders()
readmsg := NewMsgSendHeaders()
err = readmsg.BtcDecode(&buf, pver)
if err != nil {
t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf,
@ -77,9 +76,9 @@ func TestSendHeaders(t *testing.T) {
// prior to version SendHeadersVersion.
func TestSendHeadersBIP0130(t *testing.T) {
// Use the protocol version just prior to SendHeadersVersion changes.
pver := wire.SendHeadersVersion - 1
pver := SendHeadersVersion - 1
msg := wire.NewMsgSendHeaders()
msg := NewMsgSendHeaders()
// Test encode with old protocol version.
var buf bytes.Buffer
@ -90,7 +89,7 @@ func TestSendHeadersBIP0130(t *testing.T) {
}
// Test decode with old protocol version.
readmsg := wire.NewMsgSendHeaders()
readmsg := NewMsgSendHeaders()
err = readmsg.BtcDecode(&buf, pver)
if err == nil {
t.Errorf("decode of MsgSendHeaders succeeded when it should " +
@ -103,19 +102,19 @@ func TestSendHeadersBIP0130(t *testing.T) {
// TestSendHeadersCrossProtocol tests the MsgSendHeaders API when encoding with
// the latest protocol version and decoding with SendHeadersVersion.
func TestSendHeadersCrossProtocol(t *testing.T) {
msg := wire.NewMsgSendHeaders()
msg := NewMsgSendHeaders()
// Encode with latest protocol version.
var buf bytes.Buffer
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
err := msg.BtcEncode(&buf, ProtocolVersion)
if err != nil {
t.Errorf("encode of MsgSendHeaders failed %v err <%v>", msg,
err)
}
// Decode with old protocol version.
readmsg := wire.NewMsgSendHeaders()
err = readmsg.BtcDecode(&buf, wire.SendHeadersVersion)
readmsg := NewMsgSendHeaders()
err = readmsg.BtcDecode(&buf, SendHeadersVersion)
if err != nil {
t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf,
err)
@ -125,12 +124,12 @@ func TestSendHeadersCrossProtocol(t *testing.T) {
// TestSendHeadersWire tests the MsgSendHeaders wire encode and decode for
// various protocol versions.
func TestSendHeadersWire(t *testing.T) {
msgSendHeaders := wire.NewMsgSendHeaders()
msgSendHeaders := NewMsgSendHeaders()
msgSendHeadersEncoded := []byte{}
tests := []struct {
in *wire.MsgSendHeaders // Message to encode
out *wire.MsgSendHeaders // Expected decoded message
in *MsgSendHeaders // Message to encode
out *MsgSendHeaders // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -139,7 +138,7 @@ func TestSendHeadersWire(t *testing.T) {
msgSendHeaders,
msgSendHeaders,
msgSendHeadersEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version SendHeadersVersion+1
@ -147,7 +146,7 @@ func TestSendHeadersWire(t *testing.T) {
msgSendHeaders,
msgSendHeaders,
msgSendHeadersEncoded,
wire.SendHeadersVersion + 1,
SendHeadersVersion + 1,
},
// Protocol version SendHeadersVersion
@ -155,7 +154,7 @@ func TestSendHeadersWire(t *testing.T) {
msgSendHeaders,
msgSendHeaders,
msgSendHeadersEncoded,
wire.SendHeadersVersion,
SendHeadersVersion,
},
}
@ -175,7 +174,7 @@ func TestSendHeadersWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgSendHeaders
var msg MsgSendHeaders
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -11,24 +11,23 @@ import (
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestTx tests the MsgTx API.
func TestTx(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Block 100000 hash.
hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hash, err := wire.NewShaHashFromStr(hashStr)
hash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Ensure the command is expected value.
wantCmd := "tx"
msg := wire.NewMsgTx()
msg := NewMsgTx()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgAddr: wrong command - got %v want %v",
cmd, wantCmd)
@ -48,7 +47,7 @@ func TestTx(t *testing.T) {
// NOTE: This is a block hash and made up index, but we're only
// testing package functionality.
prevOutIndex := uint32(1)
prevOut := wire.NewOutPoint(hash, prevOutIndex)
prevOut := NewOutPoint(hash, prevOutIndex)
if !prevOut.Hash.IsEqual(hash) {
t.Errorf("NewOutPoint: wrong hash - got %v, want %v",
spew.Sprint(&prevOut.Hash), spew.Sprint(hash))
@ -65,7 +64,7 @@ func TestTx(t *testing.T) {
// Ensure we get the same transaction input back out.
sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
txIn := wire.NewTxIn(prevOut, sigScript)
txIn := NewTxIn(prevOut, sigScript)
if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) {
t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
spew.Sprint(&txIn.PreviousOutPoint),
@ -92,7 +91,7 @@ func TestTx(t *testing.T) {
0xa6, // 65-byte signature
0xac, // OP_CHECKSIG
}
txOut := wire.NewTxOut(txValue, pkScript)
txOut := NewTxOut(txValue, pkScript)
if txOut.Value != txValue {
t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
txOut.Value, txValue)
@ -132,23 +131,23 @@ func TestTx(t *testing.T) {
func TestTxSha(t *testing.T) {
// Hash of first transaction from block 113875.
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
wantHash, err := wire.NewShaHashFromStr(hashStr)
wantHash, err := NewShaHashFromStr(hashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
return
}
// First transaction from block 113875.
msgTx := wire.NewMsgTx()
txIn := wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash{},
msgTx := NewMsgTx()
txIn := TxIn{
PreviousOutPoint: OutPoint{
Hash: ShaHash{},
Index: 0xffffffff,
},
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
Sequence: 0xffffffff,
}
txOut := wire.TxOut{
txOut := TxOut{
Value: 5000000000,
PkScript: []byte{
0x41, // OP_DATA_65
@ -180,7 +179,7 @@ func TestTxSha(t *testing.T) {
// of transaction inputs and outputs and protocol versions.
func TestTxWire(t *testing.T) {
// Empty tx message.
noTx := wire.NewMsgTx()
noTx := NewMsgTx()
noTx.Version = 1
noTxEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Version
@ -190,8 +189,8 @@ func TestTxWire(t *testing.T) {
}
tests := []struct {
in *wire.MsgTx // Message to encode
out *wire.MsgTx // Expected decoded message
in *MsgTx // Message to encode
out *MsgTx // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -200,7 +199,7 @@ func TestTxWire(t *testing.T) {
noTx,
noTx,
noTxEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with multiple transactions.
@ -208,7 +207,7 @@ func TestTxWire(t *testing.T) {
multiTx,
multiTx,
multiTxEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version with no transactions.
@ -216,7 +215,7 @@ func TestTxWire(t *testing.T) {
noTx,
noTx,
noTxEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0035Version with multiple transactions.
@ -224,7 +223,7 @@ func TestTxWire(t *testing.T) {
multiTx,
multiTx,
multiTxEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version with no transactions.
@ -232,7 +231,7 @@ func TestTxWire(t *testing.T) {
noTx,
noTx,
noTxEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version BIP0031Version with multiple transactions.
@ -240,7 +239,7 @@ func TestTxWire(t *testing.T) {
multiTx,
multiTx,
multiTxEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion with no transactions.
@ -248,7 +247,7 @@ func TestTxWire(t *testing.T) {
noTx,
noTx,
noTxEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with multiple transactions.
@ -256,7 +255,7 @@ func TestTxWire(t *testing.T) {
multiTx,
multiTx,
multiTxEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion with no transactions.
@ -264,7 +263,7 @@ func TestTxWire(t *testing.T) {
noTx,
noTx,
noTxEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
// Protocol version MultipleAddressVersion with multiple transactions.
@ -272,7 +271,7 @@ func TestTxWire(t *testing.T) {
multiTx,
multiTx,
multiTxEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -292,7 +291,7 @@ func TestTxWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgTx
var msg MsgTx
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -316,7 +315,7 @@ func TestTxWireErrors(t *testing.T) {
pver := uint32(60002)
tests := []struct {
in *wire.MsgTx // Value to encode
in *MsgTx // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -361,7 +360,7 @@ func TestTxWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgTx
var msg MsgTx
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if err != test.readErr {
@ -374,7 +373,7 @@ func TestTxWireErrors(t *testing.T) {
// TestTxSerialize tests MsgTx serialize and deserialize.
func TestTxSerialize(t *testing.T) {
noTx := wire.NewMsgTx()
noTx := NewMsgTx()
noTx.Version = 1
noTxEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Version
@ -384,8 +383,8 @@ func TestTxSerialize(t *testing.T) {
}
tests := []struct {
in *wire.MsgTx // Message to encode
out *wire.MsgTx // Expected decoded message
in *MsgTx // Message to encode
out *MsgTx // Expected decoded message
buf []byte // Serialized data
pkScriptLocs []int // Expected output script locations
}{
@ -422,7 +421,7 @@ func TestTxSerialize(t *testing.T) {
}
// Deserialize the transaction.
var tx wire.MsgTx
var tx MsgTx
rbuf := bytes.NewReader(test.buf)
err = tx.Deserialize(rbuf)
if err != nil {
@ -460,7 +459,7 @@ func TestTxSerialize(t *testing.T) {
// of MsgTx to confirm error paths work correctly.
func TestTxSerializeErrors(t *testing.T) {
tests := []struct {
in *wire.MsgTx // Value to encode
in *MsgTx // Value to encode
buf []byte // Serialized data
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
@ -504,7 +503,7 @@ func TestTxSerializeErrors(t *testing.T) {
}
// Deserialize the transaction.
var tx wire.MsgTx
var tx MsgTx
r := newFixedReader(test.max, test.buf)
err = tx.Deserialize(r)
if err != test.readErr {
@ -538,7 +537,7 @@ func TestTxOverflowErrors(t *testing.T) {
0x00, 0x00, 0x00, 0x01, // Version
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, // Varint for number of input transactions
}, pver, txVer, &wire.MessageError{},
}, pver, txVer, &MessageError{},
},
// Transaction that claims to have ~uint64(0) outputs.
@ -548,7 +547,7 @@ func TestTxOverflowErrors(t *testing.T) {
0x00, // Varint for number of input transactions
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, // Varint for number of output transactions
}, pver, txVer, &wire.MessageError{},
}, pver, txVer, &MessageError{},
},
// Transaction that has an input with a signature script that
@ -564,7 +563,7 @@ func TestTxOverflowErrors(t *testing.T) {
0xff, 0xff, 0xff, 0xff, // Prevous output index
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, // Varint for length of signature script
}, pver, txVer, &wire.MessageError{},
}, pver, txVer, &MessageError{},
},
// Transaction that has an output with a public key script
@ -584,14 +583,14 @@ func TestTxOverflowErrors(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, // Varint for length of public key script
}, pver, txVer, &wire.MessageError{},
}, pver, txVer, &MessageError{},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Decode from wire format.
var msg wire.MsgTx
var msg MsgTx
r := bytes.NewReader(test.buf)
err := msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
@ -615,11 +614,11 @@ func TestTxOverflowErrors(t *testing.T) {
// transactions is accurate.
func TestTxSerializeSize(t *testing.T) {
// Empty tx message.
noTx := wire.NewMsgTx()
noTx := NewMsgTx()
noTx.Version = 1
tests := []struct {
in *wire.MsgTx // Tx to encode
in *MsgTx // Tx to encode
size int // Expected serialized size
}{
// No inputs or outpus.
@ -641,12 +640,12 @@ func TestTxSerializeSize(t *testing.T) {
}
// multiTx is a MsgTx with an input and output and used in various tests.
var multiTx = &wire.MsgTx{
var multiTx = &MsgTx{
Version: 1,
TxIn: []*wire.TxIn{
TxIn: []*TxIn{
{
PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash{},
PreviousOutPoint: OutPoint{
Hash: ShaHash{},
Index: 0xffffffff,
},
SignatureScript: []byte{
@ -655,7 +654,7 @@ var multiTx = &wire.MsgTx{
Sequence: 0xffffffff,
},
},
TxOut: []*wire.TxOut{
TxOut: []*TxOut{
{
Value: 0x12a05f200,
PkScript: []byte{

View file

@ -1,25 +1,24 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"reflect"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestVerAck tests the MsgVerAck API.
func TestVerAck(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Ensure the command is expected value.
wantCmd := "verack"
msg := wire.NewMsgVerAck()
msg := NewMsgVerAck()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgVerAck: wrong command - got %v want %v",
cmd, wantCmd)
@ -40,12 +39,12 @@ func TestVerAck(t *testing.T) {
// TestVerAckWire tests the MsgVerAck wire encode and decode for various
// protocol versions.
func TestVerAckWire(t *testing.T) {
msgVerAck := wire.NewMsgVerAck()
msgVerAck := NewMsgVerAck()
msgVerAckEncoded := []byte{}
tests := []struct {
in *wire.MsgVerAck // Message to encode
out *wire.MsgVerAck // Expected decoded message
in *MsgVerAck // Message to encode
out *MsgVerAck // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -54,7 +53,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAck,
msgVerAckEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0035Version.
@ -62,7 +61,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAck,
msgVerAckEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -70,7 +69,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAck,
msgVerAckEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -78,7 +77,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAck,
msgVerAckEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -86,7 +85,7 @@ func TestVerAckWire(t *testing.T) {
msgVerAck,
msgVerAck,
msgVerAckEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -106,7 +105,7 @@ func TestVerAckWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgVerAck
var msg MsgVerAck
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -13,33 +13,32 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
// TestVersion tests the MsgVersion API.
func TestVersion(t *testing.T) {
pver := wire.ProtocolVersion
pver := ProtocolVersion
// Create version message data.
lastBlock := int32(234234)
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
me, err := wire.NewNetAddress(tcpAddrMe, wire.SFNodeNetwork)
me, err := NewNetAddress(tcpAddrMe, SFNodeNetwork)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
tcpAddrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
you, err := wire.NewNetAddress(tcpAddrYou, wire.SFNodeNetwork)
you, err := NewNetAddress(tcpAddrYou, SFNodeNetwork)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
nonce, err := wire.RandomUint64()
nonce, err := RandomUint64()
if err != nil {
t.Errorf("RandomUint64: error generating nonce: %v", err)
}
// Ensure we get the correct data back out.
msg := wire.NewMsgVersion(me, you, nonce, lastBlock)
msg := NewMsgVersion(me, you, nonce, lastBlock)
if msg.ProtocolVersion != int32(pver) {
t.Errorf("NewMsgVersion: wrong protocol version - got %v, want %v",
msg.ProtocolVersion, pver)
@ -56,9 +55,9 @@ func TestVersion(t *testing.T) {
t.Errorf("NewMsgVersion: wrong nonce - got %v, want %v",
msg.Nonce, nonce)
}
if msg.UserAgent != wire.DefaultUserAgent {
if msg.UserAgent != DefaultUserAgent {
t.Errorf("NewMsgVersion: wrong user agent - got %v, want %v",
msg.UserAgent, wire.DefaultUserAgent)
msg.UserAgent, DefaultUserAgent)
}
if msg.LastBlock != lastBlock {
t.Errorf("NewMsgVersion: wrong last block - got %v, want %v",
@ -70,7 +69,7 @@ func TestVersion(t *testing.T) {
}
msg.AddUserAgent("myclient", "1.2.3", "optional", "comments")
customUserAgent := wire.DefaultUserAgent + "myclient:1.2.3(optional; comments)/"
customUserAgent := DefaultUserAgent + "myclient:1.2.3(optional; comments)/"
if msg.UserAgent != customUserAgent {
t.Errorf("AddUserAgent: wrong user agent - got %s, want %s",
msg.UserAgent, customUserAgent)
@ -85,10 +84,10 @@ func TestVersion(t *testing.T) {
// accounting for ":", "/"
err = msg.AddUserAgent(strings.Repeat("t",
wire.MaxUserAgentLen-len(customUserAgent)-2+1), "")
if _, ok := err.(*wire.MessageError); !ok {
MaxUserAgentLen-len(customUserAgent)-2+1), "")
if _, ok := err.(*MessageError); !ok {
t.Errorf("AddUserAgent: expected error not received "+
"- got %v, want %T", err, wire.MessageError{})
"- got %v, want %T", err, MessageError{})
}
@ -98,7 +97,7 @@ func TestVersion(t *testing.T) {
msg.Services, 0)
}
if msg.HasService(wire.SFNodeNetwork) {
if msg.HasService(SFNodeNetwork) {
t.Errorf("HasService: SFNodeNetwork service is set")
}
@ -123,18 +122,18 @@ func TestVersion(t *testing.T) {
}
// Ensure adding the full service node flag works.
msg.AddService(wire.SFNodeNetwork)
if msg.Services != wire.SFNodeNetwork {
msg.AddService(SFNodeNetwork)
if msg.Services != SFNodeNetwork {
t.Errorf("AddService: wrong services - got %v, want %v",
msg.Services, wire.SFNodeNetwork)
msg.Services, SFNodeNetwork)
}
if !msg.HasService(wire.SFNodeNetwork) {
if !msg.HasService(SFNodeNetwork) {
t.Errorf("HasService: SFNodeNetwork service not set")
}
// Use a fake connection.
conn := &fakeConn{localAddr: tcpAddrMe, remoteAddr: tcpAddrYou}
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
msg, err = NewMsgVersionFromConn(conn, nonce, lastBlock)
if err != nil {
t.Errorf("NewMsgVersionFromConn: %v", err)
}
@ -154,10 +153,10 @@ func TestVersion(t *testing.T) {
localAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333},
remoteAddr: tcpAddrYou,
}
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
if err != wire.ErrInvalidNetAddr {
msg, err = NewMsgVersionFromConn(conn, nonce, lastBlock)
if err != ErrInvalidNetAddr {
t.Errorf("NewMsgVersionFromConn: expected error not received "+
"- got %v, want %v", err, wire.ErrInvalidNetAddr)
"- got %v, want %v", err, ErrInvalidNetAddr)
}
// Use a fake connection with remote UDP addresses to force a failure.
@ -165,10 +164,10 @@ func TestVersion(t *testing.T) {
localAddr: tcpAddrMe,
remoteAddr: &net.UDPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333},
}
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
if err != wire.ErrInvalidNetAddr {
msg, err = NewMsgVersionFromConn(conn, nonce, lastBlock)
if err != ErrInvalidNetAddr {
t.Errorf("NewMsgVersionFromConn: expected error not received "+
"- got %v, want %v", err, wire.ErrInvalidNetAddr)
"- got %v, want %v", err, ErrInvalidNetAddr)
}
return
@ -187,8 +186,8 @@ func TestVersionWire(t *testing.T) {
verRelayTxFalseEncoded[len(verRelayTxFalseEncoded)-1] = 0
tests := []struct {
in *wire.MsgVersion // Message to encode
out *wire.MsgVersion // Expected decoded message
in *MsgVersion // Message to encode
out *MsgVersion // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
@ -197,7 +196,7 @@ func TestVersionWire(t *testing.T) {
baseVersionBIP0037,
baseVersionBIP0037,
baseVersionBIP0037Encoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version BIP0037Version with relay transactions field
@ -206,7 +205,7 @@ func TestVersionWire(t *testing.T) {
baseVersionBIP0037,
baseVersionBIP0037,
baseVersionBIP0037Encoded,
wire.BIP0037Version,
BIP0037Version,
},
// Protocol version BIP0037Version with relay transactions field
@ -215,7 +214,7 @@ func TestVersionWire(t *testing.T) {
verRelayTxFalse,
verRelayTxFalse,
verRelayTxFalseEncoded,
wire.BIP0037Version,
BIP0037Version,
},
// Protocol version BIP0035Version.
@ -223,7 +222,7 @@ func TestVersionWire(t *testing.T) {
baseVersion,
baseVersion,
baseVersionEncoded,
wire.BIP0035Version,
BIP0035Version,
},
// Protocol version BIP0031Version.
@ -231,7 +230,7 @@ func TestVersionWire(t *testing.T) {
baseVersion,
baseVersion,
baseVersionEncoded,
wire.BIP0031Version,
BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
@ -239,7 +238,7 @@ func TestVersionWire(t *testing.T) {
baseVersion,
baseVersion,
baseVersionEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
@ -247,7 +246,7 @@ func TestVersionWire(t *testing.T) {
baseVersion,
baseVersion,
baseVersionEncoded,
wire.MultipleAddressVersion,
MultipleAddressVersion,
},
}
@ -267,7 +266,7 @@ func TestVersionWire(t *testing.T) {
}
// Decode the message from wire format.
var msg wire.MsgVersion
var msg MsgVersion
rbuf := bytes.NewBuffer(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -289,7 +288,7 @@ func TestVersionWireErrors(t *testing.T) {
// because the test data is using bytes encoded with that protocol
// version.
pver := uint32(60002)
wireErr := &wire.MessageError{}
wireErr := &MessageError{}
// Ensure calling MsgVersion.BtcDecode with a non *bytes.Buffer returns
// error.
@ -302,12 +301,12 @@ func TestVersionWireErrors(t *testing.T) {
// Copy the base version and change the user agent to exceed max limits.
bvc := *baseVersion
exceedUAVer := &bvc
newUA := "/" + strings.Repeat("t", wire.MaxUserAgentLen-8+1) + ":0.0.1/"
newUA := "/" + strings.Repeat("t", MaxUserAgentLen-8+1) + ":0.0.1/"
exceedUAVer.UserAgent = newUA
// Encode the new UA length as a varint.
var newUAVarIntBuf bytes.Buffer
err := wire.TstWriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA)))
err := WriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA)))
if err != nil {
t.Errorf("WriteVarInt: error %v", err)
}
@ -324,7 +323,7 @@ func TestVersionWireErrors(t *testing.T) {
copy(exceedUAVerEncoded[83+len(newUA):], baseVersionEncoded[97:100])
tests := []struct {
in *wire.MsgVersion // Value to encode
in *MsgVersion // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
@ -353,7 +352,7 @@ func TestVersionWireErrors(t *testing.T) {
// it's optional.
{
baseVersionBIP0037, baseVersionBIP0037Encoded,
wire.BIP0037Version, 101, io.ErrShortWrite, nil,
BIP0037Version, 101, io.ErrShortWrite, nil,
},
// Force error due to user agent too big
{exceedUAVer, exceedUAVerEncoded, pver, newLen, wireErr, wireErr},
@ -370,9 +369,9 @@ func TestVersionWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, "+
"want: %v", i, err, test.writeErr)
@ -381,7 +380,7 @@ func TestVersionWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg wire.MsgVersion
var msg MsgVersion
buf := bytes.NewBuffer(test.buf[0:test.max])
err = msg.BtcDecode(buf, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@ -390,9 +389,9 @@ func TestVersionWireErrors(t *testing.T) {
continue
}
// For errors which are not of type wire.MessageError, check
// them for equality.
if _, ok := err.(*wire.MessageError); !ok {
// For errors which are not of type MessageError, check them for
// equality.
if _, ok := err.(*MessageError); !ok {
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, "+
"want: %v", i, err, test.readErr)
@ -407,13 +406,13 @@ func TestVersionWireErrors(t *testing.T) {
func TestVersionOptionalFields(t *testing.T) {
// onlyRequiredVersion is a version message that only contains the
// required versions and all other values set to their default values.
onlyRequiredVersion := wire.MsgVersion{
onlyRequiredVersion := MsgVersion{
ProtocolVersion: 60002,
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
AddrYou: wire.NetAddress{
AddrYou: NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("192.168.0.1"),
Port: 8333,
},
@ -424,9 +423,9 @@ func TestVersionOptionalFields(t *testing.T) {
// addrMeVersion is a version message that contains all fields through
// the AddrMe field.
addrMeVersion := onlyRequiredVersion
addrMeVersion.AddrMe = wire.NetAddress{
addrMeVersion.AddrMe = NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
}
@ -455,40 +454,40 @@ func TestVersionOptionalFields(t *testing.T) {
copy(lastBlockVersionEncoded, baseVersionEncoded)
tests := []struct {
msg *wire.MsgVersion // Expected message
msg *MsgVersion // Expected message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
{
&onlyRequiredVersion,
onlyRequiredVersionEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
{
&addrMeVersion,
addrMeVersionEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
{
&nonceVersion,
nonceVersionEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
{
&uaVersion,
uaVersionEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
{
&lastBlockVersion,
lastBlockVersionEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
}
for i, test := range tests {
// Decode the message from wire format.
var msg wire.MsgVersion
var msg MsgVersion
rbuf := bytes.NewBuffer(test.buf)
err := msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -504,19 +503,19 @@ func TestVersionOptionalFields(t *testing.T) {
}
// baseVersion is used in the various tests as a baseline MsgVersion.
var baseVersion = &wire.MsgVersion{
var baseVersion = &MsgVersion{
ProtocolVersion: 60002,
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
AddrYou: wire.NetAddress{
AddrYou: NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("192.168.0.1"),
Port: 8333,
},
AddrMe: wire.NetAddress{
AddrMe: NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
},
@ -550,19 +549,19 @@ var baseVersionEncoded = []byte{
// baseVersionBIP0037 is used in the various tests as a baseline MsgVersion for
// BIP0037.
var baseVersionBIP0037 = &wire.MsgVersion{
var baseVersionBIP0037 = &MsgVersion{
ProtocolVersion: 70001,
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
AddrYou: wire.NetAddress{
AddrYou: NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("192.168.0.1"),
Port: 8333,
},
AddrMe: wire.NetAddress{
AddrMe: NetAddress{
Timestamp: time.Time{}, // Zero value -- no timestamp in version
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
},

View file

@ -1,8 +1,8 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
@ -12,7 +12,6 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
)
@ -26,7 +25,7 @@ func TestNetAddress(t *testing.T) {
IP: ip,
Port: port,
}
na, err := wire.NewNetAddress(tcpAddr, 0)
na, err := NewNetAddress(tcpAddr, 0)
if err != nil {
t.Errorf("NewNetAddress: %v", err)
}
@ -43,24 +42,24 @@ func TestNetAddress(t *testing.T) {
t.Errorf("NetNetAddress: wrong services - got %v, want %v",
na.Services, 0)
}
if na.HasService(wire.SFNodeNetwork) {
if na.HasService(SFNodeNetwork) {
t.Errorf("HasService: SFNodeNetwork service is set")
}
// Ensure adding the full service node flag works.
na.AddService(wire.SFNodeNetwork)
if na.Services != wire.SFNodeNetwork {
na.AddService(SFNodeNetwork)
if na.Services != SFNodeNetwork {
t.Errorf("AddService: wrong services - got %v, want %v",
na.Services, wire.SFNodeNetwork)
na.Services, SFNodeNetwork)
}
if !na.HasService(wire.SFNodeNetwork) {
if !na.HasService(SFNodeNetwork) {
t.Errorf("HasService: SFNodeNetwork service not set")
}
// Ensure max payload is expected value for latest protocol version.
pver := wire.ProtocolVersion
pver := ProtocolVersion
wantPayload := uint32(30)
maxPayload := wire.TstMaxNetAddressPayload(wire.ProtocolVersion)
maxPayload := maxNetAddressPayload(ProtocolVersion)
if maxPayload != wantPayload {
t.Errorf("maxNetAddressPayload: wrong max payload length for "+
"protocol version %d - got %v, want %v", pver,
@ -69,9 +68,9 @@ func TestNetAddress(t *testing.T) {
// Protocol version before NetAddressTimeVersion when timestamp was
// added. Ensure max payload is expected value for it.
pver = wire.NetAddressTimeVersion - 1
pver = NetAddressTimeVersion - 1
wantPayload = 26
maxPayload = wire.TstMaxNetAddressPayload(pver)
maxPayload = maxNetAddressPayload(pver)
if maxPayload != wantPayload {
t.Errorf("maxNetAddressPayload: wrong max payload length for "+
"protocol version %d - got %v, want %v", pver,
@ -80,10 +79,10 @@ func TestNetAddress(t *testing.T) {
// Check for expected failure on wrong address type.
udpAddr := &net.UDPAddr{}
_, err = wire.NewNetAddress(udpAddr, 0)
if err != wire.ErrInvalidNetAddr {
_, err = NewNetAddress(udpAddr, 0)
if err != ErrInvalidNetAddr {
t.Errorf("NewNetAddress: expected error not received - "+
"got %v, want %v", err, wire.ErrInvalidNetAddr)
"got %v, want %v", err, ErrInvalidNetAddr)
}
}
@ -91,9 +90,9 @@ func TestNetAddress(t *testing.T) {
// protocol versions and timestamp flag combinations.
func TestNetAddressWire(t *testing.T) {
// baseNetAddr is used in the various tests as a baseline NetAddress.
baseNetAddr := wire.NetAddress{
baseNetAddr := NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
}
@ -121,8 +120,8 @@ func TestNetAddressWire(t *testing.T) {
}
tests := []struct {
in wire.NetAddress // NetAddress to encode
out wire.NetAddress // Expected decoded NetAddress
in NetAddress // NetAddress to encode
out NetAddress // Expected decoded NetAddress
ts bool // Include timestamp?
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
@ -133,7 +132,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddrNoTS,
false,
baseNetAddrNoTSEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Latest protocol version with ts flag.
@ -142,7 +141,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddr,
true,
baseNetAddrEncoded,
wire.ProtocolVersion,
ProtocolVersion,
},
// Protocol version NetAddressTimeVersion without ts flag.
@ -151,7 +150,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddrNoTS,
false,
baseNetAddrNoTSEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion with ts flag.
@ -160,7 +159,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddr,
true,
baseNetAddrEncoded,
wire.NetAddressTimeVersion,
NetAddressTimeVersion,
},
// Protocol version NetAddressTimeVersion-1 without ts flag.
@ -169,7 +168,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddrNoTS,
false,
baseNetAddrNoTSEncoded,
wire.NetAddressTimeVersion - 1,
NetAddressTimeVersion - 1,
},
// Protocol version NetAddressTimeVersion-1 with timestamp.
@ -181,7 +180,7 @@ func TestNetAddressWire(t *testing.T) {
baseNetAddrNoTS,
true,
baseNetAddrNoTSEncoded,
wire.NetAddressTimeVersion - 1,
NetAddressTimeVersion - 1,
},
}
@ -189,7 +188,7 @@ func TestNetAddressWire(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
var buf bytes.Buffer
err := wire.TstWriteNetAddress(&buf, test.pver, &test.in, test.ts)
err := writeNetAddress(&buf, test.pver, &test.in, test.ts)
if err != nil {
t.Errorf("writeNetAddress #%d error %v", i, err)
continue
@ -201,9 +200,9 @@ func TestNetAddressWire(t *testing.T) {
}
// Decode the message from wire format.
var na wire.NetAddress
var na NetAddress
rbuf := bytes.NewReader(test.buf)
err = wire.TstReadNetAddress(rbuf, test.pver, &na, test.ts)
err = readNetAddress(rbuf, test.pver, &na, test.ts)
if err != nil {
t.Errorf("readNetAddress #%d error %v", i, err)
continue
@ -219,19 +218,19 @@ func TestNetAddressWire(t *testing.T) {
// TestNetAddressWireErrors performs negative tests against wire encode and
// decode NetAddress to confirm error paths work correctly.
func TestNetAddressWireErrors(t *testing.T) {
pver := wire.ProtocolVersion
pverNAT := wire.NetAddressTimeVersion - 1
pver := ProtocolVersion
pverNAT := NetAddressTimeVersion - 1
// baseNetAddr is used in the various tests as a baseline NetAddress.
baseNetAddr := wire.NetAddress{
baseNetAddr := NetAddress{
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
Services: wire.SFNodeNetwork,
Services: SFNodeNetwork,
IP: net.ParseIP("127.0.0.1"),
Port: 8333,
}
tests := []struct {
in *wire.NetAddress // Value to encode
in *NetAddress // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
ts bool // Include timestamp flag
@ -274,7 +273,7 @@ func TestNetAddressWireErrors(t *testing.T) {
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := wire.TstWriteNetAddress(w, test.pver, test.in, test.ts)
err := writeNetAddress(w, test.pver, test.in, test.ts)
if err != test.writeErr {
t.Errorf("writeNetAddress #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
@ -282,9 +281,9 @@ func TestNetAddressWireErrors(t *testing.T) {
}
// Decode from wire format.
var na wire.NetAddress
var na NetAddress
r := newFixedReader(test.max, test.buf)
err = wire.TstReadNetAddress(r, test.pver, &na, test.ts)
err = readNetAddress(r, test.pver, &na, test.ts)
if err != test.readErr {
t.Errorf("readNetAddress #%d wrong error got: %v, want: %v",
i, err, test.readErr)

View file

@ -1,25 +1,21 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"testing"
"github.com/btcsuite/btcd/wire"
)
import "testing"
// TestServiceFlagStringer tests the stringized output for service flag types.
func TestServiceFlagStringer(t *testing.T) {
tests := []struct {
in wire.ServiceFlag
in ServiceFlag
want string
}{
{0, "0x0"},
{wire.SFNodeNetwork, "SFNodeNetwork"},
{wire.SFNodeGetUTXO, "SFNodeGetUTXO"},
{wire.SFNodeBloom, "SFNodeBloom"},
{SFNodeNetwork, "SFNodeNetwork"},
{SFNodeGetUTXO, "SFNodeGetUTXO"},
{SFNodeBloom, "SFNodeBloom"},
{0xffffffff, "SFNodeNetwork|SFNodeGetUTXO|SFNodeBloom|0xfffffff8"},
}
@ -37,13 +33,13 @@ func TestServiceFlagStringer(t *testing.T) {
// TestBitcoinNetStringer tests the stringized output for bitcoin net types.
func TestBitcoinNetStringer(t *testing.T) {
tests := []struct {
in wire.BitcoinNet
in BitcoinNet
want string
}{
{wire.MainNet, "MainNet"},
{wire.TestNet, "TestNet"},
{wire.TestNet3, "TestNet3"},
{wire.SimNet, "SimNet"},
{MainNet, "MainNet"},
{TestNet, "TestNet"},
{TestNet3, "TestNet3"},
{SimNet, "SimNet"},
{0xffffffff, "Unknown BitcoinNet (4294967295)"},
}

View file

@ -1,15 +1,13 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire_test
package wire
import (
"bytes"
"encoding/hex"
"testing"
"github.com/btcsuite/btcd/wire"
)
// TestShaHash tests the ShaHash API.
@ -17,7 +15,7 @@ func TestShaHash(t *testing.T) {
// Hash of block 234439.
blockHashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef"
blockHash, err := wire.NewShaHashFromStr(blockHashStr)
blockHash, err := NewShaHashFromStr(blockHashStr)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
@ -30,15 +28,15 @@ func TestShaHash(t *testing.T) {
0xa6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
hash, err := wire.NewShaHash(buf)
hash, err := NewShaHash(buf)
if err != nil {
t.Errorf("NewShaHash: unexpected error %v", err)
}
// Ensure proper size.
if len(hash) != wire.HashSize {
if len(hash) != HashSize {
t.Errorf("NewShaHash: hash length mismatch - got: %v, want: %v",
len(hash), wire.HashSize)
len(hash), HashSize)
}
// Ensure contents match.
@ -70,8 +68,8 @@ func TestShaHash(t *testing.T) {
}
// Invalid size for NewShaHash.
invalidHash := make([]byte, wire.HashSize+1)
_, err = wire.NewShaHash(invalidHash)
invalidHash := make([]byte, HashSize+1)
_, err = NewShaHash(invalidHash)
if err == nil {
t.Errorf("NewShaHash: failed to received expected err - got: nil")
}
@ -81,7 +79,7 @@ func TestShaHash(t *testing.T) {
func TestShaHashString(t *testing.T) {
// Block 100000 hash.
wantStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
hash := wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
hash := ShaHash([HashSize]byte{ // Make go vet happy.
0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39,
0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2,
0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa,
@ -99,7 +97,7 @@ func TestShaHashString(t *testing.T) {
func TestNewShaHashFromStr(t *testing.T) {
tests := []struct {
in string
want wire.ShaHash
want ShaHash
err error
}{
// Genesis hash.
@ -119,14 +117,14 @@ func TestNewShaHashFromStr(t *testing.T) {
// Empty string.
{
"",
wire.ShaHash{},
ShaHash{},
nil,
},
// Single digit hash.
{
"1",
wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
ShaHash([HashSize]byte{ // Make go vet happy.
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -138,7 +136,7 @@ func TestNewShaHashFromStr(t *testing.T) {
// Block 203707 with stripped leading zeros.
{
"3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc",
wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
ShaHash([HashSize]byte{ // Make go vet happy.
0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
@ -150,14 +148,14 @@ func TestNewShaHashFromStr(t *testing.T) {
// Hash string that is too long.
{
"01234567890123456789012345678901234567890123456789012345678912345",
wire.ShaHash{},
wire.ErrHashStrSize,
ShaHash{},
ErrHashStrSize,
},
// Hash string that is contains non-hex chars.
{
"abcdefg",
wire.ShaHash{},
ShaHash{},
hex.InvalidByteError('g'),
},
}
@ -166,7 +164,7 @@ func TestNewShaHashFromStr(t *testing.T) {
unexpectedResultStr := "NewShaHashFromStr #%d got: %v want: %v"
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result, err := wire.NewShaHashFromStr(test.in)
result, err := NewShaHashFromStr(test.in)
if err != test.err {
t.Errorf(unexpectedErrStr, i, err, test.err)
continue