#!/bin/sh

set -e

setup() (
	systemctl stop bind9
	systemctl stop named

	cat <<EOF >/etc/bind/named.conf.options
options {
	directory "/var/cache/bind";
	listen-on port 53 { 127.0.0.1; };
	allow-query { any; };
	recursion yes;
	minimal-responses no;
};
EOF

	cat <<EOF >/etc/bind/named.conf.local
zone "localdomain.test" { type master; file "/etc/bind/zones/forward.localdomain.test"; };
zone "rt.example" { type primary; file "/etc/bind/zones/rt.example"; };
EOF

	mkdir -p /etc/bind/zones/

	cat <<EOF >/etc/bind/zones/forward.localdomain.test
\$TTL	604800
@	IN	SOA	localdomain.test. root.localdomain.test. (
			      2		; Serial
			 604800		; Refresh
			  86400		; Retry
			2419200		; Expire
			 604800 )	; Negative Cache TTL
;
@	IN	NS	ns.localdomain.test.
ns	IN	A	127.0.0.1
EOF

	# taken from upstream bin/tests/system/additional/ns1/rt.db
	cat <<'EOF' >/etc/bind/zones/rt.example
$TTL 86400
@ IN SOA ns1 hostmaster ( 2 8H 2H 4W 1D );
    NS ns1
    NS ns1.rt2.example.
ns1 A 10.53.0.1

rt RT 2 www
www AAAA 192::99
www A 192.168.2.99
www X25 100099
EOF

	systemctl start bind9
	systemctl start named
)

run_base() (
	named-checkconf
	named-checkzone localdomain.test /etc/bind/zones/forward.localdomain.test
	dig @localhost localdomain.test | grep 'NOERROR'
)

# Adapted from upstream additional/tests.sh (testing with 'minimal-any no;'). This test is
# meant to verify that d/p/0033-Limit-the-additional-processing-for-large-RDATA-sets.patch
# works as expected, as the patch modifies the upstream test, but the test is not exercised
# neither at package build time nor as an autopkgtest.
run_minimal_any_no() (
	dig -t ANY www.rt.example @localhost | grep -q 'ANSWER: 3, AUTHORITY: 2, ADDITIONAL: 1'
)

teardown() (
	systemctl stop bind9
	systemctl stop named
)

setup
run_base
run_minimal_any_no
teardown
