diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/bech32_tests.cpp | 84 | ||||
-rw-r--r-- | src/test/crypto_tests.cpp | 4 | ||||
-rw-r--r-- | src/test/denialofservice_tests.cpp | 70 | ||||
-rw-r--r-- | src/test/fuzz/asmap.cpp | 2 | ||||
-rw-r--r-- | src/test/fuzz/connman.cpp | 1 | ||||
-rw-r--r-- | src/test/fuzz/golomb_rice.cpp | 8 | ||||
-rw-r--r-- | src/test/fuzz/rpc.cpp | 18 | ||||
-rw-r--r-- | src/test/fuzz/script_assets_test_minimizer.cpp | 4 | ||||
-rw-r--r-- | src/test/fuzz/tx_pool.cpp | 17 | ||||
-rw-r--r-- | src/test/fuzz/utxo_snapshot.cpp | 2 | ||||
-rw-r--r-- | src/test/key_io_tests.cpp | 2 | ||||
-rw-r--r-- | src/test/net_tests.cpp | 140 | ||||
-rw-r--r-- | src/test/script_tests.cpp | 10 | ||||
-rw-r--r-- | src/test/streams_tests.cpp | 6 | ||||
-rw-r--r-- | src/test/util/chainstate.h | 3 | ||||
-rw-r--r-- | src/test/util_tests.cpp | 6 |
16 files changed, 231 insertions, 146 deletions
diff --git a/src/test/bech32_tests.cpp b/src/test/bech32_tests.cpp index 8eed959228..51a1d1199e 100644 --- a/src/test/bech32_tests.cpp +++ b/src/test/bech32_tests.cpp @@ -70,34 +70,36 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid) "a12UEL5L", "A12uEL5L", "abcdef1qpzrz9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", + "test1zg69w7y6hn0aqy352euf40x77qddq3dc", }; - static const std::pair<std::string, int> ERRORS[] = { - {"Invalid character or mixed case", 0}, - {"Invalid character or mixed case", 0}, - {"Invalid character or mixed case", 0}, - {"Bech32 string too long", 90}, - {"Missing separator", -1}, - {"Invalid separator position", 0}, - {"Invalid Base 32 character", 2}, - {"Invalid separator position", 2}, - {"Invalid character or mixed case", 8}, - {"Invalid checksum", -1}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters - {"Invalid separator position", 0}, - {"Invalid separator position", 0}, - {"Invalid character or mixed case", 3}, - {"Invalid character or mixed case", 3}, - {"Invalid checksum", 11} + static const std::pair<std::string, std::vector<int>> ERRORS[] = { + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Bech32 string too long", {90}}, + {"Missing separator", {}}, + {"Invalid separator position", {0}}, + {"Invalid Base 32 character", {2}}, + {"Invalid separator position", {2}}, + {"Invalid character or mixed case", {8}}, + {"Invalid checksum", {}}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters + {"Invalid separator position", {0}}, + {"Invalid separator position", {0}}, + {"Invalid character or mixed case", {3, 4, 5, 7}}, + {"Invalid character or mixed case", {3}}, + {"Invalid Bech32 checksum", {11}}, + {"Invalid Bech32 checksum", {9, 16}}, }; + static_assert(std::size(CASES) == std::size(ERRORS), "Bech32 CASES and ERRORS should have the same length"); + int i = 0; for (const std::string& str : CASES) { const auto& err = ERRORS[i]; const auto dec = bech32::Decode(str); BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID); - std::vector<int> error_locations; - std::string error = bech32::LocateErrors(str, error_locations); + auto [error, error_locations] = bech32::LocateErrors(str); BOOST_CHECK_EQUAL(err.first, error); - if (err.second == -1) BOOST_CHECK(error_locations.empty()); - else BOOST_CHECK_EQUAL(err.second, error_locations[0]); + BOOST_CHECK(err.second == error_locations); i++; } } @@ -120,34 +122,36 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid) "16plkw9", "1p2gdwpf", "abcdef1l7aum6echk45nj2s0wdvt2fg8x9yrzpqzd3ryx", + "test1zg69v7y60n00qy352euf40x77qcusag6", }; - static const std::pair<std::string, int> ERRORS[] = { - {"Invalid character or mixed case", 0}, - {"Invalid character or mixed case", 0}, - {"Invalid character or mixed case", 0}, - {"Bech32 string too long", 90}, - {"Missing separator", -1}, - {"Invalid separator position", 0}, - {"Invalid Base 32 character", 2}, - {"Invalid Base 32 character", 3}, - {"Invalid separator position", 2}, - {"Invalid Base 32 character", 8}, - {"Invalid Base 32 character", 7}, - {"Invalid checksum", -1}, - {"Invalid separator position", 0}, - {"Invalid separator position", 0}, - {"Invalid checksum", 21}, + static const std::pair<std::string, std::vector<int>> ERRORS[] = { + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Bech32 string too long", {90}}, + {"Missing separator", {}}, + {"Invalid separator position", {0}}, + {"Invalid Base 32 character", {2}}, + {"Invalid Base 32 character", {3}}, + {"Invalid separator position", {2}}, + {"Invalid Base 32 character", {8}}, + {"Invalid Base 32 character", {7}}, + {"Invalid checksum", {}}, + {"Invalid separator position", {0}}, + {"Invalid separator position", {0}}, + {"Invalid Bech32m checksum", {21}}, + {"Invalid Bech32m checksum", {13, 32}}, }; + static_assert(std::size(CASES) == std::size(ERRORS), "Bech32m CASES and ERRORS should have the same length"); + int i = 0; for (const std::string& str : CASES) { const auto& err = ERRORS[i]; const auto dec = bech32::Decode(str); BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID); - std::vector<int> error_locations; - std::string error = bech32::LocateErrors(str, error_locations); + auto [error, error_locations] = bech32::LocateErrors(str); BOOST_CHECK_EQUAL(err.first, error); - if (err.second == -1) BOOST_CHECK(error_locations.empty()); - else BOOST_CHECK_EQUAL(err.second, error_locations[0]); + BOOST_CHECK(err.second == error_locations); i++; } } diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index cbdedd1b85..bedef5de37 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -770,8 +770,8 @@ static void TestSHA3_256(const std::string& input, const std::string& output) int s1 = InsecureRandRange(in_bytes.size() + 1); int s2 = InsecureRandRange(in_bytes.size() + 1 - s1); int s3 = in_bytes.size() - s1 - s2; - sha.Write(MakeSpan(in_bytes).first(s1)).Write(MakeSpan(in_bytes).subspan(s1, s2)); - sha.Write(MakeSpan(in_bytes).last(s3)).Finalize(out); + sha.Write(Span{in_bytes}.first(s1)).Write(Span{in_bytes}.subspan(s1, s2)); + sha.Write(Span{in_bytes}.last(s3)).Finalize(out); BOOST_CHECK(std::equal(std::begin(out_bytes), std::end(out_bytes), out)); } diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp index 1662529594..ef18ed1385 100644 --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -105,10 +105,10 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) peerLogic->FinalizeNode(dummyNode1); } -static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman) +static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType) { CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE); - vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), INVALID_SOCKET, addr, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, CAddress(), /*addrNameIn=*/"", ConnectionType::OUTBOUND_FULL_RELAY, /*inbound_onion=*/false)); + vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), INVALID_SOCKET, addr, /*nKeyedNetGroupIn=*/0, /*nLocalHostNonceIn=*/0, CAddress(), /*addrNameIn=*/"", connType, /*inbound_onion=*/false)); CNode &node = *vNodes.back(); node.SetCommonVersion(PROTOCOL_VERSION); @@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management) // Mock some outbound peers for (int i = 0; i < max_outbound_full_relay; ++i) { - AddRandomOutboundPeer(vNodes, *peerLogic, *connman); + AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY); } peerLogic->CheckForStaleTipAndEvictPeers(); @@ -161,7 +161,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management) // If we add one more peer, something should get marked for eviction // on the next check (since we're mocking the time to be in the future, the // required time connected check should be satisfied). - AddRandomOutboundPeer(vNodes, *peerLogic, *connman); + AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY); peerLogic->CheckForStaleTipAndEvictPeers(); for (int i = 0; i < max_outbound_full_relay; ++i) { @@ -190,6 +190,68 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management) connman->ClearTestNodes(); } +BOOST_AUTO_TEST_CASE(block_relay_only_eviction) +{ + const CChainParams& chainparams = Params(); + auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman); + auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr, + *m_node.chainman, *m_node.mempool, false); + + constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS}; + constexpr int64_t MINIMUM_CONNECT_TIME{30}; + CConnman::Options options; + options.nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS; + options.m_max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS; + options.m_max_outbound_block_relay = max_outbound_block_relay; + + connman->Init(options); + std::vector<CNode*> vNodes; + + // Add block-relay-only peers up to the limit + for (int i = 0; i < max_outbound_block_relay; ++i) { + AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY); + } + peerLogic->CheckForStaleTipAndEvictPeers(); + + for (int i = 0; i < max_outbound_block_relay; ++i) { + BOOST_CHECK(vNodes[i]->fDisconnect == false); + } + + // Add an extra block-relay-only peer breaking the limit (mocks logic in ThreadOpenConnections) + AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY); + peerLogic->CheckForStaleTipAndEvictPeers(); + + // The extra peer should only get marked for eviction after MINIMUM_CONNECT_TIME + for (int i = 0; i < max_outbound_block_relay; ++i) { + BOOST_CHECK(vNodes[i]->fDisconnect == false); + } + BOOST_CHECK(vNodes.back()->fDisconnect == false); + + SetMockTime(GetTime() + MINIMUM_CONNECT_TIME + 1); + peerLogic->CheckForStaleTipAndEvictPeers(); + for (int i = 0; i < max_outbound_block_relay; ++i) { + BOOST_CHECK(vNodes[i]->fDisconnect == false); + } + BOOST_CHECK(vNodes.back()->fDisconnect == true); + + // Update the last block time for the extra peer, + // and check that the next youngest peer gets evicted. + vNodes.back()->fDisconnect = false; + vNodes.back()->nLastBlockTime = GetTime(); + + peerLogic->CheckForStaleTipAndEvictPeers(); + for (int i = 0; i < max_outbound_block_relay - 1; ++i) { + BOOST_CHECK(vNodes[i]->fDisconnect == false); + } + BOOST_CHECK(vNodes[max_outbound_block_relay - 1]->fDisconnect == true); + BOOST_CHECK(vNodes.back()->fDisconnect == false); + + for (const CNode* node : vNodes) { + peerLogic->FinalizeNode(*node); + } + connman->ClearTestNodes(); +} + BOOST_AUTO_TEST_CASE(peer_discouragement) { const CChainParams& chainparams = Params(); diff --git a/src/test/fuzz/asmap.cpp b/src/test/fuzz/asmap.cpp index d402f8632c..c5e9c56049 100644 --- a/src/test/fuzz/asmap.cpp +++ b/src/test/fuzz/asmap.cpp @@ -49,7 +49,7 @@ FUZZ_TARGET(asmap) CNetAddr net_addr; if (ipv6) { assert(addr_size == ADDR_IPV6_SIZE); - net_addr.SetLegacyIPv6(Span<const uint8_t>(addr_data, addr_size)); + net_addr.SetLegacyIPv6({addr_data, addr_size}); } else { assert(addr_size == ADDR_IPV4_SIZE); in_addr ipv4; diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index b8f4ad8d94..f87b6f1503 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include <addrman.h> #include <chainparams.h> #include <chainparamsbase.h> #include <net.h> diff --git a/src/test/fuzz/golomb_rice.cpp b/src/test/fuzz/golomb_rice.cpp index c99bf940c7..746347ac95 100644 --- a/src/test/fuzz/golomb_rice.cpp +++ b/src/test/fuzz/golomb_rice.cpp @@ -82,8 +82,8 @@ FUZZ_TARGET(golomb_rice) std::vector<uint64_t> decoded_deltas; { - VectorReader stream{SER_NETWORK, 0, golomb_rice_data, 0}; - BitStreamReader<VectorReader> bitreader(stream); + SpanReader stream{SER_NETWORK, 0, golomb_rice_data}; + BitStreamReader<SpanReader> bitreader{stream}; const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream)); for (uint32_t i = 0; i < n; ++i) { decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P)); @@ -94,14 +94,14 @@ FUZZ_TARGET(golomb_rice) { const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024); - VectorReader stream{SER_NETWORK, 0, random_bytes, 0}; + SpanReader stream{SER_NETWORK, 0, random_bytes}; uint32_t n; try { n = static_cast<uint32_t>(ReadCompactSize(stream)); } catch (const std::ios_base::failure&) { return; } - BitStreamReader<VectorReader> bitreader(stream); + BitStreamReader<SpanReader> bitreader{stream}; for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) { try { (void)GolombRiceDecode(bitreader, BASIC_FILTER_P); diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp index 30518b4d77..44b98f7852 100644 --- a/src/test/fuzz/rpc.cpp +++ b/src/test/fuzz/rpc.cpp @@ -41,13 +41,17 @@ struct RPCFuzzTestingSetup : public TestingSetup { { } - UniValue CallRPC(const std::string& rpc_method, const std::vector<std::string>& arguments) + void CallRPC(const std::string& rpc_method, const std::vector<std::string>& arguments) { JSONRPCRequest request; request.context = &m_node; request.strMethod = rpc_method; - request.params = RPCConvertValues(rpc_method, arguments); - return tableRPC.execute(request); + try { + request.params = RPCConvertValues(rpc_method, arguments); + } catch (const std::runtime_error&) { + return; + } + tableRPC.execute(request); } std::vector<std::string> GetRPCCommands() const @@ -354,7 +358,11 @@ FUZZ_TARGET_INIT(rpc, initialize_rpc) } try { rpc_testing_setup->CallRPC(rpc_command, arguments); - } catch (const UniValue&) { - } catch (const std::runtime_error&) { + } catch (const UniValue& json_rpc_error) { + const std::string error_msg{find_value(json_rpc_error, "message").get_str()}; + if (error_msg.find("Internal bug detected") != std::string::npos) { + // Only allow the intentional internal bug + assert(error_msg.find("trigger_internal_bug") != std::string::npos); + } } } diff --git a/src/test/fuzz/script_assets_test_minimizer.cpp b/src/test/fuzz/script_assets_test_minimizer.cpp index 4669f783aa..00a3bed12f 100644 --- a/src/test/fuzz/script_assets_test_minimizer.cpp +++ b/src/test/fuzz/script_assets_test_minimizer.cpp @@ -54,7 +54,7 @@ CMutableTransaction TxFromHex(const std::string& str) { CMutableTransaction tx; try { - VectorReader(SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, CheckedParseHex(str), 0) >> tx; + SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, CheckedParseHex(str)} >> tx; } catch (const std::ios_base::failure&) { throw std::runtime_error("Tx deserialization failure"); } @@ -68,7 +68,7 @@ std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue) for (size_t i = 0; i < univalue.size(); ++i) { CTxOut txout; try { - VectorReader(SER_DISK, 0, CheckedParseHex(univalue[i].get_str()), 0) >> txout; + SpanReader{SER_DISK, 0, CheckedParseHex(univalue[i].get_str())} >> txout; } catch (const std::ios_base::failure&) { throw std::runtime_error("Prevout invalid format"); } diff --git a/src/test/fuzz/tx_pool.cpp b/src/test/fuzz/tx_pool.cpp index c32c965ab0..fe1b9c7c0c 100644 --- a/src/test/fuzz/tx_pool.cpp +++ b/src/test/fuzz/tx_pool.cpp @@ -29,6 +29,15 @@ struct MockedTxPool : public CTxMemPool { } }; +class DummyChainState final : public CChainState +{ +public: + void SetMempool(CTxMemPool* mempool) + { + m_mempool = mempool; + } +}; + void initialize_tx_pool() { static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(); @@ -114,7 +123,7 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool) { FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const auto& node = g_setup->m_node; - auto& chainstate = node.chainman->ActiveChainstate(); + auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())}; MockTime(fuzzed_data_provider, chainstate); SetMempoolConstraints(*node.args, fuzzed_data_provider); @@ -134,6 +143,8 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool) CTxMemPool tx_pool_{/*estimator=*/nullptr, /*check_ratio=*/1}; MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_); + chainstate.SetMempool(&tx_pool); + // Helper to query an amount const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool}; const auto GetAmount = [&](const COutPoint& outpoint) { @@ -230,7 +241,7 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool) Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID || it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID); - const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false)); + const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false)); const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID; SyncWithValidationInterfaceQueue(); UnregisterSharedValidationInterface(txr); @@ -330,7 +341,7 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool) const auto tx = MakeTransactionRef(mut_tx); const bool bypass_limits = fuzzed_data_provider.ConsumeBool(); ::fRequireStandard = fuzzed_data_provider.ConsumeBool(); - const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false)); + const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false)); const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID; if (accepted) { txids.push_back(tx->GetHash()); diff --git a/src/test/fuzz/utxo_snapshot.cpp b/src/test/fuzz/utxo_snapshot.cpp index 02039cba81..1b9f0c8a02 100644 --- a/src/test/fuzz/utxo_snapshot.cpp +++ b/src/test/fuzz/utxo_snapshot.cpp @@ -38,7 +38,7 @@ FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain) { CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION}; const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - outfile << Span<const uint8_t>{file_data}; + outfile << Span{file_data}; } const auto ActivateFuzzedSnapshot{[&] { diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 0361618c82..02268dbcf5 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse) privkey = DecodeSecret(exp_base58string); BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest); - BOOST_CHECK_MESSAGE(Span<const uint8_t>{privkey} == Span<const uint8_t>{exp_payload}, "key mismatch:" + strTest); + BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest); // Private key must be invalid public key destination = DecodeDestination(exp_base58string); diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index c5bd9c73fd..d0f0e7d50f 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -386,9 +386,9 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) s.SetVersion(s.GetVersion() | ADDRV2_FORMAT); // Valid IPv4. - s << MakeSpan(ParseHex("01" // network type (IPv4) - "04" // address length - "01020304")); // address + s << Span{ParseHex("01" // network type (IPv4) + "04" // address length + "01020304")}; // address s >> addr; BOOST_CHECK(addr.IsValid()); BOOST_CHECK(addr.IsIPv4()); @@ -397,35 +397,35 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Invalid IPv4, valid length but address itself is shorter. - s << MakeSpan(ParseHex("01" // network type (IPv4) - "04" // address length - "0102")); // address + s << Span{ParseHex("01" // network type (IPv4) + "04" // address length + "0102")}; // address BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("end of data")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Invalid IPv4, with bogus length. - s << MakeSpan(ParseHex("01" // network type (IPv4) - "05" // address length - "01020304")); // address + s << Span{ParseHex("01" // network type (IPv4) + "05" // address length + "01020304")}; // address BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("BIP155 IPv4 address with length 5 (should be 4)")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Invalid IPv4, with extreme length. - s << MakeSpan(ParseHex("01" // network type (IPv4) - "fd0102" // address length (513 as CompactSize) - "01020304")); // address + s << Span{ParseHex("01" // network type (IPv4) + "fd0102" // address length (513 as CompactSize) + "01020304")}; // address BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("Address too long: 513 > 512")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Valid IPv6. - s << MakeSpan(ParseHex("02" // network type (IPv6) - "10" // address length - "0102030405060708090a0b0c0d0e0f10")); // address + s << Span{ParseHex("02" // network type (IPv6) + "10" // address length + "0102030405060708090a0b0c0d0e0f10")}; // address s >> addr; BOOST_CHECK(addr.IsValid()); BOOST_CHECK(addr.IsIPv6()); @@ -434,10 +434,10 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Valid IPv6, contains embedded "internal". - s << MakeSpan(ParseHex( + s << Span{ParseHex( "02" // network type (IPv6) "10" // address length - "fd6b88c08724ca978112ca1bbdcafac2")); // address: 0xfd + sha256("bitcoin")[0:5] + + "fd6b88c08724ca978112ca1bbdcafac2")}; // address: 0xfd + sha256("bitcoin")[0:5] + // sha256(name)[0:10] s >> addr; BOOST_CHECK(addr.IsInternal()); @@ -446,44 +446,44 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Invalid IPv6, with bogus length. - s << MakeSpan(ParseHex("02" // network type (IPv6) - "04" // address length - "00")); // address + s << Span{ParseHex("02" // network type (IPv6) + "04" // address length + "00")}; // address BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("BIP155 IPv6 address with length 4 (should be 16)")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Invalid IPv6, contains embedded IPv4. - s << MakeSpan(ParseHex("02" // network type (IPv6) - "10" // address length - "00000000000000000000ffff01020304")); // address + s << Span{ParseHex("02" // network type (IPv6) + "10" // address length + "00000000000000000000ffff01020304")}; // address s >> addr; BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); // Invalid IPv6, contains embedded TORv2. - s << MakeSpan(ParseHex("02" // network type (IPv6) - "10" // address length - "fd87d87eeb430102030405060708090a")); // address + s << Span{ParseHex("02" // network type (IPv6) + "10" // address length + "fd87d87eeb430102030405060708090a")}; // address s >> addr; BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); // TORv2, no longer supported. - s << MakeSpan(ParseHex("03" // network type (TORv2) - "0a" // address length - "f1f2f3f4f5f6f7f8f9fa")); // address + s << Span{ParseHex("03" // network type (TORv2) + "0a" // address length + "f1f2f3f4f5f6f7f8f9fa")}; // address s >> addr; BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); // Valid TORv3. - s << MakeSpan(ParseHex("04" // network type (TORv3) - "20" // address length - "79bcc625184b05194975c28b66b66b04" // address - "69f7f6556fb1ac3189a79b40dda32f1f" - )); + s << Span{ParseHex("04" // network type (TORv3) + "20" // address length + "79bcc625184b05194975c28b66b66b04" // address + "69f7f6556fb1ac3189a79b40dda32f1f" + )}; s >> addr; BOOST_CHECK(addr.IsValid()); BOOST_CHECK(addr.IsTor()); @@ -493,20 +493,20 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Invalid TORv3, with bogus length. - s << MakeSpan(ParseHex("04" // network type (TORv3) - "00" // address length - "00" // address - )); + s << Span{ParseHex("04" // network type (TORv3) + "00" // address length + "00" // address + )}; BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("BIP155 TORv3 address with length 0 (should be 32)")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Valid I2P. - s << MakeSpan(ParseHex("05" // network type (I2P) - "20" // address length - "a2894dabaec08c0051a481a6dac88b64" // address - "f98232ae42d4b6fd2fa81952dfe36a87")); + s << Span{ParseHex("05" // network type (I2P) + "20" // address length + "a2894dabaec08c0051a481a6dac88b64" // address + "f98232ae42d4b6fd2fa81952dfe36a87")}; s >> addr; BOOST_CHECK(addr.IsValid()); BOOST_CHECK(addr.IsI2P()); @@ -516,20 +516,20 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Invalid I2P, with bogus length. - s << MakeSpan(ParseHex("05" // network type (I2P) - "03" // address length - "00" // address - )); + s << Span{ParseHex("05" // network type (I2P) + "03" // address length + "00" // address + )}; BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("BIP155 I2P address with length 3 (should be 32)")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Valid CJDNS. - s << MakeSpan(ParseHex("06" // network type (CJDNS) - "10" // address length - "fc000001000200030004000500060007" // address - )); + s << Span{ParseHex("06" // network type (CJDNS) + "10" // address length + "fc000001000200030004000500060007" // address + )}; s >> addr; BOOST_CHECK(addr.IsValid()); BOOST_CHECK(addr.IsCJDNS()); @@ -538,49 +538,49 @@ BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2) BOOST_REQUIRE(s.empty()); // Invalid CJDNS, wrong prefix. - s << MakeSpan(ParseHex("06" // network type (CJDNS) - "10" // address length - "aa000001000200030004000500060007" // address - )); + s << Span{ParseHex("06" // network type (CJDNS) + "10" // address length + "aa000001000200030004000500060007" // address + )}; s >> addr; BOOST_CHECK(addr.IsCJDNS()); BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); // Invalid CJDNS, with bogus length. - s << MakeSpan(ParseHex("06" // network type (CJDNS) - "01" // address length - "00" // address - )); + s << Span{ParseHex("06" // network type (CJDNS) + "01" // address length + "00" // address + )}; BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("BIP155 CJDNS address with length 1 (should be 16)")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Unknown, with extreme length. - s << MakeSpan(ParseHex("aa" // network type (unknown) - "fe00000002" // address length (CompactSize's MAX_SIZE) - "01020304050607" // address - )); + s << Span{ParseHex("aa" // network type (unknown) + "fe00000002" // address length (CompactSize's MAX_SIZE) + "01020304050607" // address + )}; BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("Address too long: 33554432 > 512")); BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input. s.clear(); // Unknown, with reasonable length. - s << MakeSpan(ParseHex("aa" // network type (unknown) - "04" // address length - "01020304" // address - )); + s << Span{ParseHex("aa" // network type (unknown) + "04" // address length + "01020304" // address + )}; s >> addr; BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); // Unknown, with zero length. - s << MakeSpan(ParseHex("aa" // network type (unknown) - "00" // address length - "" // address - )); + s << Span{ParseHex("aa" // network type (unknown) + "00" // address length + "" // address + )}; s >> addr; BOOST_CHECK(!addr.IsValid()); BOOST_REQUIRE(s.empty()); diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index a89eab68e9..f1304dfc82 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -284,7 +284,7 @@ public: CScript scriptPubKey = script; if (wm == WitnessMode::PKH) { uint160 hash; - CHash160().Write(MakeSpan(script).subspan(1)).Finalize(hash); + CHash160().Write(Span{script}.subspan(1)).Finalize(hash); script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG; scriptPubKey = CScript() << witnessversion << ToByteVector(hash); } else if (wm == WitnessMode::SH) { @@ -1473,7 +1473,7 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps) static CMutableTransaction TxFromHex(const std::string& str) { CMutableTransaction tx; - VectorReader(SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str), 0) >> tx; + SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str)} >> tx; return tx; } @@ -1483,7 +1483,7 @@ static std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue) std::vector<CTxOut> prevouts; for (size_t i = 0; i < univalue.size(); ++i) { CTxOut txout; - VectorReader(SER_DISK, 0, ParseHex(univalue[i].get_str()), 0) >> txout; + SpanReader{SER_DISK, 0, ParseHex(univalue[i].get_str())} >> txout; prevouts.push_back(std::move(txout)); } return prevouts; @@ -1754,7 +1754,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors) for (const auto& vec : vectors.getValues()) { auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str()); CMutableTransaction tx; - VectorReader(SER_NETWORK, PROTOCOL_VERSION, txhex, 0) >> tx; + SpanReader{SER_NETWORK, PROTOCOL_VERSION, txhex} >> tx; std::vector<CTxOut> utxos; for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) { auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str()); @@ -1812,7 +1812,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors) BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str()); // To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash. - BOOST_CHECK_EQUAL(HexStr((CHashWriter(HASHER_TAPSIGHASH) << MakeSpan(ParseHex(input["intermediary"]["sigMsg"].get_str()))).GetSHA256()), input["intermediary"]["sigHash"].get_str()); + BOOST_CHECK_EQUAL(HexStr((CHashWriter(HASHER_TAPSIGHASH) << Span{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str()); } } diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index b8d76c9608..8d44e92f97 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader) { std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6}; - VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); + SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, vch}; BOOST_CHECK_EQUAL(reader.size(), 6U); BOOST_CHECK(!reader.empty()); @@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader) BOOST_CHECK_THROW(reader >> d, std::ios_base::failure); // Read a 4 bytes as a signed int from the beginning of the buffer. - VectorReader new_reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); + SpanReader new_reader{SER_NETWORK, INIT_PROTO_VERSION, vch}; new_reader >> d; BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256 BOOST_CHECK_EQUAL(new_reader.size(), 2U); @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader) BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue) { std::vector<uint8_t> data{0x82, 0xa7, 0x31}; - VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, data, /* pos= */ 0); + SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, data}; uint32_t varint = 0; // Deserialize into r-value reader >> VARINT(varint); diff --git a/src/test/util/chainstate.h b/src/test/util/chainstate.h index 555979d94e..a9092bd0ef 100644 --- a/src/test/util/chainstate.h +++ b/src/test/util/chainstate.h @@ -34,7 +34,8 @@ CreateAndActivateUTXOSnapshot(NodeContext& node, const fs::path root, F malleati FILE* outfile{fsbridge::fopen(snapshot_path, "wb")}; CAutoFile auto_outfile{outfile, SER_DISK, CLIENT_VERSION}; - UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), auto_outfile); + UniValue result = CreateUTXOSnapshot( + node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path); BOOST_TEST_MESSAGE( "Wrote UTXO snapshot to " << fs::PathToString(snapshot_path.make_preferred()) << ": " << result.write()); diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 76a690fd28..9540cead24 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -142,13 +142,11 @@ BOOST_AUTO_TEST_CASE(util_HexStr) "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); BOOST_CHECK_EQUAL( - HexStr(Span<const unsigned char>( - ParseHex_expected + sizeof(ParseHex_expected), - ParseHex_expected + sizeof(ParseHex_expected))), + HexStr(Span{ParseHex_expected}.last(0)), ""); BOOST_CHECK_EQUAL( - HexStr(Span<const unsigned char>(ParseHex_expected, ParseHex_expected)), + HexStr(Span{ParseHex_expected}.first(0)), ""); { |