1 // Copyright (c) 2013-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
12 "github.com/davecgh/go-spew/spew"
15 // TestGetAddr tests the MsgGetAddr API.
16 func TestGetAddr(t *testing.T) {
17 pver := ProtocolVersion
19 // Ensure the command is expected value.
21 msg := NewMsgGetAddr()
22 if cmd := msg.Command(); cmd != wantCmd {
23 t.Errorf("NewMsgGetAddr: wrong command - got %v want %v",
27 // Ensure max payload is expected value for latest protocol version.
28 // Num addresses (varInt) + max allowed addresses.
29 wantPayload := uint32(0)
30 maxPayload := msg.MaxPayloadLength(pver)
31 if maxPayload != wantPayload {
32 t.Errorf("MaxPayloadLength: wrong max payload length for "+
33 "protocol version %d - got %v, want %v", pver,
34 maxPayload, wantPayload)
38 // TestGetAddrWire tests the MsgGetAddr wire encode and decode for various
40 func TestGetAddrWire(t *testing.T) {
41 msgGetAddr := NewMsgGetAddr()
42 msgGetAddrEncoded := []byte{}
45 in *MsgGetAddr // Message to encode
46 out *MsgGetAddr // Expected decoded message
47 buf []byte // Wire encoding
48 pver uint32 // Protocol version for wire encoding
49 enc MessageEncoding // Message encoding variant.
51 // Latest protocol version.
60 // Protocol version BIP0035Version.
69 // Protocol version BIP0031Version.
78 // Protocol version NetAddressTimeVersion.
83 NetAddressTimeVersion,
87 // Protocol version MultipleAddressVersion.
92 MultipleAddressVersion,
97 t.Logf("Running %d tests", len(tests))
98 for i, test := range tests {
99 // Encode the message to wire format.
101 err := test.in.BtcEncode(&buf, test.pver, test.enc)
103 t.Errorf("BtcEncode #%d error %v", i, err)
106 if !bytes.Equal(buf.Bytes(), test.buf) {
107 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
108 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
112 // Decode the message from wire format.
114 rbuf := bytes.NewReader(test.buf)
115 err = msg.BtcDecode(rbuf, test.pver, test.enc)
117 t.Errorf("BtcDecode #%d error %v", i, err)
120 if !reflect.DeepEqual(&msg, test.out) {
121 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
122 spew.Sdump(msg), spew.Sdump(test.out))