Skip to content

Commit aba0ebd

Browse files
committed
merge bitcoin#23477: tidy up unit tests
1 parent cdc8321 commit aba0ebd

File tree

2 files changed

+58
-84
lines changed

2 files changed

+58
-84
lines changed

src/addrman.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class DbInconsistentError : public std::exception
6464
*/
6565
class AddrMan
6666
{
67+
protected:
6768
const std::unique_ptr<AddrManImpl> m_impl;
6869

6970
public:
@@ -149,9 +150,6 @@ class AddrMan
149150
AddrInfo GetAddressInfo(const CService& addr);
150151

151152
const std::vector<bool>& GetAsmap() const;
152-
153-
friend class AddrManTest;
154-
friend class AddrManDeterministic;
155153
};
156154

157155
#endif // BITCOIN_ADDRMAN_H

src/test/addrman_tests.cpp

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,81 +20,20 @@
2020

2121
using namespace std::literals;
2222

23-
class AddrManSerializationMock : public AddrMan
24-
{
25-
public:
26-
virtual void Serialize(CDataStream& s) const = 0;
27-
28-
AddrManSerializationMock()
29-
: AddrMan(/* asmap */ std::vector<bool>(), /* deterministic */ true, /* consistency_check_ratio */ 100)
30-
{}
31-
};
32-
33-
class AddrManUncorrupted : public AddrManSerializationMock
34-
{
35-
public:
36-
void Serialize(CDataStream& s) const override
37-
{
38-
AddrMan::Serialize(s);
39-
}
40-
};
41-
42-
class AddrManCorrupted : public AddrManSerializationMock
43-
{
44-
public:
45-
void Serialize(CDataStream& s) const override
46-
{
47-
// Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
48-
unsigned char nVersion = 1;
49-
s << nVersion;
50-
s << ((unsigned char)32);
51-
s << uint256::ONE;
52-
s << 10; // nNew
53-
s << 10; // nTried
54-
55-
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
56-
s << nUBuckets;
57-
58-
CService serv;
59-
BOOST_CHECK(Lookup("252.1.1.1", serv, 7777, false));
60-
CAddress addr = CAddress(serv, NODE_NONE);
61-
CNetAddr resolved;
62-
BOOST_CHECK(LookupHost("252.2.2.2", resolved, false));
63-
AddrInfo info = AddrInfo(addr, resolved);
64-
s << info;
65-
}
66-
};
67-
68-
static CDataStream AddrmanToStream(const AddrManSerializationMock& _addrman)
69-
{
70-
CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
71-
ssPeersIn << Params().MessageStart();
72-
ssPeersIn << _addrman;
73-
std::string str = ssPeersIn.str();
74-
std::vector<unsigned char> vchData(str.begin(), str.end());
75-
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
76-
}
77-
7823
class AddrManTest : public AddrMan
7924
{
80-
private:
81-
bool deterministic;
82-
8325
public:
84-
explicit AddrManTest(bool makeDeterministic = true,
85-
std::vector<bool> asmap = std::vector<bool>())
86-
: AddrMan(asmap, makeDeterministic, /* consistency_check_ratio */ 100)
87-
{
88-
deterministic = makeDeterministic;
89-
}
26+
explicit AddrManTest(std::vector<bool> asmap = std::vector<bool>())
27+
: AddrMan(asmap, /*deterministic=*/true, /* consistency_check_ratio */ 100)
28+
{}
9029

91-
AddrInfo* Find(const CService& addr, int* pnId = nullptr)
30+
AddrInfo* Find(const CService& addr)
9231
{
9332
LOCK(m_impl->cs);
94-
return m_impl->Find(addr, pnId);
33+
return m_impl->Find(addr);
9534
}
9635

97-
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr)
36+
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
9837
{
9938
LOCK(m_impl->cs);
10039
return m_impl->Create(addr, addrSource, pnId);
@@ -758,8 +697,8 @@ BOOST_AUTO_TEST_CASE(addrman_serialization)
758697
{
759698
std::vector<bool> asmap1 = FromBytes(raw_tests::asmap, sizeof(raw_tests::asmap) * 8);
760699

761-
auto addrman_asmap1 = std::make_unique<AddrManTest>(true, asmap1);
762-
auto addrman_asmap1_dup = std::make_unique<AddrManTest>(true, asmap1);
700+
auto addrman_asmap1 = std::make_unique<AddrManTest>(asmap1);
701+
auto addrman_asmap1_dup = std::make_unique<AddrManTest>(asmap1);
763702
auto addrman_noasmap = std::make_unique<AddrManTest>();
764703
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
765704

@@ -790,7 +729,7 @@ BOOST_AUTO_TEST_CASE(addrman_serialization)
790729
BOOST_CHECK(bucketAndEntry_asmap1.second != bucketAndEntry_noasmap.second);
791730

792731
// deserializing non-asmaped peers.dat to asmaped addrman
793-
addrman_asmap1 = std::make_unique<AddrManTest>(true, asmap1);
732+
addrman_asmap1 = std::make_unique<AddrManTest>(asmap1);
794733
addrman_noasmap = std::make_unique<AddrManTest>();
795734
addrman_noasmap->Add({addr}, default_source);
796735
stream << *addrman_noasmap;
@@ -802,7 +741,7 @@ BOOST_AUTO_TEST_CASE(addrman_serialization)
802741
BOOST_CHECK(bucketAndEntry_asmap1_deser.second == bucketAndEntry_asmap1_dup.second);
803742

804743
// used to map to different buckets, now maps to the same bucket.
805-
addrman_asmap1 = std::make_unique<AddrManTest>(true, asmap1);
744+
addrman_asmap1 = std::make_unique<AddrManTest>(asmap1);
806745
addrman_noasmap = std::make_unique<AddrManTest>();
807746
CAddress addr1 = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
808747
CAddress addr2 = CAddress(ResolveService("250.2.1.1"), NODE_NONE);
@@ -1002,9 +941,20 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
1002941
BOOST_CHECK(addrman.SelectTriedCollision().first.ToString() == "[::]:0");
1003942
}
1004943

944+
static CDataStream AddrmanToStream(const AddrMan& addrman)
945+
{
946+
CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
947+
ssPeersIn << Params().MessageStart();
948+
ssPeersIn << addrman;
949+
std::string str = ssPeersIn.str();
950+
std::vector<unsigned char> vchData(str.begin(), str.end());
951+
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
952+
}
953+
1005954
BOOST_AUTO_TEST_CASE(load_addrman)
1006955
{
1007-
AddrManUncorrupted addrmanUncorrupted;
956+
AddrMan addrman{/*asmap=*/ std::vector<bool>(), /*deterministic=*/ true,
957+
/*consistency_check_ratio=*/ 100};
1008958

1009959
CService addr1, addr2, addr3;
1010960
BOOST_CHECK(Lookup("250.7.1.1", addr1, 8333, false));
@@ -1017,11 +967,11 @@ BOOST_AUTO_TEST_CASE(load_addrman)
1017967
CService source;
1018968
BOOST_CHECK(Lookup("252.5.1.1", source, 8333, false));
1019969
std::vector<CAddress> addresses{CAddress(addr1, NODE_NONE), CAddress(addr2, NODE_NONE), CAddress(addr3, NODE_NONE)};
1020-
BOOST_CHECK(addrmanUncorrupted.Add(addresses, source));
1021-
BOOST_CHECK(addrmanUncorrupted.size() == 3);
970+
BOOST_CHECK(addrman.Add(addresses, source));
971+
BOOST_CHECK(addrman.size() == 3);
1022972

1023973
// Test that the de-serialization does not throw an exception.
1024-
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
974+
CDataStream ssPeers1 = AddrmanToStream(addrman);
1025975
bool exceptionThrown = false;
1026976
AddrMan addrman1(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
1027977

@@ -1038,21 +988,47 @@ BOOST_AUTO_TEST_CASE(load_addrman)
1038988
BOOST_CHECK(exceptionThrown == false);
1039989

1040990
// Test that ReadFromStream creates an addrman with the correct number of addrs.
1041-
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
991+
CDataStream ssPeers2 = AddrmanToStream(addrman);
1042992

1043993
AddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
1044994
BOOST_CHECK(addrman2.size() == 0);
1045995
ReadFromStream(addrman2, ssPeers2);
1046996
BOOST_CHECK(addrman2.size() == 3);
1047997
}
1048998

999+
// Produce a corrupt peers.dat that claims 20 addrs when it only has one addr.
1000+
static CDataStream MakeCorruptPeersDat()
1001+
{
1002+
CDataStream s(SER_DISK, CLIENT_VERSION);
1003+
s << ::Params().MessageStart();
1004+
1005+
unsigned char nVersion = 1;
1006+
s << nVersion;
1007+
s << ((unsigned char)32);
1008+
s << uint256::ONE;
1009+
s << 10; // nNew
1010+
s << 10; // nTried
1011+
1012+
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
1013+
s << nUBuckets;
1014+
1015+
CService serv;
1016+
BOOST_CHECK(Lookup("252.1.1.1", serv, 7777, false));
1017+
CAddress addr = CAddress(serv, NODE_NONE);
1018+
CNetAddr resolved;
1019+
BOOST_CHECK(LookupHost("252.2.2.2", resolved, false));
1020+
AddrInfo info = AddrInfo(addr, resolved);
1021+
s << info;
1022+
1023+
std::string str = s.str();
1024+
std::vector<unsigned char> vchData(str.begin(), str.end());
1025+
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
1026+
}
10491027

10501028
BOOST_AUTO_TEST_CASE(load_addrman_corrupted)
10511029
{
1052-
AddrManCorrupted addrmanCorrupted;
1053-
1054-
// Test that the de-serialization of corrupted addrman throws an exception.
1055-
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
1030+
// Test that the de-serialization of corrupted peers.dat throws an exception.
1031+
CDataStream ssPeers1 = MakeCorruptPeersDat();
10561032
bool exceptionThrown = false;
10571033
AddrMan addrman1(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
10581034
BOOST_CHECK(addrman1.size() == 0);
@@ -1068,7 +1044,7 @@ BOOST_AUTO_TEST_CASE(load_addrman_corrupted)
10681044
BOOST_CHECK(exceptionThrown);
10691045

10701046
// Test that ReadFromStream fails if peers.dat is corrupt
1071-
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
1047+
CDataStream ssPeers2 = MakeCorruptPeersDat();
10721048

10731049
AddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
10741050
BOOST_CHECK(addrman2.size() == 0);

0 commit comments

Comments
 (0)