|
22 | 22 |
|
23 | 23 | #include <test/util/setup_common.h>
|
24 | 24 |
|
| 25 | +#include <array> |
25 | 26 | #include <stdint.h>
|
26 | 27 |
|
27 | 28 | #include <boost/test/unit_test.hpp>
|
@@ -213,42 +214,54 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
|
213 | 214 | auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
|
214 | 215 | *m_node.scheduler, *m_node.chainman, *m_node.mempool, false);
|
215 | 216 |
|
| 217 | + const std::array<CAddress, 2> addr{CAddress{ip(0xa0b0c001), NODE_NONE}, |
| 218 | + CAddress{ip(0xa0b0c002), NODE_NONE}}; |
| 219 | + |
| 220 | + const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[]. |
| 221 | + |
| 222 | + std::array<CNode*, 2> nodes; |
| 223 | + |
216 | 224 | banman->ClearBanned();
|
217 |
| - CAddress addr1(ip(0xa0b0c001), NODE_NONE); |
218 |
| - CNode dummyNode1(id++, NODE_NETWORK, INVALID_SOCKET, addr1, /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false); |
219 |
| - dummyNode1.SetCommonVersion(PROTOCOL_VERSION); |
220 |
| - peerLogic->InitializeNode(&dummyNode1); |
221 |
| - dummyNode1.fSuccessfullyConnected = true; |
222 |
| - peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged |
| 225 | + nodes[0] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[0], /* nKeyedNetGroupIn */ 0, |
| 226 | + /* nLocalHostNonceIn */ 0, CAddress(), /* pszDest */ "", |
| 227 | + ConnectionType::INBOUND, /* inbound_onion */ false}; |
| 228 | + nodes[0]->SetCommonVersion(PROTOCOL_VERSION); |
| 229 | + peerLogic->InitializeNode(nodes[0]); |
| 230 | + nodes[0]->fSuccessfullyConnected = true; |
| 231 | + peerLogic->Misbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD, /* message */ ""); // Should be discouraged |
223 | 232 | {
|
224 |
| - LOCK(dummyNode1.cs_sendProcessing); |
225 |
| - BOOST_CHECK(peerLogic->SendMessages(&dummyNode1)); |
| 233 | + LOCK(nodes[0]->cs_sendProcessing); |
| 234 | + BOOST_CHECK(peerLogic->SendMessages(nodes[0])); |
226 | 235 | }
|
227 |
| - BOOST_CHECK(banman->IsDiscouraged(addr1)); |
228 |
| - BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001|0x0000ff00))); // Different IP, not discouraged |
229 |
| - |
230 |
| - CAddress addr2(ip(0xa0b0c002), NODE_NONE); |
231 |
| - CNode dummyNode2(id++, NODE_NETWORK, INVALID_SOCKET, addr2, /* nKeyedNetGroupIn */ 1, /* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "", ConnectionType::INBOUND, /* inbound_onion */ false); |
232 |
| - dummyNode2.SetCommonVersion(PROTOCOL_VERSION); |
233 |
| - peerLogic->InitializeNode(&dummyNode2); |
234 |
| - dummyNode2.fSuccessfullyConnected = true; |
235 |
| - peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ ""); |
| 236 | + BOOST_CHECK(banman->IsDiscouraged(addr[0])); |
| 237 | + BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged |
| 238 | + |
| 239 | + nodes[1] = new CNode{id++, NODE_NETWORK, INVALID_SOCKET, addr[1], /* nKeyedNetGroupIn */ 1, |
| 240 | + /* nLocalHostNonceIn */ 1, CAddress(), /* pszDest */ "", |
| 241 | + ConnectionType::INBOUND, /* inbound_onion */ false}; |
| 242 | + nodes[1]->SetCommonVersion(PROTOCOL_VERSION); |
| 243 | + peerLogic->InitializeNode(nodes[1]); |
| 244 | + nodes[1]->fSuccessfullyConnected = true; |
| 245 | + peerLogic->Misbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1, /* message */ ""); |
236 | 246 | {
|
237 |
| - LOCK(dummyNode2.cs_sendProcessing); |
238 |
| - BOOST_CHECK(peerLogic->SendMessages(&dummyNode2)); |
| 247 | + LOCK(nodes[1]->cs_sendProcessing); |
| 248 | + BOOST_CHECK(peerLogic->SendMessages(nodes[1])); |
239 | 249 | }
|
240 |
| - BOOST_CHECK(!banman->IsDiscouraged(addr2)); // 2 not discouraged yet... |
241 |
| - BOOST_CHECK(banman->IsDiscouraged(addr1)); // ... but 1 still should be |
242 |
| - peerLogic->Misbehaving(dummyNode2.GetId(), 1, /* message */ ""); // 2 reaches discouragement threshold |
| 250 | + BOOST_CHECK(!banman->IsDiscouraged(addr[1])); // [1] not discouraged yet... |
| 251 | + BOOST_CHECK(banman->IsDiscouraged(addr[0])); // ... but [0] still should be |
| 252 | + peerLogic->Misbehaving(nodes[1]->GetId(), 1, /* message */ ""); // [1] reaches discouragement threshold |
243 | 253 | {
|
244 |
| - LOCK(dummyNode2.cs_sendProcessing); |
245 |
| - BOOST_CHECK(peerLogic->SendMessages(&dummyNode2)); |
| 254 | + LOCK(nodes[1]->cs_sendProcessing); |
| 255 | + BOOST_CHECK(peerLogic->SendMessages(nodes[1])); |
246 | 256 | }
|
247 |
| - BOOST_CHECK(banman->IsDiscouraged(addr1)); // Expect both 1 and 2 |
248 |
| - BOOST_CHECK(banman->IsDiscouraged(addr2)); // to be discouraged now |
| 257 | + // Expect both [0] and [1] to be discouraged now. |
| 258 | + BOOST_CHECK(banman->IsDiscouraged(addr[0])); |
| 259 | + BOOST_CHECK(banman->IsDiscouraged(addr[1])); |
249 | 260 |
|
250 |
| - peerLogic->FinalizeNode(dummyNode1); |
251 |
| - peerLogic->FinalizeNode(dummyNode2); |
| 261 | + for (CNode* node : nodes) { |
| 262 | + peerLogic->FinalizeNode(*node); |
| 263 | + delete node; |
| 264 | + } |
252 | 265 | }
|
253 | 266 |
|
254 | 267 | BOOST_AUTO_TEST_CASE(DoS_bantime)
|
|
0 commit comments