Add c++ unit tests with docs; split out ./rpctest and ./cpptest from ./test

This commit is contained in:
Duke
2026-03-03 17:30:56 -05:00
parent 7e1b5701a6
commit 4a69d23e05
9 changed files with 135 additions and 70 deletions

View File

@@ -7,11 +7,11 @@ set -eu -o pipefail
# run correct build script for detected OS # run correct build script for detected OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then if [[ "$OSTYPE" == "linux-gnu"* ]]; then
./util/build.sh --disable-tests $@ ./util/build.sh $@
elif [[ "$OSTYPE" == "darwin"* ]]; then elif [[ "$OSTYPE" == "darwin"* ]]; then
./util/build-mac.sh --disable-tests $@ ./util/build-mac.sh $@
elif [[ "$OSTYPE" == "msys"* ]]; then elif [[ "$OSTYPE" == "msys"* ]]; then
./util/build-win.sh --disable-tests $@ ./util/build-win.sh $@
#elif [[ "$OSTYPE" == "freebsd"* ]]; then #elif [[ "$OSTYPE" == "freebsd"* ]]; then
# placeholder # placeholder
else else

7
cpptest Executable file
View File

@@ -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 $@

View File

@@ -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. 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 ## Dealing with dependency changes

47
rpctest Executable file
View File

@@ -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);

View File

@@ -683,7 +683,7 @@ endif
$(AM_V_GEN) $(PROTOC) --cpp_out=$(@D) --proto_path=$(abspath $(<D) $<) $(AM_V_GEN) $(PROTOC) --cpp_out=$(@D) --proto_path=$(abspath $(<D) $<)
if ENABLE_TESTS if ENABLE_TESTS
#include Makefile.test-hush.include include Makefile.test-hush.include
#include Makefile.test.include #include Makefile.test.include
#include Makefile.gtest.include #include Makefile.gtest.include
endif endif

View File

@@ -7,7 +7,6 @@ bin_PROGRAMS += hush-test
# tool for generating our public parameters # tool for generating our public parameters
hush_test_SOURCES = test-hush/main.cpp hush_test_SOURCES = test-hush/main.cpp
# devs can enable this shit, it just slows down default compiles
# test-hush/testutils.cpp \ # test-hush/testutils.cpp \
# test-hush/test_cryptoconditions.cpp \ # test-hush/test_cryptoconditions.cpp \
# test-hush/test_coinimport.cpp \ # test-hush/test_coinimport.cpp \
@@ -19,6 +18,7 @@ hush_test_SOURCES = test-hush/main.cpp
hush_test_CPPFLAGS = $(hushd_CPPFLAGS) hush_test_CPPFLAGS = $(hushd_CPPFLAGS)
hush_test_LDADD = -lgtest $(hushd_LDADD) #hush_test_LDADD = -lgtest $(hushd_LDADD)
hush_test_LDADD = $(hushd_LDADD)
hush_test_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static hush_test_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static

View File

@@ -1,29 +1,37 @@
// Copyright (c) 2016-2024 The Hush developers // Copyright (c) 2016-now The Hush developers
// Distributed under the GPLv3 software license, see the accompanying // Distributed under the GPLv3 software license, see the accompanying
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
#include "key.h" #include "key.h"
#include "base58.h" #include "base58.h"
#include "chainparams.h" #include "chainparams.h"
#include "gtest/gtest.h"
#include "crypto/common.h" #include "crypto/common.h"
//#include "testutils.h" #define BOOST_TEST_MODULE HushTestSuite
#include <boost/test/included/unit_test.hpp>
std::string notaryPubkey = "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47";
std::string notarySecret = "UxFWWxsf1d7w7K5TvAWSkeX4H95XQKwdwGv49DXwWUTzPTTjHBbU";
int main(int argc, char **argv) { // Test that libsodium has been initialized correctly
/* BOOST_AUTO_TEST_CASE(test_sodium) {
assert(init_and_check_sodium() != -1); BOOST_CHECK_NE( init_and_check_sodium(), -1 );
ECC_Start(); }
ECCVerifyHandle handle; // Inits secp256k1 verify context
SelectParams(CBaseChainParams::REGTEST); BOOST_AUTO_TEST_CASE(test_ecc) {
ECC_Start();
CBitcoinSecret vchSecret; BOOST_CHECK("created secp256k1 context");
// this returns false due to network prefix mismatch but works anyway
vchSecret.SetString(notarySecret); ECCVerifyHandle handle; // Inits secp256k1 verify context
CKey notaryKey = vchSecret.GetKey(); // this value is currently private
*/ //BOOST_CHECK_EQUAL( ECCVerifyHandle::refcount, 1 );
testing::InitGoogleTest(&argc, argv); ECC_Stop();
return RUN_ALL_TESTS(); 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");
} }

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# Copyright (c) 2016-2024 The Hush developers # Copyright (c) 2016-2024 The Hush developers
# Copyright 2014 BitPay, Inc. # Copyright 2014 BitPay, Inc.
# Distributed under the GPLv3 software license, see the accompanying # Distributed under the GPLv3 software license, see the accompanying

47
test
View File

@@ -1,47 +1,8 @@
#!/usr/bin/env perl #!/bin/bash
# Copyright 2016-2026 The Hush developers # Copyright 2026-now The Hush developers
# Distributed under the GPLv3 software license, see the accompanying # Distributed under the GPLv3 software license, see the accompanying
# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html # file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
use strict; # Run c++ tests first, they are faster
use warnings; ./cpptest && ./rpctest
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);