1 // Copyright (c) 2012-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "test/test_bitcoin.h"
7 #include <boost/test/unit_test.hpp>
15 class CAddrManTest
: public CAddrMan
25 //! Ensure that bucket placement is always the same for testing purposes.
26 void MakeDeterministic()
29 insecure_rand
= FastRandomContext(true);
32 int RandomInt(int nMax
)
34 state
= (CHashWriter(SER_GETHASH
, 0) << state
).GetHash().GetCheapHash();
35 return (unsigned int)(state
% nMax
);
38 CAddrInfo
* Find(const CNetAddr
& addr
, int* pnId
= NULL
)
40 return CAddrMan::Find(addr
, pnId
);
43 CAddrInfo
* Create(const CAddress
& addr
, const CNetAddr
& addrSource
, int* pnId
= NULL
)
45 return CAddrMan::Create(addr
, addrSource
, pnId
);
50 CAddrMan::Delete(nId
);
54 static CNetAddr
ResolveIP(const char* ip
)
57 BOOST_CHECK_MESSAGE(LookupHost(ip
, addr
, false), strprintf("failed to resolve: %s", ip
));
61 static CNetAddr
ResolveIP(std::string ip
)
63 return ResolveIP(ip
.c_str());
66 static CService
ResolveService(const char* ip
, int port
= 0)
69 BOOST_CHECK_MESSAGE(Lookup(ip
, serv
, port
, false), strprintf("failed to resolve: %s:%i", ip
, port
));
73 static CService
ResolveService(std::string ip
, int port
= 0)
75 return ResolveService(ip
.c_str(), port
);
78 BOOST_FIXTURE_TEST_SUITE(addrman_tests
, BasicTestingSetup
)
80 BOOST_AUTO_TEST_CASE(addrman_simple
)
84 // Set addrman addr placement to be deterministic.
85 addrman
.MakeDeterministic();
87 CNetAddr source
= ResolveIP("252.2.2.2");
89 // Test 1: Does Addrman respond correctly when empty.
90 BOOST_CHECK(addrman
.size() == 0);
91 CAddrInfo addr_null
= addrman
.Select();
92 BOOST_CHECK(addr_null
.ToString() == "[::]:0");
94 // Test 2: Does Addrman::Add work as expected.
95 CService addr1
= ResolveService("250.1.1.1", 8333);
96 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
97 BOOST_CHECK(addrman
.size() == 1);
98 CAddrInfo addr_ret1
= addrman
.Select();
99 BOOST_CHECK(addr_ret1
.ToString() == "250.1.1.1:8333");
101 // Test 3: Does IP address deduplication work correctly.
102 // Expected dup IP should not be added.
103 CService addr1_dup
= ResolveService("250.1.1.1", 8333);
104 addrman
.Add(CAddress(addr1_dup
, NODE_NONE
), source
);
105 BOOST_CHECK(addrman
.size() == 1);
108 // Test 5: New table has one addr and we add a diff addr we should
110 CService addr2
= ResolveService("250.1.1.2", 8333);
111 addrman
.Add(CAddress(addr2
, NODE_NONE
), source
);
112 BOOST_CHECK(addrman
.size() == 2);
114 // Test 6: AddrMan::Clear() should empty the new table.
116 BOOST_CHECK(addrman
.size() == 0);
117 CAddrInfo addr_null2
= addrman
.Select();
118 BOOST_CHECK(addr_null2
.ToString() == "[::]:0");
121 BOOST_AUTO_TEST_CASE(addrman_ports
)
123 CAddrManTest addrman
;
125 // Set addrman addr placement to be deterministic.
126 addrman
.MakeDeterministic();
128 CNetAddr source
= ResolveIP("252.2.2.2");
130 BOOST_CHECK(addrman
.size() == 0);
132 // Test 7; Addr with same IP but diff port does not replace existing addr.
133 CService addr1
= ResolveService("250.1.1.1", 8333);
134 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
135 BOOST_CHECK(addrman
.size() == 1);
137 CService addr1_port
= ResolveService("250.1.1.1", 8334);
138 addrman
.Add(CAddress(addr1_port
, NODE_NONE
), source
);
139 BOOST_CHECK(addrman
.size() == 1);
140 CAddrInfo addr_ret2
= addrman
.Select();
141 BOOST_CHECK(addr_ret2
.ToString() == "250.1.1.1:8333");
143 // Test 8: Add same IP but diff port to tried table, it doesn't get added.
144 // Perhaps this is not ideal behavior but it is the current behavior.
145 addrman
.Good(CAddress(addr1_port
, NODE_NONE
));
146 BOOST_CHECK(addrman
.size() == 1);
148 CAddrInfo addr_ret3
= addrman
.Select(newOnly
);
149 BOOST_CHECK(addr_ret3
.ToString() == "250.1.1.1:8333");
153 BOOST_AUTO_TEST_CASE(addrman_select
)
155 CAddrManTest addrman
;
157 // Set addrman addr placement to be deterministic.
158 addrman
.MakeDeterministic();
160 CNetAddr source
= ResolveIP("252.2.2.2");
162 // Test 9: Select from new with 1 addr in new.
163 CService addr1
= ResolveService("250.1.1.1", 8333);
164 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
165 BOOST_CHECK(addrman
.size() == 1);
168 CAddrInfo addr_ret1
= addrman
.Select(newOnly
);
169 BOOST_CHECK(addr_ret1
.ToString() == "250.1.1.1:8333");
171 // Test 10: move addr to tried, select from new expected nothing returned.
172 addrman
.Good(CAddress(addr1
, NODE_NONE
));
173 BOOST_CHECK(addrman
.size() == 1);
174 CAddrInfo addr_ret2
= addrman
.Select(newOnly
);
175 BOOST_CHECK(addr_ret2
.ToString() == "[::]:0");
177 CAddrInfo addr_ret3
= addrman
.Select();
178 BOOST_CHECK(addr_ret3
.ToString() == "250.1.1.1:8333");
180 BOOST_CHECK(addrman
.size() == 1);
183 // Add three addresses to new table.
184 CService addr2
= ResolveService("250.3.1.1", 8333);
185 CService addr3
= ResolveService("250.3.2.2", 9999);
186 CService addr4
= ResolveService("250.3.3.3", 9999);
188 addrman
.Add(CAddress(addr2
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
189 addrman
.Add(CAddress(addr3
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
190 addrman
.Add(CAddress(addr4
, NODE_NONE
), ResolveService("250.4.1.1", 8333));
192 // Add three addresses to tried table.
193 CService addr5
= ResolveService("250.4.4.4", 8333);
194 CService addr6
= ResolveService("250.4.5.5", 7777);
195 CService addr7
= ResolveService("250.4.6.6", 8333);
197 addrman
.Add(CAddress(addr5
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
198 addrman
.Good(CAddress(addr5
, NODE_NONE
));
199 addrman
.Add(CAddress(addr6
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
200 addrman
.Good(CAddress(addr6
, NODE_NONE
));
201 addrman
.Add(CAddress(addr7
, NODE_NONE
), ResolveService("250.1.1.3", 8333));
202 addrman
.Good(CAddress(addr7
, NODE_NONE
));
204 // Test 11: 6 addrs + 1 addr from last test = 7.
205 BOOST_CHECK(addrman
.size() == 7);
207 // Test 12: Select pulls from new and tried regardless of port number.
208 BOOST_CHECK(addrman
.Select().ToString() == "250.4.6.6:8333");
209 BOOST_CHECK(addrman
.Select().ToString() == "250.3.2.2:9999");
210 BOOST_CHECK(addrman
.Select().ToString() == "250.3.3.3:9999");
211 BOOST_CHECK(addrman
.Select().ToString() == "250.4.4.4:8333");
214 BOOST_AUTO_TEST_CASE(addrman_new_collisions
)
216 CAddrManTest addrman
;
218 // Set addrman addr placement to be deterministic.
219 addrman
.MakeDeterministic();
221 CNetAddr source
= ResolveIP("252.2.2.2");
223 BOOST_CHECK(addrman
.size() == 0);
225 for (unsigned int i
= 1; i
< 18; i
++) {
226 CService addr
= ResolveService("250.1.1." + boost::to_string(i
));
227 addrman
.Add(CAddress(addr
, NODE_NONE
), source
);
229 //Test 13: No collision in new table yet.
230 BOOST_CHECK(addrman
.size() == i
);
233 //Test 14: new table collision!
234 CService addr1
= ResolveService("250.1.1.18");
235 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
236 BOOST_CHECK(addrman
.size() == 17);
238 CService addr2
= ResolveService("250.1.1.19");
239 addrman
.Add(CAddress(addr2
, NODE_NONE
), source
);
240 BOOST_CHECK(addrman
.size() == 18);
243 BOOST_AUTO_TEST_CASE(addrman_tried_collisions
)
245 CAddrManTest addrman
;
247 // Set addrman addr placement to be deterministic.
248 addrman
.MakeDeterministic();
250 CNetAddr source
= ResolveIP("252.2.2.2");
252 BOOST_CHECK(addrman
.size() == 0);
254 for (unsigned int i
= 1; i
< 80; i
++) {
255 CService addr
= ResolveService("250.1.1." + boost::to_string(i
));
256 addrman
.Add(CAddress(addr
, NODE_NONE
), source
);
257 addrman
.Good(CAddress(addr
, NODE_NONE
));
259 //Test 15: No collision in tried table yet.
260 BOOST_CHECK_EQUAL(addrman
.size(), i
);
263 //Test 16: tried table collision!
264 CService addr1
= ResolveService("250.1.1.80");
265 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
266 BOOST_CHECK(addrman
.size() == 79);
268 CService addr2
= ResolveService("250.1.1.81");
269 addrman
.Add(CAddress(addr2
, NODE_NONE
), source
);
270 BOOST_CHECK(addrman
.size() == 80);
273 BOOST_AUTO_TEST_CASE(addrman_find
)
275 CAddrManTest addrman
;
277 // Set addrman addr placement to be deterministic.
278 addrman
.MakeDeterministic();
280 BOOST_CHECK(addrman
.size() == 0);
282 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
283 CAddress addr2
= CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE
);
284 CAddress addr3
= CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE
);
286 CNetAddr source1
= ResolveIP("250.1.2.1");
287 CNetAddr source2
= ResolveIP("250.1.2.2");
289 addrman
.Add(addr1
, source1
);
290 addrman
.Add(addr2
, source2
);
291 addrman
.Add(addr3
, source1
);
293 // Test 17: ensure Find returns an IP matching what we searched on.
294 CAddrInfo
* info1
= addrman
.Find(addr1
);
297 BOOST_CHECK(info1
->ToString() == "250.1.2.1:8333");
299 // Test 18; Find does not discriminate by port number.
300 CAddrInfo
* info2
= addrman
.Find(addr2
);
303 BOOST_CHECK(info2
->ToString() == info1
->ToString());
305 // Test 19: Find returns another IP matching what we searched on.
306 CAddrInfo
* info3
= addrman
.Find(addr3
);
309 BOOST_CHECK(info3
->ToString() == "251.255.2.1:8333");
312 BOOST_AUTO_TEST_CASE(addrman_create
)
314 CAddrManTest addrman
;
316 // Set addrman addr placement to be deterministic.
317 addrman
.MakeDeterministic();
319 BOOST_CHECK(addrman
.size() == 0);
321 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
322 CNetAddr source1
= ResolveIP("250.1.2.1");
325 CAddrInfo
* pinfo
= addrman
.Create(addr1
, source1
, &nId
);
327 // Test 20: The result should be the same as the input addr.
328 BOOST_CHECK(pinfo
->ToString() == "250.1.2.1:8333");
330 CAddrInfo
* info2
= addrman
.Find(addr1
);
331 BOOST_CHECK(info2
->ToString() == "250.1.2.1:8333");
335 BOOST_AUTO_TEST_CASE(addrman_delete
)
337 CAddrManTest addrman
;
339 // Set addrman addr placement to be deterministic.
340 addrman
.MakeDeterministic();
342 BOOST_CHECK(addrman
.size() == 0);
344 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
345 CNetAddr source1
= ResolveIP("250.1.2.1");
348 addrman
.Create(addr1
, source1
, &nId
);
350 // Test 21: Delete should actually delete the addr.
351 BOOST_CHECK(addrman
.size() == 1);
353 BOOST_CHECK(addrman
.size() == 0);
354 CAddrInfo
* info2
= addrman
.Find(addr1
);
355 BOOST_CHECK(info2
== NULL
);
358 BOOST_AUTO_TEST_CASE(addrman_getaddr
)
360 CAddrManTest addrman
;
362 // Set addrman addr placement to be deterministic.
363 addrman
.MakeDeterministic();
365 // Test 22: Sanity check, GetAddr should never return anything if addrman
367 BOOST_CHECK(addrman
.size() == 0);
368 vector
<CAddress
> vAddr1
= addrman
.GetAddr();
369 BOOST_CHECK(vAddr1
.size() == 0);
371 CAddress addr1
= CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE
);
372 addr1
.nTime
= GetAdjustedTime(); // Set time so isTerrible = false
373 CAddress addr2
= CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE
);
374 addr2
.nTime
= GetAdjustedTime();
375 CAddress addr3
= CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE
);
376 addr3
.nTime
= GetAdjustedTime();
377 CAddress addr4
= CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE
);
378 addr4
.nTime
= GetAdjustedTime();
379 CAddress addr5
= CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE
);
380 addr5
.nTime
= GetAdjustedTime();
381 CNetAddr source1
= ResolveIP("250.1.2.1");
382 CNetAddr source2
= ResolveIP("250.2.3.3");
384 // Test 23: Ensure GetAddr works with new addresses.
385 addrman
.Add(addr1
, source1
);
386 addrman
.Add(addr2
, source2
);
387 addrman
.Add(addr3
, source1
);
388 addrman
.Add(addr4
, source2
);
389 addrman
.Add(addr5
, source1
);
391 // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
392 BOOST_CHECK(addrman
.GetAddr().size() == 1);
394 // Test 24: Ensure GetAddr works with new and tried addresses.
395 addrman
.Good(CAddress(addr1
, NODE_NONE
));
396 addrman
.Good(CAddress(addr2
, NODE_NONE
));
397 BOOST_CHECK(addrman
.GetAddr().size() == 1);
399 // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
400 for (unsigned int i
= 1; i
< (8 * 256); i
++) {
401 int octet1
= i
% 256;
402 int octet2
= (i
/ 256) % 256;
403 int octet3
= (i
/ (256 * 2)) % 256;
404 string strAddr
= boost::to_string(octet1
) + "." + boost::to_string(octet2
) + "." + boost::to_string(octet3
) + ".23";
405 CAddress addr
= CAddress(ResolveService(strAddr
), NODE_NONE
);
407 // Ensure that for all addrs in addrman, isTerrible == false.
408 addr
.nTime
= GetAdjustedTime();
409 addrman
.Add(addr
, ResolveIP(strAddr
));
413 vector
<CAddress
> vAddr
= addrman
.GetAddr();
415 size_t percent23
= (addrman
.size() * 23) / 100;
416 BOOST_CHECK(vAddr
.size() == percent23
);
417 BOOST_CHECK(vAddr
.size() == 461);
418 // (Addrman.size() < number of addresses added) due to address collisons.
419 BOOST_CHECK(addrman
.size() == 2007);
423 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket
)
425 CAddrManTest addrman
;
427 // Set addrman addr placement to be deterministic.
428 addrman
.MakeDeterministic();
430 CAddress addr1
= CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE
);
431 CAddress addr2
= CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE
);
433 CNetAddr source1
= ResolveIP("250.1.1.1");
436 CAddrInfo info1
= CAddrInfo(addr1
, source1
);
438 uint256 nKey1
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 1).GetHash();
439 uint256 nKey2
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 2).GetHash();
442 BOOST_CHECK(info1
.GetTriedBucket(nKey1
) == 40);
444 // Test 26: Make sure key actually randomizes bucket placement. A fail on
445 // this test could be a security issue.
446 BOOST_CHECK(info1
.GetTriedBucket(nKey1
) != info1
.GetTriedBucket(nKey2
));
448 // Test 27: Two addresses with same IP but different ports can map to
449 // different buckets because they have different keys.
450 CAddrInfo info2
= CAddrInfo(addr2
, source1
);
452 BOOST_CHECK(info1
.GetKey() != info2
.GetKey());
453 BOOST_CHECK(info1
.GetTriedBucket(nKey1
) != info2
.GetTriedBucket(nKey1
));
456 for (int i
= 0; i
< 255; i
++) {
457 CAddrInfo infoi
= CAddrInfo(
458 CAddress(ResolveService("250.1.1." + boost::to_string(i
)), NODE_NONE
),
459 ResolveIP("250.1.1." + boost::to_string(i
)));
460 int bucket
= infoi
.GetTriedBucket(nKey1
);
461 buckets
.insert(bucket
);
463 // Test 28: IP addresses in the same group (\16 prefix for IPv4) should
464 // never get more than 8 buckets
465 BOOST_CHECK(buckets
.size() == 8);
468 for (int j
= 0; j
< 255; j
++) {
469 CAddrInfo infoj
= CAddrInfo(
470 CAddress(ResolveService("250." + boost::to_string(j
) + ".1.1"), NODE_NONE
),
471 ResolveIP("250." + boost::to_string(j
) + ".1.1"));
472 int bucket
= infoj
.GetTriedBucket(nKey1
);
473 buckets
.insert(bucket
);
475 // Test 29: IP addresses in the different groups should map to more than
477 BOOST_CHECK(buckets
.size() == 160);
480 BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket
)
482 CAddrManTest addrman
;
484 // Set addrman addr placement to be deterministic.
485 addrman
.MakeDeterministic();
487 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
488 CAddress addr2
= CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE
);
490 CNetAddr source1
= ResolveIP("250.1.2.1");
492 CAddrInfo info1
= CAddrInfo(addr1
, source1
);
494 uint256 nKey1
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 1).GetHash();
495 uint256 nKey2
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 2).GetHash();
497 BOOST_CHECK(info1
.GetNewBucket(nKey1
) == 786);
499 // Test 30: Make sure key actually randomizes bucket placement. A fail on
500 // this test could be a security issue.
501 BOOST_CHECK(info1
.GetNewBucket(nKey1
) != info1
.GetNewBucket(nKey2
));
503 // Test 31: Ports should not effect bucket placement in the addr
504 CAddrInfo info2
= CAddrInfo(addr2
, source1
);
505 BOOST_CHECK(info1
.GetKey() != info2
.GetKey());
506 BOOST_CHECK(info1
.GetNewBucket(nKey1
) == info2
.GetNewBucket(nKey1
));
509 for (int i
= 0; i
< 255; i
++) {
510 CAddrInfo infoi
= CAddrInfo(
511 CAddress(ResolveService("250.1.1." + boost::to_string(i
)), NODE_NONE
),
512 ResolveIP("250.1.1." + boost::to_string(i
)));
513 int bucket
= infoi
.GetNewBucket(nKey1
);
514 buckets
.insert(bucket
);
516 // Test 32: IP addresses in the same group (\16 prefix for IPv4) should
517 // always map to the same bucket.
518 BOOST_CHECK(buckets
.size() == 1);
521 for (int j
= 0; j
< 4 * 255; j
++) {
522 CAddrInfo infoj
= CAddrInfo(CAddress(
524 boost::to_string(250 + (j
/ 255)) + "." + boost::to_string(j
% 256) + ".1.1"), NODE_NONE
),
525 ResolveIP("251.4.1.1"));
526 int bucket
= infoj
.GetNewBucket(nKey1
);
527 buckets
.insert(bucket
);
529 // Test 33: IP addresses in the same source groups should map to no more
531 BOOST_CHECK(buckets
.size() <= 64);
534 for (int p
= 0; p
< 255; p
++) {
535 CAddrInfo infoj
= CAddrInfo(
536 CAddress(ResolveService("250.1.1.1"), NODE_NONE
),
537 ResolveIP("250." + boost::to_string(p
) + ".1.1"));
538 int bucket
= infoj
.GetNewBucket(nKey1
);
539 buckets
.insert(bucket
);
541 // Test 34: IP addresses in the different source groups should map to more
543 BOOST_CHECK(buckets
.size() > 64);
545 BOOST_AUTO_TEST_SUITE_END()