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 // TestNetAddress tests the NetAddress API.
19 func TestNetAddress(t *testing.T) {
20 ip := net.ParseIP("127.0.0.1")
23 // Test NewNetAddress.
24 na := NewNetAddress(&net.TCPAddr{IP: ip, Port: port}, 0)
26 // Ensure we get the same ip, port, and services back out.
28 t.Errorf("NetNetAddress: wrong ip - got %v, want %v", na.IP, ip)
30 if na.Port != uint16(port) {
31 t.Errorf("NetNetAddress: wrong port - got %v, want %v", na.Port,
35 t.Errorf("NetNetAddress: wrong services - got %v, want %v",
38 if na.HasService(SFNodeNetwork) {
39 t.Errorf("HasService: SFNodeNetwork service is set")
42 // Ensure adding the full service node flag works.
43 na.AddService(SFNodeNetwork)
44 if na.Services != SFNodeNetwork {
45 t.Errorf("AddService: wrong services - got %v, want %v",
46 na.Services, SFNodeNetwork)
48 if !na.HasService(SFNodeNetwork) {
49 t.Errorf("HasService: SFNodeNetwork service not set")
52 // Ensure max payload is expected value for latest protocol version.
53 pver := ProtocolVersion
54 wantPayload := uint32(30)
55 maxPayload := maxNetAddressPayload(ProtocolVersion)
56 if maxPayload != wantPayload {
57 t.Errorf("maxNetAddressPayload: wrong max payload length for "+
58 "protocol version %d - got %v, want %v", pver,
59 maxPayload, wantPayload)
62 // Protocol version before NetAddressTimeVersion when timestamp was
63 // added. Ensure max payload is expected value for it.
64 pver = NetAddressTimeVersion - 1
66 maxPayload = maxNetAddressPayload(pver)
67 if maxPayload != wantPayload {
68 t.Errorf("maxNetAddressPayload: wrong max payload length for "+
69 "protocol version %d - got %v, want %v", pver,
70 maxPayload, wantPayload)
74 // TestNetAddressWire tests the NetAddress wire encode and decode for various
75 // protocol versions and timestamp flag combinations.
76 func TestNetAddressWire(t *testing.T) {
77 // baseNetAddr is used in the various tests as a baseline NetAddress.
78 baseNetAddr := NetAddress{
79 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
80 Services: SFNodeNetwork,
81 IP: net.ParseIP("127.0.0.1"),
85 // baseNetAddrNoTS is baseNetAddr with a zero value for the timestamp.
86 baseNetAddrNoTS := baseNetAddr
87 baseNetAddrNoTS.Timestamp = time.Time{}
89 // baseNetAddrEncoded is the wire encoded bytes of baseNetAddr.
90 baseNetAddrEncoded := []byte{
91 0x29, 0xab, 0x5f, 0x49, // Timestamp
92 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
95 0x20, 0x8d, // Port 8333 in big-endian
98 // baseNetAddrNoTSEncoded is the wire encoded bytes of baseNetAddrNoTS.
99 baseNetAddrNoTSEncoded := []byte{
101 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
104 0x20, 0x8d, // Port 8333 in big-endian
108 in NetAddress // NetAddress to encode
109 out NetAddress // Expected decoded NetAddress
110 ts bool // Include timestamp?
111 buf []byte // Wire encoding
112 pver uint32 // Protocol version for wire encoding
114 // Latest protocol version without ts flag.
119 baseNetAddrNoTSEncoded,
123 // Latest protocol version with ts flag.
132 // Protocol version NetAddressTimeVersion without ts flag.
137 baseNetAddrNoTSEncoded,
138 NetAddressTimeVersion,
141 // Protocol version NetAddressTimeVersion with ts flag.
147 NetAddressTimeVersion,
150 // Protocol version NetAddressTimeVersion-1 without ts flag.
155 baseNetAddrNoTSEncoded,
156 NetAddressTimeVersion - 1,
159 // Protocol version NetAddressTimeVersion-1 with timestamp.
160 // Even though the timestamp flag is set, this shouldn't have a
161 // timestamp since it is a protocol version before it was
167 baseNetAddrNoTSEncoded,
168 NetAddressTimeVersion - 1,
172 t.Logf("Running %d tests", len(tests))
173 for i, test := range tests {
174 // Encode to wire format.
176 err := writeNetAddress(&buf, test.pver, &test.in, test.ts)
178 t.Errorf("writeNetAddress #%d error %v", i, err)
181 if !bytes.Equal(buf.Bytes(), test.buf) {
182 t.Errorf("writeNetAddress #%d\n got: %s want: %s", i,
183 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
187 // Decode the message from wire format.
189 rbuf := bytes.NewReader(test.buf)
190 err = readNetAddress(rbuf, test.pver, &na, test.ts)
192 t.Errorf("readNetAddress #%d error %v", i, err)
195 if !reflect.DeepEqual(na, test.out) {
196 t.Errorf("readNetAddress #%d\n got: %s want: %s", i,
197 spew.Sdump(na), spew.Sdump(test.out))
203 // TestNetAddressWireErrors performs negative tests against wire encode and
204 // decode NetAddress to confirm error paths work correctly.
205 func TestNetAddressWireErrors(t *testing.T) {
206 pver := ProtocolVersion
207 pverNAT := NetAddressTimeVersion - 1
209 // baseNetAddr is used in the various tests as a baseline NetAddress.
210 baseNetAddr := NetAddress{
211 Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
212 Services: SFNodeNetwork,
213 IP: net.ParseIP("127.0.0.1"),
218 in *NetAddress // Value to encode
219 buf []byte // Wire encoding
220 pver uint32 // Protocol version for wire encoding
221 ts bool // Include timestamp flag
222 max int // Max size of fixed buffer to induce errors
223 writeErr error // Expected write error
224 readErr error // Expected read error
226 // Latest protocol version with timestamp and intentional
227 // read/write errors.
228 // Force errors on timestamp.
229 {&baseNetAddr, []byte{}, pver, true, 0, io.ErrShortWrite, io.EOF},
230 // Force errors on services.
231 {&baseNetAddr, []byte{}, pver, true, 4, io.ErrShortWrite, io.EOF},
232 // Force errors on ip.
233 {&baseNetAddr, []byte{}, pver, true, 12, io.ErrShortWrite, io.EOF},
234 // Force errors on port.
235 {&baseNetAddr, []byte{}, pver, true, 28, io.ErrShortWrite, io.EOF},
237 // Latest protocol version with no timestamp and intentional
238 // read/write errors.
239 // Force errors on services.
240 {&baseNetAddr, []byte{}, pver, false, 0, io.ErrShortWrite, io.EOF},
241 // Force errors on ip.
242 {&baseNetAddr, []byte{}, pver, false, 8, io.ErrShortWrite, io.EOF},
243 // Force errors on port.
244 {&baseNetAddr, []byte{}, pver, false, 24, io.ErrShortWrite, io.EOF},
246 // Protocol version before NetAddressTimeVersion with timestamp
247 // flag set (should not have timestamp due to old protocol
248 // version) and intentional read/write errors.
249 // Force errors on services.
250 {&baseNetAddr, []byte{}, pverNAT, true, 0, io.ErrShortWrite, io.EOF},
251 // Force errors on ip.
252 {&baseNetAddr, []byte{}, pverNAT, true, 8, io.ErrShortWrite, io.EOF},
253 // Force errors on port.
254 {&baseNetAddr, []byte{}, pverNAT, true, 24, io.ErrShortWrite, io.EOF},
257 t.Logf("Running %d tests", len(tests))
258 for i, test := range tests {
259 // Encode to wire format.
260 w := newFixedWriter(test.max)
261 err := writeNetAddress(w, test.pver, test.in, test.ts)
262 if err != test.writeErr {
263 t.Errorf("writeNetAddress #%d wrong error got: %v, want: %v",
264 i, err, test.writeErr)
268 // Decode from wire format.
270 r := newFixedReader(test.max, test.buf)
271 err = readNetAddress(r, test.pver, &na, test.ts)
272 if err != test.readErr {
273 t.Errorf("readNetAddress #%d wrong error got: %v, want: %v",
274 i, err, test.readErr)