# #-- remote-threaded.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="../.."

# exit value is 1 on usage
$PRE/unbound-control -h
if test $? -ne 1; then
	echo "wrong exit value for usage."
	exit 1
else
	echo "exit value for usage: OK"
fi

# use lock-verify if possible

# test if the server is up.
echo "> dig www.example.com."
dig @127.0.0.1 -p $UNBOUND_PORT www.example.com. | tee outfile
echo "> check answer"
if grep "10.20.30.40" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# exit value is 1 when a bad command is given.
echo "$PRE/unbound-control -c ub.conf blablargh"
$PRE/unbound-control -c ub.conf blablargh
if test $? -ne 1; then
	echo "wrong exit value on error."
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	exit 1
else
	echo "correct exit value on error"
fi

# reload the server. test if the server came up by putting a new
# local-data element in the server.
echo "server: local-data: 'afterreload. IN A 5.6.7.8'" >> ub.conf
echo "$PRE/unbound-control -c ub.conf reload"
$PRE/unbound-control -c ub.conf reload
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

echo "> dig afterreload."
dig @127.0.0.1 -p $UNBOUND_PORT afterreload. | tee outfile
echo "> check answer"
if grep "5.6.7.8" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# must have had queries now.  1 since reload.
echo "$PRE/unbound-control -c ub.conf stats"
$PRE/unbound-control -c ub.conf stats > tmp.$$
if test $? -ne 0; then
	echo "wrong exit value after success"
	cat fwd.log
	cat unbound.log
	exit 1
fi
if grep "^total.num.queries=[1-9][0-9]*$" tmp.$$; then
	echo "OK"
else
	echo "bad stats"
	cat tmp.$$
	exit 1
fi

# verbosity
echo "$PRE/unbound-control -c ub.conf verbosity 4"
$PRE/unbound-control -c ub.conf verbosity 4
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

# check syntax error in parse
echo "$PRE/unbound-control -c ub.conf verbosity jkdf"
$PRE/unbound-control -c ub.conf verbosity jkdf
if test $? -ne 1; then
	echo "wrong exit value after failure"
	exit 1
fi

# check bad credentials
cp ub.conf bad.conf
echo "remote-control:" >> bad.conf
echo "	server-key-file: bad_server.key" >> bad.conf
echo "	server-cert-file: bad_server.pem" >> bad.conf
echo "	control-key-file: bad_control.key" >> bad.conf
echo "	control-cert-file: bad_control.pem" >> bad.conf
echo "$PRE/unbound-control -c bad.conf verbosity 2"
$PRE/unbound-control -c bad.conf verbosity 2
if test $? -ne 1; then
	echo "wrong exit value after failure"
	exit 1
fi

# create a new local zone
echo "> test of local zone"
echo "$PRE/unbound-control -c ub.conf local_zone example.net static"
$PRE/unbound-control -c ub.conf local_zone example.net static
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
echo "$PRE/unbound-control -c ub.conf local_data www.example.net A 192.0.2.1"
$PRE/unbound-control -c ub.conf local_data www.example.net A 192.0.2.1
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

# check that www.example.net exists
echo "> dig www.example.net."
dig @127.0.0.1 -p $UNBOUND_PORT www.example.net. | tee outfile
echo "> check answer"
if grep "192.0.2.1" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# check that mail.example.net has nxdomain
echo "> dig mail.example.net."
dig @127.0.0.1 -p $UNBOUND_PORT mail.example.net. | tee outfile
echo "> check answer"
if grep "NXDOMAIN" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# remove www.example.net - check it gets nxdomain
echo "$PRE/unbound-control -c ub.conf local_data_remove www.example.net"
$PRE/unbound-control -c ub.conf local_data_remove www.example.net
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
echo "> dig www.example.net."
dig @127.0.0.1 -p $UNBOUND_PORT www.example.net. | tee outfile
echo "> check answer"
if grep "NXDOMAIN" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# remove example.net - check its gone.
echo "$PRE/unbound-control -c ub.conf local_zone_remove example.net"
$PRE/unbound-control -c ub.conf local_zone_remove example.net
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
echo "> dig www.example.net."
dig @127.0.0.1 -p $UNBOUND_PORT www.example.net. | tee outfile
echo "> check answer"
if grep "SERVFAIL" outfile; then
	echo "OK"
else
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "Not OK"
	exit 1
fi

# dump the cache
echo "> test cache dump"
# fillup cache
echo "dig www.example.com"
dig @127.0.0.1 -p $UNBOUND_PORT www.example.com.
echo "$PRE/unbound-control -c ub.conf dump_cache"
$PRE/unbound-control -c ub.conf dump_cache > tmp.$$
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
cat tmp.$$
# we do not look at content. Only thread 0 content.
# because it may not be there when it is compiled with processes only.
if grep MSG_CACHE tmp.$$; then
	echo "OK this is a cache dump"
else
	echo "Not OK cache dump"
	exit 1
fi

# test lookup
echo "$PRE/unbound-control -c ub.conf lookup www.example.com"
$PRE/unbound-control -c ub.conf lookup www.example.com
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
# answer to lookup is meaningless because of use a forwarder, oh well.

# load the cache dump.
echo "$PRE/unbound-control -c ub.conf load_cache < tmp.$$"
$PRE/unbound-control -c ub.conf load_cache < tmp.$$
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
# do not check if cache dump contents are present ; other threads
# may not have gotten it when it is compiled with processes only.

# flushing
echo "$PRE/unbound-control -c ub.conf flush www.example.net"
$PRE/unbound-control -c ub.conf flush www.example.net
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

echo "$PRE/unbound-control -c ub.conf flush_type www.example.net TXT"
$PRE/unbound-control -c ub.conf flush_type www.example.net TXT
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

echo "$PRE/unbound-control -c ub.conf flush_zone example.net"
$PRE/unbound-control -c ub.conf flush_zone example.net
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi

# now stop the server
echo "$PRE/unbound-control -c ub.conf stop"
$PRE/unbound-control -c ub.conf stop
if test $? -ne 0; then
	echo "wrong exit value after success"
	exit 1
fi
# see if the server has really exited.
TRY_MAX=20
for (( try=0 ; try <= $TRY_MAX ; try++ )) ; do
	if kill -0 $UNBOUND_PID 2>&1 | tee tmp.$$; then
		echo "not stopped yet, waiting"
		sleep 1
	else
		echo "stopped OK; break"
		break;
	fi
	if grep "No such process" tmp.$$; then
		echo "stopped OK; break"
		break;
	fi
done
if kill -0 $UNBOUND_PID; then
	echo "still up!"
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	echo "not stopped, failure"
	exit 1
else
	echo "stopped OK"

        if test -f ublocktrace.0; then
		if $PRE/lock-verify ublocktrace.*; then
			echo "lock-verify test worked."
		else
			echo "lock-verify test failed."
			cat fwd.log
			cat unbound.log
			exit 1
		fi
	fi
fi

echo "> cat logfiles"
cat fwd.log
cat unbound.log
echo "> OK"
exit 0
