netaddressv2_test.go raw
1 package wire
2
3 import (
4 "bytes"
5 "io"
6 "testing"
7 "time"
8 )
9
10 // TestIsIPv4Mapped tests that isIPv4Mapped correctly identifies IPv4-mapped
11 // IPv6 addresses.
12 func TestIsIPv4Mapped(t *testing.T) {
13 tests := []struct {
14 name string
15 addr []byte
16 expected bool
17 }{
18 {
19 name: "IPv4-mapped ::ffff:192.0.2.1",
20 addr: []byte{
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22 0x00, 0x00, 0xff, 0xff, 0xc0, 0x00, 0x02, 0x01,
23 },
24 expected: true,
25 },
26 {
27 name: "IPv4-mapped ::ffff:127.0.0.1",
28 addr: []byte{
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01,
31 },
32 expected: true,
33 },
34 {
35 name: "Regular IPv6",
36 addr: []byte{
37 0x20, 0x01, 0x09, 0xe8, 0x26, 0x15, 0x73, 0x00,
38 0x09, 0x54, 0x12, 0x63, 0xef, 0xc8, 0x2e, 0x34,
39 },
40 expected: false,
41 },
42 {
43 name: "OnionCat Tor address",
44 addr: []byte{
45 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43, 0xff, 0xfe,
46 0xcc, 0x39, 0xa8, 0x73, 0x69, 0x15, 0xff, 0xff,
47 },
48 expected: false,
49 },
50 {
51 name: "Short address (IPv4)",
52 addr: []byte{0x7f, 0x00, 0x00, 0x01},
53 expected: false,
54 },
55 {
56 name: "Empty address",
57 addr: []byte{},
58 expected: false,
59 },
60 }
61
62 for _, test := range tests {
63 t.Run(test.name, func(t *testing.T) {
64 result := isIPv4Mapped(test.addr)
65 if result != test.expected {
66 t.Errorf("isIPv4Mapped(%v) = %v, want %v",
67 test.addr, result, test.expected)
68 }
69 })
70 }
71 }
72
73 // TestNetAddressV2FromBytes tests that NetAddressV2FromBytes works as
74 // expected.
75 func TestNetAddressV2FromBytes(t *testing.T) {
76 tests := []struct {
77 addrBytes []byte
78 expectedString string
79 expectedNetwork string
80 }{
81 // Ipv4 encoding
82 {
83 []byte{0x7f, 0x00, 0x00, 0x01},
84 "127.0.0.1",
85 string(ipv4),
86 },
87
88 // Ipv6 encoding
89 {
90 []byte{
91 0x20, 0x01, 0x09, 0xe8, 0x26, 0x15, 0x73, 0x00,
92 0x09, 0x54, 0x12, 0x63, 0xef, 0xc8, 0x2e, 0x34,
93 },
94 "2001:9e8:2615:7300:954:1263:efc8:2e34",
95 string(ipv6),
96 },
97
98 // IPv4-mapped IPv6 encoding (::ffff:127.0.0.1) should be
99 // converted to IPv4.
100 {
101 []byte{
102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01,
104 },
105 "127.0.0.1",
106 string(ipv4),
107 },
108
109 // OnionCat encoding
110 {
111 []byte{
112 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43, 0xff, 0xfe,
113 0xcc, 0x39, 0xa8, 0x73, 0x69, 0x15, 0xff, 0xff,
114 },
115 "777myonionurl777.onion",
116 string(torv2),
117 },
118
119 // Torv2 encoding
120 {
121 []byte{
122 0xff, 0xfe, 0xcc, 0x39, 0xa8, 0x73, 0x69, 0x15,
123 0xff, 0xff,
124 },
125 "777myonionurl777.onion",
126 string(torv2),
127 },
128
129 // Torv3 encoding
130 {
131 []byte{
132 0xca, 0xd2, 0xd3, 0xc8, 0xdc, 0x9c, 0xc4, 0xd3,
133 0x70, 0x33, 0x30, 0xc5, 0x23, 0xaf, 0x02, 0xed,
134 0xc4, 0x9d, 0xf8, 0xc6, 0xb0, 0x4e, 0x74, 0x6d,
135 0x3b, 0x51, 0x57, 0xa7, 0x15, 0xfe, 0x98, 0x35,
136 },
137 "zljnhsg4ttcng4btgdcshlyc5xcj36ggwbhhi3j3kfl2ofp6ta26jlid.onion",
138 string(torv3),
139 },
140 }
141
142 t.Logf("Running %d tests", len(tests))
143 for i, test := range tests {
144 na := NetAddressV2FromBytes(time.Time{}, 0, test.addrBytes, 0)
145
146 if test.expectedNetwork != string(torv3) {
147 if na.ToLegacy() == nil {
148 t.Errorf("Test #%d has nil legacy encoding", i)
149 }
150 } else {
151 if !na.IsTorV3() {
152 t.Errorf("Test #%d is not torv3 address", i)
153 }
154 }
155
156 if na.Addr.String() != test.expectedString {
157 t.Errorf("Test #%d did not match expected string", i)
158 }
159
160 if na.Addr.Network() != test.expectedNetwork {
161 t.Errorf("Test #%d did not match expected network", i)
162 }
163
164 var b bytes.Buffer
165 if err := writeNetAddressV2(&b, 0, na); err != nil {
166 t.Errorf("Test #%d failed writing address %v", i, err)
167 }
168
169 // Assert that the written netID is equivalent to the above.
170 if string(b.Bytes()[5]) != test.expectedNetwork {
171 t.Errorf("Test #%d did not match expected network", i)
172 }
173 }
174 }
175
176 // TestReadNetAddressV2 tests that readNetAddressV2 behaves as expected in
177 // different scenarios.
178 func TestReadNetAddressV2(t *testing.T) {
179 tests := []struct {
180 buf []byte
181 expectedNetwork string
182 expectedError error
183 }{
184 // Invalid address size for unknown netID.
185 {
186 []byte{
187 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfd, 0xff,
188 0xff,
189 },
190 "",
191 ErrInvalidAddressSize,
192 },
193
194 // Valid address size for unknown netID.
195 {
196 []byte{
197 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x20, 0x10,
198 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
199 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
200 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
201 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22,
202 0x22,
203 },
204 "",
205 ErrSkippedNetworkID,
206 },
207
208 // Invalid ipv4 size.
209 {
210 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05},
211 "",
212 ErrInvalidAddressSize,
213 },
214
215 // Valid ipv4 encoding.
216 {
217 []byte{
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x7f,
219 0x00, 0x00, 0x01, 0x22, 0x22,
220 },
221 string(ipv4),
222 nil,
223 },
224
225 // Invalid ipv6 size.
226 {
227 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfc},
228 "",
229 ErrInvalidAddressSize,
230 },
231
232 // OnionCat encoding is skipped.
233 {
234 []byte{
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0xfd,
236 0x87, 0xd8, 0x7e, 0xeb, 0x43, 0xff, 0xfe, 0xcc,
237 0x39, 0xa8, 0x73, 0x69, 0x15, 0xff, 0xff, 0x22,
238 0x22,
239 },
240 "",
241 ErrSkippedNetworkID,
242 },
243
244 // IPv4-mapped IPv6 encoding is skipped (::ffff:192.0.2.1).
245 {
246 []byte{
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00,
248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249 0x00, 0xff, 0xff, 0xc0, 0x00, 0x02, 0x01, 0x22,
250 0x22,
251 },
252 "",
253 ErrSkippedNetworkID,
254 },
255
256 // Valid ipv6 encoding.
257 {
258 []byte{
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x10, 0xff,
260 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
261 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x22,
262 0x22,
263 },
264 string(ipv6),
265 nil,
266 },
267
268 // Invalid torv2 size.
269 {
270 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x02},
271 "",
272 ErrInvalidAddressSize,
273 },
274
275 // Valid torv2 encoding.
276 {
277 []byte{
278 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0a, 0x20,
279 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
280 0x20, 0x22, 0x22,
281 },
282 string(torv2),
283 nil,
284 },
285
286 // Invalid torv3 size.
287 {
288 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x02},
289 "",
290 ErrInvalidAddressSize,
291 },
292
293 // Valid torv3 encoding.
294 {
295 []byte{
296 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x20, 0x10,
297 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
298 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
299 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
300 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22,
301 0x22,
302 },
303 string(torv3),
304 nil,
305 },
306
307 // Invalid i2p size.
308 {
309 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x02},
310 "",
311 ErrInvalidAddressSize,
312 },
313
314 // Valid i2p encoding.
315 {
316 []byte{
317 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x10,
318 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
319 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
320 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
321 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x22,
322 0x22,
323 },
324 string(i2p),
325 ErrSkippedNetworkID,
326 },
327
328 // Invalid cjdns size.
329 {
330 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x02},
331 "",
332 ErrInvalidAddressSize,
333 },
334
335 // Valid cjdns encoding.
336 {
337 []byte{
338 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x20,
339 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
340 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x22,
341 0x22,
342 },
343 string(cjdns),
344 ErrSkippedNetworkID,
345 },
346 }
347
348 t.Logf("Running %d tests", len(tests))
349 for i, test := range tests {
350 r := bytes.NewReader(test.buf)
351 na := &NetAddressV2{}
352
353 err := readNetAddressV2(r, 0, na)
354 if err != test.expectedError {
355 t.Errorf("Test #%d had unexpected error %v", i, err)
356 } else if err != nil {
357 continue
358 }
359
360 // Trying to read more should give EOF.
361 var b [1]byte
362 if _, err := r.Read(b[:]); err != io.EOF {
363 t.Errorf("Test #%d did not cleanly finish reading", i)
364 }
365
366 if na.Addr.Network() != test.expectedNetwork {
367 t.Errorf("Test #%d had unexpected network %v", i,
368 na.Addr.Network())
369 }
370 }
371 }
372