1 // Copyright (c) 2012-2013 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>
14 class CAddrManTest : public CAddrMan
24 //! Ensure that bucket placement is always the same for testing purposes.
25 void MakeDeterministic()
28 seed_insecure_rand(true);
31 int RandomInt(int nMax)
33 state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
34 return (unsigned int)(state % nMax);
37 CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL)
39 return CAddrMan::Find(addr, pnId);
42 CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = NULL)
44 return CAddrMan::Create(addr, addrSource, pnId);
49 CAddrMan::Delete(nId);
53 BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
55 BOOST_AUTO_TEST_CASE(addrman_simple)
59 // Set addrman addr placement to be deterministic.
60 addrman.MakeDeterministic();
62 CNetAddr source = CNetAddr("252.2.2.2");
64 // Test 1: Does Addrman respond correctly when empty.
65 BOOST_CHECK(addrman.size() == 0);
66 CAddrInfo addr_null = addrman.Select();
67 BOOST_CHECK(addr_null.ToString() == "[::]:0");
69 // Test 2: Does Addrman::Add work as expected.
70 CService addr1 = CService("250.1.1.1", 8333);
71 addrman.Add(CAddress(addr1), source);
72 BOOST_CHECK(addrman.size() == 1);
73 CAddrInfo addr_ret1 = addrman.Select();
74 BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
76 // Test 3: Does IP address deduplication work correctly.
77 // Expected dup IP should not be added.
78 CService addr1_dup = CService("250.1.1.1", 8333);
79 addrman.Add(CAddress(addr1_dup), source);
80 BOOST_CHECK(addrman.size() == 1);
83 // Test 5: New table has one addr and we add a diff addr we should
85 CService addr2 = CService("250.1.1.2", 8333);
86 addrman.Add(CAddress(addr2), source);
87 BOOST_CHECK(addrman.size() == 2);
89 // Test 6: AddrMan::Clear() should empty the new table.
91 BOOST_CHECK(addrman.size() == 0);
92 CAddrInfo addr_null2 = addrman.Select();
93 BOOST_CHECK(addr_null2.ToString() == "[::]:0");
96 BOOST_AUTO_TEST_CASE(addrman_ports)
100 // Set addrman addr placement to be deterministic.
101 addrman.MakeDeterministic();
103 CNetAddr source = CNetAddr("252.2.2.2");
105 BOOST_CHECK(addrman.size() == 0);
107 // Test 7; Addr with same IP but diff port does not replace existing addr.
108 CService addr1 = CService("250.1.1.1", 8333);
109 addrman.Add(CAddress(addr1), source);
110 BOOST_CHECK(addrman.size() == 1);
112 CService addr1_port = CService("250.1.1.1", 8334);
113 addrman.Add(CAddress(addr1_port), source);
114 BOOST_CHECK(addrman.size() == 1);
115 CAddrInfo addr_ret2 = addrman.Select();
116 BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333");
118 // Test 8: Add same IP but diff port to tried table, it doesn't get added.
119 // Perhaps this is not ideal behavior but it is the current behavior.
120 addrman.Good(CAddress(addr1_port));
121 BOOST_CHECK(addrman.size() == 1);
123 CAddrInfo addr_ret3 = addrman.Select(newOnly);
124 BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
128 BOOST_AUTO_TEST_CASE(addrman_select)
130 CAddrManTest addrman;
132 // Set addrman addr placement to be deterministic.
133 addrman.MakeDeterministic();
135 CNetAddr source = CNetAddr("252.2.2.2");
137 // Test 9: Select from new with 1 addr in new.
138 CService addr1 = CService("250.1.1.1", 8333);
139 addrman.Add(CAddress(addr1), source);
140 BOOST_CHECK(addrman.size() == 1);
143 CAddrInfo addr_ret1 = addrman.Select(newOnly);
144 BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
146 // Test 10: move addr to tried, select from new expected nothing returned.
147 addrman.Good(CAddress(addr1));
148 BOOST_CHECK(addrman.size() == 1);
149 CAddrInfo addr_ret2 = addrman.Select(newOnly);
150 BOOST_CHECK(addr_ret2.ToString() == "[::]:0");
152 CAddrInfo addr_ret3 = addrman.Select();
153 BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
155 BOOST_CHECK(addrman.size() == 1);
158 // Add three addresses to new table.
159 CService addr2 = CService("250.3.1.1", 8333);
160 CService addr3 = CService("250.3.2.2", 9999);
161 CService addr4 = CService("250.3.3.3", 9999);
163 addrman.Add(CAddress(addr2), CService("250.3.1.1", 8333));
164 addrman.Add(CAddress(addr3), CService("250.3.1.1", 8333));
165 addrman.Add(CAddress(addr4), CService("250.4.1.1", 8333));
167 // Add three addresses to tried table.
168 CService addr5 = CService("250.4.4.4", 8333);
169 CService addr6 = CService("250.4.5.5", 7777);
170 CService addr7 = CService("250.4.6.6", 8333);
172 addrman.Add(CAddress(addr5), CService("250.3.1.1", 8333));
173 addrman.Good(CAddress(addr5));
174 addrman.Add(CAddress(addr6), CService("250.3.1.1", 8333));
175 addrman.Good(CAddress(addr6));
176 addrman.Add(CAddress(addr7), CService("250.1.1.3", 8333));
177 addrman.Good(CAddress(addr7));
179 // Test 11: 6 addrs + 1 addr from last test = 7.
180 BOOST_CHECK(addrman.size() == 7);
182 // Test 12: Select pulls from new and tried regardless of port number.
183 BOOST_CHECK(addrman.Select().ToString() == "250.4.6.6:8333");
184 BOOST_CHECK(addrman.Select().ToString() == "250.3.2.2:9999");
185 BOOST_CHECK(addrman.Select().ToString() == "250.3.3.3:9999");
186 BOOST_CHECK(addrman.Select().ToString() == "250.4.4.4:8333");
189 BOOST_AUTO_TEST_CASE(addrman_new_collisions)
191 CAddrManTest addrman;
193 // Set addrman addr placement to be deterministic.
194 addrman.MakeDeterministic();
196 CNetAddr source = CNetAddr("252.2.2.2");
198 BOOST_CHECK(addrman.size() == 0);
200 for (unsigned int i = 1; i < 18; i++) {
201 CService addr = CService("250.1.1." + boost::to_string(i));
202 addrman.Add(CAddress(addr), source);
204 //Test 13: No collision in new table yet.
205 BOOST_CHECK(addrman.size() == i);
208 //Test 14: new table collision!
209 CService addr1 = CService("250.1.1.18");
210 addrman.Add(CAddress(addr1), source);
211 BOOST_CHECK(addrman.size() == 17);
213 CService addr2 = CService("250.1.1.19");
214 addrman.Add(CAddress(addr2), source);
215 BOOST_CHECK(addrman.size() == 18);
218 BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
220 CAddrManTest addrman;
222 // Set addrman addr placement to be deterministic.
223 addrman.MakeDeterministic();
225 CNetAddr source = CNetAddr("252.2.2.2");
227 BOOST_CHECK(addrman.size() == 0);
229 for (unsigned int i = 1; i < 80; i++) {
230 CService addr = CService("250.1.1." + boost::to_string(i));
231 addrman.Add(CAddress(addr), source);
232 addrman.Good(CAddress(addr));
234 //Test 15: No collision in tried table yet.
235 BOOST_TEST_MESSAGE(addrman.size());
236 BOOST_CHECK(addrman.size() == i);
239 //Test 16: tried table collision!
240 CService addr1 = CService("250.1.1.80");
241 addrman.Add(CAddress(addr1), source);
242 BOOST_CHECK(addrman.size() == 79);
244 CService addr2 = CService("250.1.1.81");
245 addrman.Add(CAddress(addr2), source);
246 BOOST_CHECK(addrman.size() == 80);
249 BOOST_AUTO_TEST_CASE(addrman_find)
251 CAddrManTest addrman;
253 // Set addrman addr placement to be deterministic.
254 addrman.MakeDeterministic();
256 BOOST_CHECK(addrman.size() == 0);
258 CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
259 CAddress addr2 = CAddress(CService("250.1.2.1", 9999));
260 CAddress addr3 = CAddress(CService("251.255.2.1", 8333));
262 CNetAddr source1 = CNetAddr("250.1.2.1");
263 CNetAddr source2 = CNetAddr("250.1.2.2");
265 addrman.Add(addr1, source1);
266 addrman.Add(addr2, source2);
267 addrman.Add(addr3, source1);
269 // Test 17: ensure Find returns an IP matching what we searched on.
270 CAddrInfo* info1 = addrman.Find(addr1);
273 BOOST_CHECK(info1->ToString() == "250.1.2.1:8333");
275 // Test 18; Find does not discriminate by port number.
276 CAddrInfo* info2 = addrman.Find(addr2);
279 BOOST_CHECK(info2->ToString() == info1->ToString());
281 // Test 19: Find returns another IP matching what we searched on.
282 CAddrInfo* info3 = addrman.Find(addr3);
285 BOOST_CHECK(info3->ToString() == "251.255.2.1:8333");
288 BOOST_AUTO_TEST_CASE(addrman_create)
290 CAddrManTest addrman;
292 // Set addrman addr placement to be deterministic.
293 addrman.MakeDeterministic();
295 BOOST_CHECK(addrman.size() == 0);
297 CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
298 CNetAddr source1 = CNetAddr("250.1.2.1");
301 CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);
303 // Test 20: The result should be the same as the input addr.
304 BOOST_CHECK(pinfo->ToString() == "250.1.2.1:8333");
306 CAddrInfo* info2 = addrman.Find(addr1);
307 BOOST_CHECK(info2->ToString() == "250.1.2.1:8333");
311 BOOST_AUTO_TEST_CASE(addrman_delete)
313 CAddrManTest addrman;
315 // Set addrman addr placement to be deterministic.
316 addrman.MakeDeterministic();
318 BOOST_CHECK(addrman.size() == 0);
320 CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
321 CNetAddr source1 = CNetAddr("250.1.2.1");
324 addrman.Create(addr1, source1, &nId);
326 // Test 21: Delete should actually delete the addr.
327 BOOST_CHECK(addrman.size() == 1);
329 BOOST_CHECK(addrman.size() == 0);
330 CAddrInfo* info2 = addrman.Find(addr1);
331 BOOST_CHECK(info2 == NULL);
334 BOOST_AUTO_TEST_CASE(addrman_getaddr)
336 CAddrManTest addrman;
338 // Set addrman addr placement to be deterministic.
339 addrman.MakeDeterministic();
341 // Test 22: Sanity check, GetAddr should never return anything if addrman
343 BOOST_CHECK(addrman.size() == 0);
344 vector<CAddress> vAddr1 = addrman.GetAddr();
345 BOOST_CHECK(vAddr1.size() == 0);
347 CAddress addr1 = CAddress(CService("250.250.2.1", 8333));
348 addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
349 CAddress addr2 = CAddress(CService("250.251.2.2", 9999));
350 addr2.nTime = GetAdjustedTime();
351 CAddress addr3 = CAddress(CService("251.252.2.3", 8333));
352 addr3.nTime = GetAdjustedTime();
353 CAddress addr4 = CAddress(CService("252.253.3.4", 8333));
354 addr4.nTime = GetAdjustedTime();
355 CAddress addr5 = CAddress(CService("252.254.4.5", 8333));
356 addr5.nTime = GetAdjustedTime();
357 CNetAddr source1 = CNetAddr("250.1.2.1");
358 CNetAddr source2 = CNetAddr("250.2.3.3");
360 // Test 23: Ensure GetAddr works with new addresses.
361 addrman.Add(addr1, source1);
362 addrman.Add(addr2, source2);
363 addrman.Add(addr3, source1);
364 addrman.Add(addr4, source2);
365 addrman.Add(addr5, source1);
367 // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
368 BOOST_CHECK(addrman.GetAddr().size() == 1);
370 // Test 24: Ensure GetAddr works with new and tried addresses.
371 addrman.Good(CAddress(addr1));
372 addrman.Good(CAddress(addr2));
373 BOOST_CHECK(addrman.GetAddr().size() == 1);
375 // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
376 for (unsigned int i = 1; i < (8 * 256); i++) {
377 int octet1 = i % 256;
378 int octet2 = (i / 256) % 256;
379 int octet3 = (i / (256 * 2)) % 256;
380 string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23";
381 CAddress addr = CAddress(CService(strAddr));
383 // Ensure that for all addrs in addrman, isTerrible == false.
384 addr.nTime = GetAdjustedTime();
385 addrman.Add(addr, CNetAddr(strAddr));
389 vector<CAddress> vAddr = addrman.GetAddr();
391 size_t percent23 = (addrman.size() * 23) / 100;
392 BOOST_CHECK(vAddr.size() == percent23);
393 BOOST_CHECK(vAddr.size() == 461);
394 // (Addrman.size() < number of addresses added) due to address collisons.
395 BOOST_CHECK(addrman.size() == 2007);
399 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
401 CAddrManTest addrman;
403 // Set addrman addr placement to be deterministic.
404 addrman.MakeDeterministic();
406 CAddress addr1 = CAddress(CService("250.1.1.1", 8333));
407 CAddress addr2 = CAddress(CService("250.1.1.1", 9999));
409 CNetAddr source1 = CNetAddr("250.1.1.1");
412 CAddrInfo info1 = CAddrInfo(addr1, source1);
414 uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
415 uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
418 BOOST_CHECK(info1.GetTriedBucket(nKey1) == 40);
420 // Test 26: Make sure key actually randomizes bucket placement. A fail on
421 // this test could be a security issue.
422 BOOST_CHECK(info1.GetTriedBucket(nKey1) != info1.GetTriedBucket(nKey2));
424 // Test 27: Two addresses with same IP but different ports can map to
425 // different buckets because they have different keys.
426 CAddrInfo info2 = CAddrInfo(addr2, source1);
428 BOOST_CHECK(info1.GetKey() != info2.GetKey());
429 BOOST_CHECK(info1.GetTriedBucket(nKey1) != info2.GetTriedBucket(nKey1));
432 for (int i = 0; i < 255; i++) {
433 CAddrInfo infoi = CAddrInfo(
434 CAddress(CService("250.1.1." + boost::to_string(i))),
435 CNetAddr("250.1.1." + boost::to_string(i)));
436 int bucket = infoi.GetTriedBucket(nKey1);
437 buckets.insert(bucket);
439 // Test 28: IP addresses in the same group (\16 prefix for IPv4) should
440 // never get more than 8 buckets
441 BOOST_CHECK(buckets.size() == 8);
444 for (int j = 0; j < 255; j++) {
445 CAddrInfo infoj = CAddrInfo(
446 CAddress(CService("250." + boost::to_string(j) + ".1.1")),
447 CNetAddr("250." + boost::to_string(j) + ".1.1"));
448 int bucket = infoj.GetTriedBucket(nKey1);
449 buckets.insert(bucket);
451 // Test 29: IP addresses in the different groups should map to more than
453 BOOST_CHECK(buckets.size() == 160);
456 BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
458 CAddrManTest addrman;
460 // Set addrman addr placement to be deterministic.
461 addrman.MakeDeterministic();
463 CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
464 CAddress addr2 = CAddress(CService("250.1.2.1", 9999));
466 CNetAddr source1 = CNetAddr("250.1.2.1");
468 CAddrInfo info1 = CAddrInfo(addr1, source1);
470 uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
471 uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
473 BOOST_CHECK(info1.GetNewBucket(nKey1) == 786);
475 // Test 30: Make sure key actually randomizes bucket placement. A fail on
476 // this test could be a security issue.
477 BOOST_CHECK(info1.GetNewBucket(nKey1) != info1.GetNewBucket(nKey2));
479 // Test 31: Ports should not affect bucket placement in the addr
480 CAddrInfo info2 = CAddrInfo(addr2, source1);
481 BOOST_CHECK(info1.GetKey() != info2.GetKey());
482 BOOST_CHECK(info1.GetNewBucket(nKey1) == info2.GetNewBucket(nKey1));
485 for (int i = 0; i < 255; i++) {
486 CAddrInfo infoi = CAddrInfo(
487 CAddress(CService("250.1.1." + boost::to_string(i))),
488 CNetAddr("250.1.1." + boost::to_string(i)));
489 int bucket = infoi.GetNewBucket(nKey1);
490 buckets.insert(bucket);
492 // Test 32: IP addresses in the same group (\16 prefix for IPv4) should
493 // always map to the same bucket.
494 BOOST_CHECK(buckets.size() == 1);
497 for (int j = 0; j < 4 * 255; j++) {
498 CAddrInfo infoj = CAddrInfo(CAddress(
500 boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1")),
501 CNetAddr("251.4.1.1"));
502 int bucket = infoj.GetNewBucket(nKey1);
503 buckets.insert(bucket);
505 // Test 33: IP addresses in the same source groups should map to no more
507 BOOST_CHECK(buckets.size() <= 64);
510 for (int p = 0; p < 255; p++) {
511 CAddrInfo infoj = CAddrInfo(
512 CAddress(CService("250.1.1.1")),
513 CNetAddr("250." + boost::to_string(p) + ".1.1"));
514 int bucket = infoj.GetNewBucket(nKey1);
515 buckets.insert(bucket);
517 // Test 34: IP addresses in the different source groups should map to more
519 BOOST_CHECK(buckets.size() > 64);
521 BOOST_AUTO_TEST_SUITE_END()