1 // Copyright (c) 2012-2016 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>
13 class CAddrManTest
: public CAddrMan
18 explicit CAddrManTest(bool makeDeterministic
= true)
22 if (makeDeterministic
) {
23 // Set addrman addr placement to be deterministic.
28 //! Ensure that bucket placement is always the same for testing purposes.
29 void MakeDeterministic()
32 insecure_rand
= FastRandomContext(true);
35 int RandomInt(int nMax
) override
37 state
= (CHashWriter(SER_GETHASH
, 0) << state
).GetHash().GetCheapHash();
38 return (unsigned int)(state
% nMax
);
41 CAddrInfo
* Find(const CNetAddr
& addr
, int* pnId
= nullptr)
43 return CAddrMan::Find(addr
, pnId
);
46 CAddrInfo
* Create(const CAddress
& addr
, const CNetAddr
& addrSource
, int* pnId
= nullptr)
48 return CAddrMan::Create(addr
, addrSource
, pnId
);
53 CAddrMan::Delete(nId
);
57 static CNetAddr
ResolveIP(const char* ip
)
60 BOOST_CHECK_MESSAGE(LookupHost(ip
, addr
, false), strprintf("failed to resolve: %s", ip
));
64 static CNetAddr
ResolveIP(std::string ip
)
66 return ResolveIP(ip
.c_str());
69 static CService
ResolveService(const char* ip
, int port
= 0)
72 BOOST_CHECK_MESSAGE(Lookup(ip
, serv
, port
, false), strprintf("failed to resolve: %s:%i", ip
, port
));
76 static CService
ResolveService(std::string ip
, int port
= 0)
78 return ResolveService(ip
.c_str(), port
);
81 BOOST_FIXTURE_TEST_SUITE(addrman_tests
, BasicTestingSetup
)
83 BOOST_AUTO_TEST_CASE(addrman_simple
)
87 CNetAddr source
= ResolveIP("252.2.2.2");
89 // Test: Does Addrman respond correctly when empty.
90 BOOST_CHECK_EQUAL(addrman
.size(), 0);
91 CAddrInfo addr_null
= addrman
.Select();
92 BOOST_CHECK_EQUAL(addr_null
.ToString(), "[::]:0");
94 // Test: Does Addrman::Add work as expected.
95 CService addr1
= ResolveService("250.1.1.1", 8333);
96 BOOST_CHECK(addrman
.Add(CAddress(addr1
, NODE_NONE
), source
));
97 BOOST_CHECK_EQUAL(addrman
.size(), 1);
98 CAddrInfo addr_ret1
= addrman
.Select();
99 BOOST_CHECK_EQUAL(addr_ret1
.ToString(), "250.1.1.1:8333");
101 // Test: 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 BOOST_CHECK(!addrman
.Add(CAddress(addr1_dup
, NODE_NONE
), source
));
105 BOOST_CHECK_EQUAL(addrman
.size(), 1);
108 // Test: New table has one addr and we add a diff addr we should
109 // have at least one addr.
110 // Note that addrman's size cannot be tested reliably after insertion, as
111 // hash collisions may occur. But we can always be sure of at least one
114 CService addr2
= ResolveService("250.1.1.2", 8333);
115 BOOST_CHECK(addrman
.Add(CAddress(addr2
, NODE_NONE
), source
));
116 BOOST_CHECK(addrman
.size() >= 1);
118 // Test: AddrMan::Clear() should empty the new table.
120 BOOST_CHECK_EQUAL(addrman
.size(), 0);
121 CAddrInfo addr_null2
= addrman
.Select();
122 BOOST_CHECK_EQUAL(addr_null2
.ToString(), "[::]:0");
124 // Test: AddrMan::Add multiple addresses works as expected
125 std::vector
<CAddress
> vAddr
;
126 vAddr
.push_back(CAddress(ResolveService("250.1.1.3", 8333), NODE_NONE
));
127 vAddr
.push_back(CAddress(ResolveService("250.1.1.4", 8333), NODE_NONE
));
128 BOOST_CHECK(addrman
.Add(vAddr
, source
));
129 BOOST_CHECK(addrman
.size() >= 1);
132 BOOST_AUTO_TEST_CASE(addrman_ports
)
134 CAddrManTest addrman
;
136 CNetAddr source
= ResolveIP("252.2.2.2");
138 BOOST_CHECK_EQUAL(addrman
.size(), 0);
140 // Test 7; Addr with same IP but diff port does not replace existing addr.
141 CService addr1
= ResolveService("250.1.1.1", 8333);
142 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
143 BOOST_CHECK_EQUAL(addrman
.size(), 1);
145 CService addr1_port
= ResolveService("250.1.1.1", 8334);
146 addrman
.Add(CAddress(addr1_port
, NODE_NONE
), source
);
147 BOOST_CHECK_EQUAL(addrman
.size(), 1);
148 CAddrInfo addr_ret2
= addrman
.Select();
149 BOOST_CHECK_EQUAL(addr_ret2
.ToString(), "250.1.1.1:8333");
151 // Test: Add same IP but diff port to tried table, it doesn't get added.
152 // Perhaps this is not ideal behavior but it is the current behavior.
153 addrman
.Good(CAddress(addr1_port
, NODE_NONE
));
154 BOOST_CHECK_EQUAL(addrman
.size(), 1);
156 CAddrInfo addr_ret3
= addrman
.Select(newOnly
);
157 BOOST_CHECK_EQUAL(addr_ret3
.ToString(), "250.1.1.1:8333");
161 BOOST_AUTO_TEST_CASE(addrman_select
)
163 CAddrManTest addrman
;
165 CNetAddr source
= ResolveIP("252.2.2.2");
167 // Test: Select from new with 1 addr in new.
168 CService addr1
= ResolveService("250.1.1.1", 8333);
169 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
170 BOOST_CHECK_EQUAL(addrman
.size(), 1);
173 CAddrInfo addr_ret1
= addrman
.Select(newOnly
);
174 BOOST_CHECK_EQUAL(addr_ret1
.ToString(), "250.1.1.1:8333");
176 // Test: move addr to tried, select from new expected nothing returned.
177 addrman
.Good(CAddress(addr1
, NODE_NONE
));
178 BOOST_CHECK_EQUAL(addrman
.size(), 1);
179 CAddrInfo addr_ret2
= addrman
.Select(newOnly
);
180 BOOST_CHECK_EQUAL(addr_ret2
.ToString(), "[::]:0");
182 CAddrInfo addr_ret3
= addrman
.Select();
183 BOOST_CHECK_EQUAL(addr_ret3
.ToString(), "250.1.1.1:8333");
185 BOOST_CHECK_EQUAL(addrman
.size(), 1);
188 // Add three addresses to new table.
189 CService addr2
= ResolveService("250.3.1.1", 8333);
190 CService addr3
= ResolveService("250.3.2.2", 9999);
191 CService addr4
= ResolveService("250.3.3.3", 9999);
193 addrman
.Add(CAddress(addr2
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
194 addrman
.Add(CAddress(addr3
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
195 addrman
.Add(CAddress(addr4
, NODE_NONE
), ResolveService("250.4.1.1", 8333));
197 // Add three addresses to tried table.
198 CService addr5
= ResolveService("250.4.4.4", 8333);
199 CService addr6
= ResolveService("250.4.5.5", 7777);
200 CService addr7
= ResolveService("250.4.6.6", 8333);
202 addrman
.Add(CAddress(addr5
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
203 addrman
.Good(CAddress(addr5
, NODE_NONE
));
204 addrman
.Add(CAddress(addr6
, NODE_NONE
), ResolveService("250.3.1.1", 8333));
205 addrman
.Good(CAddress(addr6
, NODE_NONE
));
206 addrman
.Add(CAddress(addr7
, NODE_NONE
), ResolveService("250.1.1.3", 8333));
207 addrman
.Good(CAddress(addr7
, NODE_NONE
));
209 // Test: 6 addrs + 1 addr from last test = 7.
210 BOOST_CHECK_EQUAL(addrman
.size(), 7);
212 // Test: Select pulls from new and tried regardless of port number.
213 std::set
<uint16_t> ports
;
214 for (int i
= 0; i
< 20; ++i
) {
215 ports
.insert(addrman
.Select().GetPort());
217 BOOST_CHECK_EQUAL(ports
.size(), 3);
220 BOOST_AUTO_TEST_CASE(addrman_new_collisions
)
222 CAddrManTest addrman
;
224 CNetAddr source
= ResolveIP("252.2.2.2");
226 BOOST_CHECK_EQUAL(addrman
.size(), 0);
228 for (unsigned int i
= 1; i
< 18; i
++) {
229 CService addr
= ResolveService("250.1.1." + boost::to_string(i
));
230 addrman
.Add(CAddress(addr
, NODE_NONE
), source
);
232 //Test: No collision in new table yet.
233 BOOST_CHECK_EQUAL(addrman
.size(), i
);
236 //Test: new table collision!
237 CService addr1
= ResolveService("250.1.1.18");
238 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
239 BOOST_CHECK_EQUAL(addrman
.size(), 17);
241 CService addr2
= ResolveService("250.1.1.19");
242 addrman
.Add(CAddress(addr2
, NODE_NONE
), source
);
243 BOOST_CHECK_EQUAL(addrman
.size(), 18);
246 BOOST_AUTO_TEST_CASE(addrman_tried_collisions
)
248 CAddrManTest addrman
;
250 CNetAddr source
= ResolveIP("252.2.2.2");
252 BOOST_CHECK_EQUAL(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: No collision in tried table yet.
260 BOOST_CHECK_EQUAL(addrman
.size(), i
);
263 //Test: tried table collision!
264 CService addr1
= ResolveService("250.1.1.80");
265 addrman
.Add(CAddress(addr1
, NODE_NONE
), source
);
266 BOOST_CHECK_EQUAL(addrman
.size(), 79);
268 CService addr2
= ResolveService("250.1.1.81");
269 addrman
.Add(CAddress(addr2
, NODE_NONE
), source
);
270 BOOST_CHECK_EQUAL(addrman
.size(), 80);
273 BOOST_AUTO_TEST_CASE(addrman_find
)
275 CAddrManTest addrman
;
277 BOOST_CHECK_EQUAL(addrman
.size(), 0);
279 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
280 CAddress addr2
= CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE
);
281 CAddress addr3
= CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE
);
283 CNetAddr source1
= ResolveIP("250.1.2.1");
284 CNetAddr source2
= ResolveIP("250.1.2.2");
286 addrman
.Add(addr1
, source1
);
287 addrman
.Add(addr2
, source2
);
288 addrman
.Add(addr3
, source1
);
290 // Test: ensure Find returns an IP matching what we searched on.
291 CAddrInfo
* info1
= addrman
.Find(addr1
);
292 BOOST_REQUIRE(info1
);
293 BOOST_CHECK_EQUAL(info1
->ToString(), "250.1.2.1:8333");
295 // Test 18; Find does not discriminate by port number.
296 CAddrInfo
* info2
= addrman
.Find(addr2
);
297 BOOST_REQUIRE(info2
);
298 BOOST_CHECK_EQUAL(info2
->ToString(), info1
->ToString());
300 // Test: Find returns another IP matching what we searched on.
301 CAddrInfo
* info3
= addrman
.Find(addr3
);
302 BOOST_REQUIRE(info3
);
303 BOOST_CHECK_EQUAL(info3
->ToString(), "251.255.2.1:8333");
306 BOOST_AUTO_TEST_CASE(addrman_create
)
308 CAddrManTest addrman
;
310 BOOST_CHECK_EQUAL(addrman
.size(), 0);
312 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
313 CNetAddr source1
= ResolveIP("250.1.2.1");
316 CAddrInfo
* pinfo
= addrman
.Create(addr1
, source1
, &nId
);
318 // Test: The result should be the same as the input addr.
319 BOOST_CHECK_EQUAL(pinfo
->ToString(), "250.1.2.1:8333");
321 CAddrInfo
* info2
= addrman
.Find(addr1
);
322 BOOST_CHECK_EQUAL(info2
->ToString(), "250.1.2.1:8333");
326 BOOST_AUTO_TEST_CASE(addrman_delete
)
328 CAddrManTest addrman
;
330 BOOST_CHECK_EQUAL(addrman
.size(), 0);
332 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
333 CNetAddr source1
= ResolveIP("250.1.2.1");
336 addrman
.Create(addr1
, source1
, &nId
);
338 // Test: Delete should actually delete the addr.
339 BOOST_CHECK_EQUAL(addrman
.size(), 1);
341 BOOST_CHECK_EQUAL(addrman
.size(), 0);
342 CAddrInfo
* info2
= addrman
.Find(addr1
);
343 BOOST_CHECK(info2
== nullptr);
346 BOOST_AUTO_TEST_CASE(addrman_getaddr
)
348 CAddrManTest addrman
;
350 // Test: Sanity check, GetAddr should never return anything if addrman
352 BOOST_CHECK_EQUAL(addrman
.size(), 0);
353 std::vector
<CAddress
> vAddr1
= addrman
.GetAddr();
354 BOOST_CHECK_EQUAL(vAddr1
.size(), 0);
356 CAddress addr1
= CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE
);
357 addr1
.nTime
= GetAdjustedTime(); // Set time so isTerrible = false
358 CAddress addr2
= CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE
);
359 addr2
.nTime
= GetAdjustedTime();
360 CAddress addr3
= CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE
);
361 addr3
.nTime
= GetAdjustedTime();
362 CAddress addr4
= CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE
);
363 addr4
.nTime
= GetAdjustedTime();
364 CAddress addr5
= CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE
);
365 addr5
.nTime
= GetAdjustedTime();
366 CNetAddr source1
= ResolveIP("250.1.2.1");
367 CNetAddr source2
= ResolveIP("250.2.3.3");
369 // Test: Ensure GetAddr works with new addresses.
370 addrman
.Add(addr1
, source1
);
371 addrman
.Add(addr2
, source2
);
372 addrman
.Add(addr3
, source1
);
373 addrman
.Add(addr4
, source2
);
374 addrman
.Add(addr5
, source1
);
376 // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
377 BOOST_CHECK_EQUAL(addrman
.GetAddr().size(), 1);
379 // Test: Ensure GetAddr works with new and tried addresses.
380 addrman
.Good(CAddress(addr1
, NODE_NONE
));
381 addrman
.Good(CAddress(addr2
, NODE_NONE
));
382 BOOST_CHECK_EQUAL(addrman
.GetAddr().size(), 1);
384 // Test: Ensure GetAddr still returns 23% when addrman has many addrs.
385 for (unsigned int i
= 1; i
< (8 * 256); i
++) {
386 int octet1
= i
% 256;
387 int octet2
= i
>> 8 % 256;
388 std::string strAddr
= boost::to_string(octet1
) + "." + boost::to_string(octet2
) + ".1.23";
389 CAddress addr
= CAddress(ResolveService(strAddr
), NODE_NONE
);
391 // Ensure that for all addrs in addrman, isTerrible == false.
392 addr
.nTime
= GetAdjustedTime();
393 addrman
.Add(addr
, ResolveIP(strAddr
));
397 std::vector
<CAddress
> vAddr
= addrman
.GetAddr();
399 size_t percent23
= (addrman
.size() * 23) / 100;
400 BOOST_CHECK_EQUAL(vAddr
.size(), percent23
);
401 BOOST_CHECK_EQUAL(vAddr
.size(), 461);
402 // (Addrman.size() < number of addresses added) due to address collisions.
403 BOOST_CHECK_EQUAL(addrman
.size(), 2006);
407 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket
)
409 CAddrManTest addrman
;
411 CAddress addr1
= CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE
);
412 CAddress addr2
= CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE
);
414 CNetAddr source1
= ResolveIP("250.1.1.1");
417 CAddrInfo info1
= CAddrInfo(addr1
, source1
);
419 uint256 nKey1
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 1).GetHash();
420 uint256 nKey2
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 2).GetHash();
423 BOOST_CHECK_EQUAL(info1
.GetTriedBucket(nKey1
), 40);
425 // Test: Make sure key actually randomizes bucket placement. A fail on
426 // this test could be a security issue.
427 BOOST_CHECK(info1
.GetTriedBucket(nKey1
) != info1
.GetTriedBucket(nKey2
));
429 // Test: Two addresses with same IP but different ports can map to
430 // different buckets because they have different keys.
431 CAddrInfo info2
= CAddrInfo(addr2
, source1
);
433 BOOST_CHECK(info1
.GetKey() != info2
.GetKey());
434 BOOST_CHECK(info1
.GetTriedBucket(nKey1
) != info2
.GetTriedBucket(nKey1
));
436 std::set
<int> buckets
;
437 for (int i
= 0; i
< 255; i
++) {
438 CAddrInfo infoi
= CAddrInfo(
439 CAddress(ResolveService("250.1.1." + boost::to_string(i
)), NODE_NONE
),
440 ResolveIP("250.1.1." + boost::to_string(i
)));
441 int bucket
= infoi
.GetTriedBucket(nKey1
);
442 buckets
.insert(bucket
);
444 // Test: IP addresses in the same group (\16 prefix for IPv4) should
445 // never get more than 8 buckets
446 BOOST_CHECK_EQUAL(buckets
.size(), 8);
449 for (int j
= 0; j
< 255; j
++) {
450 CAddrInfo infoj
= CAddrInfo(
451 CAddress(ResolveService("250." + boost::to_string(j
) + ".1.1"), NODE_NONE
),
452 ResolveIP("250." + boost::to_string(j
) + ".1.1"));
453 int bucket
= infoj
.GetTriedBucket(nKey1
);
454 buckets
.insert(bucket
);
456 // Test: IP addresses in the different groups should map to more than
458 BOOST_CHECK_EQUAL(buckets
.size(), 160);
461 BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket
)
463 CAddrManTest addrman
;
465 CAddress addr1
= CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE
);
466 CAddress addr2
= CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE
);
468 CNetAddr source1
= ResolveIP("250.1.2.1");
470 CAddrInfo info1
= CAddrInfo(addr1
, source1
);
472 uint256 nKey1
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 1).GetHash();
473 uint256 nKey2
= (uint256
)(CHashWriter(SER_GETHASH
, 0) << 2).GetHash();
475 // Test: Make sure the buckets are what we expect
476 BOOST_CHECK_EQUAL(info1
.GetNewBucket(nKey1
), 786);
477 BOOST_CHECK_EQUAL(info1
.GetNewBucket(nKey1
, source1
), 786);
479 // Test: Make sure key actually randomizes bucket placement. A fail on
480 // this test could be a security issue.
481 BOOST_CHECK(info1
.GetNewBucket(nKey1
) != info1
.GetNewBucket(nKey2
));
483 // Test: Ports should not effect bucket placement in the addr
484 CAddrInfo info2
= CAddrInfo(addr2
, source1
);
485 BOOST_CHECK(info1
.GetKey() != info2
.GetKey());
486 BOOST_CHECK_EQUAL(info1
.GetNewBucket(nKey1
), info2
.GetNewBucket(nKey1
));
488 std::set
<int> buckets
;
489 for (int i
= 0; i
< 255; i
++) {
490 CAddrInfo infoi
= CAddrInfo(
491 CAddress(ResolveService("250.1.1." + boost::to_string(i
)), NODE_NONE
),
492 ResolveIP("250.1.1." + boost::to_string(i
)));
493 int bucket
= infoi
.GetNewBucket(nKey1
);
494 buckets
.insert(bucket
);
496 // Test: IP addresses in the same group (\16 prefix for IPv4) should
497 // always map to the same bucket.
498 BOOST_CHECK_EQUAL(buckets
.size(), 1);
501 for (int j
= 0; j
< 4 * 255; j
++) {
502 CAddrInfo infoj
= CAddrInfo(CAddress(
504 boost::to_string(250 + (j
/ 255)) + "." + boost::to_string(j
% 256) + ".1.1"), NODE_NONE
),
505 ResolveIP("251.4.1.1"));
506 int bucket
= infoj
.GetNewBucket(nKey1
);
507 buckets
.insert(bucket
);
509 // Test: IP addresses in the same source groups should map to no more
511 BOOST_CHECK(buckets
.size() <= 64);
514 for (int p
= 0; p
< 255; p
++) {
515 CAddrInfo infoj
= CAddrInfo(
516 CAddress(ResolveService("250.1.1.1"), NODE_NONE
),
517 ResolveIP("250." + boost::to_string(p
) + ".1.1"));
518 int bucket
= infoj
.GetNewBucket(nKey1
);
519 buckets
.insert(bucket
);
521 // Test: IP addresses in the different source groups should map to more
523 BOOST_CHECK(buckets
.size() > 64);
525 BOOST_AUTO_TEST_SUITE_END()