# Copyright (c) 2013-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 ENABLE_FUZZ_BINARY
noinst_PROGRAMS += test/fuzz/fuzz
endif

if ENABLE_TESTS
bin_PROGRAMS += test/test_bitcoin
endif

TEST_SRCDIR = test
TEST_BINARY=test/test_bitcoin$(EXEEXT)
FUZZ_BINARY=test/fuzz/fuzz$(EXEEXT)

JSON_TEST_FILES = \
  test/data/script_tests.json \
  test/data/bip341_wallet_vectors.json \
  test/data/base58_encode_decode.json \
  test/data/blockfilters.json \
  test/data/key_io_valid.json \
  test/data/key_io_invalid.json \
  test/data/script_tests.json \
  test/data/sighash.json \
  test/data/tx_invalid.json \
  test/data/tx_valid.json

RAW_TEST_FILES = \
  test/data/asmap.raw

GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)

BITCOIN_TEST_SUITE = \
  test/main.cpp \
  $(TEST_UTIL_H)

FUZZ_SUITE_LD_COMMON = \
 $(LIBTEST_UTIL) \
 $(LIBTEST_FUZZ) \
 $(LIBBITCOIN_NODE) \
 $(LIBBITCOIN_WALLET) \
 $(LIBBITCOIN_COMMON) \
 $(LIBBITCOIN_UTIL) \
 $(LIBBITCOIN_CONSENSUS) \
 $(LIBBITCOIN_CRYPTO) \
 $(LIBBITCOIN_CLI) \
 $(LIBUNIVALUE) \
 $(LIBLEVELDB) \
 $(LIBMEMENV) \
 $(LIBSECP256K1) \
 $(MINISKETCH_LIBS) \
 $(EVENT_LIBS) \
 $(EVENT_PTHREADS_LIBS)

if USE_UPNP
FUZZ_SUITE_LD_COMMON += $(MINIUPNPC_LIBS)
endif

if USE_NATPMP
FUZZ_SUITE_LD_COMMON += $(NATPMP_LIBS)
endif

# test_bitcoin binary #
BITCOIN_TESTS =\
  test/addrman_tests.cpp \
  test/allocator_tests.cpp \
  test/amount_tests.cpp \
  test/argsman_tests.cpp \
  test/arith_uint256_tests.cpp \
  test/banman_tests.cpp \
  test/base32_tests.cpp \
  test/base58_tests.cpp \
  test/base64_tests.cpp \
  test/bech32_tests.cpp \
  test/bip32_tests.cpp \
  test/bip324_tests.cpp \
  test/blockchain_tests.cpp \
  test/blockencodings_tests.cpp \
  test/blockfilter_index_tests.cpp \
  test/blockfilter_tests.cpp \
  test/blockmanager_tests.cpp \
  test/bloom_tests.cpp \
  test/bswap_tests.cpp \
  test/checkqueue_tests.cpp \
  test/coins_tests.cpp \
  test/coinstatsindex_tests.cpp \
  test/compilerbug_tests.cpp \
  test/compress_tests.cpp \
  test/crypto_tests.cpp \
  test/cuckoocache_tests.cpp \
  test/dbwrapper_tests.cpp \
  test/denialofservice_tests.cpp \
  test/descriptor_tests.cpp \
  test/flatfile_tests.cpp \
  test/fs_tests.cpp \
  test/getarg_tests.cpp \
  test/hash_tests.cpp \
  test/headers_sync_chainwork_tests.cpp \
  test/httpserver_tests.cpp \
  test/i2p_tests.cpp \
  test/interfaces_tests.cpp \
  test/key_io_tests.cpp \
  test/key_tests.cpp \
  test/logging_tests.cpp \
  test/mempool_tests.cpp \
  test/merkle_tests.cpp \
  test/merkleblock_tests.cpp \
  test/miner_tests.cpp \
  test/miniminer_tests.cpp \
  test/miniscript_tests.cpp \
  test/minisketch_tests.cpp \
  test/multisig_tests.cpp \
  test/net_peer_eviction_tests.cpp \
  test/net_tests.cpp \
  test/netbase_tests.cpp \
  test/orphanage_tests.cpp \
  test/pmt_tests.cpp \
  test/policy_fee_tests.cpp \
  test/policyestimator_tests.cpp \
  test/pool_tests.cpp \
  test/pow_tests.cpp \
  test/prevector_tests.cpp \
  test/raii_event_tests.cpp \
  test/random_tests.cpp \
  test/rbf_tests.cpp \
  test/rest_tests.cpp \
  test/result_tests.cpp \
  test/reverselock_tests.cpp \
  test/rpc_tests.cpp \
  test/sanity_tests.cpp \
  test/scheduler_tests.cpp \
  test/script_p2sh_tests.cpp \
  test/script_parse_tests.cpp \
  test/script_segwit_tests.cpp \
  test/script_standard_tests.cpp \
  test/script_tests.cpp \
  test/scriptnum10.h \
  test/scriptnum_tests.cpp \
  test/serfloat_tests.cpp \
  test/serialize_tests.cpp \
  test/settings_tests.cpp \
  test/sighash_tests.cpp \
  test/sigopcount_tests.cpp \
  test/skiplist_tests.cpp \
  test/sock_tests.cpp \
  test/streams_tests.cpp \
  test/sync_tests.cpp \
  test/system_tests.cpp \
  test/timedata_tests.cpp \
  test/torcontrol_tests.cpp \
  test/transaction_tests.cpp \
  test/translation_tests.cpp \
  test/txindex_tests.cpp \
  test/txpackage_tests.cpp \
  test/txreconciliation_tests.cpp \
  test/txrequest_tests.cpp \
  test/txvalidation_tests.cpp \
  test/txvalidationcache_tests.cpp \
  test/uint256_tests.cpp \
  test/util_tests.cpp \
  test/util_threadnames_tests.cpp \
  test/validation_block_tests.cpp \
  test/validation_chainstate_tests.cpp \
  test/validation_chainstatemanager_tests.cpp \
  test/validation_flush_tests.cpp \
  test/validation_tests.cpp \
  test/validationinterface_tests.cpp \
  test/versionbits_tests.cpp \
  test/xoroshiro128plusplus_tests.cpp

if ENABLE_WALLET
BITCOIN_TESTS += \
  wallet/test/feebumper_tests.cpp \
  wallet/test/psbt_wallet_tests.cpp \
  wallet/test/spend_tests.cpp \
  wallet/test/wallet_tests.cpp \
  wallet/test/walletdb_tests.cpp \
  wallet/test/wallet_crypto_tests.cpp \
  wallet/test/wallet_transaction_tests.cpp \
  wallet/test/coinselector_tests.cpp \
  wallet/test/init_tests.cpp \
  wallet/test/ismine_tests.cpp \
  wallet/test/rpc_util_tests.cpp \
  wallet/test/scriptpubkeyman_tests.cpp \
  wallet/test/walletload_tests.cpp \
  wallet/test/group_outputs_tests.cpp

FUZZ_SUITE_LD_COMMON +=\
 $(SQLITE_LIBS) \
 $(BDB_LIBS)

if USE_BDB
BITCOIN_TESTS += wallet/test/db_tests.cpp
endif

FUZZ_WALLET_SRC = \
 wallet/test/fuzz/coincontrol.cpp \
 wallet/test/fuzz/coinselection.cpp \
 wallet/test/fuzz/fees.cpp \
 wallet/test/fuzz/parse_iso8601.cpp

if USE_SQLITE
FUZZ_WALLET_SRC += \
 wallet/test/fuzz/notifications.cpp
endif # USE_SQLITE

BITCOIN_TEST_SUITE += \
  wallet/test/wallet_test_fixture.cpp \
  wallet/test/wallet_test_fixture.h \
  wallet/test/init_test_fixture.cpp \
  wallet/test/init_test_fixture.h
endif # ENABLE_WALLET

test_test_bitcoin_SOURCES = $(BITCOIN_TEST_SUITE) $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(BOOST_CPPFLAGS) $(EVENT_CFLAGS)
test_test_bitcoin_LDADD = $(LIBTEST_UTIL)
if ENABLE_WALLET
test_test_bitcoin_LDADD += $(LIBBITCOIN_WALLET)
test_test_bitcoin_CPPFLAGS += $(BDB_CPPFLAGS)
endif

test_test_bitcoin_LDADD += $(LIBBITCOIN_NODE) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) \
  $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS) $(MINISKETCH_LIBS)
test_test_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

test_test_bitcoin_LDADD += $(BDB_LIBS) $(MINIUPNPC_LIBS) $(NATPMP_LIBS) $(SQLITE_LIBS)
test_test_bitcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(PTHREAD_FLAGS) -static

if ENABLE_ZMQ
test_test_bitcoin_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
FUZZ_SUITE_LD_COMMON += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
endif

if ENABLE_FUZZ_BINARY
test_fuzz_fuzz_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
test_fuzz_fuzz_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
test_fuzz_fuzz_LDADD = $(FUZZ_SUITE_LD_COMMON)
test_fuzz_fuzz_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(PTHREAD_FLAGS) $(RUNTIME_LDFLAGS)
test_fuzz_fuzz_SOURCES = \
 $(FUZZ_WALLET_SRC) \
 test/fuzz/addition_overflow.cpp \
 test/fuzz/addrman.cpp \
 test/fuzz/asmap.cpp \
 test/fuzz/asmap_direct.cpp \
 test/fuzz/autofile.cpp \
 test/fuzz/banman.cpp \
 test/fuzz/base_encode_decode.cpp \
 test/fuzz/bech32.cpp \
 test/fuzz/bip324.cpp \
 test/fuzz/bitdeque.cpp \
 test/fuzz/block.cpp \
 test/fuzz/block_header.cpp \
 test/fuzz/blockfilter.cpp \
 test/fuzz/bloom_filter.cpp \
 test/fuzz/buffered_file.cpp \
 test/fuzz/chain.cpp \
 test/fuzz/checkqueue.cpp \
 test/fuzz/coins_view.cpp \
 test/fuzz/coinscache_sim.cpp \
 test/fuzz/connman.cpp \
 test/fuzz/crypto.cpp \
 test/fuzz/crypto_aes256.cpp \
 test/fuzz/crypto_aes256cbc.cpp \
 test/fuzz/crypto_chacha20.cpp \
 test/fuzz/crypto_common.cpp \
 test/fuzz/crypto_diff_fuzz_chacha20.cpp \
 test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \
 test/fuzz/crypto_poly1305.cpp \
 test/fuzz/cuckoocache.cpp \
 test/fuzz/decode_tx.cpp \
 test/fuzz/descriptor_parse.cpp \
 test/fuzz/deserialize.cpp \
 test/fuzz/eval_script.cpp \
 test/fuzz/fee_rate.cpp \
 test/fuzz/fees.cpp \
 test/fuzz/flatfile.cpp \
 test/fuzz/float.cpp \
 test/fuzz/golomb_rice.cpp \
 test/fuzz/headerssync.cpp \
 test/fuzz/hex.cpp \
 test/fuzz/http_request.cpp \
 test/fuzz/integer.cpp \
 test/fuzz/key.cpp \
 test/fuzz/key_io.cpp \
 test/fuzz/kitchen_sink.cpp \
 test/fuzz/load_external_block_file.cpp \
 test/fuzz/locale.cpp \
 test/fuzz/merkleblock.cpp \
 test/fuzz/message.cpp \
 test/fuzz/miniscript.cpp \
 test/fuzz/minisketch.cpp \
 test/fuzz/mini_miner.cpp \
 test/fuzz/muhash.cpp \
 test/fuzz/multiplication_overflow.cpp \
 test/fuzz/net.cpp \
 test/fuzz/net_permissions.cpp \
 test/fuzz/netaddress.cpp \
 test/fuzz/netbase_dns_lookup.cpp \
 test/fuzz/node_eviction.cpp \
 test/fuzz/p2p_transport_serialization.cpp \
 test/fuzz/package_eval.cpp \
 test/fuzz/parse_hd_keypath.cpp \
 test/fuzz/parse_numbers.cpp \
 test/fuzz/parse_script.cpp \
 test/fuzz/parse_univalue.cpp \
 test/fuzz/partially_downloaded_block.cpp \
 test/fuzz/policy_estimator.cpp \
 test/fuzz/policy_estimator_io.cpp \
 test/fuzz/poolresource.cpp \
 test/fuzz/pow.cpp \
 test/fuzz/prevector.cpp \
 test/fuzz/primitives_transaction.cpp \
 test/fuzz/process_message.cpp \
 test/fuzz/process_messages.cpp \
 test/fuzz/protocol.cpp \
 test/fuzz/psbt.cpp \
 test/fuzz/random.cpp \
 test/fuzz/rbf.cpp \
 test/fuzz/rolling_bloom_filter.cpp \
 test/fuzz/rpc.cpp \
 test/fuzz/script.cpp \
 test/fuzz/script_assets_test_minimizer.cpp \
 test/fuzz/script_bitcoin_consensus.cpp \
 test/fuzz/script_descriptor_cache.cpp \
 test/fuzz/script_flags.cpp \
 test/fuzz/script_format.cpp \
 test/fuzz/script_interpreter.cpp \
 test/fuzz/script_ops.cpp \
 test/fuzz/script_sigcache.cpp \
 test/fuzz/script_sign.cpp \
 test/fuzz/scriptnum_ops.cpp \
 test/fuzz/secp256k1_ec_seckey_import_export_der.cpp \
 test/fuzz/secp256k1_ecdsa_signature_parse_der_lax.cpp \
 test/fuzz/signature_checker.cpp \
 test/fuzz/signet.cpp \
 test/fuzz/socks5.cpp \
 test/fuzz/span.cpp \
 test/fuzz/spanparsing.cpp \
 test/fuzz/string.cpp \
 test/fuzz/strprintf.cpp \
 test/fuzz/system.cpp \
 test/fuzz/timedata.cpp \
 test/fuzz/torcontrol.cpp \
 test/fuzz/transaction.cpp \
 test/fuzz/tx_in.cpp \
 test/fuzz/tx_out.cpp \
 test/fuzz/tx_pool.cpp \
 test/fuzz/txorphan.cpp \
 test/fuzz/txrequest.cpp \
 test/fuzz/utxo_snapshot.cpp \
 test/fuzz/utxo_total_supply.cpp \
 test/fuzz/validation_load_mempool.cpp \
 test/fuzz/versionbits.cpp
endif # ENABLE_FUZZ_BINARY

nodist_test_test_bitcoin_SOURCES = $(GENERATED_TEST_FILES)

$(BITCOIN_TESTS): $(GENERATED_TEST_FILES)

CLEAN_BITCOIN_TEST = test/*.gcda test/*.gcno test/fuzz/*.gcda test/fuzz/*.gcno test/util/*.gcda test/util/*.gcno $(GENERATED_TEST_FILES) $(addsuffix .log,$(basename $(BITCOIN_TESTS)))

CLEANFILES += $(CLEAN_BITCOIN_TEST)

if TARGET_WINDOWS
bitcoin_test: $(TEST_BINARY)
else
if ENABLE_BENCH
bitcoin_test: $(TEST_BINARY) $(BENCH_BINARY)
else
bitcoin_test: $(TEST_BINARY)
endif
endif

bitcoin_test_check: $(TEST_BINARY) FORCE
	$(MAKE) check-TESTS TESTS=$^

bitcoin_test_clean : FORCE
	rm -f $(CLEAN_BITCOIN_TEST) $(test_test_bitcoin_OBJECTS) $(TEST_BINARY)

check-local: $(BITCOIN_TESTS:.cpp=.cpp.test)
if BUILD_BITCOIN_TX
	@echo "Running test/util/test_runner.py..."
	$(PYTHON) $(top_builddir)/test/util/test_runner.py
endif
	@echo "Running test/util/rpcauth-test.py..."
	$(PYTHON) $(top_builddir)/test/util/rpcauth-test.py
if TARGET_WINDOWS
else
if ENABLE_BENCH
	@echo "Running bench/bench_bitcoin (one iteration sanity check, only high priority)..."
	$(BENCH_BINARY) -sanity-check -priority-level=high
endif
endif
	$(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C secp256k1 check

if ENABLE_TESTS
UNIVALUE_TESTS = univalue/test/object univalue/test/unitester
noinst_PROGRAMS += $(UNIVALUE_TESTS)
TESTS += $(UNIVALUE_TESTS)

univalue_test_unitester_SOURCES = $(UNIVALUE_TEST_UNITESTER_INT)
univalue_test_unitester_LDADD = $(LIBUNIVALUE)
univalue_test_unitester_CPPFLAGS = -I$(srcdir)/$(UNIVALUE_INCLUDE_DIR_INT) -DJSON_TEST_SRC=\"$(srcdir)/$(UNIVALUE_TEST_DATA_DIR_INT)\"
univalue_test_unitester_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)

univalue_test_object_SOURCES = $(UNIVALUE_TEST_OBJECT_INT)
univalue_test_object_LDADD = $(LIBUNIVALUE)
univalue_test_object_CPPFLAGS = -I$(srcdir)/$(UNIVALUE_INCLUDE_DIR_INT)
univalue_test_object_LDFLAGS = -static $(LIBTOOL_APP_LDFLAGS)
endif

%.cpp.test: %.cpp
	@echo Running tests: $$(\
	  cat $< | \
	  grep -E "(BOOST_FIXTURE_TEST_SUITE\\(|BOOST_AUTO_TEST_SUITE\\()" | \
	  cut -d '(' -f 2 | cut -d ',' -f 1 | cut -d ')' -f 1\
	) from $<
	$(AM_V_at)export TEST_LOGFILE=$(abs_builddir)/$$(\
	  echo $< | grep -E -o "(wallet/test/.*\.cpp|test/.*\.cpp)" | $(SED) -e s/\.cpp/.log/ \
	) && \
	$(TEST_BINARY) --catch_system_errors=no -l test_suite -t "$$(\
	  cat $< | \
	  grep -E "(BOOST_FIXTURE_TEST_SUITE\\(|BOOST_AUTO_TEST_SUITE\\()" | \
	  cut -d '(' -f 2 | cut -d ',' -f 1 | cut -d ')' -f 1\
	)" -- DEBUG_LOG_OUT > "$$TEST_LOGFILE" 2>&1 || (cat "$$TEST_LOGFILE" && false)

%.json.h: %.json
	@$(MKDIR_P) $(@D)
	$(AM_V_GEN) { \
	 echo "#include <string>" && \
	 echo "namespace json_tests{" && \
	 echo "static const std::string $(*F){" && \
	 $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x  ,//g' && \
	 echo "};};"; \
	} > "$@.new" && mv -f "$@.new" "$@"