]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/testdata/common.sh
1 # common.sh - an include file for commonly used functions for test code.
2 # BSD licensed (see LICENSE file).
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.
9 # include this file from a tpkg script with
12 # overview of functions available:
13 # error x : print error and exit
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_make : set MAKE to gmake or make tool.
18 # get_gcc : set cc or gcc in CC
19 # get_pcat : set PCAT, PCAT_DIFF and PCAT_PRINT executables.
20 # set_doxygen_path : set doxygen path
21 # skip_if_in_list : set SKIP=1 if name in list and tool not available.
22 # get_random_port x : get RND_PORT a sequence of free random port numbers.
23 # wait_server_up : wait on logfile to see when server comes up.
24 # wait_ldns_testns_up : wait for ldns-testns to come up.
25 # wait_unbound_up : wait for unbound to come up.
26 # wait_petal_up : wait for petal to come up.
27 # wait_nsd_up : wait for nsd to come up.
28 # wait_server_up_or_fail: wait for server to come up or print a failure string
29 # kill_pid : kill a server, make sure and wait for it to go down.
32 # print error and exit
34 # $1: error to printout.
36 echo "$0: error: $1" >&2
47 # test if 'tool' is available in path and complain otherwise.
50 if test ! -x "`which $1 2>&1`"; then
56 # get ldns-testns tool in LDNS_TESTNS variable.
58 if test -x "`which ldns-testns 2>&1`"; then
59 LDNS_TESTNS
=ldns
-testns
61 LDNS_TESTNS
=/home
/wouter
/bin
/ldns
-testns
65 # get make tool in MAKE variable, gmake is used if present.
67 if test -x "`which gmake 2>&1`"; then
74 # get cc tool in CC variable, gcc is used if present.
76 if test -x "`which gcc 2>&1`"; then
83 # get pcat, pcat-print and pcat-diff
86 PCAT_PRINT
=`which pcat-print`
87 PCAT_DIFF
=`which pcat-diff`
90 # set SKIP=1 if the name is in list and tool is not available.
91 # $1: name of package to check.
92 # $2: list of packages that need the tool.
93 # #3: name of the tool required.
95 if echo $2 | grep $1 >/dev
/null
; then
96 if test ! -x "`which $3 2>&1`"; then
102 # function to get a number of random port numbers.
103 # $1: number of random ports.
104 # RND_PORT is returned as the starting port number
110 local MAXCOLLISION
=1000
113 while test "$cont" = 1; do
114 #netstat -n -A ip -A ip6 -a | sed -e "s/^.*:\([0-9]*\) .*$/\1/"
115 RND_PORT
=$(( $RANDOM + 5354 ))
116 # depending on uname try to check for collisions in port numbers
119 plist
=`netstat -n -A ip -A ip6 -a 2>/dev/null | sed -e 's/^.*:\([0-9]*\) .*$/\1/'`
121 FreeBSD
|freebsd
|NetBSD
|netbsd
|OpenBSD
|openbsd
)
122 plist
=`netstat -n -a | grep "^[ut][dc]p[46] " | sed -e 's/^.*\.\([0-9]*\) .*$/\1/'`
125 plist
=`netstat -n -a | sed -e 's/^.*\.\([0-9]*\) .*$/\1/' | grep '^[0-9]*$'`
132 for (( i
=0 ; i
< $1 ; i
++ )); do
133 if echo "$plist" | grep '^'`expr $i + $RND_PORT`'$' >/dev
/null
2>&1; then
135 collisions
=`expr $collisions + 1`
138 if test $collisions = $MAXCOLLISION; then
139 error
"too many collisions getting random port number"
144 # wait for server to go up, pass <logfilename> <string to watch>
146 # $2 : string to watch for.
147 # exits with failure if it does not come up
152 for (( try
=0 ; try
<= $MAX_UP_TRY ; try
++ )) ; do
153 if test -f $1 && fgrep
"$2" $1 >/dev
/null
; then
154 #echo "done on try $try"
157 if test $try -eq $MAX_UP_TRY; then
158 echo "Server in $1 did not go up!"
162 if test $try -ge $WAIT_THRES; then
168 # wait for ldns-testns to come up
169 # $1 : logfilename that is watched.
170 wait_ldns_testns_up
() {
171 wait_server_up
"$1" "Listening on port"
174 # wait for unbound to come up
175 # string 'Start of service' in log.
176 # $1 : logfilename that is watched.
178 wait_server_up
"$1" "start of service"
181 # wait for petal to come up
182 # string 'petal start' in log.
183 # $1 : logfilename that is watched.
185 wait_server_up
"$1" "petal start"
188 # wait for nsd to come up
189 # string nsd start in log.
190 # $1 : logfilename that is watched.
192 wait_server_up
"$1" " started (NSD "
195 # wait for server to go up, pass <logfilename> <string to watch> <badstr>
197 # $2 : success string
198 # $3 : failure string
199 wait_server_up_or_fail
() {
203 for (( try
=0 ; try
<= $MAX_UP_TRY ; try
++ )) ; do
204 if test -f $1 && fgrep
"$2" $1 >/dev
/null
; then
205 echo "done on try $try"
208 if test -f $1 && fgrep
"$3" $1 >/dev
/null
; then
209 echo "failed on try $try"
212 if test $try -eq $MAX_UP_TRY; then
213 echo "Server in $1 did not go up!"
217 if test $try -ge $WAIT_THRES; then
223 # kill a pid, make sure and wait for it to go down.
226 local MAX_DOWN_TRY
=120
230 for (( try
=0 ; try
<= $MAX_DOWN_TRY ; try
++ )) ; do
231 if kill -0 $1 >/dev
/null
2>&1; then
234 #echo "done on try $try"
237 if test $try -eq $MAX_DOWN_TRY; then
238 echo "Server in $1 did not go down! Send SIGKILL"
239 kill -9 $1 >/dev
/null
2>&1
241 if test $try -ge $WAIT_THRES; then
245 kill $1 >/dev
/null
2>&1
250 # set doxygen path, so that make doc can find doxygen
251 set_doxygen_path
() {
252 if test -x '/home/wouter/bin/doxygen'; then
253 export PATH
="/home/wouter/bin:$PATH"