#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"

setupenvironment
configarchitecture 'i386'

changetohttpswebserver

echo 'foo' > aptarchive/foo
echo 'bar' > aptarchive/foo2

test_apt_helper_download() {
    msgmsg 'Test with' "$1"

    msgtest 'apt-file download-file' 'md5sum'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo2' 'MD5Sum:d3b07384d113edec49eaa6238ad5ff00'
    testfileequal ./downloaded/foo2 'foo'

    msgtest 'apt-file download-file' 'sha1'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo1' 'SHA1:f1d2d2f924e986ac86fdf7b36c94bcdf32beec15'
    testfileequal ./downloaded/foo1 'foo'

    msgtest 'apt-file download-file' 'sha256'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo3' 'SHA256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'
    testfileequal ./downloaded/foo3 'foo'

    msgtest 'apt-file download-file' 'no-hash'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo4'
    testfileequal ./downloaded/foo4 'foo'

    msgtest 'apt-file download-file' 'wrong md5sum'
    testfailure --nomsg apthelper -qq download-file "${1}/foo" './downloaded/foo5' 'MD5Sum:aabbcc'
    testfileequal rootdir/tmp/testfailure.output "E: Failed to fetch ${1}/foo  Hash Sum mismatch
   Hashes of expected file:
    - MD5Sum:aabbcc [weak]
   Hashes of received file:
    - SHA512:0cf9180a764aba863a67b6d72f0918bc131c6772642cb2dce5a34f0a702f9470ddc2bf125c12198b1995c233c34b4afd346c54a2334c350a948a51b6e8b4e6b6
    - SHA256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
    - SHA1:f1d2d2f924e986ac86fdf7b36c94bcdf32beec15 [weak]
    - MD5Sum:d3b07384d113edec49eaa6238ad5ff00 [weak]
    - Checksum-FileSize:4 [weak]
   Last modification reported: $(lastmodification 'aptarchive/foo')
E: Download Failed"
    testfileequal ./downloaded/foo5.FAILED 'foo'

    msgtest 'apt-file download-file' 'wrong sha256'
    testfailure --nomsg apthelper -qq download-file "${1}/foo" './downloaded/foo6' 'SHA256:aabbcc'
    testfileequal rootdir/tmp/testfailure.output "E: Failed to fetch ${1}/foo  Hash Sum mismatch
   Hashes of expected file:
    - SHA256:aabbcc
   Hashes of received file:
    - SHA256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
    - Checksum-FileSize:4 [weak]
   Last modification reported: $(lastmodification 'aptarchive/foo')
E: Download Failed"
    testfileequal './downloaded/foo6.FAILED' 'foo'

    msgtest 'apt-file download-file' 'sha256 sha1'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo8' 'SHA256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c' \
	    "${1}/foo2" './downloaded/foo7' 'SHA1:e242ed3bffccdf271b7fbaf34ed72d089537b42f'
    testfileequal './downloaded/foo8' 'foo'
    testfileequal './downloaded/foo7' 'bar'

    msgtest 'apt-file download-file' 'md5sum sha1'
    testsuccess --nomsg apthelper download-file "${1}/foo" './downloaded/foo9' 'MD5Sum:d3b07384d113edec49eaa6238ad5ff00' \
	    "${1}/foo2" './downloaded/foo10' 'SHA1:e242ed3bffccdf271b7fbaf34ed72d089537b42f'
    testfileequal './downloaded/foo9' 'foo'
    testfileequal './downloaded/foo10' 'bar'
}

setupproxydetect() {
	local METH="$1"
	shift
	{
		echo '#!/bin/sh -e'
		echo "$@"
	} > "${TMPWORKINGDIRECTORY}/apt-proxy-detect"
	chmod 755 "${TMPWORKINGDIRECTORY}/apt-proxy-detect"
	echo "Acquire::${METH}::Proxy-Auto-Detect \"${TMPWORKINGDIRECTORY}/apt-proxy-detect\";" > rootdir/etc/apt/apt.conf.d/02proxy-detect
}

test_apt_helper_detect_proxy() {
    msgmsg 'apt-helper auto-detect-proxy' 'no proxy'
    testsuccessequal "Using proxy '' for URL 'http://example.com/'" apthelper auto-detect-proxy http://example.com/
    setupproxydetect 'http' 'exit 0'
    testsuccessequal "Using proxy '' for URL 'http://example.com/'" apthelper auto-detect-proxy http://example.com/
    setupproxydetect 'http' 'exit 1'
    testfailureequal 'E: Sub-process ProxyAutoDetect returned an error code (1)' apthelper auto-detect-proxy http://example.com/
    setupproxydetect 'http' 'echo'
    testwarningequal "Using proxy '' for URL 'http://example.com/'
W: ProxyAutoDetect command returned an empty line" apthelper auto-detect-proxy http://example.com/
    setupproxydetect 'http' 'echo DIRECT'
    testsuccessequal "Using proxy 'DIRECT' for URL 'http://example.com/'" apthelper auto-detect-proxy http://example.com/

    msgmsg 'apt-helper auto-detect-proxy' 'http proxy'
    setupproxydetect 'http' 'echo "http://some-proxy"'
    testsuccessequal "Using proxy 'http://some-proxy' for URL 'http://www.example.com/'" apthelper auto-detect-proxy http://www.example.com

    msgmsg 'apt-helper auto-detect-proxy' 'https proxy'
    setupproxydetect 'https' 'echo "https://https-proxy"'
    testsuccessequal "Using proxy 'https://https-proxy' for URL 'https://ssl.example.com/'" apthelper auto-detect-proxy https://ssl.example.com
}

test_apt_helper_download "http://localhost:${APTHTTPPORT}"
test_apt_helper_download "https://localhost:${APTHTTPSPORT}"
test_apt_helper_detect_proxy
msgmsg 'test various failure modes'
testfailureequal 'E: Invalid operation download' apthelper download
testfailureequal 'E: Must specify at least one pair url/filename' apthelper download-file
testfailureequal 'E: Must specify at least one pair url/filename' apthelper download-file 'http://example.org/'
testfailureequal 'E: Need one URL as argument' apthelper auto-detect-proxy
testfailureequal 'E: Must specify at least one SRV record' apthelper srv-lookup
testfailureequal 'E: GetSrvRec failed for localhost' apthelper -q=1 srv-lookup 'localhost'
testfailureequal "E: GetSrvRec failed for localhost:${APTHTTPPORT}" apthelper -q=1 srv-lookup "localhost:${APTHTTPPORT}"
testfailureequal "E: GetSrvRec failed for localhost:${APTHTTPSPORT}" apthelper -q=1 srv-lookup "localhost:${APTHTTPSPORT}"