netaddress_test.go raw
1 package wire
2
3 import (
4 "net"
5 "testing"
6 )
7
8 // TestNetAddress tests the NetAddress API.
9 func TestNetAddress(t *testing.T) {
10 ip := net.ParseIP("127.0.0.1")
11 port := 11047
12 // Test NewNetAddress.
13 na := NewNetAddress(&net.TCPAddr{IP: ip, Port: port}, 0)
14 // Ensure we get the same ip, port, and services back out.
15 if !na.IP.Equal(ip) {
16 t.Errorf("NetNetAddress: wrong ip - got %v, want %v", na.IP, ip)
17 }
18 if na.Port != uint16(port) {
19 t.Errorf("NetNetAddress: wrong port - got %v, want %v", na.Port,
20 port,
21 )
22 }
23 if na.Services != 0 {
24 t.Errorf("NetNetAddress: wrong services - got %v, want %v",
25 na.Services, 0,
26 )
27 }
28 if na.HasService(SFNodeNetwork) {
29 t.Errorf("HasService: SFNodeNetwork service is set")
30 }
31 // Ensure adding the full service node flag works.
32 na.AddService(SFNodeNetwork)
33 if na.Services != SFNodeNetwork {
34 t.Errorf("AddService: wrong services - got %v, want %v",
35 na.Services, SFNodeNetwork,
36 )
37 }
38 if !na.HasService(SFNodeNetwork) {
39 t.Errorf("HasService: SFNodeNetwork service not set")
40 }
41 // Ensure max payload is expected value for latest protocol version.
42 pver := ProtocolVersion
43 wantPayload := uint32(30)
44 maxPayload := maxNetAddressPayload(ProtocolVersion)
45 if maxPayload != wantPayload {
46 t.Errorf("maxNetAddressPayload: wrong max payload length for "+
47 "protocol version %d - got %v, want %v", pver,
48 maxPayload, wantPayload,
49 )
50 }
51 // Protocol version before NetAddressTimeVersion when timestamp was added. Ensure max payload is expected value for
52 // it.
53 pver = NetAddressTimeVersion - 1
54 wantPayload = 26
55 maxPayload = maxNetAddressPayload(pver)
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,
60 )
61 }
62 }
63
64 // // TestNetAddressWire tests the NetAddress wire encode and decode for various protocol versions and timestamp flag combinations.
65 // func TestNetAddressWire(// t *testing.T) {
66 // // baseNetAddr is used in the various tests as a baseline NetAddress.
67 // baseNetAddr := NetAddress{
68 // Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
69 // Services: SFNodeNetwork,
70 // IP: net.ParseIP("127.0.0.1"),
71 // Port: 11047,
72 // }
73 // // baseNetAddrNoTS is baseNetAddr with a zero value for the timestamp.
74 // baseNetAddrNoTS := baseNetAddr
75 // baseNetAddrNoTS.Timestamp = time.Time{}
76 // // baseNetAddrEncoded is the wire encoded bytes of baseNetAddr.
77 // baseNetAddrEncoded := []byte{
78 // 0x29, 0xab, 0x5f, 0x49, // Timestamp
79 // 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
80 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 // 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
82 // 0x2b, 0x87, // Port 11047 in big-endian
83 // }
84 // // baseNetAddrNoTSEncoded is the wire encoded bytes of baseNetAddrNoTS.
85 // baseNetAddrNoTSEncoded := []byte{
86 // // No timestamp
87 // 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
88 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 // 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
90 // 0x2b, 0x27, // Port 11047 in big-endian
91 // }
92 // tests := []struct {
93 // in NetAddress // NetAddress to encode
94 // out NetAddress // Expected decoded NetAddress
95 // ts bool // Include timestamp?
96 // buf []byte // Wire encoding
97 // pver uint32 // Protocol version for wire encoding
98 // }{
99 // // Latest protocol version without ts flag.
100 // {
101 // baseNetAddr,
102 // baseNetAddrNoTS,
103 // false,
104 // baseNetAddrNoTSEncoded,
105 // ProtocolVersion,
106 // },
107 // // Latest protocol version with ts flag.
108 // {
109 // baseNetAddr,
110 // baseNetAddr,
111 // true,
112 // baseNetAddrEncoded,
113 // ProtocolVersion,
114 // },
115 // // Protocol version NetAddressTimeVersion without ts flag.
116 // {
117 // baseNetAddr,
118 // baseNetAddrNoTS,
119 // false,
120 // baseNetAddrNoTSEncoded,
121 // NetAddressTimeVersion,
122 // },
123 // // Protocol version NetAddressTimeVersion with ts flag.
124 // {
125 // baseNetAddr,
126 // baseNetAddr,
127 // true,
128 // baseNetAddrEncoded,
129 // NetAddressTimeVersion,
130 // },
131 // // Protocol version NetAddressTimeVersion-1 without ts flag.
132 // {
133 // baseNetAddr,
134 // baseNetAddrNoTS,
135 // false,
136 // baseNetAddrNoTSEncoded,
137 // NetAddressTimeVersion - 1,
138 // },
139 // // Protocol version NetAddressTimeVersion-1 with timestamp. Even though the timestamp flag is set, this shouldn't have a timestamp since it is a protocol version before it was added.
140 // {
141 // baseNetAddr,
142 // baseNetAddrNoTS,
143 // true,
144 // baseNetAddrNoTSEncoded,
145 // NetAddressTimeVersion - 1,
146 // },
147 // }
148 // t.Logf("Running %d tests", len(tests))
149 // for i, test := range tests {
150 // // Encode to wire format.
151 // var buf bytes.Buffer
152 // e := writeNetAddress(&buf, test.pver, &test.in, test.ts)
153 // if e != nil {
154 // t.Errorf("writeNetAddress #%d error %v", i, err)
155 // continue
156 // }
157 // if !bytes.Equal(buf.Hash(), test.buf) {
158 // t.Errorf("writeNetAddress #%d\n got: %s want: %s", i,
159 // spew.Sdump(buf.Hash()), spew.Sdump(test.buf))
160 // continue
161 // }
162 // // Decode the message from wire format.
163 // var na NetAddress
164 // rbuf := bytes.NewReader(test.buf)
165 // e = readNetAddress(rbuf, test.pver, &na, test.ts)
166 // if e != nil {
167 // t.Errorf("readNetAddress #%d error %v", i, err)
168 // continue
169 // }
170 // if !reflect.DeepEqual(na, test.out) {
171 // t.Errorf("readNetAddress #%d\n got: %s want: %s", i,
172 // spew.Sdump(na), spew.Sdump(test.out))
173 // continue
174 // }
175 // }
176 // }
177
178 // // TestNetAddressWireErrors performs negative tests against wire encode and decode NetAddress to confirm error paths work correctly.
179 // func TestNetAddressWireErrors(// t *testing.T) {
180 // pver := ProtocolVersion
181 // pverNAT := NetAddressTimeVersion - 1
182 // // baseNetAddr is used in the various tests as a baseline NetAddress.
183 // baseNetAddr := NetAddress{
184 // Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
185 // Services: SFNodeNetwork,
186 // IP: net.ParseIP("127.0.0.1"),
187 // Port: 11047,
188 // }
189 // tests := []struct {
190 // in *NetAddress // value to encode
191 // buf []byte // Wire encoding
192 // pver uint32 // Protocol version for wire encoding
193 // ts bool // Include timestamp flag
194 // max int // Max size of fixed buffer to induce errors
195 // writeErr error // Expected write error
196 // readErr error // Expected read error
197 // }{
198 // // Latest protocol version with timestamp and intentional read/write errors. Force errors on timestamp.
199 // {&baseNetAddr, []byte{}, pver, true, 0, io.ErrShortWrite, io.EOF},
200 // // Force errors on services.
201 // {&baseNetAddr, []byte{}, pver, true, 4, io.ErrShortWrite, io.EOF},
202 // // Force errors on ip.
203 // {&baseNetAddr, []byte{}, pver, true, 12, io.ErrShortWrite, io.EOF},
204 // // Force errors on port.
205 // {&baseNetAddr, []byte{}, pver, true, 28, io.ErrShortWrite, io.EOF},
206 // // Latest protocol version with no timestamp and intentional read/write errors. Force errors on services.
207 // {&baseNetAddr, []byte{}, pver, false, 0, io.ErrShortWrite, io.EOF},
208 // // Force errors on ip.
209 // {&baseNetAddr, []byte{}, pver, false, 8, io.ErrShortWrite, io.EOF},
210 // // Force errors on port.
211 // {&baseNetAddr, []byte{}, pver, false, 24, io.ErrShortWrite, io.EOF},
212 // // Protocol version before NetAddressTimeVersion with timestamp flag set (should not have timestamp due to old protocol version) and intentional read/write errors. Force errors on services.
213 // {&baseNetAddr, []byte{}, pverNAT, true, 0, io.ErrShortWrite, io.EOF},
214 // // Force errors on ip.
215 // {&baseNetAddr, []byte{}, pverNAT, true, 8, io.ErrShortWrite, io.EOF},
216 // // Force errors on port.
217 // {&baseNetAddr, []byte{}, pverNAT, true, 24, io.ErrShortWrite, io.EOF},
218 // }
219 // t.Logf("Running %d tests", len(tests))
220 // for i, test := range tests {
221 // // Encode to wire format.
222 // w := newFixedWriter(test.max)
223 // e := writeNetAddress(w, test.pver, test.in, test.ts)
224 // if err != test.writeErr {
225 // t.Errorf("writeNetAddress #%d wrong error got: %v, want: %v",
226 // i, e, test.writeErr)
227 // continue
228 // }
229 // // Decode from wire format.
230 // var na NetAddress
231 // r := newFixedReader(test.max, test.buf)
232 // e = readNetAddress(r, test.pver, &na, test.ts)
233 // if err != test.readErr {
234 // t.Errorf("readNetAddress #%d wrong error got: %v, want: %v",
235 // i, e, test.readErr)
236 // continue
237 // }
238 // }
239 // }
240