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.
15 "github.com/davecgh/go-spew/spew"
18 // TestAddr tests the MsgAddr API.
19 func TestAddr(t *testing.T) {
20 pver := ProtocolVersion
22 // Ensure the command is expected value.
25 if cmd := msg.Command(); cmd != wantCmd {
26 t.Errorf("NewMsgAddr: wrong command - got %v want %v",
30 // Ensure max payload is expected value for latest protocol version.
31 // Num addresses (varInt) + max allowed addresses.
32 wantPayload := uint32(30009)
33 maxPayload := msg.MaxPayloadLength(pver)
34 if maxPayload != wantPayload {
35 t.Errorf("MaxPayloadLength: wrong max payload length for "+
36 "protocol version %d - got %v, want %v", pver,
37 maxPayload, wantPayload)
40 // Ensure NetAddresses are added properly.
41 tcpAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
42 na := NewNetAddress(tcpAddr, SFNodeNetwork)
43 err := msg.AddAddress(na)
45 t.Errorf("AddAddress: %v", err)
47 if msg.AddrList[0] != na {
48 t.Errorf("AddAddress: wrong address added - got %v, want %v",
49 spew.Sprint(msg.AddrList[0]), spew.Sprint(na))
52 // Ensure the address list is cleared properly.
54 if len(msg.AddrList) != 0 {
55 t.Errorf("ClearAddresses: address list is not empty - "+
56 "got %v [%v], want %v", len(msg.AddrList),
57 spew.Sprint(msg.AddrList[0]), 0)
60 // Ensure adding more than the max allowed addresses per message returns
62 for i := 0; i < MaxAddrPerMsg+1; i++ {
63 err = msg.AddAddress(na)
66 t.Errorf("AddAddress: expected error on too many addresses " +
69 err = msg.AddAddresses(na)
71 t.Errorf("AddAddresses: expected error on too many addresses " +
75 // Ensure max payload is expected value for protocol versions before
76 // timestamp was added to NetAddress.
77 // Num addresses (varInt) + max allowed addresses.
78 pver = NetAddressTimeVersion - 1
79 wantPayload = uint32(26009)
80 maxPayload = msg.MaxPayloadLength(pver)
81 if maxPayload != wantPayload {
82 t.Errorf("MaxPayloadLength: wrong max payload length for "+
83 "protocol version %d - got %v, want %v", pver,
84 maxPayload, wantPayload)
87 // Ensure max payload is expected value for protocol versions before
88 // multiple addresses were allowed.
89 // Num addresses (varInt) + a single net addresses.
90 pver = MultipleAddressVersion - 1
91 wantPayload = uint32(35)
92 maxPayload = msg.MaxPayloadLength(pver)
93 if maxPayload != wantPayload {
94 t.Errorf("MaxPayloadLength: wrong max payload length for "+
95 "protocol version %d - got %v, want %v", pver,
96 maxPayload, wantPayload)
100 // TestAddrWire tests the MsgAddr wire encode and decode for various numbers
101 // of addresses and protocol versions.
102 func TestAddrWire(t *testing.T) {
103 // A couple of NetAddresses to use for testing.
105 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
106 Services: SFNodeNetwork,
107 IP: net.ParseIP("127.0.0.1"),
111 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
112 Services: SFNodeNetwork,
113 IP: net.ParseIP("192.168.0.1"),
117 // Empty address message.
118 noAddr := NewMsgAddr()
119 noAddrEncoded := []byte{
120 0x00, // Varint for number of addresses
123 // Address message with multiple addresses.
124 multiAddr := NewMsgAddr()
125 multiAddr.AddAddresses(na, na2)
126 multiAddrEncoded := []byte{
127 0x02, // Varint for number of addresses
128 0x29, 0xab, 0x5f, 0x49, // Timestamp
129 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
132 0x20, 0x8d, // Port 8333 in big-endian
133 0x29, 0xab, 0x5f, 0x49, // Timestamp
134 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01, // IP 192.168.0.1
137 0x20, 0x8e, // Port 8334 in big-endian
142 in *MsgAddr // Message to encode
143 out *MsgAddr // Expected decoded message
144 buf []byte // Wire encoding
145 pver uint32 // Protocol version for wire encoding
146 enc MessageEncoding // Message encoding format
148 // Latest protocol version with no addresses.
157 // Latest protocol version with multiple addresses.
166 // Protocol version MultipleAddressVersion-1 with no addresses.
171 MultipleAddressVersion - 1,
176 t.Logf("Running %d tests", len(tests))
177 for i, test := range tests {
178 // Encode the message to wire format.
180 err := test.in.BtcEncode(&buf, test.pver, test.enc)
182 t.Errorf("BtcEncode #%d error %v", i, err)
185 if !bytes.Equal(buf.Bytes(), test.buf) {
186 t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
187 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
191 // Decode the message from wire format.
193 rbuf := bytes.NewReader(test.buf)
194 err = msg.BtcDecode(rbuf, test.pver, test.enc)
196 t.Errorf("BtcDecode #%d error %v", i, err)
199 if !reflect.DeepEqual(&msg, test.out) {
200 t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
201 spew.Sdump(msg), spew.Sdump(test.out))
207 // TestAddrWireErrors performs negative tests against wire encode and decode
208 // of MsgAddr to confirm error paths work correctly.
209 func TestAddrWireErrors(t *testing.T) {
210 pver := ProtocolVersion
211 pverMA := MultipleAddressVersion
212 wireErr := &MessageError{}
214 // A couple of NetAddresses to use for testing.
216 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
217 Services: SFNodeNetwork,
218 IP: net.ParseIP("127.0.0.1"),
222 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
223 Services: SFNodeNetwork,
224 IP: net.ParseIP("192.168.0.1"),
228 // Address message with multiple addresses.
229 baseAddr := NewMsgAddr()
230 baseAddr.AddAddresses(na, na2)
231 baseAddrEncoded := []byte{
232 0x02, // Varint for number of addresses
233 0x29, 0xab, 0x5f, 0x49, // Timestamp
234 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
237 0x20, 0x8d, // Port 8333 in big-endian
238 0x29, 0xab, 0x5f, 0x49, // Timestamp
239 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
240 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01, // IP 192.168.0.1
242 0x20, 0x8e, // Port 8334 in big-endian
246 // Message that forces an error by having more than the max allowed
248 maxAddr := NewMsgAddr()
249 for i := 0; i < MaxAddrPerMsg; i++ {
250 maxAddr.AddAddress(na)
252 maxAddr.AddrList = append(maxAddr.AddrList, na)
253 maxAddrEncoded := []byte{
254 0xfd, 0x03, 0xe9, // Varint for number of addresses (1001)
258 in *MsgAddr // Value to encode
259 buf []byte // Wire encoding
260 pver uint32 // Protocol version for wire encoding
261 enc MessageEncoding // Message encoding format
262 max int // Max size of fixed buffer to induce errors
263 writeErr error // Expected write error
264 readErr error // Expected read error
266 // Latest protocol version with intentional read/write errors.
267 // Force error in addresses count
268 {baseAddr, baseAddrEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
269 // Force error in address list.
270 {baseAddr, baseAddrEncoded, pver, BaseEncoding, 1, io.ErrShortWrite, io.EOF},
271 // Force error with greater than max inventory vectors.
272 {maxAddr, maxAddrEncoded, pver, BaseEncoding, 3, wireErr, wireErr},
273 // Force error with greater than max inventory vectors for
274 // protocol versions before multiple addresses were allowed.
275 {maxAddr, maxAddrEncoded, pverMA - 1, BaseEncoding, 3, wireErr, wireErr},
278 t.Logf("Running %d tests", len(tests))
279 for i, test := range tests {
280 // Encode to wire format.
281 w := newFixedWriter(test.max)
282 err := test.in.BtcEncode(w, test.pver, test.enc)
283 if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
284 t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
285 i, err, test.writeErr)
289 // For errors which are not of type MessageError, check them for
291 if _, ok := err.(*MessageError); !ok {
292 if err != test.writeErr {
293 t.Errorf("BtcEncode #%d wrong error got: %v, "+
294 "want: %v", i, err, test.writeErr)
299 // Decode from wire format.
301 r := newFixedReader(test.max, test.buf)
302 err = msg.BtcDecode(r, test.pver, test.enc)
303 if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
304 t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
305 i, err, test.readErr)
309 // For errors which are not of type MessageError, check them for
311 if _, ok := err.(*MessageError); !ok {
312 if err != test.readErr {
313 t.Errorf("BtcDecode #%d wrong error got: %v, "+
314 "want: %v", i, err, test.readErr)