Skip to content

Commit aaa6ad5

Browse files
committed
[MOVEONLY] [tests] Move addrman ser/deser tests to addrman_tests.cpp
Addrman serialization/deserialization tests are currently in net_tests.cpp. Move them to addrman_tests.cpp with the rest of the addrman tests. Reviewer hint: review using `git diff --color-moved=dimmed-zebra`
1 parent fdd80b0 commit aaa6ad5

File tree

2 files changed

+134
-131
lines changed

2 files changed

+134
-131
lines changed

src/test/addrman_tests.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (c) 2012-2020 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <addrdb.h>
46
#include <addrman.h>
7+
#include <chainparams.h>
58
#include <test/data/asmap.raw.h>
69
#include <test/util/setup_common.h>
710
#include <util/asmap.h>
@@ -15,6 +18,63 @@
1518
#include <optional>
1619
#include <string>
1720

21+
using namespace std::literals;
22+
23+
class CAddrManSerializationMock : public CAddrMan
24+
{
25+
public:
26+
virtual void Serialize(CDataStream& s) const = 0;
27+
28+
CAddrManSerializationMock()
29+
: CAddrMan(/* deterministic */ true, /* consistency_check_ratio */ 100)
30+
{}
31+
};
32+
33+
class CAddrManUncorrupted : public CAddrManSerializationMock
34+
{
35+
public:
36+
void Serialize(CDataStream& s) const override
37+
{
38+
CAddrMan::Serialize(s);
39+
}
40+
};
41+
42+
class CAddrManCorrupted : public CAddrManSerializationMock
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 << nKey;
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+
CAddrInfo info = CAddrInfo(addr, resolved);
64+
s << info;
65+
}
66+
};
67+
68+
static CDataStream AddrmanToStream(const CAddrManSerializationMock& _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+
1878
class CAddrManTest : public CAddrMan
1979
{
2080
private:
@@ -958,5 +1018,79 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
9581018
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
9591019
}
9601020

1021+
BOOST_AUTO_TEST_CASE(caddrdb_read)
1022+
{
1023+
CAddrManUncorrupted addrmanUncorrupted;
1024+
1025+
CService addr1, addr2, addr3;
1026+
BOOST_CHECK(Lookup("250.7.1.1", addr1, 8333, false));
1027+
BOOST_CHECK(Lookup("250.7.2.2", addr2, 9999, false));
1028+
BOOST_CHECK(Lookup("250.7.3.3", addr3, 9999, false));
1029+
BOOST_CHECK(Lookup("250.7.3.3"s, addr3, 9999, false));
1030+
BOOST_CHECK(!Lookup("250.7.3.3\0example.com"s, addr3, 9999, false));
1031+
1032+
// Add three addresses to new table.
1033+
CService source;
1034+
BOOST_CHECK(Lookup("252.5.1.1", source, 8333, false));
1035+
std::vector<CAddress> addresses{CAddress(addr1, NODE_NONE), CAddress(addr2, NODE_NONE), CAddress(addr3, NODE_NONE)};
1036+
BOOST_CHECK(addrmanUncorrupted.Add(addresses, source));
1037+
BOOST_CHECK(addrmanUncorrupted.size() == 3);
1038+
1039+
// Test that the de-serialization does not throw an exception.
1040+
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
1041+
bool exceptionThrown = false;
1042+
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
1043+
1044+
BOOST_CHECK(addrman1.size() == 0);
1045+
try {
1046+
unsigned char pchMsgTmp[4];
1047+
ssPeers1 >> pchMsgTmp;
1048+
ssPeers1 >> addrman1;
1049+
} catch (const std::exception&) {
1050+
exceptionThrown = true;
1051+
}
1052+
1053+
BOOST_CHECK(addrman1.size() == 3);
1054+
BOOST_CHECK(exceptionThrown == false);
1055+
1056+
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
1057+
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
1058+
1059+
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
1060+
BOOST_CHECK(addrman2.size() == 0);
1061+
BOOST_CHECK(CAddrDB::Read(addrman2, ssPeers2));
1062+
BOOST_CHECK(addrman2.size() == 3);
1063+
}
1064+
1065+
1066+
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
1067+
{
1068+
CAddrManCorrupted addrmanCorrupted;
1069+
1070+
// Test that the de-serialization of corrupted addrman throws an exception.
1071+
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
1072+
bool exceptionThrown = false;
1073+
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
1074+
BOOST_CHECK(addrman1.size() == 0);
1075+
try {
1076+
unsigned char pchMsgTmp[4];
1077+
ssPeers1 >> pchMsgTmp;
1078+
ssPeers1 >> addrman1;
1079+
} catch (const std::exception&) {
1080+
exceptionThrown = true;
1081+
}
1082+
// Even through de-serialization failed addrman is not left in a clean state.
1083+
BOOST_CHECK(addrman1.size() == 1);
1084+
BOOST_CHECK(exceptionThrown);
1085+
1086+
// Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
1087+
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
1088+
1089+
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
1090+
BOOST_CHECK(addrman2.size() == 0);
1091+
BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2));
1092+
BOOST_CHECK(addrman2.size() == 0);
1093+
}
1094+
9611095

9621096
BOOST_AUTO_TEST_SUITE_END()

src/test/net_tests.cpp

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <addrdb.h>
6-
#include <addrman.h>
75
#include <chainparams.h>
86
#include <clientversion.h>
97
#include <cstdint>
@@ -29,61 +27,6 @@
2927

3028
using namespace std::literals;
3129

32-
class CAddrManSerializationMock : public CAddrMan
33-
{
34-
public:
35-
virtual void Serialize(CDataStream& s) const = 0;
36-
37-
CAddrManSerializationMock()
38-
: CAddrMan(/* deterministic */ true, /* consistency_check_ratio */ 100)
39-
{}
40-
};
41-
42-
class CAddrManUncorrupted : public CAddrManSerializationMock
43-
{
44-
public:
45-
void Serialize(CDataStream& s) const override
46-
{
47-
CAddrMan::Serialize(s);
48-
}
49-
};
50-
51-
class CAddrManCorrupted : public CAddrManSerializationMock
52-
{
53-
public:
54-
void Serialize(CDataStream& s) const override
55-
{
56-
// Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
57-
unsigned char nVersion = 1;
58-
s << nVersion;
59-
s << ((unsigned char)32);
60-
s << nKey;
61-
s << 10; // nNew
62-
s << 10; // nTried
63-
64-
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
65-
s << nUBuckets;
66-
67-
CService serv;
68-
BOOST_CHECK(Lookup("252.1.1.1", serv, 7777, false));
69-
CAddress addr = CAddress(serv, NODE_NONE);
70-
CNetAddr resolved;
71-
BOOST_CHECK(LookupHost("252.2.2.2", resolved, false));
72-
CAddrInfo info = CAddrInfo(addr, resolved);
73-
s << info;
74-
}
75-
};
76-
77-
static CDataStream AddrmanToStream(const CAddrManSerializationMock& _addrman)
78-
{
79-
CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
80-
ssPeersIn << Params().MessageStart();
81-
ssPeersIn << _addrman;
82-
std::string str = ssPeersIn.str();
83-
std::vector<unsigned char> vchData(str.begin(), str.end());
84-
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
85-
}
86-
8730
BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)
8831

8932
BOOST_AUTO_TEST_CASE(cnode_listen_port)
@@ -98,80 +41,6 @@ BOOST_AUTO_TEST_CASE(cnode_listen_port)
9841
BOOST_CHECK(port == altPort);
9942
}
10043

101-
BOOST_AUTO_TEST_CASE(caddrdb_read)
102-
{
103-
CAddrManUncorrupted addrmanUncorrupted;
104-
105-
CService addr1, addr2, addr3;
106-
BOOST_CHECK(Lookup("250.7.1.1", addr1, 8333, false));
107-
BOOST_CHECK(Lookup("250.7.2.2", addr2, 9999, false));
108-
BOOST_CHECK(Lookup("250.7.3.3", addr3, 9999, false));
109-
BOOST_CHECK(Lookup("250.7.3.3"s, addr3, 9999, false));
110-
BOOST_CHECK(!Lookup("250.7.3.3\0example.com"s, addr3, 9999, false));
111-
112-
// Add three addresses to new table.
113-
CService source;
114-
BOOST_CHECK(Lookup("252.5.1.1", source, 8333, false));
115-
std::vector<CAddress> addresses{CAddress(addr1, NODE_NONE), CAddress(addr2, NODE_NONE), CAddress(addr3, NODE_NONE)};
116-
BOOST_CHECK(addrmanUncorrupted.Add(addresses, source));
117-
BOOST_CHECK(addrmanUncorrupted.size() == 3);
118-
119-
// Test that the de-serialization does not throw an exception.
120-
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
121-
bool exceptionThrown = false;
122-
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
123-
124-
BOOST_CHECK(addrman1.size() == 0);
125-
try {
126-
unsigned char pchMsgTmp[4];
127-
ssPeers1 >> pchMsgTmp;
128-
ssPeers1 >> addrman1;
129-
} catch (const std::exception&) {
130-
exceptionThrown = true;
131-
}
132-
133-
BOOST_CHECK(addrman1.size() == 3);
134-
BOOST_CHECK(exceptionThrown == false);
135-
136-
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
137-
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
138-
139-
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
140-
BOOST_CHECK(addrman2.size() == 0);
141-
BOOST_CHECK(CAddrDB::Read(addrman2, ssPeers2));
142-
BOOST_CHECK(addrman2.size() == 3);
143-
}
144-
145-
146-
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
147-
{
148-
CAddrManCorrupted addrmanCorrupted;
149-
150-
// Test that the de-serialization of corrupted addrman throws an exception.
151-
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
152-
bool exceptionThrown = false;
153-
CAddrMan addrman1(/* deterministic */ false, /* consistency_check_ratio */ 100);
154-
BOOST_CHECK(addrman1.size() == 0);
155-
try {
156-
unsigned char pchMsgTmp[4];
157-
ssPeers1 >> pchMsgTmp;
158-
ssPeers1 >> addrman1;
159-
} catch (const std::exception&) {
160-
exceptionThrown = true;
161-
}
162-
// Even through de-serialization failed addrman is not left in a clean state.
163-
BOOST_CHECK(addrman1.size() == 1);
164-
BOOST_CHECK(exceptionThrown);
165-
166-
// Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
167-
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
168-
169-
CAddrMan addrman2(/* deterministic */ false, /* consistency_check_ratio */ 100);
170-
BOOST_CHECK(addrman2.size() == 0);
171-
BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2));
172-
BOOST_CHECK(addrman2.size() == 0);
173-
}
174-
17544
BOOST_AUTO_TEST_CASE(cnode_simple_test)
17645
{
17746
SOCKET hSocket = INVALID_SOCKET;

0 commit comments

Comments
 (0)