# #-- fast_reload_fwd.test --#
# source the master var file when it's there
[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
# use .tpkg.var.test for in test variable passing
[ -f .tpkg.var.test ] && source .tpkg.var.test

PRE="../.."
. ../common.sh

echo "> unbound-control status"
$PRE/unbound-control -c ub.conf status
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi

# test that the forwards and stubs point to the right upstream.
for x in example1.org example2.org example3.org stub1.org stub2.org stub3.org; do
	echo ""
	echo "dig www.$x [upstream is NS1]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.1" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

for x in example4.org example5.org example6.org stub4.org stub5.org stub6.org; do
	echo ""
	echo "dig www.$x [upstream is NS2]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.2" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

for x in auth1.org auth2.org auth3.org auth5.org auth6.org auth7.org; do
	echo ""
	echo "dig www.$x [auth is 1.2.3.4]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.4" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

echo ""
echo "> list_insecure"
$PRE/unbound-control -c ub.conf list_insecure 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
fi
if grep "insec1.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "insec2.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "insec3.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
echo ""
echo "> trustanchor.unbound"
dig @127.0.0.1 -p $UNBOUND_PORT trustanchor.unbound CH TXT 2>&1 | tee outfile
if grep "ta1.example.com. 55566" outfile >/dev/null; then :; else
	echo "wrong output ta1"
	exit 1
fi
if grep "ta2.example.com. 55566" outfile >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "ta3.example.com. 55566" outfile >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi

echo ""
echo "> replace config file ub.conf"
mv ub.conf ub.conf.orig
mv ub.conf2 ub.conf
echo ""
echo "> unbound-control fast_reload"
$PRE/unbound-control -c ub.conf fast_reload +vv 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi

# for the previous digs to www.x the cached value should remain the same
# but for new lookups, to www2.x the new upstream should be used.
for x in example1.org example2.org example3.org stub1.org stub2.org stub3.org; do
	echo ""
	echo "dig www.$x [upstream is NS1]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.1" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

for x in example4.org example5.org example6.org stub4.org stub5.org stub6.org; do
	echo ""
	echo "dig www.$x [upstream is NS2]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.2" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

# new lookups for www2 go to the upstream.
for x in example2.org example4.org example6.org stub2.org stub4.org stub6.org; do
	echo ""
	echo "dig www2.$x [upstream is NS1]"
	dig @127.0.0.1 -p $UNBOUND_PORT www2.$x A 2>&1 | tee outfile
	if grep "1.2.3.1" outfile; then
		echo "response OK"
	else
		echo "www2.$x got the wrong answer"
		exit 1
	fi
done

for x in example1.org example3.org example5.org stub1.org stub3.org stub5.org; do
	echo ""
	echo "dig www2.$x [upstream is NS2]"
	dig @127.0.0.1 -p $UNBOUND_PORT www2.$x A 2>&1 | tee outfile
	if grep "1.2.3.2" outfile; then
		echo "response OK"
	else
		echo "www2.$x got the wrong answer"
		exit 1
	fi
done

# auth is unchanged, or at ns1.
for x in auth1.org auth5.org auth8.org; do
	echo ""
	echo "dig www.$x [auth is 1.2.3.4]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.4" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

# deleted auth
for x in auth2.org auth6.org; do
	echo ""
	echo "dig www.$x [auth is deleted]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "SERVFAIL" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

# changed and added auth
for x in auth3.org auth4.org auth7.org; do
	echo ""
	echo "dig www.$x [auth is 1.2.3.5]"
	dig @127.0.0.1 -p $UNBOUND_PORT www.$x A 2>&1 | tee outfile
	if grep "1.2.3.5" outfile; then
		echo "response OK"
	else
		echo "www.$x got the wrong answer"
		exit 1
	fi
done

echo ""
echo "> list_insecure"
$PRE/unbound-control -c ub.conf list_insecure 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
fi
if grep "insec1.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "insec2.ta1.example.com" output >/dev/null; then
	echo "wrong output"
	exit 1
fi
if grep "insec3.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "insec4.ta1.example.com" output >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
echo ""
echo "> trustanchor.unbound"
dig @127.0.0.1 -p $UNBOUND_PORT trustanchor.unbound CH TXT 2>&1 | tee outfile
if grep "ta1.example.com. 55566" outfile >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "ta2.example.com. 55566" outfile >/dev/null; then
	echo "wrong output"
	exit 1
fi
if grep "ta3.example.com. 55566" outfile >/dev/null; then
	echo "wrong output"
	exit 1
fi
if grep "ta3.example.com. 55567" outfile >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi
if grep "ta4.example.com. 55566" outfile >/dev/null; then :; else
	echo "wrong output"
	exit 1
fi

echo ""
echo "> test change: add tag1 tag2"
cp ub.conf ub.conf.orig2
echo "server:" >> ub.conf
echo '	define-tag: "tag1 tag2"' >> ub.conf
echo "> unbound-control fast_reload"
$PRE/unbound-control -c ub.conf fast_reload +vv 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi

echo ""
echo "> test change: change to tag2 tag3"
cp ub.conf.orig2 ub.conf
echo "server:" >> ub.conf
echo '	define-tag: "tag2 tag3"' >> ub.conf
echo "> unbound-control fast_reload"
$PRE/unbound-control -c ub.conf fast_reload +vv 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi
if grep "tags have changed" output; then
	echo "output OK"
else
	echo "wrong output"
	exit 1
fi

echo ""
echo "> test change: change cache size"
cp ub.conf.orig2 ub.conf
echo "server:" >> ub.conf
echo "	msg-cache-size: 10m" >> ub.conf
echo "	rrset-cache-size: 5m" >> ub.conf
echo "> unbound-control fast_reload"
$PRE/unbound-control -c ub.conf fast_reload +vv 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi

echo ""
echo "> test change: change nothing, +p too"
$PRE/unbound-control -c ub.conf fast_reload +vv +p 2>&1 | tee output
if test $? -ne 0; then
	echo "wrong exit value."
	exit 1
else
	echo "exit value: OK"
fi

echo ""
echo "> stop unbound"
kill_pid $UNBOUND_PID
if test -f unbound.pid; then sleep 1; fi
if test -f unbound.pid; then sleep 1; fi
if test -f unbound.pid; then sleep 1; fi
if test -f unbound.pid; then echo "unbound.pid still there"; fi
# check the locks.
function locktest() {
	if test -x $PRE/lock-verify -a -f ublocktrace.0; then
		$PRE/lock-verify ublocktrace.*
		if test $? -ne 0; then
			echo "lock-verify error"
			exit 1
		fi
	fi
}
locktest

exit 0
