#!/bin/bash
#Copyright 1999, William Stearns <wstearns@pobox.com>
#FIXME - test all vars after running checksys for default values.
#FIXME - feed log entry into mason and compare to all output formats.
#FIXME - test that identical log entries, even separated, produce only one line of output.

error () {
	echo 
	echo Failed test: $*
	if [ -d /usr/src/mason-work ]; then
		echo Failed test: $* on $MASONVER >>/usr/src/mason-work/regression-log
	fi
	echo -n -e "\a" >/dev/stderr
	sleep 1
	echo -n -e "\a" >/dev/stderr
	sleep 1
	echo -n -e "\a" >/dev/stderr
	exit 1
}

echo -n Load library:...
. ./masonlib
echo Done

#Internal check.  If you want to check that the error function actually catches failures, uncomment the following.
#if ! false ; then																error testfail ; fi


echo ---------- General checks ----------
echo -n masonver...
if [ -z "$MASONVER" ]; then														error masonver ; fi

echo done.

echo ---------- Test should return true: ----------
#if !  ; then		error tx ; fi
echo -n ipeq...
if ! ipeq 1.1.1.1 1.1.1.1 ; then												error ipeq-t1 ; fi
if ! ipeq 255.255.255.255 255.255.255.255 ; then								error ipeq-t2 ; fi

echo -n iple...
if ! iple 12.13.14.15 12.13.14.15 ; then										error iple-t1 ; fi
if ! iple 12.13.14.15 12.13.14.16 ; then										error iple-t2 ; fi
if ! iple 0.0.0.0 12.13.14.15 ; then											error iple-t3 ; fi
if ! iple 0.0.0.0 255.255.255.255 ; then										error iple-t4 ; fi
if ! iple 12.11.11.11 12.44.45.46 ; then										error iple-t5 ; fi
if ! iple 12.11.11.11 12.11.45.46 ; then										error iple-t6 ; fi
if ! iple 12.11.11.11 12.11.11.46 ; then										error iple-t7 ; fi

echo -n iplt...
if ! iplt 12.13.14.15 12.13.14.16 ; then										error iplt-t1 ; fi
if ! iplt 0.0.0.0 12.13.14.15 ; then											error iplt-t2 ; fi
if ! iplt 0.0.0.0 255.255.255.255 ; then										error iplt-t3 ; fi
if ! iplt 12.11.11.11 12.44.45.46 ; then										error iplt-t4 ; fi

echo -n isdigits...
if ! isdigits 0 ; then															error isdigits-t1 ; fi
if ! isdigits 00 ; then															error isdigits-t2 ; fi
if ! isdigits 101 ; then														error isdigits-t3 ; fi
if ! isdigits 4987 ; then														error isdigits-t4 ; fi
if ! isdigits 44567 ; then														error isdigits-t5 ; fi
if ! isdigits 00000 ; then														error isdigits-t6 ; fi
if ! isdigits 99999 ; then														error isdigits-t7 ; fi

echo -n isnumericip...
if ! isnumericip 1.1.1.1 ; then													error isnumericip-t1 ; fi
if ! isnumericip 0.0.0.0 ; then													error isnumericip-t2 ; fi
if ! isnumericip 255.255.255.255 ; then											error isnumericip-t3 ; fi
if ! isnumericip 1.2.3.4 ; then													error isnumericip-t4 ; fi
if ! isnumericip 127.0.0.1 ; then												error isnumericip-t5 ; fi
if ! isnumericip 192.168.234.243 ; then											error isnumericip-t6 ; fi

echo -n networksoverlap...
if ! networksoverlap 127.0.0.0/8 127.0.0.0/16 ; then							error networksoverlap-t1 ; fi
if ! networksoverlap 192.168.1.0/24 192.168.1.252/30 ; then						error networksoverlap-t2 ; fi
if ! networksoverlap 12.13.14.0/24 0.0.0.0/0; then								error networksoverlap-t3 ; fi
if ! networksoverlap 0.0.0.0/0 0.0.0.0/0 ; then									error networksoverlap-t4 ; fi

echo done.

echo ---------- Test should return false: ----------
#if  ; then		error fx ; fi
echo -n ipeq...
if ipeq 1.1.1.1 1.2.3.4 ; then													error ipeq-f1 ; fi

echo -n iple...
if iple 12.13.14.16 12.13.14.15 ; then											error iple-f1 ; fi
if iple 12.13.14.15 0.0.0.0 ; then												error iple-f2 ; fi
if iple 255.255.255.255 0.0.0.0 ; then											error iple-f3 ; fi
if iple 12.44.45.46 12.11.11.11 ; then											error iple-f4 ; fi

echo -n iplt...
if iplt 12.13.14.16 12.13.14.15 ; then											error iplt-f1 ; fi
if iplt 12.13.14.15 0.0.0.0 ; then												error iplt-f2 ; fi
if iplt 255.255.255.255 0.0.0.0 ; then											error iplt-f3 ; fi
if iplt 12.44.45.46 12.11.11.11 ; then											error iplt-f4 ; fi

echo -n isdigits...
if isdigits '' ; then															error isdigits-f1 ; fi
if isdigits a ; then															error isdigits-f2 ; fi
if isdigits a0a ; then															error isdigits-f3 ; fi
if isdigits 00B ; then															error isdigits-f4 ; fi
if isdigits 123456 ; then														error isdigits-f5 ; fi
if isdigits "" ; then															error isdigits-f6 ; fi

echo -n isnumericip...
if isnumericip 1.1.1. ; then													error isnumericip-f1 ; fi
if isnumericip a ; then															error isnumericip-f2 ; fi
if isnumericip 1..1.1.1 ; then													error isnumericip-f3 ; fi
if isnumericip .1.1.1.1 ; then													error isnumericip-f4 ; fi
if isnumericip 1.1.1.1.1 ; then													error isnumericip-f5 ; fi
if isnumericip 256.1.1.1 ; then													error isnumericip-f6 ; fi
if isnumericip 1.256.1.1 ; then													error isnumericip-f7 ; fi
if isnumericip 1.1.256.1 ; then													error isnumericip-f8 ; fi
if isnumericip 1.1.1.256 ; then													error isnumericip-f9 ; fi
if isnumericip -1.5.6.7 ; then													error isnumericip-f10 ; fi
if isnumericip 12.13.14.15a ; then												error isnumericip-f11 ; fi
if isnumericip a.b.c.d ; then													error isnumericip-f12 ; fi

echo -n networksoverlap...
if networksoverlap 1.2.3.0/24 5.6.7.0/24 ; then									error networksoverlap-f1 ; fi
if networksoverlap 0.0.0.0/1 128.0.0.0/1 ; then									error networksoverlap-f2 ; fi
if networksoverlap 1.2.3.248/30 1.2.3.252/30 ; then								error networksoverlap-f3 ; fi

echo done.


echo ---------- General return values ----------
#if [ ! `` = "" ]; then			error -x ; fi

echo -n broadcastof...
if [ ! `broadcastof 0.0.0.0 0.0.0.0` = "255.255.255.255" ]; then				error broadcastof-1 ; fi
if [ ! `broadcastof 1.2.3.4 255.255.255.0` = "1.2.3.255" ]; then				error broadcastof-2 ; fi
if [ ! `broadcastof 15.1.2.3 255.0.0.0` = "15.255.255.255" ]; then				error broadcastof-3 ; fi
if [ ! `broadcastof 1.2.3.4 128.0.0.0` = "127.255.255.255" ]; then				error broadcastof-4 ; fi

echo -n clientportrange...
#CLIENTPORT, SERVERPORT, PRPROTO, ACKFLAG
#if [ ! `GENERALIZETCPACK= UCPOLICY= clientportrange ` = "" ]; then	error clientportrange-x ; fi
PORT_MASQ_BEGIN=61000 ; PORT_MASQ_END=65096 ; TRACEROUTE_BEGIN=33434 ; TRACEROUTE_END=33524
if [ ! `GENERALIZETCPACK= UCPOLICY= clientportrange 1056 113 tcp ""` = "1024:65535" ]; then		error clientportrange-1 ; fi
if [ ! `GENERALIZETCPACK= UCPOLICY= clientportrange 61000 113 tcp ""` = "61000:65096" ]; then	error clientportrange-2 ; fi
if [ ! `GENERALIZETCPACK= UCPOLICY= clientportrange 65096 113 tcp ""` = "61000:65096" ]; then	error clientportrange-3 ; fi
if [ ! `GENERALIZETCPACK= UCPOLICY= clientportrange 65097 113 tcp ""` = "1024:65535" ]; then	error clientportrange-4 ; fi
unset PORT_MASQ_BEGIN PORT_MASQ_END TRACEROUTE_BEGIN TRACEROUTE_END

echo -n encompassingnetworkof...
if [ ! `encompassingnetworkof 1.2.3.1 1.2.3.1` = "1.2.3.1/32" ]; then			error encompassingnetworkof-1 ; fi
if [ ! `encompassingnetworkof 1.2.3.2 1.2.3.3` = "1.2.3.2/31" ]; then			error encompassingnetworkof-2 ; fi
if [ ! `encompassingnetworkof 1.2.3.1 1.2.3.3` = "1.2.3.0/30" ]; then			error encompassingnetworkof-3 ; fi
if [ ! `encompassingnetworkof 1.2.3.0 1.2.3.255` = "1.2.3.0/24" ]; then			error encompassingnetworkof-4 ; fi
if [ ! `encompassingnetworkof 0.0.0.0 255.255.255.255` = "0/0" ]; then			error encompassingnetworkof-5 ; fi
if [ ! `encompassingnetworkof 127.255.255.255 128.0.0.0` = "0/0" ]; then		error encompassingnetworkof-6 ; fi
if [ ! `encompassingnetworkof 172.16.0.1 172.16.5.5` = "172.16.0.0/21" ]; then	error encompassingnetworkof-7 ; fi
if [ ! `encompassingnetworkof 206.231.24.3 206.231.24.254 209.91.2.2 209.91.2.253 209.91.28.2 209.91.28.252 209.91.3.1 209.91.3.254 209.91.32.54 209.91.32.72` = "192.0.0.0/3" ]; then	error encompassingnetworkof-8 ; fi
if [ ! `encompassingnetworkof 10.1.2.3 9.1.2.3` = "8.0.0.0/6" ]; then			error encompassingnetworkof-9 ; fi
if [ ! `encompassingnetworkof 10.1.2.3 10.255.1.1` = "10.0.0.0/8" ]; then		error encompassingnetworkof-10 ; fi
if [ ! `encompassingnetworkof 10.1.2.3 11.12.13.14 10.2.3.4` = "10.0.0.0/7" ]; then		error encompassingnetworkof-11 ; fi
if [ ! `encompassingnetworkof 14.12.1.2 14.12.129.0` = "14.12.0.0/16" ]; then	error encompassingnetworkof-12 ; fi
if [ ! `encompassingnetworkof 14.13.1.1 14.12.255.255` = "14.12.0.0/15" ]; then	error encompassingnetworkof-13 ; fi


echo -n generalportrange...
PORT_MASQ_BEGIN=61000 ; PORT_MASQ_END=65096
if [ ! `generalportrange 0` = "0:1023" ]; then									error generalportrange-1 ; fi
if [ ! `generalportrange 1` = "0:1023" ]; then									error generalportrange-2 ; fi
if [ ! `generalportrange 1023` = "0:1023" ]; then								error generalportrange-3 ; fi
if [ ! `generalportrange 1024` = "1024:65535" ]; then							error generalportrange-4 ; fi
if [ ! `generalportrange 60999` = "1024:65535" ]; then							error generalportrange-5 ; fi
if [ ! `generalportrange 61000` = "61000:65096" ]; then							error generalportrange-6 ; fi
if [ ! `generalportrange 65096` = "61000:65096" ]; then							error generalportrange-7 ; fi
if [ ! `generalportrange 65097` = "1024:65535" ]; then							error generalportrange-8 ; fi
if [ ! `generalportrange 65535` = "1024:65535" ]; then							error generalportrange-9 ; fi
unset PORT_MASQ_BEGIN PORT_MASQ_END

echo -n mask2bits...
if [ ! `mask2bits 255.255.255.255` = "32" ]; then								error mask2bits-1 ; fi
if [ ! `mask2bits 255.255.255.254` = "31" ]; then								error mask2bits-2 ; fi
if [ ! `mask2bits 255.255.255.252` = "30" ]; then								error mask2bits-3 ; fi
if [ ! `mask2bits 255.255.255.248` = "29" ]; then								error mask2bits-4 ; fi
if [ ! `mask2bits 128.0.0.0` = "1" ]; then										error mask2bits-5 ; fi
if [ ! `mask2bits 0.0.0.0` = "0" ]; then										error mask2bits-6 ; fi

echo -n nameof...
if [ ! `HOSTLOOKUP=NONE nameof 127.0.0.1` = "127.0.0.1" ]; then					error nameof-1 ; fi
if [ ! `HOSTLOOKUP=FILESONLY nameof 127.0.0.1` = "localhost" ]; then			error nameof-2 ; fi
if [ ! `HOSTLOOKUP=FULL nameof 127.0.0.1` = "localhost" ]; then					error nameof-3 ; fi

echo -n networkof...
if [ ! `networkof 0.0.0.0 0.0.0.0` = "0.0.0.0" ]; then							error networkof-1 ; fi
if [ ! `networkof 1.2.3.4 255.255.255.0` = "1.2.3.0" ]; then					error networkof-2 ; fi
if [ ! `networkof 15.1.2.3 255.0.0.0` = "15.0.0.0" ]; then						error networkof-3 ; fi
if [ ! `networkof 1.2.3.4 128.0.0.0` = "0.0.0.0" ]; then						error networkof-4 ; fi
if [ ! `networkof 0.0.0.0 0` = "0.0.0.0" ]; then								error networkof-5 ; fi
if [ ! `networkof 1.2.3.4 24` = "1.2.3.0" ]; then								error networkof-6 ; fi
if [ ! `networkof 15.1.2.3 8` = "15.0.0.0" ]; then								error networkof-7 ; fi
if [ ! `networkof 1.2.3.4 1` = "0.0.0.0" ]; then								error networkof-8 ; fi
if [ ! `networkof 1.2.3.4 0.0.0.0` = "0.0.0.0" ]; then							error networkof-9 ; fi

echo -n mask2cisco...
if [ ! `mask2cisco 255.255.255.128` = "0.0.0.127" ]; then						error mask2cisco-1 ; fi

echo done.


echo ---------- Exit with a fanfare ----------
echo `cat $0 | sed -e 's/#.*//' | grep 'error .* fi' | grep -v regression | wc -l` regression tests successful on $MASONVER
if [ -d /usr/src/mason-work ]; then
	date >>/usr/src/mason-work/regression-log
	echo `cat $0 | sed -e 's/#.*//' | grep 'error .* fi' | grep -v regression | wc -l` regression tests successful on $MASONVER >>/usr/src/mason-work/regression-log
fi
exit 0

