From 4a69d23e0561f519b5288f07a3f57abeed2113bd Mon Sep 17 00:00:00 2001 From: Duke Date: Tue, 3 Mar 2026 17:30:56 -0500 Subject: [PATCH] Add c++ unit tests with docs; split out ./rpctest and ./cpptest from ./test --- build.sh | 6 ++--- cpptest | 7 +++++ doc/DEVELOPING.md | 42 +++++++++++++++++++++++++++++ rpctest | 47 +++++++++++++++++++++++++++++++++ src/Makefile.am | 2 +- src/Makefile.test-hush.include | 4 +-- src/test-hush/main.cpp | 48 ++++++++++++++++++++-------------- src/test/bitcoin-util-test.py | 2 +- test | 47 +++------------------------------ 9 files changed, 135 insertions(+), 70 deletions(-) create mode 100755 cpptest create mode 100755 rpctest diff --git a/build.sh b/build.sh index d73119a08..e0891af08 100755 --- a/build.sh +++ b/build.sh @@ -7,11 +7,11 @@ set -eu -o pipefail # run correct build script for detected OS if [[ "$OSTYPE" == "linux-gnu"* ]]; then - ./util/build.sh --disable-tests $@ + ./util/build.sh $@ elif [[ "$OSTYPE" == "darwin"* ]]; then - ./util/build-mac.sh --disable-tests $@ + ./util/build-mac.sh $@ elif [[ "$OSTYPE" == "msys"* ]]; then - ./util/build-win.sh --disable-tests $@ + ./util/build-win.sh $@ #elif [[ "$OSTYPE" == "freebsd"* ]]; then # placeholder else diff --git a/cpptest b/cpptest new file mode 100755 index 000000000..8aff71e94 --- /dev/null +++ b/cpptest @@ -0,0 +1,7 @@ +#!/bin/bash +# Copyright 2026-now The Hush developers +# Distributed under the GPLv3 software license, see the accompanying +# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html + +# Run all tests by default +./src/hush-test $@ diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md index 4b6302daf..885becd08 100644 --- a/doc/DEVELOPING.md +++ b/doc/DEVELOPING.md @@ -16,6 +16,48 @@ other programs and Operating System overhead. A good rule of thumb is: Divide how many GBs of RAM you have by 2, subtract one. Use that many jobs. +# Run all tests + +To run both C++ and RPC tests: + + ./test + +C++ test run much faster than the RPC tests. + +## Run C++ Unit tests + +To run the C++ unit tests + + ./src/hush-test + +Example successful output: + + Running 42 test cases... + + *** No errors detected + +Example failure output: + + Running 42 test cases... + test-hush/main.cpp(16): error: in "test_sodium": check init_and_check_sodium() != 0 has failed [0 == 0] + + *** 1 failure is detected in the test module "HushTestSuite" + +# Run Python RPC tests + +To run our QA/functional tests: + + ./rpctest + +Example successful output: + + # Running 2 tests.. + PASS! + +Example failure output: + + FAIL! Number of failed tests: 1 . Details in test-1234567.txt + ## Dealing with dependency changes diff --git a/rpctest b/rpctest new file mode 100755 index 000000000..c9c507ca8 --- /dev/null +++ b/rpctest @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# Copyright 2016-2026 The Hush developers +# Distributed under the GPLv3 software license, see the accompanying +# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html + +use strict; +use warnings; +use 5.010; + +my $flags = $ENV{TEST_FLAGS} || '--tracerpc'; +my $test_dir = './qa/rpc-tests'; + +$ENV{PYTHONPATH} = "./qa/rpc-tests/test_framework/"; +#$ENV{PYTHON_DEBUG} = 1; + +my @tests_to_run = qw{ + lockzins.py + shieldcoinbase_donation.py +}; + +my $exit = 0; +my $failed_tests = 0; +my $time=time(); +my $num_tests = @tests_to_run; + +print "# Running $num_tests tests"; +for my $test (@tests_to_run) { + # send both stderr+stdout to our output file + my $cmd = "$test_dir/$test $flags 1>test-$time.txt 2>&1"; + system($cmd); + + print "."; + + if($?) { + say "$cmd FAILED!"; + $exit = 1; + $failed_tests++; + } +} +print "\n"; + +if ($exit) { + say "FAIL! Number of failed tests: $failed_tests . Details in test-$time.txt"; +} else { + say "PASS!"; +} +exit($exit); diff --git a/src/Makefile.am b/src/Makefile.am index 979df1a58..1eb1f59f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -683,7 +683,7 @@ endif $(AM_V_GEN) $(PROTOC) --cpp_out=$(@D) --proto_path=$(abspath $( -std::string notaryPubkey = "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47"; -std::string notarySecret = "UxFWWxsf1d7w7K5TvAWSkeX4H95XQKwdwGv49DXwWUTzPTTjHBbU"; -int main(int argc, char **argv) { - /* - assert(init_and_check_sodium() != -1); - ECC_Start(); - ECCVerifyHandle handle; // Inits secp256k1 verify context - SelectParams(CBaseChainParams::REGTEST); - - CBitcoinSecret vchSecret; - // this returns false due to network prefix mismatch but works anyway - vchSecret.SetString(notarySecret); - CKey notaryKey = vchSecret.GetKey(); - */ - - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); +// Test that libsodium has been initialized correctly +BOOST_AUTO_TEST_CASE(test_sodium) { + BOOST_CHECK_NE( init_and_check_sodium(), -1 ); +} + +BOOST_AUTO_TEST_CASE(test_ecc) { + ECC_Start(); + BOOST_CHECK("created secp256k1 context"); + + ECCVerifyHandle handle; // Inits secp256k1 verify context + // this value is currently private + //BOOST_CHECK_EQUAL( ECCVerifyHandle::refcount, 1 ); + + ECC_Stop(); + BOOST_CHECK("destroyed secp256k1 context"); +} + +BOOST_AUTO_TEST_CASE(test_nets) { + + SelectParams(CBaseChainParams::REGTEST); + BOOST_CHECK("regtest"); + SelectParams(CBaseChainParams::MAIN); + BOOST_CHECK("mainnet"); + SelectParams(CBaseChainParams::TESTNET); + BOOST_CHECK("testnet"); } diff --git a/src/test/bitcoin-util-test.py b/src/test/bitcoin-util-test.py index 9aa9b3453..545f48116 100755 --- a/src/test/bitcoin-util-test.py +++ b/src/test/bitcoin-util-test.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright (c) 2016-2024 The Hush developers # Copyright 2014 BitPay, Inc. # Distributed under the GPLv3 software license, see the accompanying diff --git a/test b/test index c9c507ca8..222272305 100755 --- a/test +++ b/test @@ -1,47 +1,8 @@ -#!/usr/bin/env perl -# Copyright 2016-2026 The Hush developers +#!/bin/bash +# Copyright 2026-now The Hush developers # Distributed under the GPLv3 software license, see the accompanying # file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html -use strict; -use warnings; -use 5.010; +# Run c++ tests first, they are faster +./cpptest && ./rpctest -my $flags = $ENV{TEST_FLAGS} || '--tracerpc'; -my $test_dir = './qa/rpc-tests'; - -$ENV{PYTHONPATH} = "./qa/rpc-tests/test_framework/"; -#$ENV{PYTHON_DEBUG} = 1; - -my @tests_to_run = qw{ - lockzins.py - shieldcoinbase_donation.py -}; - -my $exit = 0; -my $failed_tests = 0; -my $time=time(); -my $num_tests = @tests_to_run; - -print "# Running $num_tests tests"; -for my $test (@tests_to_run) { - # send both stderr+stdout to our output file - my $cmd = "$test_dir/$test $flags 1>test-$time.txt 2>&1"; - system($cmd); - - print "."; - - if($?) { - say "$cmd FAILED!"; - $exit = 1; - $failed_tests++; - } -} -print "\n"; - -if ($exit) { - say "FAIL! Number of failed tests: $failed_tests . Details in test-$time.txt"; -} else { - say "PASS!"; -} -exit($exit);