#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: |
##  Check updater script signature
##  1. setup local repository for 2 patchlevels with dummy package
##  2. check that scripts were executed
## roles-not: [basesystem]
## tags: [basic]
## packages:
##  - apache2 | apache2-mpm-prefork
## versions:
##  1.0-0: skip
##  3.2-0: fixed
## exposure: dangerous

. pool.sh || exit 137
# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137

SH_LOG="${BASEDIR}/sh.log"

setup_apache "${repoprefix}"

mkpdir "${_version_version}-0" maintained "${ARCH}"
mkdeb "${pkgname}" 1 all "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"
mksh "${DIR}" preup postup

mkpdir "${_version_version}-1" maintained "${ARCH}"
mkdeb "${pkgname}" 2 all "${DIR_POOL}"
mkpkg "${DIR}" "${DIR_POOL}"
mksh "${DIR}" preup postup

cat >"${SH_LOG}" <<__SH__
${DIR}/preup.sh [0-9]\\+ ${major}.${minor}-1
${DIR}/postup.sh [0-9]\\+ ${major}.${minor}-1
__SH__

config_repo version/patchlevel=0 version/erratalevel=0 repository/online/verify=yes

	set -e
	checkapt "http://localhost\\(:80\\)\\?/${repoprefix}/" "${DIRS[0]}"
	apt-get -qq update
	apt-get -qq install "${pkgname}"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	1"

	info "missing signature for postup"
	mv "${DIR}/postup.sh.gpg" "${DIR}/postup.sh.GPG"
	/usr/share/univention-updater/univention-updater net && fail "Should not update"
	grep -xf "${SH_LOG}" "${BASEDIR}/install.log" && fail "should not exec"
	mv "${DIR}/postup.sh.GPG" "${DIR}/postup.sh.gpg"

	info "missing signature for pretup"
	mv "${DIR}/preup.sh.gpg" "${DIR}/preup.sh.GPG"
	/usr/share/univention-updater/univention-updater net && fail "should not update"
	grep -xf "${SH_LOG}" "${BASEDIR}/install.log" && fail "should not exec"
	mv "${DIR}/preup.sh.GPG" "${DIR}/preup.sh.gpg"

	info "wrong signature for postup"
	mv "${DIR}/postup.sh" "${DIR}/postup.SH"
	echo '#!/bin/sh' >"${DIR}/postup.sh"
	/usr/share/univention-updater/univention-updater net && fail "should not update"
	grep -xf "${SH_LOG}" "${BASEDIR}/install.log" && fail "should not exec"
	mv "${DIR}/postup.SH" "${DIR}/postup.sh"

	info "wrong signature for preup"
	mv "${DIR}/preup.sh" "${DIR}/preup.SH"
	echo '#!/bin/sh' >"${DIR}/preup.sh"
	/usr/share/univention-updater/univention-updater net && fail "should not update"
	grep -xf "${SH_LOG}" "${BASEDIR}/install.log" && fail "should not exec"
	mv "${DIR}/preup.SH" "${DIR}/preup.sh"

	info "rightly prpared"
	/usr/share/univention-updater/univention-updater net
	grep -qxf "${SH_LOG}" "${BASEDIR}/install.log"
	checkapt "http://localhost\\(:80\\)\\?/${repoprefix}/" "${DIRS[@]}"
	dpkg-query -W "${pkgname}" | grep -Fqx "${pkgname}	2"
	test "${_version_version}" = "$(ucr get version/version)"
	test 1 = "$(ucr get version/patchlevel)"
	checkdeb "${pkgname}" 1 2
# vim:set ft=sh:
