#!/bin/bash
#
# Project    : ipv6calc/ipv6calcweb
# File       : test_ipv6calcweb.sh
# Version    : $Id: test_ipv6calcweb.sh,v 1.13 2014/02/01 14:56:18 ds6peter Exp $
# Copyright  : 2012-2014 by Peter Bieringer <pb (at) bieringer.de>
#
# Information:
#  Test script for ipv6calcweb
#

while getopts "f:d" opt; do
	case $opt in
	    f)
		opt_format="$OPTARG"
		;;
	    d)
		opt_debug=1
		;;
	esac
done

shift $[ $OPTIND - 1 ]

if [ -z "$1" ]; then
	echo "Need IPv4/v6 address as argument #1"
	exit 1
fi

if [ ! -f ipv6calcweb.cgi ]; then
	make || exit 1
fi
if [ ! -x ipv6calcweb.cgi ]; then
	chmod u+x ipv6calcweb.cgi
fi

## very basic output format tests
for format in textkeyvalue text html htmlfull; do
	if [ -n "$opt_format" -a "$opt_format" != "$format" ]; then
		echo "NOTICE: skip format: $format"
		continue
	fi
	echo "INFO  : test format: $format"
	echo "DEBUG : execute: HTTP_IPV6CALCWEB_DEBUG=0x1000 HTTP_IPV6CALCWEB_OUTPUT_FORMAT=\"$format\" ./ipv6calcweb.cgi"
	if [ "$opt_debug" = "1" ]; then
		HTTP_IPV6CALCWEB_DEBUG="0x1000" HTTP_IPV6CALCWEB_OUTPUT_FORMAT="$format" ./ipv6calcweb.cgi
	else
		HTTP_IPV6CALCWEB_DEBUG="0x1000" HTTP_IPV6CALCWEB_OUTPUT_FORMAT="$format" ./ipv6calcweb.cgi >/dev/null
	fi
	if [ $? -ne 0 ];then
		echo "ERROR : output format reports an error: $format"
		if [ "$opt_debug" != "1" ]; then
			HTTP_IPV6CALCWEB_OUTPUT_FORMAT="$format" ./ipv6calcweb.cgi
		fi
		exit 1
	fi
done || exit 1


## more sophisticated checks
REMOTE_ADDR="$1"
REMOTE_HOST="client.domain.example"
HTTP_USER_AGENT="test_ipv6calcweb"

SERVER_ADDR="192.0.2.1"
SERVER_NAME="server.domain.example"

QUERY_STRING="$2"

HTTP_IPV6CALCWEB_DEBUG="0x1000"
HTTP_IPV6CALCWEB_INFO_SERVER="1"

HTTP_X_FORWARDED_FOR="5.6.7.8, 9.10.11.12"
HTTP_VIA="1.0 fred, 1.1 nowhere.com"

export REMOTE_ADDR REMOTE_HOST HTTP_USER_AGENT SERVER_ADDR SERVER_NAME QUERY_STRING HTTP_IPV6CALCWEB_DEBUG HTTP_IPV6CALCWEB_INFO_SERVER HTTP_X_FORWARDED_FOR HTTP_VIA

for format in textkeyvalue text html htmlfull; do
	if [ -n "$opt_format" -a "$opt_format" != "$format" ]; then
		echo "NOTICE: skip format: $format"
		continue
	fi

	HTTP_IPV6CALCWEB_OUTPUT_FORMAT="$format"
	export HTTP_IPV6CALCWEB_OUTPUT_FORMAT

	OUTPUT="`./ipv6calcweb.cgi`"
	if [ "$opt_debug" = "1" ]; then
		echo "$OUTPUT"
	fi

	result=$?
	if [ $result -ne 0 ];then
		echo "ERROR : output format reports an error: $format"
		if [ "$opt_debug" != "1" ]; then
			echo "$OUTPUT"
		fi
		exit 1
	fi
done || exit 1

if [ $result -ne 0 ]; then
	echo "TEST FAILED (exit code != 0)"
	HTTP_IPV6CALCWEB_DEBUG="0xffff"
	export HTTP_IPV6CALCWEB_DEBUG
	./ipv6calcweb.cgi
	exit 1
else
	# check output
	if echo "$OUTPUT" | egrep -q "(ERROR|problem)"; then
		echo "TEST FAILED (ERROR|problem) occurs"
		HTTP_IPV6CALCWEB_DEBUG="0xffff"
		export HTTP_IPV6CALCWEB_DEBUG
		./ipv6calcweb.cgi
		exit 1
	fi
	if echo "$OUTPUT" | egrep -q "(reserved)"; then
		echo "$OUTPUT"
	fi
fi
