#!/bin/sh # Automated tests for the addrset functions in ncat_hostmatch.c. This # program runs various addresses against different host specifications # and checks that the output is what is expected. ADDRSET=./addrset TESTS=0 TEST_PASS=0 TEST_FAIL=0 # Takes as arguments a whitespace-separated list of host specifications # and a space-separated list of expected matching addresses. Tests hosts # are passed in stdin. test_addrset() { specs=$1 expected=$2 result=$($ADDRSET $specs) ret=$? # Change newlines to spaces. result=$(echo $result) TESTS=$(expr $TESTS + 1); if [ "$ret" != "0" ]; then echo "FAIL $specs: $ADDRSET returned $ret." TEST_FAIL=$(expr $TEST_FAIL + 1) elif [ "$result" != "$expected" ]; then echo "FAIL $specs: \"$result\" !=" echo " \"$expected\"." TEST_FAIL=$(expr $TEST_FAIL + 1) else echo "PASS $specs" TEST_PASS=$(expr $TEST_PASS + 1) fi } # Takes as an argument a host specification with invalid syntax. The # test passes if addrset returns with a non-zero exit code. expect_fail() { specs=$1 $ADDRSET $specs < /dev/null 2> /dev/null ret=$? TESTS=$(expr $TESTS + 1) if [ "$ret" = "0" ]; then echo "FAIL $ADDRSET $specs was expected to fail, but didn't." TEST_FAIL=$(expr $TEST_FAIL + 1) else echo "PASS $specs" TEST_PASS=$(expr $TEST_PASS + 1) fi } # seq replacement for systems without seq. seq() { low=$1 high=$2 while [ $low -le $high ]; do echo $low low=$(expr $low + 1) done } # No specifications. test_addrset "" "" <