lbcd/msgverack_test.go
Dave Collins ee46a0b108 Use bytes.NewReader for deserialize when possible.
Rather than using bytes.NewBuffer, which is a read/write entity
(io.ReadWriter), use bytes.NewReader which is only a read entitiy
(io.Reader) in all cases where it is possible.  Benchmarking shows it's
slightly faster and it's also technically more accurate since it ensures
the data is read-only.

There are a few cases where bytes.NewBuffer must still be used since a
buffer with a known length is required for those instances.
2014-06-04 23:39:03 -05:00

121 lines
2.7 KiB
Go

// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcwire_test
import (
"bytes"
"github.com/conformal/btcwire"
"github.com/davecgh/go-spew/spew"
"reflect"
"testing"
)
// TestVerAck tests the MsgVerAck API.
func TestVerAck(t *testing.T) {
pver := btcwire.ProtocolVersion
// Ensure the command is expected value.
wantCmd := "verack"
msg := btcwire.NewMsgVerAck()
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgVerAck: wrong command - got %v want %v",
cmd, wantCmd)
}
// Ensure max payload is expected value.
wantPayload := uint32(0)
maxPayload := msg.MaxPayloadLength(pver)
if maxPayload != wantPayload {
t.Errorf("MaxPayloadLength: wrong max payload length for "+
"protocol version %d - got %v, want %v", pver,
maxPayload, wantPayload)
}
return
}
// TestVerAckWire tests the MsgVerAck wire encode and decode for various
// protocol versions.
func TestVerAckWire(t *testing.T) {
msgVerAck := btcwire.NewMsgVerAck()
msgVerAckEncoded := []byte{}
tests := []struct {
in *btcwire.MsgVerAck // Message to encode
out *btcwire.MsgVerAck // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
{
msgVerAck,
msgVerAck,
msgVerAckEncoded,
btcwire.ProtocolVersion,
},
// Protocol version BIP0035Version.
{
msgVerAck,
msgVerAck,
msgVerAckEncoded,
btcwire.BIP0035Version,
},
// Protocol version BIP0031Version.
{
msgVerAck,
msgVerAck,
msgVerAckEncoded,
btcwire.BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
{
msgVerAck,
msgVerAck,
msgVerAckEncoded,
btcwire.NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
{
msgVerAck,
msgVerAck,
msgVerAckEncoded,
btcwire.MultipleAddressVersion,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode the message to wire format.
var buf bytes.Buffer
err := test.in.BtcEncode(&buf, test.pver)
if err != nil {
t.Errorf("BtcEncode #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
// Decode the message from wire format.
var msg btcwire.MsgVerAck
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
spew.Sdump(msg), spew.Sdump(test.out))
continue
}
}
}