# #-- 275-server-capabilities.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


make && "${BUILDDIR}/build-stub-only/libtool" exec valgrind -v --log-file=valgrind.log --leak-check=full --error-exitcode=1 --track-origins=yes "./${TPKG_NAME}" | (
	read PORT
	read PORT2

	${GETDNS_STUB_QUERY} -s -t 1000 @127.0.0.1:$PORT TXT cancel. +return_call_reporting 2>&1 > time_out

	${GETDNS_STUB_QUERY} -s @127.0.0.1:$PORT TXT test +return_call_reporting 2>&1 > tcp_out

	${GETDNS_STUB_QUERY} -s -U @127.0.0.1:$PORT2 TXT test +return_call_reporting 2>&1 > udp_out

	${GETDNS_STUB_QUERY} -s -q @127.0.0.1:$PORT TXT quit.
)
if grep -q 'definitely lost: [^0]' valgrind.log
then
	cat valgrind.log
	echo "error: Memory loss!"
	exit 1
fi
if ! grep -q '"status": GETDNS_RESPSTATUS_ALL_TIMEOUT' time_out
then
	cat time_out
	echo 'error: Query was answered (i think)!'
	exit 1

elif ! grep -q '"transport": GETDNS_TRANSPORT_TCP' tcp_out
then
	cat tcp_out
	echo 'error: Query was not over TCP!'
	exit 1

elif ! grep -q '"transport": GETDNS_TRANSPORT_UDP' udp_out
then
	cat udp_out
	echo 'error: Query was not over UDP!'
	exit 1

elif ! grep -q '"Some answer"' tcp_out
then
	cat tcp_out
	echo 'error: Query was not answered!'
	exit 1
else
	exit 0
fi
