diff --git a/src/avalanche/test/fixture.cpp b/src/avalanche/test/fixture.cpp index dfbcc223d..dc7e29c73 100644 --- a/src/avalanche/test/fixture.cpp +++ b/src/avalanche/test/fixture.cpp @@ -1,6 +1,19 @@ // Copyright (c) 2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #define BOOST_TEST_MODULE Avalanche Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index 9d42e10ed..48b89796f 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -1,84 +1,86 @@ // Copyright (c) 2015-2019 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 #include #include #include #include +const std::function G_TEST_LOG_FUN{}; + namespace { void GenerateTemplateResults( const std::vector &benchmarkResults, const std::string &filename, const char *tpl) { if (benchmarkResults.empty() || filename.empty()) { // nothing to write, bail out return; } std::ofstream fout(filename); if (fout.is_open()) { ankerl::nanobench::render(tpl, benchmarkResults, fout); } else { std::cout << "Could write to file '" << filename << "'" << std::endl; } std::cout << "Created '" << filename << "'" << std::endl; } } // namespace benchmark::BenchRunner::BenchmarkMap &benchmark::BenchRunner::benchmarks() { static std::map benchmarks_map; return benchmarks_map; } benchmark::BenchRunner::BenchRunner(std::string name, benchmark::BenchFunction func) { benchmarks().insert(std::make_pair(name, func)); } void benchmark::BenchRunner::RunAll(const Args &args) { std::regex reFilter(args.regex_filter); std::smatch baseMatch; std::vector benchmarkResults; for (const auto &p : benchmarks()) { if (!std::regex_match(p.first, baseMatch, reFilter)) { continue; } if (args.is_list_only) { std::cout << p.first << std::endl; continue; } Bench bench; bench.name(p.first); if (args.asymptote.empty()) { p.second(bench); } else { for (auto n : args.asymptote) { bench.complexityN(n); p.second(bench); } std::cout << bench.complexityBigO() << std::endl; } benchmarkResults.push_back(bench.results().back()); } GenerateTemplateResults( benchmarkResults, args.output_csv, "# Benchmark, evals, iterations, total, min, max, median\n" "{{#result}}{{name}}, {{epochs}}, {{average(iterations)}}, " "{{sumProduct(iterations, elapsed)}}, {{minimum(elapsed)}}, " "{{maximum(elapsed)}}, {{median(elapsed)}}\n" "{{/result}}"); GenerateTemplateResults(benchmarkResults, args.output_json, ankerl::nanobench::templates::json()); } diff --git a/src/pow/test/fixture.cpp b/src/pow/test/fixture.cpp index b811fca2d..2c3685cda 100644 --- a/src/pow/test/fixture.cpp +++ b/src/pow/test/fixture.cpp @@ -1,6 +1,19 @@ // Copyright (c) 2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #define BOOST_TEST_MODULE Difficulty Adjustement Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp index ec9202c2f..8761287ad 100644 --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -1,120 +1,122 @@ // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #if defined(HAVE_CONFIG_H) #include #endif #include #include #include #include #include #include #include #include #include #include #ifdef ENABLE_WALLET #include #ifdef ENABLE_BIP70 #include #endif // ENABLE_BIP70 #include #endif // ENABLE_WALLET #include #include #include #include #if defined(QT_STATICPLUGIN) #include #if defined(QT_QPA_PLATFORM_MINIMAL) Q_IMPORT_PLUGIN(QMinimalIntegrationPlugin); #endif #if defined(QT_QPA_PLATFORM_XCB) Q_IMPORT_PLUGIN(QXcbIntegrationPlugin); #elif defined(QT_QPA_PLATFORM_WINDOWS) Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin); #elif defined(QT_QPA_PLATFORM_COCOA) Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin); #endif #endif +const std::function G_TEST_LOG_FUN{}; + // This is all you need to run all the tests int main(int argc, char *argv[]) { // Initialize persistent globals with the testing setup state for sanity. // E.g. -datadir in gArgs is set to a temp directory dummy value (instead // of defaulting to the default datadir), or globalChainParams is set to // regtest params. // // All tests must use their own testing setup (if needed). { BasicTestingSetup dummy{CBaseChainParams::REGTEST}; } NodeContext node_context; std::unique_ptr node = interfaces::MakeNode(&node_context); bool fInvalid = false; // Prefer the "minimal" platform for the test instead of the normal default // platform ("xcb", "windows", or "cocoa") so tests can't unintentionally // interfere with any background GUIs and don't require extra resources. setenv("QT_QPA_PLATFORM", "minimal", /* overwrite */ 0); // Don't remove this, it's needed to access // QApplication:: and QCoreApplication:: in the tests BitcoinApplication app; app.setNode(*node); app.setApplicationName("BitcoinABC-Qt-test"); // Make gArgs available in the NodeContext app.node().context()->args = &gArgs; AppTests app_tests(app); if (QTest::qExec(&app_tests) != 0) { fInvalid = true; } URITests test1; if (QTest::qExec(&test1) != 0) { fInvalid = true; } #if defined(ENABLE_WALLET) && defined(ENABLE_BIP70) PaymentServerTests test2; if (QTest::qExec(&test2) != 0) { fInvalid = true; } #endif RPCNestedTests test3(app.node()); if (QTest::qExec(&test3) != 0) { fInvalid = true; } CompatTests test4; if (QTest::qExec(&test4) != 0) { fInvalid = true; } GUIUtilTests test5; if (QTest::qExec(&test5) != 0) { fInvalid = true; } BitcoinAddressValidatorTests test6; if (QTest::qExec(&test6) != 0) { fInvalid = true; } #ifdef ENABLE_WALLET WalletTests test7(app.node()); if (QTest::qExec(&test7) != 0) { fInvalid = true; } AddressBookTests test8(app.node()); if (QTest::qExec(&test8) != 0) { fInvalid = true; } #endif return fInvalid; } diff --git a/src/seeder/test/fixture.cpp b/src/seeder/test/fixture.cpp index a15983139..043117f4e 100644 --- a/src/seeder/test/fixture.cpp +++ b/src/seeder/test/fixture.cpp @@ -1,7 +1,20 @@ // Copyright (c) 2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #define BOOST_TEST_MODULE Seeder Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/test/fixture.cpp b/src/test/fixture.cpp index 8c6e85908..6c683b730 100644 --- a/src/test/fixture.cpp +++ b/src/test/fixture.cpp @@ -1,43 +1,58 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +/** + * See + * https://www.boost.org/doc/libs/1_71_0/libs/test/doc/html/boost_test/utf_reference/link_references/link_boost_test_module_macro.html + */ #define BOOST_TEST_MODULE Bitcoin ABC unit tests #include #include #include namespace utf = boost::unit_test::framework; +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; + /** * Global fixture for passing custom arguments, and clearing them all after each * test case. */ struct CustomArgumentsFixture { CustomArgumentsFixture() { auto &master_test_suite = utf::master_test_suite(); for (int i = 1; i < master_test_suite.argc; i++) { std::string key(master_test_suite.argv[i]); std::string val; if (!ParseKeyValue(key, val)) { break; } if (key == "-testsuitename") { master_test_suite.p_name.value = val; continue; } fixture_extra_args.push_back(master_test_suite.argv[i]); } } ~CustomArgumentsFixture(){}; }; BOOST_TEST_GLOBAL_FIXTURE(CustomArgumentsFixture); diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index 1750581c3..ebdaa5882 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -1,75 +1,80 @@ // Copyright (c) 2009-2019 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 +#include + #include #include #include +const std::function G_TEST_LOG_FUN{}; + // Decide if main(...) should be provided: // * AFL needs main(...) regardless of platform. // * macOS handles __attribute__((weak)) main(...) poorly when linking // against libFuzzer. See https://github.com/bitcoin/bitcoin/pull/18008 // for details. #if defined(__AFL_COMPILER) || !defined(MAC_OSX) #define PROVIDE_MAIN_FUNCTION #endif #if defined(PROVIDE_MAIN_FUNCTION) + static bool read_stdin(std::vector &data) { uint8_t buffer[1024]; ssize_t length = 0; while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) { data.insert(data.end(), buffer, buffer + length); } return length == 0; } #endif // Default initialization: Override using a non-weak initialize(). __attribute__((weak)) void initialize() {} // This function is used by libFuzzer extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { const std::vector input(data, data + size); test_one_input(input); return 0; } // This function is used by libFuzzer extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { initialize(); return 0; } #if defined(PROVIDE_MAIN_FUNCTION) __attribute__((weak)) int main(int argc, char **argv) { initialize(); #ifdef __AFL_INIT // Enable AFL deferred forkserver mode. Requires compilation using // afl-clang-fast++. See fuzzing.md for details. __AFL_INIT(); #endif #ifdef __AFL_LOOP // Enable AFL persistent mode. Requires compilation using afl-clang-fast++. // See fuzzing.md for details. while (__AFL_LOOP(1000)) { std::vector buffer; if (!read_stdin(buffer)) { continue; } test_one_input(buffer); } #else std::vector buffer; if (!read_stdin(buffer)) { return 0; } test_one_input(buffer); #endif return 0; } #endif diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index adc5edb25..c5dd196e1 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -1,414 +1,417 @@ // Copyright (c) 2011-2019 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include