Skip to content

Commit d479311

Browse files
committed
Merge pull request #6720
1534d9a Creates unittests for addrman, makes addrman testable. Adds several unittests for addrman to verify it works as expected. Makes small modifications to addrman to allow deterministic and targeted tests. (EthanHeilman)
2 parents 49dd5c6 + 1534d9a commit d479311

File tree

4 files changed

+199
-6
lines changed

4 files changed

+199
-6
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.r
3636
BITCOIN_TESTS =\
3737
test/arith_uint256_tests.cpp \
3838
test/bignum.h \
39+
test/addrman_tests.cpp \
3940
test/alert_tests.cpp \
4041
test/allocator_tests.cpp \
4142
test/base32_tests.cpp \

src/addrman.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,17 @@ void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
329329
info.nAttempts++;
330330
}
331331

332-
CAddrInfo CAddrMan::Select_()
332+
CAddrInfo CAddrMan::Select_(bool newOnly)
333333
{
334334
if (size() == 0)
335335
return CAddrInfo();
336336

337+
if (newOnly && nNew == 0)
338+
return CAddrInfo();
339+
337340
// Use a 50% chance for choosing between tried and new table entries.
338-
if (nTried > 0 && (nNew == 0 || GetRandInt(2) == 0)) {
341+
if (!newOnly &&
342+
(nTried > 0 && (nNew == 0 || GetRandInt(2) == 0))) {
339343
// use a tried node
340344
double fChanceFactor = 1.0;
341345
while (1) {

src/addrman.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
class CAddrInfo : public CAddress
2424
{
25+
26+
2527
public:
2628
//! last try whatsoever by us (memory only)
2729
int64_t nLastTry;
@@ -230,8 +232,8 @@ class CAddrMan
230232
//! Mark an entry as attempted to connect.
231233
void Attempt_(const CService &addr, int64_t nTime);
232234

233-
//! Select an address to connect to.
234-
CAddrInfo Select_();
235+
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
236+
CAddrInfo Select_(bool newOnly);
235237

236238
#ifdef DEBUG_ADDRMAN
237239
//! Perform consistency check. Returns an error code or zero.
@@ -532,13 +534,13 @@ class CAddrMan
532534
/**
533535
* Choose an address to connect to.
534536
*/
535-
CAddrInfo Select()
537+
CAddrInfo Select(bool newOnly = false)
536538
{
537539
CAddrInfo addrRet;
538540
{
539541
LOCK(cs);
540542
Check();
541-
addrRet = Select_();
543+
addrRet = Select_(newOnly);
542544
Check();
543545
}
544546
return addrRet;
@@ -567,6 +569,12 @@ class CAddrMan
567569
Check();
568570
}
569571
}
572+
573+
//! Ensure that bucket placement is always the same for testing purposes.
574+
void MakeDeterministic(){
575+
nKey.SetNull(); //Do not use outside of tests.
576+
}
577+
570578
};
571579

572580
#endif // BITCOIN_ADDRMAN_H

src/test/addrman_tests.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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.
4+
#include "addrman.h"
5+
#include "test/test_bitcoin.h"
6+
#include <string>
7+
#include <boost/test/unit_test.hpp>
8+
9+
#include "random.h"
10+
11+
using namespace std;
12+
13+
class CAddrManTest : public CAddrMan{};
14+
15+
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
16+
17+
BOOST_AUTO_TEST_CASE(addrman_simple)
18+
{
19+
CAddrManTest addrman;
20+
21+
// Set addrman addr placement to be deterministic.
22+
addrman.MakeDeterministic();
23+
24+
CNetAddr source = CNetAddr("252.2.2.2:8333");
25+
26+
// Test 1: Does Addrman respond correctly when empty.
27+
BOOST_CHECK(addrman.size() == 0);
28+
CAddrInfo addr_null = addrman.Select();
29+
BOOST_CHECK(addr_null.ToString() == "[::]:0");
30+
31+
// Test 2: Does Addrman::Add work as expected.
32+
CService addr1 = CService("250.1.1.1:8333");
33+
addrman.Add(CAddress(addr1), source);
34+
BOOST_CHECK(addrman.size() == 1);
35+
CAddrInfo addr_ret1 = addrman.Select();
36+
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
37+
38+
// Test 3: Does IP address deduplication work correctly.
39+
// Expected dup IP should not be added.
40+
CService addr1_dup = CService("250.1.1.1:8333");
41+
addrman.Add(CAddress(addr1_dup), source);
42+
BOOST_CHECK(addrman.size() == 1);
43+
44+
45+
// Test 5: New table has one addr and we add a diff addr we should
46+
// have two addrs.
47+
CService addr2 = CService("250.1.1.2:8333");
48+
addrman.Add(CAddress(addr2), source);
49+
BOOST_CHECK(addrman.size() == 2);
50+
51+
// Test 6: AddrMan::Clear() should empty the new table.
52+
addrman.Clear();
53+
BOOST_CHECK(addrman.size() == 0);
54+
CAddrInfo addr_null2 = addrman.Select();
55+
BOOST_CHECK(addr_null2.ToString() == "[::]:0");
56+
}
57+
58+
BOOST_AUTO_TEST_CASE(addrman_ports)
59+
{
60+
CAddrManTest addrman;
61+
62+
// Set addrman addr placement to be deterministic.
63+
addrman.MakeDeterministic();
64+
65+
CNetAddr source = CNetAddr("252.2.2.2:8333");
66+
67+
BOOST_CHECK(addrman.size() == 0);
68+
69+
// Test 7; Addr with same IP but diff port does not replace existing addr.
70+
CService addr1 = CService("250.1.1.1:8333");
71+
addrman.Add(CAddress(addr1), source);
72+
BOOST_CHECK(addrman.size() == 1);
73+
74+
CService addr1_port = CService("250.1.1.1:8334");
75+
addrman.Add(CAddress(addr1_port), source);
76+
BOOST_CHECK(addrman.size() == 1);
77+
CAddrInfo addr_ret2 = addrman.Select();
78+
BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333");
79+
80+
// Test 8: Add same IP but diff port to tried table, it doesn't get added.
81+
// Perhaps this is not ideal behavior but it is the current behavior.
82+
addrman.Good(CAddress(addr1_port));
83+
BOOST_CHECK(addrman.size() == 1);
84+
bool newOnly = true;
85+
CAddrInfo addr_ret3 = addrman.Select(newOnly);
86+
BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
87+
}
88+
89+
90+
BOOST_AUTO_TEST_CASE(addrman_select)
91+
{
92+
CAddrManTest addrman;
93+
94+
// Set addrman addr placement to be deterministic.
95+
addrman.MakeDeterministic();
96+
97+
CNetAddr source = CNetAddr("252.2.2.2:8333");
98+
99+
// Test 9: Select from new with 1 addr in new.
100+
CService addr1 = CService("250.1.1.1:8333");
101+
addrman.Add(CAddress(addr1), source);
102+
BOOST_CHECK(addrman.size() == 1);
103+
104+
bool newOnly = true;
105+
CAddrInfo addr_ret1 = addrman.Select(newOnly);
106+
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
107+
108+
109+
// Test 10: move addr to tried, select from new expected nothing returned.
110+
addrman.Good(CAddress(addr1));
111+
BOOST_CHECK(addrman.size() == 1);
112+
CAddrInfo addr_ret2 = addrman.Select(newOnly);
113+
BOOST_CHECK(addr_ret2.ToString() == "[::]:0");
114+
115+
CAddrInfo addr_ret3 = addrman.Select();
116+
BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
117+
}
118+
119+
BOOST_AUTO_TEST_CASE(addrman_new_collisions)
120+
{
121+
CAddrManTest addrman;
122+
123+
// Set addrman addr placement to be deterministic.
124+
addrman.MakeDeterministic();
125+
126+
CNetAddr source = CNetAddr("252.2.2.2:8333");
127+
128+
BOOST_CHECK(addrman.size() == 0);
129+
130+
for (unsigned int i = 1; i < 4; i++){
131+
CService addr = CService("250.1.1."+boost::to_string(i));
132+
addrman.Add(CAddress(addr), source);
133+
134+
//Test 11: No collision in new table yet.
135+
BOOST_CHECK(addrman.size() == i);
136+
}
137+
138+
//Test 12: new table collision!
139+
CService addr1 = CService("250.1.1.4");
140+
addrman.Add(CAddress(addr1), source);
141+
BOOST_CHECK(addrman.size() == 3);
142+
143+
CService addr2 = CService("250.1.1.5");
144+
addrman.Add(CAddress(addr2), source);
145+
BOOST_CHECK(addrman.size() == 4);
146+
}
147+
148+
BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
149+
{
150+
CAddrManTest addrman;
151+
152+
// Set addrman addr placement to be deterministic.
153+
addrman.MakeDeterministic();
154+
155+
CNetAddr source = CNetAddr("252.2.2.2:8333");
156+
157+
BOOST_CHECK(addrman.size() == 0);
158+
159+
for (unsigned int i = 1; i < 75; i++){
160+
CService addr = CService("250.1.1."+boost::to_string(i));
161+
addrman.Add(CAddress(addr), source);
162+
addrman.Good(CAddress(addr));
163+
164+
//Test 13: No collision in tried table yet.
165+
BOOST_TEST_MESSAGE(addrman.size());
166+
BOOST_CHECK(addrman.size() == i);
167+
}
168+
169+
//Test 14: tried table collision!
170+
CService addr1 = CService("250.1.1.76");
171+
addrman.Add(CAddress(addr1), source);
172+
BOOST_CHECK(addrman.size() == 74);
173+
174+
CService addr2 = CService("250.1.1.77");
175+
addrman.Add(CAddress(addr2), source);
176+
BOOST_CHECK(addrman.size() == 75);
177+
}
178+
179+
180+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)