1# #-- 09-unbound-control.test --# 2# source the master var file when it's there 3[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master 4# use .tpkg.var.test for in test variable passing 5[ -f .tpkg.var.test ] && source .tpkg.var.test 6 7PRE="../.." 8. ../common.sh 9 10# End the test 11# $1: exit value 12end () { 13 echo "> cat logfiles" 14 cat fwd.log 15 cat unbound.log 16 exit $1 17} 18 19# Expect a given exit value of the previous command 20# $1: the expected exit value 21# $2: optional text to print when failing 22expect_exit_value () { 23 if test $? -ne $1; then 24 if test -z "$2"; then 25 if test $1 -eq 1; then 26 msg="on error" 27 else 28 msg="after success" 29 fi 30 else 31 msg="$2" 32 fi 33 echo "wrong exit value $msg" 34 end 1 35 fi 36} 37 38# Helper function for quering 39# $@: at least the domain name to query and optional dig arguments 40query () { 41 echo "> dig $@" 42 dig @127.0.0.1 -p $UNBOUND_PORT $@ | tee outfile 43} 44 45# Expect something in the answer 46# $1: expected regular expression 47expect_answer () { 48 echo "> check answer for \"$1\"" 49 if grep "$1" outfile; then 50 echo "OK" 51 else 52 echo "Not OK" 53 end 1 54 fi 55} 56 57# Fail the test for unexpected answers 58# $1: unexpected regular expression 59fail_answer () { 60 echo "> \"$1\" should not be in answer" 61 if grep "$1" outfile; then 62 echo "Not OK" 63 end 1 64 else 65 echo "OK" 66 fi 67} 68 69# Issue an unbound-control command 70# $@: command arguments 71control_command () { 72 echo "$PRE/unbound-control $@" 73 $PRE/unbound-control $@ > outfile 74} 75 76# Dump the cache contents 77# $@: optional options to unbound-control 78cache_dump () { 79 echo "$PRE/unbound-control $@ dump_cache > cache.dump" 80 $PRE/unbound-control $@ dump_cache > cache.dump 81} 82 83# Load cache contents 84# $@: optional options to unbound-control 85cache_load () { 86 echo "$PRE/unbound-control $@ load_cache < cache.dump" 87 $PRE/unbound-control $@ load_cache < cache.dump 88} 89 90# Expect an entry in the cache dump 91# $1: expected regular expression 92expect_in_cache_dump () { 93 echo "> check cache dump for \"$1\"" 94 if grep "$1" cache.dump; then 95 echo "OK cache dump" 96 else 97 echo "Not OK cache dump" 98 end 1 99 fi 100} 101 102# Fail the test for unexpected entry in the cache dump 103# $1: unexpected regular expression 104fail_in_cache_dump () { 105 echo "> \"$1\" should not be in cache dump" 106 if grep "$1" cache.dump; then 107 echo "Not OK cache dump" 108 end 1 109 else 110 echo "OK cache dump" 111 fi 112} 113 114# start the test 115cp ub.conf main.conf 116 117teststep "exit value is 1 on usage" 118control_command -h 119expect_exit_value 1 "for usage" 120 121# use lock-verify if possible 122 123teststep "test if the server is up" 124query www.example.com. 125expect_answer "10.20.30.40" 126 127teststep "exit value is 1 when a bad command is given" 128control_command -c ub.conf blablargh 129expect_exit_value 1 130 131# reload the server. test if the server came up by putting a new 132# local-data element in the server. 133teststep "reload the server" 134echo "server: local-data: 'afterreload. IN A 5.6.7.8'" >> ub.conf 135control_command -c ub.conf reload 136expect_exit_value 0 137query afterreload. 138expect_answer "5.6.7.8" 139 140teststep "must have had at least 1 query since reload" 141control_command -c ub.conf stats 142expect_exit_value 0 143expect_answer "^total.num.queries=[1-9][0-9]*$" 144 145teststep "check verbosity" 146control_command -c ub.conf verbosity 2 147expect_exit_value 0 148 149teststep "check syntax error in parse" 150control_command -c ub.conf verbosity jkdf 151expect_exit_value 1 152 153teststep "check bad credentials" 154cp ub.conf bad.conf 155cat conf.bad_credentials >> bad.conf 156control_command -c bad.conf verbosity 2 157expect_exit_value 1 158 159teststep "check spoofed client credentials" 160rm -f bad.conf 161cp ub.conf bad.conf 162cat conf.spoofed_credentials >> bad.conf 163control_command -c bad.conf verbosity 2 164expect_exit_value 1 165 166teststep "create a new local zone" 167control_command -c ub.conf local_zone example.net static 168expect_exit_value 0 169control_command -c ub.conf local_data www.example.net A 192.0.2.1 170expect_exit_value 0 171 172teststep "check that www.example.net exists" 173query www.example.net. 174expect_answer "192.0.2.1" 175 176teststep "check that mail.example.net has nxdomain" 177query mail.example.net. 178expect_answer "NXDOMAIN" 179 180teststep "remove www.example.net - check it gets nxdomain" 181control_command -c ub.conf local_data_remove www.example.net 182expect_exit_value 0 183query www.example.net. 184expect_answer "NXDOMAIN" 185 186teststep "remove nonexistent name - check bug#287(segfault) does not happen" 187control_command -c ub.conf local_data_remove test.example.net 188# if crash then then we get: error: could not SSL_read from unbound-control 189expect_exit_value 0 190 191teststep "remove example.net - check its gone" 192control_command -c ub.conf local_zone_remove example.net 193expect_exit_value 0 194query www.example.net. 195expect_answer "SERVFAIL" 196 197teststep "dump the cache" 198query www.example.com. 199cache_dump -c ub.conf 200expect_exit_value 0 201cat cache.dump 202expect_in_cache_dump "10.20.30.40" 203 204control_command -c ub.conf lookup www.example.com 205expect_exit_value 0 206# answer to lookup is meaningless because of use a forwarder, oh well. 207 208teststep "load the cache dump" 209cache_load -c ub.conf 210expect_exit_value 0 211query www.example.com. 212expect_answer "10.20.30.40" 213 214teststep "load local-zones from file" 215control_command -c ub.conf local_zones < local_zones 216expect_exit_value 0 217query localzonefromfile 218expect_answer "REFUSED" 219 220teststep "load local-data from file" 221control_command -c ub.conf local_datas < local_data 222expect_exit_value 0 223query -t txt localdatafromfile 224expect_answer "local data from file OK" 225 226teststep "remove local-zone and local-data from file" 227control_command -c ub.conf local_zones_remove < local_zones_remove 228expect_exit_value 0 229control_command -c ub.conf local_datas_remove < local_data_remove 230expect_exit_value 0 231control_command -c ub.conf list_local_zones 232fail_answer "localzonefromfile" 233fail_answer "local data from file OK" 234expect_answer "otherlocalzone" 235 236teststep "flushing" 237control_command -c ub.conf flush www.example.net 238expect_exit_value 0 239control_command -c ub.conf flush_type www.example.net TXT 240expect_exit_value 0 241control_command -c ub.conf flush_zone example.net 242expect_exit_value 0 243 244teststep "reload the server for a clean state and populate the cache" 245cp main.conf ub.conf 246control_command -c ub.conf reload 247expect_exit_value 0 248query www.example.com 249expect_answer "10.20.30.40" 250 251teststep "reload and check cache dump - should be empty" 252control_command -c ub.conf reload 253expect_exit_value 0 254cache_dump -c ub.conf 255expect_exit_value 0 256fail_in_cache_dump "www.example.com.*10.20.30.40" 257fail_in_cache_dump "msg www.example.com. IN A" 258 259query www.example.com 260expect_answer "10.20.30.40" 261 262teststep "reload_keep_cache and check cache dump - should not be empty" 263control_command -c ub.conf reload_keep_cache 264expect_exit_value 0 265cache_dump -c ub.conf 266expect_exit_value 0 267cat cache.dump 268expect_in_cache_dump "www.example.com.*10.20.30.40" 269expect_in_cache_dump "msg www.example.com. IN A" 270query www.example.com +nordflag 271expect_answer "10.20.30.40" 272 273teststep "change msg-cache-size and reload_keep_cache - should be empty" 274echo "server: msg-cache-size: 2m" >> ub.conf 275control_command -c ub.conf reload_keep_cache 276expect_exit_value 0 277cache_dump -c ub.conf 278expect_exit_value 0 279fail_in_cache_dump "www.example.com.*10.20.30.40" 280fail_in_cache_dump "msg www.example.com. IN A" 281query www.example.com 282expect_answer "10.20.30.40" 283 284teststep "change rrset-cache-size and reload_keep_cache - should be empty" 285echo "server: rrset-cache-size: 2m" >> ub.conf 286control_command -c ub.conf reload_keep_cache 287expect_exit_value 0 288cache_dump -c ub.conf 289expect_exit_value 0 290fail_in_cache_dump "www.example.com.*10.20.30.40" 291fail_in_cache_dump "msg www.example.com. IN A" 292query www.example.com 293expect_answer "10.20.30.40" 294 295# See if this part of the test can be enabled, it needs threads for combined 296# output. 297have_threads="no" 298if grep "define HAVE_PTHREAD 1" $PRE/config.h; then have_threads="yes"; fi 299if grep "define HAVE_SOLARIS_THREADS 1" $PRE/config.h; then have_threads="yes"; fi 300if grep "define HAVE_WINDOWS_THREADS 1" $PRE/config.h; then have_threads="yes"; fi 301if test "$have_threads" = "yes"; then 302 303teststep "change num-threads and reload_keep_cache - should be empty" 304echo "server: num-threads: 2" >> ub.conf 305control_command -c ub.conf reload_keep_cache 306expect_exit_value 0 307cache_dump -c ub.conf 308expect_exit_value 0 309fail_in_cache_dump "www.example.com.*10.20.30.40" 310fail_in_cache_dump "msg www.example.com. IN A" 311query www.example.com 312expect_answer "10.20.30.40" 313 314teststep "change minimal-responses and reload_keep_cache - should not be empty" 315echo "server: minimal-responses: no" >> ub.conf 316control_command -c ub.conf reload_keep_cache 317expect_exit_value 0 318cache_dump -c ub.conf 319expect_exit_value 0 320expect_in_cache_dump "www.example.com.*10.20.30.40" 321expect_in_cache_dump "msg www.example.com. IN A" 322 323else 324 echo "" 325 echo "> skip test parts that need threads, have_threads=no" 326# end of check for have_threads 327fi 328 329teststep "now stop the server" 330control_command -c ub.conf stop 331expect_exit_value 0 332 333teststep "see if the server has really exited" 334TRY_MAX=20 335for (( try=0 ; try <= $TRY_MAX ; try++ )) ; do 336 if kill -0 $UNBOUND_PID 2>&1 | tee tmp.$$; then 337 echo "not stopped yet, waiting" 338 sleep 1 339 else 340 echo "stopped OK; break" 341 break; 342 fi 343 if grep "No such process" tmp.$$; then 344 echo "stopped OK; break" 345 break; 346 fi 347done 348if kill -0 $UNBOUND_PID; then 349 echo "still up!" 350 echo "not stopped, failure" 351 end 1 352else 353 echo "stopped OK" 354 355 if test -f ublocktrace.0; then 356 if $PRE/lock-verify ublocktrace.*; then 357 echo "lock-verify test worked." 358 else 359 echo "lock-verify test failed." 360 end 1 361 fi 362 fi 363fi 364 365end 0 366