Changeset View
Changeset View
Standalone View
Standalone View
src/test/denialofservice_tests.cpp
// Copyright (c) 2011-2019 The Bitcoin Core developers | // Copyright (c) 2011-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
// Unit tests for denial-of-service detection/prevention code | // Unit tests for denial-of-service detection/prevention code | ||||
#include <arith_uint256.h> | |||||
#include <banman.h> | #include <banman.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <net.h> | #include <net.h> | ||||
#include <net_processing.h> | #include <net_processing.h> | ||||
#include <pubkey.h> | |||||
#include <script/sign.h> | #include <script/sign.h> | ||||
#include <script/signingprovider.h> | #include <script/signingprovider.h> | ||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
▲ Show 20 Lines • Show All 322 Lines • ▼ Show 20 Lines | static CTransactionRef RandomOrphan() { | ||||
LOCK2(cs_main, g_cs_orphans); | LOCK2(cs_main, g_cs_orphans); | ||||
it = mapOrphanTransactions.lower_bound(InsecureRand256()); | it = mapOrphanTransactions.lower_bound(InsecureRand256()); | ||||
if (it == mapOrphanTransactions.end()) { | if (it == mapOrphanTransactions.end()) { | ||||
it = mapOrphanTransactions.begin(); | it = mapOrphanTransactions.begin(); | ||||
} | } | ||||
return it->second.tx; | return it->second.tx; | ||||
} | } | ||||
static void MakeNewKeyWithFastRandomContext(CKey &key) { | |||||
std::vector<uint8_t> keydata; | |||||
keydata = g_insecure_rand_ctx.randbytes(32); | |||||
key.Set(keydata.data(), keydata.data() + keydata.size(), | |||||
/*fCompressedIn*/ true); | |||||
assert(key.IsValid()); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { | BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { | ||||
// This test had non-deterministic coverage due to | |||||
// randomly selected seeds. | |||||
// This seed is chosen so that all branches of the function | |||||
// ecdsa_signature_parse_der_lax are executed during this test. | |||||
// Specifically branches that run only when an ECDSA | |||||
// signature's R and S values have leading zeros. | |||||
g_insecure_rand_ctx = FastRandomContext(ArithToUint256(arith_uint256(33))); | |||||
CKey key; | CKey key; | ||||
key.MakeNewKey(true); | MakeNewKeyWithFastRandomContext(key); | ||||
FillableSigningProvider keystore; | FillableSigningProvider keystore; | ||||
BOOST_CHECK(keystore.AddKey(key)); | BOOST_CHECK(keystore.AddKey(key)); | ||||
// 50 orphan transactions: | // 50 orphan transactions: | ||||
for (int i = 0; i < 50; i++) { | for (int i = 0; i < 50; i++) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].prevout = COutPoint(TxId(InsecureRand256()), 0); | tx.vin[0].prevout = COutPoint(TxId(InsecureRand256()), 0); | ||||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |