diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/coins_tests.cpp | 54 | ||||
-rw-r--r-- | src/test/dbwrapper_tests.cpp | 43 | ||||
-rw-r--r-- | src/test/logging_tests.cpp | 15 | ||||
-rw-r--r-- | src/test/serialize_tests.cpp | 3 | ||||
-rw-r--r-- | src/test/transaction_tests.cpp | 45 |
5 files changed, 75 insertions, 85 deletions
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 312f417129..55ecd41af1 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -131,8 +131,8 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) std::map<COutPoint, Coin> result; // The cache stack. - std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top. - stack.push_back(new CCoinsViewCacheTest(base)); // Start with one cache. + std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top. + stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache. // Use a limited set of random transaction ids, so we do test overwriting entries. std::vector<uint256> txids; @@ -218,7 +218,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) found_an_entry = true; } } - for (const CCoinsViewCacheTest *test : stack) { + for (const auto& test : stack) { test->SelfTest(); } } @@ -241,18 +241,17 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) bool should_erase = InsecureRandRange(4) < 3; BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync()); flushed_without_erase |= !should_erase; - delete stack.back(); stack.pop_back(); } if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { //Add a new cache CCoinsView* tip = base; if (stack.size() > 0) { - tip = stack.back(); + tip = stack.back().get(); } else { removed_all_caches = true; } - stack.push_back(new CCoinsViewCacheTest(tip)); + stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip)); if (stack.size() == 4) { reached_4_caches = true; } @@ -260,12 +259,6 @@ void SimulationTest(CCoinsView* base, bool fake_best_block) } } - // Clean up the stack. - while (stack.size() > 0) { - delete stack.back(); - stack.pop_back(); - } - // Verify coverage. BOOST_CHECK(removed_all_caches); BOOST_CHECK(reached_4_caches); @@ -321,8 +314,8 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test) // The cache stack. CCoinsViewTest base; // A CCoinsViewTest at the bottom. - std::vector<CCoinsViewCacheTest*> stack; // A stack of CCoinsViewCaches on top. - stack.push_back(new CCoinsViewCacheTest(&base)); // Start with one cache. + std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top. + stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache. // Track the txids we've used in various sets std::set<COutPoint> coinbase_coins; @@ -487,25 +480,18 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test) // Every 100 iterations, change the cache stack. if (stack.size() > 0 && InsecureRandBool() == 0) { BOOST_CHECK(stack.back()->Flush()); - delete stack.back(); stack.pop_back(); } if (stack.size() == 0 || (stack.size() < 4 && InsecureRandBool())) { CCoinsView* tip = &base; if (stack.size() > 0) { - tip = stack.back(); + tip = stack.back().get(); } - stack.push_back(new CCoinsViewCacheTest(tip)); + stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip)); } } } - // Clean up the stack. - while (stack.size() > 0) { - delete stack.back(); - stack.pop_back(); - } - // Verify coverage. BOOST_CHECK(spent_a_duplicate_coinbase); @@ -918,7 +904,7 @@ Coin MakeCoin() void TestFlushBehavior( CCoinsViewCacheTest* view, CCoinsViewDB& base, - std::vector<CCoinsViewCacheTest*>& all_caches, + std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches, bool do_erasing_flush) { CAmount value; @@ -928,7 +914,7 @@ void TestFlushBehavior( auto flush_all = [&all_caches](bool erase) { // Flush in reverse order to ensure that flushes happen from children up. for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) { - auto cache = *i; + auto& cache = *i; // hashBlock must be filled before flushing to disk; value is // unimportant here. This is normally done during connect/disconnect block. cache->SetBestBlock(InsecureRand256()); @@ -1079,19 +1065,13 @@ BOOST_AUTO_TEST_CASE(ccoins_flush_behavior) { // Create two in-memory caches atop a leveldb view. CCoinsViewDB base{"test", /*nCacheSize=*/ 1 << 23, /*fMemory=*/ true, /*fWipe=*/ false}; - std::vector<CCoinsViewCacheTest*> caches; - caches.push_back(new CCoinsViewCacheTest(&base)); - caches.push_back(new CCoinsViewCacheTest(caches.back())); - - for (CCoinsViewCacheTest* view : caches) { - TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ false); - TestFlushBehavior(view, base, caches, /*do_erasing_flush=*/ true); - } + std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches; + caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); + caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get())); - // Clean up the caches. - while (caches.size() > 0) { - delete caches.back(); - caches.pop_back(); + for (const auto& view : caches) { + TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false); + TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true); } } diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index 2447c882ae..7ad123754b 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -5,6 +5,7 @@ #include <dbwrapper.h> #include <test/util/setup_common.h> #include <uint256.h> +#include <util/string.h> #include <memory> @@ -324,12 +325,6 @@ struct StringContentsSerializer { StringContentsSerializer() = default; explicit StringContentsSerializer(const std::string& inp) : str(inp) {} - StringContentsSerializer& operator+=(const std::string& s) { - str += s; - return *this; - } - StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; } - template<typename Stream> void Serialize(Stream& s) const { @@ -343,44 +338,34 @@ struct StringContentsSerializer { { str.clear(); uint8_t c{0}; - while (true) { - try { - s >> c; - str.push_back(c); - } catch (const std::ios_base::failure&) { - break; - } + while (!s.eof()) { + s >> c; + str.push_back(c); } } }; BOOST_AUTO_TEST_CASE(iterator_string_ordering) { - char buf[10]; - fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering"; CDBWrapper dbw(ph, (1 << 20), true, false, false); - for (int x=0x00; x<10; ++x) { - for (int y = 0; y < 10; y++) { - snprintf(buf, sizeof(buf), "%d", x); - StringContentsSerializer key(buf); - for (int z = 0; z < y; z++) + for (int x = 0; x < 10; ++x) { + for (int y = 0; y < 10; ++y) { + std::string key{ToString(x)}; + for (int z = 0; z < y; ++z) key += key; uint32_t value = x*x; - BOOST_CHECK(dbw.Write(key, value)); + BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value)); } } std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator()); for (const int seek_start : {0, 5}) { - snprintf(buf, sizeof(buf), "%d", seek_start); - StringContentsSerializer seek_key(buf); - it->Seek(seek_key); - for (unsigned int x=seek_start; x<10; ++x) { - for (int y = 0; y < 10; y++) { - snprintf(buf, sizeof(buf), "%d", x); - std::string exp_key(buf); - for (int z = 0; z < y; z++) + it->Seek(StringContentsSerializer{ToString(seek_start)}); + for (unsigned int x = seek_start; x < 10; ++x) { + for (int y = 0; y < 10; ++y) { + std::string exp_key{ToString(x)}; + for (int z = 0; z < y; ++z) exp_key += exp_key; StringContentsSerializer key; uint32_t value; diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index 022e33f99d..beb9398c74 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -75,20 +75,9 @@ struct LogSetup : public BasicTestingSetup { BOOST_AUTO_TEST_CASE(logging_timer) { - SetMockTime(1); auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg"); - SetMockTime(2); - BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)"); - - SetMockTime(1); - auto ms_timer = BCLog::Timer<std::chrono::milliseconds>("tests", "end_msg"); - SetMockTime(2); - BOOST_CHECK_EQUAL(ms_timer.LogMsg("test ms"), "tests: test ms (1000.00ms)"); - - SetMockTime(1); - auto sec_timer = BCLog::Timer<std::chrono::seconds>("tests", "end_msg"); - SetMockTime(2); - BOOST_CHECK_EQUAL(sec_timer.LogMsg("test secs"), "tests: test secs (1.00s)"); + const std::string_view result_prefix{"tests: msg ("}; + BOOST_CHECK_EQUAL(micro_timer.LogMsg("msg").substr(0, result_prefix.size()), result_prefix); } BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup) diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index f583109e16..09f77d2b61 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -237,7 +237,8 @@ BOOST_AUTO_TEST_CASE(class_methods) BOOST_CHECK(methodtest2 == methodtest3); BOOST_CHECK(methodtest3 == methodtest4); - CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, charstrval, txval); + CDataStream ss2{SER_DISK, PROTOCOL_VERSION}; + ss2 << intval << boolval << stringval << charstrval << txval; ss2 >> methodtest3; BOOST_CHECK(methodtest3 == methodtest4); } diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index d00de9df9b..0180fa47e8 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -935,23 +935,58 @@ BOOST_AUTO_TEST_CASE(test_IsStandard) CheckIsNotStandard(t, "bare-multisig"); g_bare_multi = DEFAULT_PERMIT_BAREMULTISIG; + // Check compressed P2PK outputs dust threshold (must have leading 02 or 03) + t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG; + t.vout[0].nValue = 576; + CheckIsStandard(t); + t.vout[0].nValue = 575; + CheckIsNotStandard(t, "dust"); + + // Check uncompressed P2PK outputs dust threshold (must have leading 04/06/07) + t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(65, 0x04) << OP_CHECKSIG; + t.vout[0].nValue = 672; + CheckIsStandard(t); + t.vout[0].nValue = 671; + CheckIsNotStandard(t, "dust"); + + // Check P2PKH outputs dust threshold + t.vout[0].scriptPubKey = CScript() << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUALVERIFY << OP_CHECKSIG; + t.vout[0].nValue = 546; + CheckIsStandard(t); + t.vout[0].nValue = 545; + CheckIsNotStandard(t, "dust"); + + // Check P2SH outputs dust threshold + t.vout[0].scriptPubKey = CScript() << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUAL; + t.vout[0].nValue = 540; + CheckIsStandard(t); + t.vout[0].nValue = 539; + CheckIsNotStandard(t, "dust"); + // Check P2WPKH outputs dust threshold - t.vout[0].scriptPubKey = CScript() << OP_0 << ParseHex("ffffffffffffffffffffffffffffffffffffffff"); + t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(20, 0); t.vout[0].nValue = 294; CheckIsStandard(t); t.vout[0].nValue = 293; CheckIsNotStandard(t, "dust"); // Check P2WSH outputs dust threshold - t.vout[0].scriptPubKey = CScript() << OP_0 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); + t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(32, 0); + t.vout[0].nValue = 330; + CheckIsStandard(t); + t.vout[0].nValue = 329; + CheckIsNotStandard(t, "dust"); + + // Check P2TR outputs dust threshold (Invalid xonly key ok!) + t.vout[0].scriptPubKey = CScript() << OP_1 << std::vector<unsigned char>(32, 0); t.vout[0].nValue = 330; CheckIsStandard(t); t.vout[0].nValue = 329; CheckIsNotStandard(t, "dust"); - // Check future Witness Program versions dust threshold - for (int op = OP_2; op <= OP_16; op += 1) { - t.vout[0].scriptPubKey = CScript() << (opcodetype)op << ParseHex("ffff"); + // Check future Witness Program versions dust threshold (non-32-byte pushes are undefined for version 1) + for (int op = OP_1; op <= OP_16; op += 1) { + t.vout[0].scriptPubKey = CScript() << (opcodetype)op << std::vector<unsigned char>(2, 0); t.vout[0].nValue = 240; CheckIsStandard(t); |