#!/bin/sh
#
# Copyright (C) 2016, 2017  Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

#
# Common configuration data for system tests, to be sourced into
# other shell scripts.
#

# For specific stuff (currently kill HUP -> rdnc reload in notify)
CYGWIN=1

# Find the top of the BIND9 tree.
TOP=${SYSTEMTESTTOP:=.}/../../..

# Make it absolute so that it continues to work after we cd.
TOP=`cd $TOP && pwd`

# Visual Studio build configurations are Release and Debug
VSCONF=Debug

NAMED=$TOP/Build/$VSCONF/named@EXEEXT@
DIG=$TOP/Build/$VSCONF/dig@EXEEXT@
DELV=$TOP/Build/$VSCONF/delv@EXEEXT@
RNDC=$TOP/Build/$VSCONF/rndc@EXEEXT@
NSUPDATE=$TOP/Build/$VSCONF/nsupdate@EXEEXT@
DDNSCONFGEN=$TOP/Build/$VSCONF/ddns-confgen@EXEEXT@
TSIGKEYGEN=$TOP/Build/$VSCONF/tsig-keygen@EXEEXT@
RNDCCONFGEN=$TOP/Build/$VSCONF/rndc-confgen@EXEEXT@
KEYGEN=$TOP/Build/$VSCONF/dnssec-keygen@EXEEXT@
KEYFRLAB=$TOP/Build/$VSCONF/dnssec-keyfromlabel@EXEEXT@
SIGNER=$TOP/Build/$VSCONF/dnssec-signzone@EXEEXT@
REVOKE=$TOP/Build/$VSCONF/dnssec-revoke@EXEEXT@
SETTIME=$TOP/Build/$VSCONF/dnssec-settime@EXEEXT@
DSFROMKEY=$TOP/Build/$VSCONF/dnssec-dsfromkey@EXEEXT@
IMPORTKEY=$TOP/Build/$VSCONF/dnssec-importkey@EXEEXT@
CHECKDS="$PYTHON `cygpath -w $TOP/bin/python/dnssec-checkds.py`"
COVERAGE="$PYTHON `cygpath -w $TOP/bin/python/dnssec-coverage.py`"
KEYMGR="$PYTHON `cygpath -w $TOP/bin/python/dnssec-keymgr.py`"
CHECKZONE=$TOP/Build/$VSCONF/named-checkzone@EXEEXT@
CHECKCONF=$TOP/Build/$VSCONF/named-checkconf@EXEEXT@
PK11GEN="$TOP/Build/$VSCONF/pkcs11-keygen@EXEEXT@ -q -s ${SLOT:-0} -p ${HSMPIN:-1234}"
PK11LIST="$TOP/Build/$VSCONF/pkcs11-list@EXEEXT@ -s ${SLOT:-0} -p ${HSMPIN:-1234}"
PK11DEL="$TOP/Build/$VSCONF/pkcs11-destroy@EXEEXT@ -s ${SLOT:-0} -p ${HSMPIN:-1234} -w 0"
JOURNALPRINT=$TOP/Build/$VSCONF/named-journalprint@EXEEXT@
VERIFY=$TOP/Build/$VSCONF/dnssec-verify@EXEEXT@
ARPANAME=$TOP/Build/$VSCONF/arpaname@EXEEXT@
RESOLVE=$TOP/lib/samples/resolve
RRCHECKER=$TOP/Build/$VSCONF/named-rrchecker@EXEEXT@
GENRANDOM=$TOP/Build/$VSCONF/genrandom@EXEEXT@
NSLOOKUP=$TOP/Build/$VSCONF/nslookup@EXEEXT@
DNSTAPREAD=$TOP/Build/$VSCONF/dnstap-read@EXEEXT@
MDIG=$TOP/Build/$VSCONF/mdig@EXEEXT@
NZD2NZF=$TOP/Build/$VSCONF/named-nzd2nzf@EXEEXT@
FSTRM_CAPTURE=@FSTRM_CAPTURE@
FEATURETEST=$TOP/Build/$VSCONF/feature-test@EXEEXT@
SAMPLEUPDATE=$TOP/Build/$VSCONF/update@EXEEXT@

# to port WIRETEST=$TOP/Build/$VSCONF/wire_test@EXEEXT@

# this is given as argument to native WIN32 executables
RANDFILE=`cygpath -w $TOP/bin/tests/system/random.data`

BIGKEY=$TOP/Build/$VSCONF/bigkey@EXEEXT@
GENCHECK=$TOP/Build/$VSCONF/gencheck@EXEEXT@
KEYCREATE=$TOP/Build/$VSCONF/keycreate@EXEEXT@
KEYDELETE=$TOP/Build/$VSCONF/keydelete@EXEEXT@
MAKEJOURNAL=$TOP/Build/$VSCONF/makejournal@EXEEXT@
PIPEQUERIES=$TOP/Build/$VSCONF/pipequeries@EXEEXT@
# to port SAMPLEUPDATE=$TOP/lib/samples/sample-update

# we don't want a KRB5_CONFIG setting breaking the tests
KRB5_CONFIG=NUL

# The "stress" test is not run by default since it creates enough
# load on the machine to make it unusable to other users.
# v6synth
SUBDIRS="acl additional addzone allow_query autosign builtin
	 cacheclean case catz
	 checkconf @CHECKDS@ checknames checkzone cookie @COVERAGE@
	 database digdelv dlv dlvauto dlz dlzexternal dname
	 dns64 dnssec @DNSTAP@ dscp dsdigest dyndb ecdsa eddsa
	 ednscompliance emptyzones
	 fetchlimit filter-aaaa formerr forward geoip glue gost inline ixfr
	 keepalive @KEYMGR@ legacy limits logfileconfig masterfile
	 masterformat metadata mkeys names notify nslookup nsupdate
	 nzd2nzf padding pending pipelined @PKCS11_TEST@ reclimit
	 redirect resolver rndc rpz rpzrecurse rrchecker rrl
	 rrsetorder rsabigexponent runtime sfcache smartsign sortlist
	 spf staticstub statistics statschannel stub tcp tkey tsig
	 tsiggss unknown upforwd verify views wildcard xfer xferquota
	 zero zonechecks"

# missing: chain integrity
# extra: dname ednscompliance forward 

#Things that are different on Windows
KILL="/bin/kill -f"
DIFF="diff --strip-trailing-cr"
DOS2UNIX=dos2unix
# No trailing period
TP=

# Configure is launched from native environment, but tests are run in Cygwin -
# so any detection is unreliable. 
SHELL="/bin/bash -o igncr"
CURL=/usr/bin/curl
XMLLINT=/usr/bin/xmllint
PERL=/usr/bin/perl

if test -n "$PERL"
then
	if $PERL -e "use IO::Socket::INET6;" 2> /dev/null
	then
		TESTSOCK6="$PERL $TOP/bin/tests/system/testsock6.pl"
	else
		TESTSOCK6=false
	fi
else
	TESTSOCK6=false
fi

if grep "^#define WANT_IPV6 1" $TOP/config.h > /dev/null 2>&1 ; then
        TESTSOCK6="$TESTSOCK6"
else
        TESTSOCK6=false
fi


PYTHON=@PYTHON@

#
# Determine if we support various optional features.
#
CHECK_DSA=@CHECK_DSA@
HAVEXMLSTATS=@XMLSTATS@
HAVEJSONSTATS=@JSONSTATS@
ZLIB=@ZLIB@
NZD=@NZD_TOOLS@

. ${TOP}/version

#
# Set up color-coded test output
#
if test -t 1 && type tput > /dev/null; then
    COLOR_FAIL=`tput setaf 1`   # red
    COLOR_WARN=`tput setaf 3`   # yellow
    COLOR_PASS=`tput setaf 2`   # green
    COLOR_INFO=`tput bold`      # bold
    COLOR_NONE=`tput sgr0`
else
    # set to empty strings so printf succeeds
    COLOR_FAIL=''
    COLOR_WARN=''
    COLOR_PASS=''
    COLOR_INFO=''
    COLOR_NONE=''
fi

echofail () {
        printf "${COLOR_FAIL}%s${COLOR_NONE}\n" "$*"
}

echowarn () {
        printf "${COLOR_WARN}%s${COLOR_NONE}\n" "$*"
}

echopass () {
        printf "${COLOR_PASS}%s${COLOR_NONE}\n" "$*"
}

echoinfo () {
        printf "${COLOR_INFO}%s${COLOR_NONE}\n" "$*"
}

#
# Useful functions in test scripts
#

# nextpart: read everything that's been appended to a file since the
# last time 'nextpart' was called.
nextpart () {
    [ -f $1.prev ] || echo "0" > $1.prev
    prev=`cat $1.prev`
    awk "NR > $prev "'{ print }
         END          { print NR > "/dev/stderr" }' $1 2> $1.prev
}

#
# Export command paths
#
export ARPANAME
export BIGKEY
export CHECKZONE
export DESCRIPTION
export DIG
export FEATURETEST
export FSTRM_CAPTURE
export GENCHECK
export JOURNALPRINT
export KEYCREATE
export KEYDELETE
export KEYFRLAB
export KEYGEN
export KEYSETTOOL
export KEYSIGNER
export KRB5_CONFIG
export MAKEJOURNAL
export MDIG
export NAMED
export NSLOOKUP
export NSUPDATE
export NZD2NZF
export PERL
export PIPEQUERIES
export PK11DEL
export PK11GEN
export PK11LIST
export PYTHON
export RANDFILE
export RESOLVE
export RNDC
export RRCHECKER
export SAMPLEUPDATE
export SIGNER
export SUBDIRS
export TESTSOCK6
