Files
mirror-3x-ui/scripts/test/ip-ssl-entrypoint-test.sh
T

76 lines
4.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT="${IP_SSL_ENTRY_ROOT:-$(cd "$(dirname "$0")/../.." && pwd)}"
source_function() {
source /dev/stdin < <(awk -v name="$2" '$0==name"() {" {body=1} body {print} body && /^}/ {exit}' "$1")
}
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
TEMP=$(mktemp -d)
trap 'rm -rf "$TEMP"' EXIT
export red='' green='' yellow='' blue='' plain='' release=ubuntu
source /dev/stdin < <(awk '/^# BEGIN GENERATED IP SSL HELPERS/ {block=1;next} /^# END GENERATED IP SSL HELPERS/ {block=0} block {print}' "$ROOT/install.sh")
source_function "$ROOT/install.sh" prompt_or_default
source_function "$ROOT/install.sh" prompt_and_setup_ssl
# Invoked by the real installer function loaded above, before the failure stubs.
# shellcheck disable=SC2317,SC2329
setup_ip_certificate() { captured="${2:-}"; called=1; }
# shellcheck disable=SC2317,SC2329
systemctl() { stopped=1; }
export NONINTERACTIVE=1 XUI_SSL_MODE=ip XUI_SSL_IPV6='not:ipv6'
called=0; stopped=0
SSL_HOST=''
prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/invalid" 2>&1 || fail 'an invalid XUI_SSL_IPV6 aborted the installer instead of skipping the certificate'
[[ "$called" == 0 && "$stopped" == 0 ]] || fail 'invalid IPv6 stopped panel or reached issuance'
grep -q 'Invalid public IPv6' "$TEMP/invalid" || fail 'invalid environment value has no clear diagnostic'
[[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after skipping the certificate'
unset XUI_SSL_IPV6
called=0; stopped=0
prompt_and_setup_ssl 2053 / 198.51.100.900 > "$TEMP/invalid-v4" 2>&1 || fail 'an invalid IPv4 aborted the installer'
[[ "$called" == 0 && "$stopped" == 0 ]] || fail 'invalid IPv4 stopped panel or reached issuance'
called=0
prompt_and_setup_ssl 2053 / 198.51.100.8 </dev/null > "$TEMP/unattended" 2>&1
[[ "$called" == 1 && -z "$captured" ]] || fail 'unattended unset override did not retain IPv4-only issuance'
if grep -q 'Detecting public IPv6' "$TEMP/unattended"; then fail 'unattended installer detected IPv6 without opt-in'; fi
export XUI_SSL_IPV6='2001:4860::1'; called=0
prompt_and_setup_ssl 2053 / 198.51.100.8 </dev/null > /dev/null
[[ "$called" == 1 && "$captured" == '2001:4860::1' ]] || fail 'explicit IPv6 override was lost'
unset XUI_SSL_IPV6
ip() {
[[ "$*" != '-6 route get '* ]] || return 0
printf '2: eth0 inet6 2001:4860::1/64 scope global valid_lft forever preferred_lft forever\n'
}
NONINTERACTIVE=0
prompt_and_setup_ssl 2053 / 198.51.100.8 <<< $'\ny\ny' > "$TEMP/accepted" 2>&1
[[ "$captured" == '2001:4860::1' ]] || fail 'interactive installer did not include confirmed detected address'
prompt_and_setup_ssl 2053 / 198.51.100.8 <<< $'\ny\nn\n' > "$TEMP/rejected" 2>&1
[[ -z "$captured" ]] || fail 'interactive installer included a rejected address'
printf 'PASS: installer consent, explicit configuration, no unattended prompts, and validation before service stop\n'
setup_ip_certificate() { return 1; }
systemctl() { :; }
rc-service() {
case "$*" in
'x-ui stop') : ;;
'crond status') [[ "${CROND_RUNNING:-0}" == 1 ]] ;;
'dcron start') printf 'rc-service %s\n' "$*" >> "$TEMP/cron" ;;
*) return 1 ;;
esac
}
NONINTERACTIVE=1
export XUI_SSL_IPV6='2001:4860::1'
for release in ubuntu alpine; do
SSL_HOST=''
prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/failed" 2>&1 || fail "a failed IP certificate aborted the $release installer: the panel must still be installed over HTTP"
grep -q 'IP certificate setup failed' "$TEMP/failed" || fail 'failed issuance was not reported'
[[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after a failed certificate'
done
printf 'PASS: a failed IP certificate is reported and the installer carries on on systemd/OpenRC\n'
rc-update() { printf 'rc-update %s\n' "$*" >> "$TEMP/cron"; }
ip_ssl_enable_openrc_cron ubuntu
CROND_RUNNING=1 ip_ssl_enable_openrc_cron alpine
[[ ! -e "$TEMP/cron" ]] || fail 'an existing cron service was disturbed'
ip_ssl_enable_openrc_cron alpine
[[ "$(cat "$TEMP/cron")" == $'rc-update add dcron default\nrc-service dcron start' ]] || fail 'Alpine cron was not enabled and started'
printf 'PASS: IP setup keeps an existing daemon and enables Alpine cron when needed\n'