#!/bin/sh

# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
# Copyright (c) International Business Machines  Corp., 2005
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# Author: Mitsuru Chinen <mitch@jp.ibm.com>

TCID=dns-stress
TST_TOTAL=2
TST_CLEANUP="cleanup"

. test_net.sh

# Minimum host ID in the zone file.
# The ID is used as the host portion of the address
MIN_ID=3
# Maximum host ID in the zone file.
MAX_ID=254
# Domain name for testing
DOMAIN="ltp-ns.org"

cleanup()
{
	# Stop the dns daemon
	test -s named.pid && kill -9 $(cat named.pid) > /dev/null
	tst_rmdir
}

common_setup()
{
	tst_require_root
	tst_check_cmds named dig

	ipver=${TST_IPV6:-'4'}

	if [ "$TST_IPV6" ]; then
		record="AAAA"
		net="$IPV6_NETWORK"
		net_rev="$IPV6_NET_REV"
	else
		record="A"
		net="$IPV4_NETWORK"
		net_rev="$IPV4_NET_REV"
	fi

	trap "tst_brkm TBROK 'test interrupted'" INT

	check_icmpv${ipver}_connectivity $(tst_iface) $(tst_ipaddr rhost) || \
		tst_brkm TBROK "Failed to ping to $(tst_ipaddr rhost)"

	tst_tmpdir

	ip6_opt=
	[ "$TST_IPV6" ] && ip6_opt="listen-on-v6 { any; };"

	ip_zone_opt="in-addr"
	[ "$TST_IPV6" ] && ip_zone_opt="ip6"

	cat << EOD > named.conf
	options {
		directory "$(pwd)";
		pid-file "$(pwd)/named.pid";
		recursion no;
		$ip6_opt
	};

	zone "$DOMAIN" {
		type master;
		file "ltp-ns.zone";
	};

	zone "$net_rev.$ip_zone_opt.arpa" {
		type master;
		file "ltp-ns.rev";
	};
EOD

	# zone file
	cat << EOD > ltp-ns.zone
\$TTL 10
@	IN	SOA dns.$DOMAIN. root.$DOMAIN. (
	2005092701 ; serial
	3600       ; dummy value
	900        ; dummy value
	604800     ; dummy value
	86400      ; dummy value
)
	IN	NS	dns.$DOMAIN.
dns	IN	$record	$(tst_ipaddr)
client	IN	$record	$(tst_ipaddr rhost)
EOD
}

setup_4()
{
	id=$MIN_ID
	while [ $id -le $MAX_ID ]; do
		printf "node$id\tIN\tA\t$IPV4_NETWORK.$id\n" >> ltp-ns.zone
		id=$(($id + 1))
	done

	# reverse zone file
	cat << EOD > ltp-ns.rev
\$TTL 10
@	IN	SOA dns.$DOMAIN. root.$DOMAIN. (
	2005092701 ; serial
	3600       ; dummy value
	900        ; dummy value
	604800     ; dummy value
	86400      ; dummy value
)
        IN      NS      dns.$DOMAIN.
$LHOST_IPV4_HOST	IN	PTR	dns.$DOMAIN.
$RHOST_IPV4_HOST	IN	PTR	client.$DOMAIN.
EOD

	id=$MIN_ID
	while [ $id -le $MAX_ID ]; do
		printf "$id\tIN\tPTR\tnode$id.$DOMAIN.\n" >> ltp-ns.rev
		id=$(($id + 1))
	done
}

setup_6()
{
	id=$MIN_ID
	while [ $id -le $MAX_ID ]; do
		printf "node$id\tIN\tAAAA\t$IPV6_NETWORK::%x\n" $id >> ltp-ns.zone
		id=$(($id + 1))
	done

	# reverse zone file
	cat << EOD > ltp-ns.rev
\$TTL 10
@	IN	SOA dns.$DOMAIN. root.$DOMAIN. (
	2005092701 ; serial
	3600       ; dummy value
	900        ; dummy value
	604800     ; dummy value
	86400      ; dummy value
)
        IN      NS      dns.$DOMAIN.
$LHOST_IPV6_REV	IN	PTR	dns.$DOMAIN.
$RHOST_IPV6_REV	IN	PTR	client.$DOMAIN.
EOD

	id=$MIN_ID
	while [ $id -le $MAX_ID ]; do
		local rev_ip="0.0.0.0.0.0.0.0.0.0.0.0.0.0"
		printf "%x.%x.$rev_ip\tIN\tPTR\tnode$id.$DOMAIN.\n" \
			$(($id % 16)) $(($id / 16)) >> ltp-ns.rev
		id=$(($id + 1))
	done
}

start_named()
{
	chmod 770 $TST_TMPDIR
	chmod 660 $TST_TMPDIR/*

	port=$(tst_get_unused_port ipv${ipver} stream)

	# Start named daemon
	named -$ipver -c named.conf -p $port || \
		tst_brkm TBROK "Failed to run named daemon"

	# Make sure named.pid is created.
	while true ; do
		test -s named.pid && break
		sleep 1
	done
}

test01()
{
	tst_resm TINFO "Handling name lookup queries '$NS_TIMES' times"

	tst_rhost_run -s -c "dns-stress01-rmt $ipver $(tst_ipaddr) $port \
		$DOMAIN $MIN_ID $MAX_ID $NS_TIMES"

	tst_resm TPASS "Test is finished successfully"
}

test02()
{
	tst_resm TINFO "Handling reverse lookup queries '$NS_TIMES' times"

	tst_rhost_run -s -c "dns-stress02-rmt $ipver $(tst_ipaddr) $port $net \
		$MIN_ID $MAX_ID $NS_TIMES"

	tst_resm TPASS "Test is finished successfully"
}

common_setup

setup_$ipver

start_named

test01
test02

tst_exit
