#!/bin/sh
#
# Run the upstream acmetest harness from the source tree over the cases that
# need neither a real domain nor a CA account. The other le_test_* cases are
# integration tests: they issue certificates for a public host against the
# Let's Encrypt staging server, so they cannot run here.
#
set -u

SRC="$(pwd)"
WORK="${AUTOPKGTEST_TMP:-$(mktemp -d)}"

# letest.sh runs "cd acme.sh; ./acme.sh install" before every case, and the
# cases then use $HOME/.acme.sh/acme.sh: point both at the installed package.
# The wrapper has to be executed (it execs the real script), while the tests
# source it, so they get the script body and ACME_PACKAGED=1 from the wrapper.
mkdir -p "$WORK/acme.sh" "$HOME/.acme.sh"
ln -sf /usr/bin/acme.sh "$WORK/acme.sh/acme.sh"
ln -sf /usr/share/acme.sh/acme.sh "$HOME/.acme.sh/acme.sh"
cp -a /usr/share/acme.sh/dnsapi /usr/share/acme.sh/deploy /usr/share/acme.sh/notify "$HOME/.acme.sh/"

# ACME_PACKAGED: what /usr/bin/acme.sh exports, so the cases take the packaged
# code paths. TEST_LOCAL: use the local install, do not download a branch.
# QUICK_TEST: skip the slow cases. TestingDomain: without it letest.sh
# downloads cloudflared and opens a tunnel.
export ACME_PACKAGED=1 TEST_LOCAL=1 QUICK_TEST=1 TestingDomain=acmetest.invalid

cd "$WORK" || exit 1

# letest.sh enumerates its own cases by grepping FILE_NAME="letest.sh", so it
# has to run from a directory holding that name, next to the acme.sh/ symlink.
ln -sf "$SRC/acmetest/letest.sh" letest.sh

# Cases to run, one per line: add or remove a line, keep them sorted.
CASES="$(tr '\n' ',' <<'EOF'
le_test_ari_replaces_rejected
le_test_assertcmd_timeout
le_test_calc_ari_renew_time
le_test_calc_next_renew_time
le_test_calc_validto_renew_time
le_test_cleardeployconf
le_test_conf_skip_unchanged
le_test_dependencies
le_test_dns_persist_txt_name
le_test_extract_aki
le_test_filter_cron_bin
le_test_filter_listen_port
le_test_gateway_error_codes
le_test_getaccountemail
le_test_head_nonce
le_test_httpdate2time
le_test_insecure_not_saved
le_test_installcronjob_no_wipe
le_test_isIPv4
le_test_is_valid_cn
le_test_json_encode
le_test_mailto_contacts
le_test_order_not_ready
le_test_post_file
le_test_read_altnames_from_csr
le_test_retry_backoff
le_test_retryafter_seconds
le_test_revoked_response
le_test_save_conf_quote
le_test_send_notify_clears_headers
le_test_setopt_escape
le_test_shell
le_test_signed_request_transport_error
le_test_ss_port_in_use
le_test_startserver_listen_both
le_test_strip_blank_lines
le_test_wget2_headers
EOF
)"
CASES="${CASES%,}"

./letest.sh "$CASES"
