# When the day comes that Tor requires Automake >= 1.12 change
# TESTS_ENVIRONMENT to AM_TESTS_ENVIRONMENT because the former is reserved for
# users while the later is reserved for developers.
TESTS_ENVIRONMENT = \
	export PYTHON="$(PYTHON)"; \
	export SHELL="$(SHELL)"; \
	export abs_top_srcdir="$(abs_top_srcdir)"; \
	export abs_top_builddir="$(abs_top_builddir)"; \
	export builddir="$(builddir)"; \
	export TESTING_TOR_BINARY="$(TESTING_TOR_BINARY)"; \
	export CCLD="$(CCLD)";

TESTSCRIPTS = \
	src/test/fuzz_static_testcases.sh \
	src/test/test_zero_length_keys.sh \
	src/test/test_workqueue_cancel.sh \
	src/test/test_workqueue_efd.sh \
	src/test/test_workqueue_efd2.sh \
	src/test/test_workqueue_pipe.sh \
	src/test/test_workqueue_pipe2.sh \
	src/test/test_workqueue_socketpair.sh \
	src/test/test_switch_id.sh \
	src/test/test_cmdline.sh \
	src/test/test_parseconf.sh \
	src/test/unittest_part1.sh \
	src/test/unittest_part2.sh \
	src/test/unittest_part3.sh \
	src/test/unittest_part4.sh \
	src/test/unittest_part5.sh \
	src/test/unittest_part6.sh \
	src/test/unittest_part7.sh \
	src/test/unittest_part8.sh

if USEPYTHON
TESTSCRIPTS += \
	src/test/test_ntor.sh \
	src/test/test_hs_ntor.sh \
	src/test/test_bt.sh \
	scripts/maint/practracker/test_practracker.sh \
	scripts/maint/run_check_subsystem_order.sh

if COVERAGE_ENABLED
# ...
else
# Only do this when coverage is not on, since it invokes lots of code
# in a kind of unpredictable way.
TESTSCRIPTS += src/test/test_rebind.sh
TESTSCRIPTS += src/test/test_include.sh
endif
endif

if USE_PERL
TESTSCRIPTS += \
	scripts/maint/checkSpaceTest.sh
endif

TESTS += src/test/test-slow src/test/test-memwipe \
	src/test/test_workqueue \
	src/test/test_keygen.sh \
	src/test/test_key_expiration.sh \
	src/test/test-timers \
	$(TESTSCRIPTS)

# These flavors are run using automake's test-driver and test-network.sh

# run a quick test or two
# this test only uses IPv4
TEST_CHUTNEY_FLAVOR_QUICK = bridges+hs-v3
# only run if we can ping6 ::1 (localhost)
TEST_CHUTNEY_FLAVOR_QUICK_IPV6 = single-onion-v3-ipv6-md

# run a basic set of tests, which only use IPv4
TEST_CHUTNEY_FLAVORS = basic-min bridges-min hs-v3-min single-onion-v3

# only run if we can ping ::1 (localhost)
TEST_CHUTNEY_FLAVORS_IPV6 = bridges+ipv6-min ipv6-exit-min hs-v3-ipv6 \
	single-onion-v3-ipv6-md

# only run if we can find a stable (or simply another) version of tor
TEST_CHUTNEY_FLAVORS_MIXED = mixed+hs-v3

# only run if IPv6 and mixed networks are run
TEST_CHUTNEY_FLAVORS_IPV6_MIXED = mixed+hs-v3-ipv6

### This is a lovely feature, but it requires automake >= 1.12, and Tor
###  doesn't require that yet.
###
# TEST_EXTENSIONS = .sh
# SH_LOG_COMPILER = $(SHELL)

noinst_PROGRAMS+= src/test/bench
if UNITTESTS_ENABLED
noinst_PROGRAMS+= \
	src/test/test \
	src/test/test-slow \
	src/test/test-memwipe \
	src/test/test-process \
	src/test/test_workqueue \
	src/test/test-switch-id \
	src/test/test-timers \
	src/test/test-rng
endif

src_test_AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
	-DLOCALSTATEDIR="\"$(localstatedir)\"" \
	-DBINDIR="\"$(bindir)\"" \
	-DTOR_UNIT_TESTS \
	$(AM_CPPFLAGS)

# -L flags need to go in LDFLAGS. -l flags need to go in LDADD.
# This seems to matter nowhere but on Windows, but I assure you that it
# matters a lot there, and is quite hard to debug if you forget to do it.

src_test_test_SOURCES =

if UNITTESTS_ENABLED

# ADD_C_FILE: INSERT SOURCES HERE.
src_test_test_SOURCES += \
	src/test/fakecircs.c \
	src/test/log_test_helpers.c \
	src/test/hs_test_helpers.c \
	src/test/opts_test_helpers.c \
	src/test/resolve_test_helpers.c \
	src/test/rng_test_helpers.c \
	src/test/test.c \
	src/test/test_accounting.c \
	src/test/test_addr.c \
	src/test/test_address.c \
	src/test/test_address_set.c \
	src/test/test_bridges.c \
	src/test/test_btrack.c \
	src/test/test_buffers.c \
	src/test/test_bwmgt.c \
	src/test/test_cell_formats.c \
	src/test/test_cell_queue.c \
	src/test/test_channel.c \
	src/test/test_channelpadding.c \
	src/test/test_circuitpadding.c \
	src/test/test_channeltls.c \
	src/test/test_checkdir.c \
	src/test/test_circuitlist.c \
	src/test/test_circuitmux.c \
	src/test/test_circuitmux_ewma.c \
	src/test/test_circuitbuild.c \
	src/test/test_circuituse.c \
	src/test/test_circuitstats.c \
	src/test/test_compat_libevent.c \
	src/test/test_config.c \
	src/test/test_conflux_cell.c \
	src/test/test_conflux_pool.c \
	src/test/test_confmgr.c \
	src/test/test_confparse.c \
	src/test/test_congestion_control.c \
	src/test/test_connection.c \
	src/test/test_conscache.c \
	src/test/test_consdiff.c \
	src/test/test_consdiffmgr.c \
	src/test/test_containers.c \
	src/test/test_controller.c \
	src/test/test_controller_events.c \
	src/test/test_crypto.c \
	src/test/test_crypto_ope.c \
	src/test/test_crypto_rng.c \
	src/test/test_crypto_cgo.c \
	src/test/test_data.c \
	src/test/test_dir.c \
	src/test/test_dirauth_ports.c \
	src/test/test_dirvote.c \
	src/test/test_dir_common.c \
	src/test/test_dir_handle_get.c \
	src/test/test_dispatch.c \
	src/test/test_dos.c \
	src/test/test_entryconn.c \
	src/test/test_entrynodes.c \
	src/test/test_geoip.c \
	src/test/test_guardfraction.c \
	src/test/test_extorport.c \
	src/test/test_hs_common.c \
	src/test/test_hs_config.c \
	src/test/test_hs_cell.c \
	src/test/test_hs_ntor.c \
	src/test/test_hs_service.c \
	src/test/test_hs_client.c  \
	src/test/test_hs_intropoint.c \
	src/test/test_hs_control.c \
	src/test/test_hs_ob.c \
	src/test/test_handles.c \
	src/test/test_hs_cache.c \
	src/test/test_hs_descriptor.c \
	src/test/test_hs_dos.c \
	src/test/test_hs_metrics.c \
	src/test/test_hs_pow.c \
	src/test/test_keypin.c \
	src/test/test_link_handshake.c \
	src/test/test_logging.c \
	src/test/test_mainloop.c \
	src/test/test_metrics.c \
	src/test/test_microdesc.c \
	src/test/test_namemap.c \
	src/test/test_netinfo.c \
	src/test/test_nodelist.c \
	src/test/test_ntor_v3.c \
	src/test/test_oom.c \
	src/test/test_oos.c \
	src/test/test_options.c \
	src/test/test_options_act.c \
	src/test/test_pem.c \
	src/test/test_periodic_event.c \
	src/test/test_policy.c \
	src/test/test_process.c \
	src/test/test_process_descs.c \
	src/test/test_prob_distr.c \
	src/test/test_procmon.c \
	src/test/test_proto_haproxy.c \
	src/test/test_proto_http.c \
	src/test/test_proto_misc.c \
	src/test/test_protover.c \
	src/test/test_pt.c \
	src/test/test_pubsub_build.c \
	src/test/test_pubsub_msg.c \
	src/test/test_relay.c \
	src/test/test_relaycell.c \
	src/test/test_relaycrypt.c \
	src/test/test_replay.c \
	src/test/test_router.c \
	src/test/test_routerkeys.c \
	src/test/test_routerlist.c \
	src/test/test_routerset.c \
	src/test/test_sandbox.c \
	src/test/test_scheduler.c \
	src/test/test_sendme.c \
	src/test/test_shared_random.c \
	src/test/test_socks.c \
	src/test/test_statefile.c \
	src/test/test_stats.c \
	src/test/test_status.c \
	src/test/test_storagedir.c \
	src/test/test_threads.c \
	src/test/test_token_bucket.c \
	src/test/test_tortls.c \
	src/test/test_util.c \
	src/test/test_util_format.c \
	src/test/test_util_process.c \
	src/test/test_voting_flags.c \
	src/test/test_voting_schedule.c \
	src/test/test_x509.c \
	src/test/test_helpers.c \
	src/test/test_dns.c \
	src/test/test_parsecommon.c \
	src/test/testing_common.c \
	src/test/testing_rsakeys.c \
	src/ext/tinytest.c

if USE_NSS
# ...
else
src_test_test_SOURCES += \
	src/test/test_crypto_openssl.c \
	src/test/test_tortls_openssl.c
endif

endif

src_test_test_slow_SOURCES =
if UNITTESTS_ENABLED
src_test_test_slow_SOURCES += \
	src/test/rng_test_helpers.c \
	src/test/test_slow.c \
	src/test/test_crypto_slow.c \
	src/test/test_process_slow.c \
	src/test/test_hs_pow_slow.c \
	src/test/test_prob_distr.c \
	src/test/ptr_helpers.c \
	src/test/test_ptr_slow.c \
	src/test/testing_common.c \
	src/test/testing_rsakeys.c \
	src/ext/tinytest.c
endif

src_test_test_memwipe_SOURCES = \
	src/test/test-memwipe.c

src_test_test_timers_SOURCES = \
	src/test/test-timers.c

src_test_test_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)

src_test_test_CPPFLAGS= $(src_test_AM_CPPFLAGS) $(TEST_CPPFLAGS)

src_test_bench_SOURCES = \
	src/test/bench.c

src_test_test_workqueue_SOURCES = \
	src/test/test_workqueue.c
src_test_test_workqueue_CPPFLAGS= $(src_test_AM_CPPFLAGS)
src_test_test_workqueue_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)

src_test_test_switch_id_SOURCES = \
	src/test/test_switch_id.c
src_test_test_switch_id_CPPFLAGS= $(src_test_AM_CPPFLAGS)
src_test_test_switch_id_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
src_test_test_switch_id_LDFLAGS = @TOR_LDFLAGS_zlib@
src_test_test_switch_id_LDADD = \
	$(TOR_UTIL_TESTING_LIBS) \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
	@TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_USERENV@ \
	@TOR_LZMA_LIBS@ @TOR_ZSTD_LIBS@ @TOR_TRACE_LIBS@
src_test_test_LDFLAGS = @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB) \
	@TOR_LDFLAGS_libevent@
src_test_test_LDADD = \
	src/test/libtor-testing.a \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ \
	@TOR_SYSTEMD_LIBS@ @TOR_LZMA_LIBS@ @TOR_ZSTD_LIBS@ @TOR_TRACE_LIBS@

src_test_test_slow_CPPFLAGS = $(src_test_test_CPPFLAGS)
src_test_test_slow_CFLAGS = $(src_test_test_CFLAGS)
src_test_test_slow_LDADD = $(src_test_test_LDADD)
src_test_test_slow_LDFLAGS = $(src_test_test_LDFLAGS)

src_test_test_rng_CPPFLAGS = $(src_test_test_CPPFLAGS)
src_test_test_rng_CFLAGS = $(src_test_test_CFLAGS)
src_test_test_rng_SOURCES = src/test/test_rng.c
src_test_test_rng_LDFLAGS =  $(src_test_test_LDFLAGS)
src_test_test_rng_LDADD = $(src_test_test_LDADD)

src_test_test_memwipe_CPPFLAGS = $(src_test_test_CPPFLAGS)
# Don't use bugtrap cflags here: memwipe tests require memory violations.
src_test_test_memwipe_CFLAGS = $(TEST_CFLAGS)
src_test_test_memwipe_LDADD = $(src_test_test_LDADD)
# The LDFLAGS need to include the bugtrap cflags, or else we won't link
# successfully with the libraries built with them.
src_test_test_memwipe_LDFLAGS = $(src_test_test_LDFLAGS) @CFLAGS_BUGTRAP@

src_test_bench_LDFLAGS = @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB) \
	@TOR_LDFLAGS_libevent@
src_test_bench_LDADD = \
	libtor.a \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ \
	@TOR_SYSTEMD_LIBS@ @TOR_LZMA_LIBS@ @TOR_ZSTD_LIBS@ @TOR_TRACE_LIBS@

src_test_test_workqueue_LDFLAGS = @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB) \
	@TOR_LDFLAGS_libevent@
src_test_test_workqueue_LDADD = \
        src/test/libtor-testing.a \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ \
	@TOR_LZMA_LIBS@ @TOR_ZSTD_LIBS@ @TOR_TRACE_LIBS@

src_test_test_timers_CPPFLAGS = $(src_test_test_CPPFLAGS)
src_test_test_timers_CFLAGS = $(src_test_test_CFLAGS)
src_test_test_timers_LDADD = \
	src/lib/libtor-evloop-testing.a \
	$(TOR_CRYPTO_TESTING_LIBS) \
	$(TOR_UTIL_TESTING_LIBS) \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ @TOR_LIBEVENT_LIBS@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ \
	@TOR_LZMA_LIBS@ @TOR_TRACE_LIBS@
src_test_test_timers_LDFLAGS = $(src_test_test_LDFLAGS)

# ADD_C_FILE: INSERT HEADERS HERE.
noinst_HEADERS+= \
	src/test/compression_bomb.h \
	src/test/fakechans.h \
	src/test/fakecircs.h \
	src/test/hs_test_helpers.h \
	src/test/log_test_helpers.h \
	src/test/opts_test_helpers.h \
	src/test/resolve_test_helpers.h \
	src/test/rng_test_helpers.h \
	src/test/test.h \
	src/test/ptr_helpers.h \
	src/test/test_helpers.h \
	src/test/test_dir_common.h \
	src/test/test_connection.h \
	src/test/test_tortls.h \
	src/test/test_descriptors.inc \
	src/test/example_extrainfo.inc \
	src/test/failing_routerdescs.inc \
	src/test/ed25519_vectors.inc \
	src/test/cgo_vectors.inc \
	src/test/test_descriptors.inc \
	src/test/test_hs_descriptor.inc \
	src/test/vote_descriptors.inc

noinst_PROGRAMS+= src/test/test-ntor-cl
noinst_PROGRAMS+= src/test/test-hs-ntor-cl
src_test_test_ntor_cl_SOURCES  = src/test/test_ntor_cl.c
src_test_test_ntor_cl_LDFLAGS = @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB)
src_test_test_ntor_cl_LDADD = \
	libtor.a \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ @TOR_LZMA_LIBS@ @TOR_TRACE_LIBS@
src_test_test_ntor_cl_AM_CPPFLAGS =	       \
	$(AM_CPPFLAGS)

src_test_test_hs_ntor_cl_SOURCES  = src/test/test_hs_ntor_cl.c
src_test_test_hs_ntor_cl_LDFLAGS = @TOR_LDFLAGS_zlib@ $(TOR_LDFLAGS_CRYPTLIB)
src_test_test_hs_ntor_cl_LDADD = \
	libtor.a \
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
	$(TOR_LIBS_CRYPTLIB) @TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ \
	@CURVE25519_LIBS@ @TOR_TRACE_LIBS@
src_test_test_hs_ntor_cl_AM_CPPFLAGS =	       \
	$(AM_CPPFLAGS)

if UNITTESTS_ENABLED
noinst_PROGRAMS += src/test/test-bt-cl
src_test_test_bt_cl_SOURCES = src/test/test_bt_cl.c
src_test_test_bt_cl_LDADD = \
	$(TOR_UTIL_TESTING_LIBS) \
	@TOR_LIB_MATH@ \
	@TOR_LIB_WS32@ @TOR_LIB_IPHLPAPI@ @TOR_LIB_SHLWAPI@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@TOR_TRACE_LIBS@
src_test_test_bt_cl_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
src_test_test_bt_cl_CPPFLAGS= $(src_test_AM_CPPFLAGS) $(TEST_CPPFLAGS)
endif

EXTRA_DIST += \
	src/test/bt_test.py \
	src/test/ntor_ref.py \
	src/test/hs_ntor_ref.py \
	src/test/hs_build_address.py \
	src/test/hs_indexes.py \
	src/test/fuzz_static_testcases.sh \
	src/test/slownacl_curve25519.py \
	src/test/test_rebind.sh \
	src/test/test_rebind.py \
	src/test/test_include.sh \
	src/test/test_include.py \
	src/test/zero_length_keys.sh \
	scripts/maint/run_check_subsystem_order.sh \
	src/test/test_keygen.sh \
	src/test/test_key_expiration.sh \
	src/test/test_zero_length_keys.sh \
	src/test/test_ntor.sh src/test/test_hs_ntor.sh \
	src/test/test_bt.sh \
	src/test/test-network.sh \
	src/test/test_switch_id.sh \
	src/test/test_workqueue_cancel.sh \
	src/test/test_workqueue_efd.sh \
	src/test/test_workqueue_efd2.sh \
	src/test/test_workqueue_pipe.sh \
	src/test/test_workqueue_pipe2.sh \
	src/test/test_workqueue_socketpair.sh \
	src/test/test_cmdline.sh \
	src/test/test_parseconf.sh \
	src/test/unittest_part1.sh \
	src/test/unittest_part2.sh \
	src/test/unittest_part3.sh \
	src/test/unittest_part4.sh \
	src/test/unittest_part5.sh \
	src/test/unittest_part6.sh \
	src/test/unittest_part7.sh \
	src/test/unittest_part8.sh
