2016-08-08 18:42:54 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-08-08 18:42:54 +02:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-07-03 02:43:33 +02:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestGetAddr tests the MsgGetAddr API.
|
|
|
|
func TestGetAddr(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
pver := ProtocolVersion
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// Ensure the command is expected value.
|
|
|
|
wantCmd := "getaddr"
|
2016-08-08 18:42:54 +02:00
|
|
|
msg := NewMsgGetAddr()
|
2013-05-08 21:31:00 +02:00
|
|
|
if cmd := msg.Command(); cmd != wantCmd {
|
|
|
|
t.Errorf("NewMsgGetAddr: wrong command - got %v want %v",
|
|
|
|
cmd, wantCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure max payload is expected value for latest protocol version.
|
|
|
|
// Num addresses (varInt) + max allowed addresses.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGetAddrWire tests the MsgGetAddr wire encode and decode for various
|
|
|
|
// protocol versions.
|
|
|
|
func TestGetAddrWire(t *testing.T) {
|
2016-08-08 18:42:54 +02:00
|
|
|
msgGetAddr := NewMsgGetAddr()
|
2013-05-08 21:31:00 +02:00
|
|
|
msgGetAddrEncoded := []byte{}
|
|
|
|
|
|
|
|
tests := []struct {
|
2016-10-19 01:21:48 +02:00
|
|
|
in *MsgGetAddr // Message to encode
|
|
|
|
out *MsgGetAddr // Expected decoded message
|
|
|
|
buf []byte // Wire encoding
|
|
|
|
pver uint32 // Protocol version for wire encoding
|
|
|
|
enc MessageEncoding // Message encoding variant.
|
2013-05-08 21:31:00 +02:00
|
|
|
}{
|
|
|
|
// Latest protocol version.
|
|
|
|
{
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
ProtocolVersion,
|
2016-10-19 01:21:48 +02:00
|
|
|
BaseEncoding,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0035Version.
|
|
|
|
{
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
BIP0035Version,
|
2016-10-19 01:21:48 +02:00
|
|
|
BaseEncoding,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version BIP0031Version.
|
|
|
|
{
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
BIP0031Version,
|
2016-10-19 01:21:48 +02:00
|
|
|
BaseEncoding,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version NetAddressTimeVersion.
|
|
|
|
{
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
NetAddressTimeVersion,
|
2016-10-19 01:21:48 +02:00
|
|
|
BaseEncoding,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Protocol version MultipleAddressVersion.
|
|
|
|
{
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddr,
|
|
|
|
msgGetAddrEncoded,
|
2016-08-08 18:42:54 +02:00
|
|
|
MultipleAddressVersion,
|
2016-10-19 01:21:48 +02:00
|
|
|
BaseEncoding,
|
2013-05-08 21:31:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
// Encode the message to wire format.
|
|
|
|
var buf bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
err := test.in.BtcEncode(&buf, test.pver, test.enc)
|
2013-05-08 21:31:00 +02:00
|
|
|
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.
|
2016-08-08 18:42:54 +02:00
|
|
|
var msg MsgGetAddr
|
2014-06-05 06:39:03 +02:00
|
|
|
rbuf := bytes.NewReader(test.buf)
|
2016-10-19 01:21:48 +02:00
|
|
|
err = msg.BtcDecode(rbuf, test.pver, test.enc)
|
2013-05-08 21:31:00 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|