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