xref: /netbsd-src/external/bsd/unbound/dist/testdata/common.sh (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
1# common.sh - an include file for commonly used functions for test code.
2# BSD licensed (see LICENSE file).
3#
4# Version 3
5# 2011-02-23: get_pcat for PCAT, PCAT_DIFF and PCAT_PRINT defines.
6# 2011-02-18: ports check on BSD,Solaris. wait_nsd_up.
7# 2011-02-11: first version.
8#
9# include this file from a tdir script with
10#   . ../common.sh
11#
12# overview of functions available:
13# error x		: print error and exit
14# info x		: print info
15# test_tool_avail x	: see if program in path and complain, exit if not.
16# get_ldns_testns	: set LDNS_TESTNS to executable ldns-testns
17# get_ldns_notify	: set LDNS_NOTIFY to executable ldns-notify
18# get_make		: set MAKE to gmake or make tool.
19# get_gcc		: set cc or gcc in CC
20# get_pcat		: set PCAT, PCAT_DIFF and PCAT_PRINT executables.
21# set_doxygen_path	: set doxygen path
22# skip_if_in_list	: set SKIP=1 if name in list and tool not available.
23# get_random_port x	: get RND_PORT a sequence of free random port numbers.
24# wait_server_up	: wait on logfile to see when server comes up.
25# wait_ldns_testns_up   : wait for ldns-testns to come up.
26# wait_unbound_up	: wait for unbound to come up.
27# wait_petal_up		: wait for petal to come up.
28# wait_nsd_up		: wait for nsd to come up.
29# wait_server_up_or_fail: wait for server to come up or print a failure string
30# skip_test x		: print message and skip test (must be called in .pre)
31# kill_pid		: kill a server, make sure and wait for it to go down.
32# teststep		: print the current test step in the output
33
34
35# print error and exit
36# $0: name of program
37# $1: error to printout.
38error () {
39	echo "$0: error: $1" >&2
40	exit 1
41}
42
43# print info
44# $0: name of program
45# $1: to printout.
46info () {
47	echo "$0: info: $1"
48}
49
50# test if 'tool' is available in path and complain otherwise.
51# $1: tool
52test_tool_avail () {
53	if test ! -x "`which $1 2>&1`"; then
54		echo No "$1" in path
55		exit 1
56	fi
57}
58
59# get ldns-testns tool in LDNS_TESTNS variable.
60get_ldns_testns () {
61	if test -x "`which ldns-testns 2>&1`"; then
62		LDNS_TESTNS=ldns-testns
63	else
64		LDNS_TESTNS=/home/wouter/bin/ldns-testns
65	fi
66}
67
68# get ldns-notify tool in LDNS_NOTIFY variable.
69get_ldns_notify () {
70	if test -x "`which ldns-notify 2>&1`"; then
71		LDNS_NOTIFY=ldns-notify
72	else
73		LDNS_NOTIFY=/home/wouter/bin/ldns-notify
74	fi
75}
76
77# get make tool in MAKE variable, gmake is used if present.
78get_make () {
79	if test -x "`which gmake 2>&1`"; then
80		MAKE=gmake
81	else
82		MAKE=make
83	fi
84}
85
86# get cc tool in CC variable, gcc is used if present.
87get_gcc () {
88	if test -x "`which gcc 2>&1`"; then
89		CC=gcc
90	else
91		CC=cc
92	fi
93}
94
95# get pcat, pcat-print and pcat-diff
96get_pcat () {
97	PCAT=`which pcat`
98	PCAT_PRINT=`which pcat-print`
99	PCAT_DIFF=`which pcat-diff`
100}
101
102# set SKIP=1 if the name is in list and tool is not available.
103# $1: name of package to check.
104# $2: list of packages that need the tool.
105# #3: name of the tool required.
106skip_if_in_list () {
107	if echo $2 | grep $1 >/dev/null; then
108		if test ! -x "`which $3 2>&1`"; then
109			SKIP=1;
110		fi
111	fi
112}
113
114# Print a message and skip the test. Must be called in the .pre file.
115# $1: message to print.
116skip_test () {
117	echo "$1"
118	exit 3
119}
120
121# function to get a number of random port numbers.
122# $1: number of random ports.
123# RND_PORT is returned as the starting port number
124get_random_port () {
125	local plist
126	local cont
127	local collisions
128	local i
129	local MAXCOLLISION=1000
130	cont=1
131	collisions=0
132	while test "$cont" = 1; do
133		#netstat -n -A ip -A ip6 -a | sed -e "s/^.*:\([0-9]*\) .*$/\1/"
134		RND_PORT=$(( $RANDOM + 5354 ))
135		# depending on uname try to check for collisions in port numbers
136		case "`uname`" in
137		linux|Linux)
138			plist=`netstat -n -A ip -A ip6 -a 2>/dev/null | sed -e 's/^.*:\([0-9]*\) .*$/\1/'`
139		;;
140		FreeBSD|freebsd|NetBSD|netbsd|OpenBSD|openbsd)
141			plist=`netstat -n -a | grep "^[ut][dc]p[46] " | sed -e 's/^.*\.\([0-9]*\) .*$/\1/'`
142		;;
143		Solaris|SunOS)
144			plist=`netstat -n -a | sed -e 's/^.*\.\([0-9]*\) .*$/\1/' | grep '^[0-9]*$'`
145		;;
146		*)
147			plist=""
148		;;
149		esac
150		cont=0
151		for (( i=0 ; i < $1 ; i++ )); do
152			if echo "$plist" | grep '^'`expr $i + $RND_PORT`'$' >/dev/null 2>&1; then
153				cont=1;
154				collisions=`expr $collisions + 1`
155			fi
156		done
157		if test $collisions = $MAXCOLLISION; then
158			error "too many collisions getting random port number"
159		fi
160	done
161}
162
163# wait for server to go up, pass <logfilename> <string to watch>
164# $1 : logfilename
165# $2 : string to watch for.
166# exits with failure if it does not come up
167wait_server_up () {
168	local MAX_UP_TRY=120
169	local WAIT_THRES=30
170	local try
171	for (( try=0 ; try <= $MAX_UP_TRY ; try++ )) ; do
172		if test -f $1 && fgrep "$2" $1 >/dev/null; then
173			#echo "done on try $try"
174			break;
175		fi
176		if test $try -eq $MAX_UP_TRY; then
177			echo "Server in $1 did not go up!"
178			cat $1
179			exit 1;
180		fi
181		if test $try -ge $WAIT_THRES; then
182			sleep 1
183		fi
184	done
185}
186
187# wait for ldns-testns to come up
188# $1 : logfilename that is watched.
189wait_ldns_testns_up () {
190	wait_server_up "$1" "Listening on port"
191}
192
193# wait for unbound to come up
194# string 'Start of service' in log.
195# $1 : logfilename that is watched.
196wait_unbound_up () {
197	wait_server_up "$1" "start of service"
198}
199
200# wait for petal to come up
201# string 'petal start' in log.
202# $1 : logfilename that is watched.
203wait_petal_up () {
204	wait_server_up "$1" "petal start"
205}
206
207# wait for nsd to come up
208# string nsd start in log.
209# $1 : logfilename that is watched.
210wait_nsd_up () {
211	wait_server_up "$1" " started (NSD "
212}
213
214# wait for server to go up, pass <logfilename> <string to watch> <badstr>
215# $1 : logfile
216# $2 : success string
217# $3 : failure string
218wait_server_up_or_fail () {
219	local MAX_UP_TRY=120
220	local WAIT_THRES=30
221	local try
222	for (( try=0 ; try <= $MAX_UP_TRY ; try++ )) ; do
223		if test -f $1 && fgrep "$2" $1 >/dev/null; then
224			echo "done on try $try"
225			break;
226		fi
227		if test -f $1 && fgrep "$3" $1 >/dev/null; then
228			echo "failed on try $try"
229			break;
230		fi
231		if test $try -eq $MAX_UP_TRY; then
232			echo "Server in $1 did not go up!"
233			cat $1
234			exit 1;
235		fi
236		if test $try -ge $WAIT_THRES; then
237			sleep 1
238		fi
239	done
240}
241
242# kill a pid, make sure and wait for it to go down.
243# $1 : pid to kill
244kill_pid () {
245	local MAX_DOWN_TRY=120
246	local WAIT_THRES=30
247	local try
248	kill $1
249	for (( try=0 ; try <= $MAX_DOWN_TRY ; try++ )) ; do
250		if kill -0 $1 >/dev/null 2>&1; then
251			:
252		else
253			#echo "done on try $try"
254			break;
255		fi
256		if test $try -eq $MAX_DOWN_TRY; then
257			echo "Server in $1 did not go down! Send SIGKILL"
258			kill -9 $1 >/dev/null 2>&1
259		fi
260		if test $try -ge $WAIT_THRES; then
261			sleep 1
262		fi
263		# re-send the signal
264		kill $1 >/dev/null 2>&1
265	done
266	return 0
267}
268
269# set doxygen path, so that make doc can find doxygen
270set_doxygen_path () {
271	if test -x '/home/wouter/bin/doxygen'; then
272	        export PATH="/home/wouter/bin:$PATH"
273	fi
274}
275
276# Print the current test step in the output
277teststep () {
278	echo
279	echo "STEP [ $1 ]"
280}
281