Skip to content

Commit c9dd5c8

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23492: test: tidy up addrman unit tests
0000eda style: Use 4 spaces for indentation, not 5 (MarcoFalke) fab9264 test: Remove unused CDataStream copy (MarcoFalke) Pull request description: Fix two issues: * Remove unneeded code, which makes test harder to read and write * Fix wrong indentation, which breaks some editors ACKs for top commit: jnewbery: Code review ACK 0000eda brunoerg: cr/tACK 0000eda shaavan: ACK 0000eda Tree-SHA512: babec1dfb840bd11ec89ec3d0e2529f826f5775fc7625e987480fac5251f6a6cdcf9a1051c8364d6ca2f66ce9934dcb8447a8cf7376b3952920faf2afd02be99
2 parents 1ff265a + 0000eda commit c9dd5c8

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/test/addrman_tests.cpp

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ class AddrManTest : public AddrMan
6565
// Simulates connection failure so that we can test eviction of offline nodes
6666
void SimConnFail(const CService& addr)
6767
{
68-
int64_t nLastSuccess = 1;
69-
// Set last good connection in the deep past.
70-
Good(addr, nLastSuccess);
71-
72-
bool count_failure = false;
73-
int64_t nLastTry = GetAdjustedTime()-61;
74-
Attempt(addr, count_failure, nLastTry);
75-
}
68+
int64_t nLastSuccess = 1;
69+
// Set last good connection in the deep past.
70+
Good(addr, nLastSuccess);
71+
72+
bool count_failure = false;
73+
int64_t nLastTry = GetAdjustedTime() - 61;
74+
Attempt(addr, count_failure, nLastTry);
75+
}
7676
};
7777

7878
static CNetAddr ResolveIP(const std::string& ip)
@@ -90,7 +90,8 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
9090
}
9191

9292

93-
static std::vector<bool> FromBytes(const unsigned char* source, int vector_size) {
93+
static std::vector<bool> FromBytes(const unsigned char* source, int vector_size)
94+
{
9495
std::vector<bool> result(vector_size);
9596
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
9697
unsigned char cur_byte = source[byte_i];
@@ -246,15 +247,15 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
246247

247248
BOOST_CHECK_EQUAL(addrman.size(), num_addrs);
248249

249-
while (num_addrs < 22) { // Magic number! 250.1.1.1 - 250.1.1.22 do not collide with deterministic key = 1
250+
while (num_addrs < 22) { // Magic number! 250.1.1.1 - 250.1.1.22 do not collide with deterministic key = 1
250251
CService addr = ResolveService("250.1.1." + ToString(++num_addrs));
251252
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
252253

253-
//Test: No collision in new table yet.
254+
// Test: No collision in new table yet.
254255
BOOST_CHECK_EQUAL(addrman.size(), num_addrs);
255256
}
256257

257-
//Test: new table collision!
258+
// Test: new table collision!
258259
CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
259260
uint32_t collisions{1};
260261
BOOST_CHECK(addrman.Add({CAddress(addr1, NODE_NONE)}, source));
@@ -275,16 +276,16 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
275276

276277
BOOST_CHECK_EQUAL(addrman.size(), num_addrs);
277278

278-
while (num_addrs < 64) { // Magic number! 250.1.1.1 - 250.1.1.64 do not collide with deterministic key = 1
279+
while (num_addrs < 64) { // Magic number! 250.1.1.1 - 250.1.1.64 do not collide with deterministic key = 1
279280
CService addr = ResolveService("250.1.1." + ToString(++num_addrs));
280281
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
281282
addrman.Good(CAddress(addr, NODE_NONE));
282283

283-
//Test: No collision in tried table yet.
284+
// Test: No collision in tried table yet.
284285
BOOST_CHECK_EQUAL(addrman.size(), num_addrs);
285286
}
286287

287-
//Test: tried table collision!
288+
// Test: tried table collision!
288289
CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
289290
uint32_t collisions{1};
290291
BOOST_CHECK(!addrman.Add({CAddress(addr1, NODE_NONE)}, source));
@@ -693,7 +694,6 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
693694
// Test: IP addresses in the different source /16 prefixes sometimes map to NO MORE
694695
// than 1 bucket.
695696
BOOST_CHECK(buckets.size() == 1);
696-
697697
}
698698

699699
BOOST_AUTO_TEST_CASE(addrman_serialization)
@@ -814,7 +814,7 @@ BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision)
814814
// Add twenty two addresses.
815815
CNetAddr source = ResolveIP("252.2.2.2");
816816
for (unsigned int i = 1; i < 23; i++) {
817-
CService addr = ResolveService("250.1.1."+ToString(i));
817+
CService addr = ResolveService("250.1.1." + ToString(i));
818818
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
819819
addrman.Good(addr);
820820

@@ -825,13 +825,12 @@ BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision)
825825

826826
// Ensure Good handles duplicates well.
827827
for (unsigned int i = 1; i < 23; i++) {
828-
CService addr = ResolveService("250.1.1."+ToString(i));
828+
CService addr = ResolveService("250.1.1." + ToString(i));
829829
addrman.Good(addr);
830830

831831
BOOST_CHECK(addrman.size() == 22);
832832
BOOST_CHECK(addrman.SelectTriedCollision().first.ToString() == "[::]:0");
833833
}
834-
835834
}
836835

837836
BOOST_AUTO_TEST_CASE(addrman_noevict)
@@ -841,7 +840,7 @@ BOOST_AUTO_TEST_CASE(addrman_noevict)
841840
// Add 35 addresses.
842841
CNetAddr source = ResolveIP("252.2.2.2");
843842
for (unsigned int i = 1; i < 36; i++) {
844-
CService addr = ResolveService("250.1.1."+ToString(i));
843+
CService addr = ResolveService("250.1.1." + ToString(i));
845844
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
846845
addrman.Good(addr);
847846

@@ -864,7 +863,7 @@ BOOST_AUTO_TEST_CASE(addrman_noevict)
864863

865864
// Lets create two collisions.
866865
for (unsigned int i = 37; i < 59; i++) {
867-
CService addr = ResolveService("250.1.1."+ToString(i));
866+
CService addr = ResolveService("250.1.1." + ToString(i));
868867
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
869868
addrman.Good(addr);
870869

@@ -902,7 +901,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
902901
// Add 35 addresses
903902
CNetAddr source = ResolveIP("252.2.2.2");
904903
for (unsigned int i = 1; i < 36; i++) {
905-
CService addr = ResolveService("250.1.1."+ToString(i));
904+
CService addr = ResolveService("250.1.1." + ToString(i));
906905
BOOST_CHECK(addrman.Add({CAddress(addr, NODE_NONE)}, source));
907906
addrman.Good(addr);
908907

@@ -949,9 +948,7 @@ static CDataStream AddrmanToStream(const AddrMan& addrman)
949948
CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
950949
ssPeersIn << Params().MessageStart();
951950
ssPeersIn << addrman;
952-
std::string str = ssPeersIn.str();
953-
std::vector<unsigned char> vchData(str.begin(), str.end());
954-
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
951+
return ssPeersIn;
955952
}
956953

957954
BOOST_AUTO_TEST_CASE(load_addrman)
@@ -1023,9 +1020,7 @@ static CDataStream MakeCorruptPeersDat()
10231020
AddrInfo info = AddrInfo(addr, resolved);
10241021
s << info;
10251022

1026-
std::string str = s.str();
1027-
std::vector<unsigned char> vchData(str.begin(), str.end());
1028-
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
1023+
return s;
10291024
}
10301025

10311026
BOOST_AUTO_TEST_CASE(load_addrman_corrupted)

0 commit comments

Comments
 (0)