xref: /freebsd-head/contrib/unbound/testdata/09-unbound-control.tdir/09-unbound-control.test (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
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	exitstatus=$?
75	cat outfile
76	return $exitstatus
77}
78
79# Reload the server and check the reload has finished processing
80# when a lot of debug is enabled, a lot of log needs to be printed.
81control_reload () {
82	prelines=`wc -l unbound.log | awk '{print $1;}'`
83	cmd="$1"
84	if test -z "$cmd"; then cmd="reload"; fi
85	control_command -c ub.conf $cmd
86	expect_exit_value 0
87	# see if the reload has completed.
88	lines1=`wc -l unbound.log | awk '{print $1;}'`
89	count=0
90	lines2=`wc -l unbound.log | awk '{print $1;}'`
91	# See if the log finishes up without sleeping too long.
92	while test "$lines1" -ne "$lines2"; do
93		lines1=`wc -l unbound.log | awk '{print $1;}'`
94		# There is no sleep here. The add and compare are a
95		# brief wait.
96		count=`expr "$count" + 1`
97		if test "$count" -gt 30; then
98			break;
99		fi
100		lines2=`wc -l unbound.log | awk '{print $1;}'`
101	done
102	if test "$lines1" -ne "$lines2"; then
103		count=0
104		while test "$lines1" -ne "$lines2"; do
105			tail -1 unbound.log
106			lines1=`wc -l unbound.log | awk '{print $1;}'`
107			sleep 1
108			count=`expr "$count" + 1`
109			if test "$count" -gt 30; then
110				echo "reload is taking too long"
111				exit 1
112			fi
113			lines2=`wc -l unbound.log | awk '{print $1;}'`
114		done
115		if test "$count" -ne "0"; then
116			echo "reload done with $count sec"
117		fi
118	fi
119}
120
121# Reload the server for a clean state
122clean_reload () {
123	echo "> Reloading the server for a clean state"
124	cp main.conf ub.conf
125	control_reload
126}
127
128# Reload the server for a clean state and populate the cache
129clean_reload_and_fill_cache () {
130	clean_reload
131	echo "> Populating the cache"
132	query www.example.com
133	expect_answer "10.20.30.40"
134	if test "$have_threads" = "no"; then
135		# Try to get the answer in all processes' cache.
136		for (( try=0 ; try < num_threads * 2 * 2 ; try++ )) ; do
137			query www.example.com
138			expect_answer "10.20.30.40"
139		done
140	fi
141}
142
143# Dump the cache contents
144# $@: optional options to unbound-control
145cache_dump () {
146	echo "$PRE/unbound-control $@ dump_cache > cache.dump"
147	$PRE/unbound-control $@ dump_cache > cache.dump
148}
149
150# Load cache contents
151# $@: optional options to unbound-control
152cache_load () {
153	echo "$PRE/unbound-control $@ load_cache < cache.dump"
154	$PRE/unbound-control $@ load_cache < cache.dump
155}
156
157# Expect an entry in the cache dump
158# $1: expected regular expression
159expect_in_cache_dump () {
160	echo "> check cache dump for \"$1\""
161	if grep "$1" cache.dump; then
162		echo "OK cache dump"
163	else
164		echo "Not OK cache dump"
165		end 1
166	fi
167}
168
169# Fail the test for unexpected entry in the cache dump
170# $1: unexpected regular expression
171fail_in_cache_dump () {
172	echo "> \"$1\" should not be in cache dump"
173	if grep "$1" cache.dump; then
174		echo "Not OK cache dump"
175		end 1
176	else
177		echo "OK cache dump"
178	fi
179}
180
181# Check if multi-threading or multi-process environment
182have_threads="no"
183if grep "define HAVE_PTHREAD 1" $PRE/config.h; then have_threads="yes"; fi
184if grep "define HAVE_SOLARIS_THREADS 1" $PRE/config.h; then have_threads="yes"; fi
185if grep "define HAVE_WINDOWS_THREADS 1" $PRE/config.h; then have_threads="yes"; fi
186
187# start the test; keep the original conf file around
188cp ub.conf orig.conf
189
190
191# START - thread configuration
192# Do both single thread/process and multi thread/process runs.
193# The number of threads can only go up from the initial configuration between
194# reloads so starting with 1.
195for num_threads in 1 4; do
196
197cp orig.conf ub.conf
198echo "> setting num-threads: $num_threads"
199echo "server: num-threads: $num_threads" >> ub.conf
200cp ub.conf main.conf
201clean_reload
202
203
204teststep "exit value is 1 on usage"
205control_command -h
206expect_exit_value 1 "for usage"
207
208# use lock-verify if possible
209
210teststep "test if the server is up"
211query www.example.com.
212expect_answer "10.20.30.40"
213
214teststep "exit value is 1 when a bad command is given"
215control_command -c ub.conf blablargh
216expect_exit_value 1
217
218# reload the server. test if the server came up by putting a new
219# local-data element in the server.
220teststep "reload the server"
221echo "server: local-data: 'afterreload. IN A 5.6.7.8'" >> ub.conf
222control_reload
223query afterreload.
224expect_answer "5.6.7.8"
225
226teststep "must have had at least 1 query since reload"
227control_command -c ub.conf stats
228expect_exit_value 0
229expect_answer "^total.num.queries=[1-9][0-9]*$"
230
231teststep "check verbosity"
232control_command -c ub.conf verbosity 2
233expect_exit_value 0
234
235teststep "check syntax error in parse"
236control_command -c ub.conf verbosity jkdf
237expect_exit_value 1
238
239teststep "check bad credentials"
240cp ub.conf bad.conf
241cat conf.bad_credentials >> bad.conf
242control_command -c bad.conf verbosity 2
243expect_exit_value 1
244
245teststep "check spoofed client credentials"
246rm -f bad.conf
247cp ub.conf bad.conf
248cat conf.spoofed_credentials >> bad.conf
249control_command -c bad.conf verbosity 2
250expect_exit_value 1
251
252teststep "clean reload"
253clean_reload
254
255# The flush negative only works if the server is either on 1 thread,
256# or there is threading enabled. Multiple processes does not work for the
257# test, since the printout does not have the stats of a global cache.
258if test $num_threads -le 1 -o "$have_threads" = "yes"; then
259teststep "Check negative flushing"
260query always.empty.
261expect_answer "SERVFAIL"
262query always.empty. DNSKEY
263expect_answer "SERVFAIL"
264control_command -c ub.conf flush_negative
265expect_exit_value 0
266expect_answer "^ok removed .*, 3 messages and 1 key entries"
267control_command -c ub.conf flush_negative
268expect_exit_value 0
269expect_answer "^ok removed .*, 0 messages and 0 key entries"
270else
271	echo "> skip Check negative flushing, because no threads"
272fi
273
274teststep "create a new local zone"
275control_command -c ub.conf local_zone example.net static
276expect_exit_value 0
277control_command -c ub.conf local_data www.example.net A 192.0.2.1
278expect_exit_value 0
279
280teststep "check that www.example.net exists"
281query www.example.net.
282expect_answer "192.0.2.1"
283
284teststep "check that mail.example.net has nxdomain"
285query mail.example.net.
286expect_answer "NXDOMAIN"
287
288teststep "remove www.example.net - check it gets nxdomain"
289control_command -c ub.conf local_data_remove www.example.net
290expect_exit_value 0
291query www.example.net.
292expect_answer "NXDOMAIN"
293
294teststep "remove nonexistent name - check bug#287(segfault) does not happen"
295control_command -c ub.conf local_data_remove test.example.net
296# if crash then then we get: error: could not SSL_read from unbound-control
297expect_exit_value 0
298
299teststep "remove example.net - check its gone"
300control_command -c ub.conf local_zone_remove example.net
301expect_exit_value 0
302query www.example.net.
303expect_answer "SERVFAIL"
304
305teststep "load local-zones from file"
306control_command -c ub.conf local_zones < local_zones
307expect_exit_value 0
308query localzonefromfile
309expect_answer "REFUSED"
310if test "$have_threads" = "no"; then
311	# Try to see if a process other than the first one
312	# has updated data from stdin.
313	for (( try=0 ; try < num_threads * 2 ; try++ )) ; do
314		query localzonefromfile
315		expect_answer "REFUSED"
316	done
317fi
318
319teststep "load local-data from file"
320control_command -c ub.conf local_datas < local_data
321expect_exit_value 0
322query -t txt localdatafromfile
323expect_answer "local data from file OK"
324if test "$have_threads" = "no"; then
325	# Try to see if a process other than the first one
326	# has updated data from stdin.
327	for (( try=0 ; try < num_threads * 2 ; try++ )) ; do
328		query -t txt localdatafromfile
329		expect_answer "local data from file OK"
330	done
331fi
332
333teststep "load view-local-data from file"
334control_command -c ub.conf view_local_datas testview < view_local_data
335expect_exit_value 0
336control_command -c ub.conf view_list_local_zones testview
337query -t txt viewlocaldatafromfile
338expect_answer "view local data from file OK"
339if test "$have_threads" = "no"; then
340	# Try to see if a process other than the first one
341	# has updated data from stdin.
342	for (( try=0 ; try < num_threads * 2 ; try++ )) ; do
343		query -t txt viewlocaldatafromfile
344		expect_answer "view local data from file OK"
345	done
346fi
347
348teststep "remove local-zone, local-data and view-local-data from file"
349control_command -c ub.conf local_zones_remove < local_zones_remove
350expect_exit_value 0
351control_command -c ub.conf local_datas_remove < local_data_remove
352expect_exit_value 0
353control_command -c ub.conf view_local_datas_remove testview < view_local_data_remove
354expect_exit_value 0
355control_command -c ub.conf list_local_zones
356fail_answer "localzonefromfile"
357fail_answer "local data from file OK"
358expect_answer "otherlocalzone"
359control_command -c ub.conf view_list_local_data testview
360fail_answer "viewlocaldatafromfile"
361
362teststep "flushing"
363control_command -c ub.conf flush www.example.net
364expect_exit_value 0
365control_command -c ub.conf flush_type www.example.net TXT
366expect_exit_value 0
367control_command -c ub.conf flush_zone example.net
368expect_exit_value 0
369
370# START - single thread/process tests only
371if test $num_threads -le 1; then
372
373clean_reload_and_fill_cache
374
375teststep "dump the cache"
376query www.example.com.
377cache_dump -c ub.conf
378expect_exit_value 0
379cat cache.dump
380expect_in_cache_dump "10.20.30.40"
381
382control_command -c ub.conf lookup www.example.com
383expect_exit_value 0
384# answer to lookup is meaningless because of use a forwarder, oh well.
385
386teststep "load the cache dump"
387cache_load -c ub.conf
388expect_exit_value 0
389query www.example.com. +nordflag
390expect_answer "10.20.30.40"
391
392else
393	echo ""
394	echo "> skip test parts that need single thread/process"
395fi
396# END - single thread/process tests only
397
398clean_reload_and_fill_cache
399
400teststep "reload and check cache - should be empty"
401control_reload
402query www.example.com +nordflag
403fail_answer "10.20.30.40"
404
405clean_reload_and_fill_cache
406
407teststep "reload_keep_cache and check cache - should not be empty"
408control_reload reload_keep_cache
409query www.example.com +nordflag
410expect_answer "10.20.30.40"
411
412clean_reload_and_fill_cache
413
414teststep "change msg-cache-size and reload_keep_cache - should be empty"
415echo "server: msg-cache-size: 2m" >> ub.conf
416control_reload reload_keep_cache
417query www.example.com +nordflag
418fail_answer "10.20.30.40"
419
420clean_reload_and_fill_cache
421
422teststep "change rrset-cache-size and reload_keep_cache - should be empty"
423echo "server: rrset-cache-size: 2m" >> ub.conf
424control_reload reload_keep_cache
425query www.example.com +nordflag
426fail_answer "10.20.30.40"
427
428# START - have_threads tests
429# This part of the test needs threads for combined output.
430if test "$have_threads" = "yes"; then
431
432clean_reload_and_fill_cache
433
434teststep "change num-threads and reload_keep_cache - should be empty"
435echo "server: num-threads: 2" >> ub.conf
436control_reload reload_keep_cache
437query www.example.com +nordflag
438fail_answer "10.20.30.40"
439
440clean_reload_and_fill_cache
441
442teststep "change minimal-responses and reload_keep_cache - should not be empty"
443echo "server: minimal-responses: no" >> ub.conf
444control_reload reload_keep_cache
445query www.example.com +nordflag
446expect_answer "10.20.30.40"
447
448else
449	echo ""
450	echo "> skip test parts that need threads, have_threads=no"
451fi
452# END - have_threads tests
453
454done
455# END - thread configuration
456
457teststep "now stop the server"
458control_command -c ub.conf stop
459expect_exit_value 0
460
461teststep "see if the server has really exited"
462TRY_MAX=20
463for (( try=0 ; try <= $TRY_MAX ; try++ )) ; do
464	if kill -0 $UNBOUND_PID 2>&1 | tee tmp.$$; then
465		echo "not stopped yet, waiting"
466		sleep 1
467	else
468		echo "stopped OK; break"
469		break;
470	fi
471	if grep "No such process" tmp.$$; then
472		echo "stopped OK; break"
473		break;
474	fi
475done
476if kill -0 $UNBOUND_PID; then
477	echo "still up!"
478	echo "not stopped, failure"
479	end 1
480else
481	echo "stopped OK"
482
483        if test -f ublocktrace.0; then
484		if $PRE/lock-verify ublocktrace.*; then
485			echo "lock-verify test worked."
486		else
487			echo "lock-verify test failed."
488			end 1
489		fi
490	fi
491fi
492
493end 0
494