mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
test: Allow testing of check failures
This allows specific tests to mock the check behavior to consistently use exceptions instead of aborts for intentionally failing checks in all build configurations.
This commit is contained in:
@@ -114,6 +114,7 @@ add_executable(test_bitcoin
|
||||
txvalidation_tests.cpp
|
||||
txvalidationcache_tests.cpp
|
||||
uint256_tests.cpp
|
||||
util_check_tests.cpp
|
||||
util_string_tests.cpp
|
||||
util_tests.cpp
|
||||
util_threadnames_tests.cpp
|
||||
|
||||
33
src/test/util_check_tests.cpp
Normal file
33
src/test/util_check_tests.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <util/check.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <test/util/setup_common.h>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(util_check_tests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(check_pass)
|
||||
{
|
||||
Assume(true);
|
||||
Assert(true);
|
||||
CHECK_NONFATAL(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(check_fail)
|
||||
{
|
||||
// Disable aborts for easier testing here
|
||||
test_only_CheckFailuresAreExceptionsNotAborts mock_checks{};
|
||||
|
||||
if constexpr (G_ABORT_ON_FAILED_ASSUME) {
|
||||
BOOST_CHECK_EXCEPTION(Assume(false), NonFatalCheckError, HasReason{"Internal bug detected: false"});
|
||||
} else {
|
||||
BOOST_CHECK_NO_THROW(Assume(false));
|
||||
}
|
||||
BOOST_CHECK_EXCEPTION(Assert(false), NonFatalCheckError, HasReason{"Internal bug detected: false"});
|
||||
BOOST_CHECK_EXCEPTION(CHECK_NONFATAL(false), NonFatalCheckError, HasReason{"Internal bug detected: false"});
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
@@ -27,8 +27,13 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, std::string_view fi
|
||||
{
|
||||
}
|
||||
|
||||
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
|
||||
|
||||
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
|
||||
{
|
||||
if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
|
||||
throw NonFatalCheckError{assertion, file, line, func};
|
||||
}
|
||||
auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
|
||||
fwrite(str.data(), 1, str.size(), stderr);
|
||||
std::abort();
|
||||
|
||||
@@ -46,6 +46,12 @@ inline bool EnableFuzzDeterminism()
|
||||
}
|
||||
}
|
||||
|
||||
extern bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts;
|
||||
struct test_only_CheckFailuresAreExceptionsNotAborts {
|
||||
test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = true; };
|
||||
~test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = false; };
|
||||
};
|
||||
|
||||
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
|
||||
|
||||
class NonFatalCheckError : public std::runtime_error
|
||||
|
||||
Reference in New Issue
Block a user