#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: |
##  Mirror localhost repository testing errors
##  1. setup local repository with 1 update
##  2. check if upgrade of package work
##  3. check if --updateto is honored
## roles-not: [basesystem]
## tags: [basic, proxy]
## packages:
##  - apache2 | apache2-mpm-prefork
##  - apt-mirror
## bugs: [21127, 22567]
## versions:
##  2.4-2: fixed
## exposure: dangerous

. pool.sh || exit 137
FIXED_12571=false # ucr set proxy/* -> ucr commit /etc/apt/*

compname="test${RANDOM}"
username="user${RANDOM}"
password="pass${RANDOM}"

setup_apache "${repoprefix}"
htpasswd -c -m -b "${REPODIR}/.htpasswd" "${username}" "${password}"

mkpdir "${_version_version}-0" maintained "${ARCH}"
# 0:dists/ucs500/main/binary-amd64/
# 1:dists/errata500/main/binary-amd64/
mkdeb "${pkgname}" 1 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"

mkpdir "${_version_version}--component/${compname}" maintained "${ARCH}"
# 2:5.0/maintained/component/test00000/amd64
mkdeb "${pkgname}c" 1 "${ARCH}" "${DIR}"
mkpkg "${DIR}" "${DIR}"

cat <<-EOF >"${DIR}/../.htaccess"
AuthType Basic
AuthUserFile ${REPODIR}/.htpasswd
AuthGroupFile /dev/null
AuthName "${compname}"
Require user ${username}
EOF

minor+=1
mkpdir "${major}.${minor}-0" maintained "${ARCH}"
# 3:dists/ucs510/main/binary-amd64/
# 4:dists/errata510/main/binary-amd64/
mkdeb "${pkgname}" 3 "${ARCH}" "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"

mkpdir "${major}.${minor}--component/${compname}" maintained "${ARCH}"
# 5:5.1/maintained/component/test00000/amd64/
mkdeb "${pkgname}c" 2 "${ARCH}" "${DIR}"
mkpkg "${DIR}" "${DIR}"

cat <<-EOF >"${DIR}/../.htaccess"
AuthType Basic
AuthUserFile ${REPODIR}/.htpasswd
AuthGroupFile /dev/null
AuthName "${compname}"
Require user ${username}
EOF

comp="repository/online/component/${compname}"
config_mirror version/patchlevel=0 version/erratalevel=0

config_proxy -a -u "${username}" -w "${password}"
invoke-rc.d univention-firewall stop

cleanup_extra () {
	iptables -D INPUT   -p tcp --dport discard -j DROP >&3 2>&3 || true
	invoke-rc.d univention-firewall start
}

run_tests () { # execute all updater programs
	local msg="$1"
	grep -F "$msg" /etc/apt/mirror.list

	univention-repository-update net 2>&1 | grep -F "$msg"
}

	set -e
	ucr set repository/mirror/server=http://DUMMY/
	run_tests "Configuration error: host is unresolvable"

	ucr set repository/mirror/server=http://localhost:9/
	run_tests "Configuration error: port is closed"

	ucr set repository/mirror/server=http://localhost/DUMMY
	run_tests "Configuration error: non-existing prefix"

	ucr set repository/mirror/server="http://localhost/${repoprefix#/}"

	ucr set "${comp}=yes" "${comp}/parts=maintained" "${comp}/username=DUMMY" "${comp}/password=DUMMY" "${comp}/version=current"
	run_tests "Configuration error: credentials not accepted"
	ucr unset "${comp}" "${comp}/parts" "${comp}/username" "${comp}/password"

	ucr set "${comp}DUMMY=yes" "${comp}DUMMY/parts=maintained" "${comp}DUMMY/version=current"
	grep -F "Component ${compname}DUMMY: Error downloading" /etc/apt/mirror.list
	univention-repository-update net 2>&1
	ucr unset "${comp}DUMMY" "${comp}DUMMY/parts" "${comp}DUMMY/version"

	ucr set proxy/http="http://${username}:${password}@DUMMY:${proxy_port:?}"
	"${FIXED_12571}" || ucr commit /etc/apt/mirror.list >&3
	run_tests "Proxy configuration error: host is unresolvable"

	ucr set proxy/http="http://${username}:${password}@localhost:9"
	"${FIXED_12571}" || ucr commit /etc/apt/mirror.list >&3
	run_tests "Proxy configuration error: port is closed"

	ucr set proxy/http="http://DUMMY:DUMMY@localhost:${proxy_port}"
	"${FIXED_12571}" || ucr commit /etc/apt/mirror.list >&3
	run_tests "Proxy configuration error: credentials not accepted"
	ucr unset proxy/http

	# check timeout last
	iptables -I INPUT 1 -p tcp --dport discard -j DROP >&3 2>&3
	ucr set repository/mirror/port=9
	run_tests "Configuration error: "
	ucr set repository/mirror/port=80

	ucr set proxy/http="http://${username}:${password}@localhost:9"
	"${FIXED_12571}" || ucr commit /etc/apt/mirror.list >&3
	run_tests "onfiguration error: "
	ucr unset proxy/http
# vim:set ft=sh:
