msggetaddr_test.go raw
1 package wire
2
3 import (
4 "bytes"
5 "reflect"
6 "testing"
7
8 "github.com/davecgh/go-spew/spew"
9 )
10
11 // TestGetAddr tests the MsgGetAddr API.
12 func TestGetAddr(t *testing.T) {
13 pver := ProtocolVersion
14 // Ensure the command is expected value.
15 wantCmd := "getaddr"
16 msg := NewMsgGetAddr()
17 if cmd := msg.Command(); cmd != wantCmd {
18 t.Errorf("NewMsgGetAddr: wrong command - got %v want %v",
19 cmd, wantCmd,
20 )
21 }
22 // Ensure max payload is expected value for latest protocol version. Num addresses (varInt) + max allowed addresses.
23 wantPayload := uint32(0)
24 maxPayload := msg.MaxPayloadLength(pver)
25 if maxPayload != wantPayload {
26 t.Errorf("MaxPayloadLength: wrong max payload length for "+
27 "protocol version %d - got %v, want %v", pver,
28 maxPayload, wantPayload,
29 )
30 }
31 }
32
33 // TestGetAddrWire tests the MsgGetAddr wire encode and decode for various protocol versions.
34 func TestGetAddrWire(t *testing.T) {
35 msgGetAddr := NewMsgGetAddr()
36 msgGetAddrEncoded := []byte{}
37 tests := []struct {
38 in *MsgGetAddr // Message to encode
39 out *MsgGetAddr // Expected decoded message
40 buf []byte // Wire encoding
41 pver uint32 // Protocol version for wire encoding
42 enc MessageEncoding // Message encoding variant.
43 }{
44 // Latest protocol version.
45 {
46 msgGetAddr,
47 msgGetAddr,
48 msgGetAddrEncoded,
49 ProtocolVersion,
50 BaseEncoding,
51 },
52 // Protocol version BIP0035Version.
53 {
54 msgGetAddr,
55 msgGetAddr,
56 msgGetAddrEncoded,
57 BIP0035Version,
58 BaseEncoding,
59 },
60 // Protocol version BIP0031Version.
61 {
62 msgGetAddr,
63 msgGetAddr,
64 msgGetAddrEncoded,
65 BIP0031Version,
66 BaseEncoding,
67 },
68 // Protocol version NetAddressTimeVersion.
69 {
70 msgGetAddr,
71 msgGetAddr,
72 msgGetAddrEncoded,
73 NetAddressTimeVersion,
74 BaseEncoding,
75 },
76 // Protocol version MultipleAddressVersion.
77 {
78 msgGetAddr,
79 msgGetAddr,
80 msgGetAddrEncoded,
81 MultipleAddressVersion,
82 BaseEncoding,
83 },
84 }
85 t.Logf("Running %d tests", len(tests))
86 for i, test := range tests {
87 // Encode the message to wire format.
88 var buf bytes.Buffer
89 e := test.in.BtcEncode(&buf, test.pver, test.enc)
90 if e != nil {
91 t.Errorf("BtcEncode #%d error %v", i, e)
92 continue
93 }
94 if !bytes.Equal(buf.Bytes(), test.buf) {
95 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
96 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf),
97 )
98 continue
99 }
100 // Decode the message from wire format.
101 var msg MsgGetAddr
102 rbuf := bytes.NewReader(test.buf)
103 e = msg.BtcDecode(rbuf, test.pver, test.enc)
104 if e != nil {
105 t.Errorf("BtcDecode #%d error %v", i, e)
106 continue
107 }
108 if !reflect.DeepEqual(&msg, test.out) {
109 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
110 spew.Sdump(msg), spew.Sdump(test.out),
111 )
112 continue
113 }
114 }
115 }
116