1#!/bin/sh
2#
3# Copyright (C) 2004, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
4# Copyright (C) 2001  Internet Software Consortium.
5#
6# Permission to use, copy, modify, and/or distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16# PERFORMANCE OF THIS SOFTWARE.
17
18# Id: tests.sh,v 1.10 2011/09/01 05:28:14 marka Exp 
19
20SYSTEMTESTTOP=..
21. $SYSTEMTESTTOP/conf.sh
22
23status=0
24
25RNDCOPTS="-c ../common/rndc.conf -s 10.53.0.2 -p 9953"
26DIGOPTS="+nosea +nocomm +nocmd +noquest +noadd +noauth +nocomm \
27         +nostat @10.53.0.2 -p 5300"
28
29# fill the cache with nodes from flushtest.example zone
30load_cache () {
31        # empty all existing cache data
32        $RNDC $RNDCOPTS flush
33
34	# load the positive cache entries
35	$DIG $DIGOPTS -f - << EOF > /dev/null 2>&1
36txt top1.flushtest.example
37txt second1.top1.flushtest.example
38txt third1.second1.top1.flushtest.example
39txt third2.second1.top1.flushtest.example
40txt second2.top1.flushtest.example
41txt second3.top1.flushtest.example
42txt second1.top2.flushtest.example
43txt second2.top2.flushtest.example
44txt second3.top2.flushtest.example
45txt top3.flushtest.example
46txt second1.top3.flushtest.example
47txt third1.second1.top3.flushtest.example
48txt third2.second1.top3.flushtest.example
49txt third1.second2.top3.flushtest.example
50txt third2.second2.top3.flushtest.example
51txt second3.top3.flushtest.example
52EOF
53
54	# load the negative cache entries
55        # nxrrset:
56	$DIG $DIGOPTS a third1.second1.top1.flushtest.example > /dev/null
57        # nxdomain:
58	$DIG $DIGOPTS txt top4.flushtest.example > /dev/null
59        # empty nonterminal:
60	$DIG $DIGOPTS txt second2.top3.flushtest.example > /dev/null
61
62	# sleep 2 seconds ensure the TTLs will be lower on cached data
63	sleep 2
64}
65
66dump_cache () {
67        rm -f ns2/named_dump.db
68        $RNDC $RNDCOPTS dumpdb -cache
69        sleep 1
70}
71
72clear_cache () {
73        $RNDC $RNDCOPTS flush
74}
75
76in_cache () {
77        ttl=`$DIG $DIGOPTS "$@" | awk '{print $2}'`
78        [ -z "$ttl" ] && {
79                ttl=`$DIG $DIGOPTS +noanswer +auth "$@" | awk '{print $2}'`
80                [ "$ttl" -ge 3599 ] && return 1
81                return 0
82        }
83        [ "$ttl" -ge 3599 ] && return 1
84        return 0
85}
86
87echo "I:check correctness of routine cache cleaning"
88$DIG $DIGOPTS -f dig.batch > dig.out.ns2 || status=1
89grep ";" dig.out.ns2
90
91$PERL ../digcomp.pl dig.out.ns2 knowngood.dig.out || status=1
92
93echo "I:reset and check that records are correctly cached initially"
94ret=0
95load_cache
96dump_cache
97nrecords=`grep flushtest.example ns2/named_dump.db | grep -v '^;' | wc -l`
98[ $nrecords -eq 20 ] || ret=1
99if [ $ret != 0 ]; then echo "I:failed"; fi
100status=`expr $status + $ret`
101
102echo "I:check flushing of the full cache"
103ret=0
104clear_cache
105dump_cache
106nrecords=`grep flushtest.example ns2/named_dump.db | grep -v '^;' | wc -l`
107[ $nrecords -eq 0 ] || ret=1
108if [ $ret != 0 ]; then echo "I:failed"; fi
109status=`expr $status + $ret`
110
111echo "I:check flushing of individual nodes (interior node)"
112ret=0
113clear_cache
114load_cache
115# interior node
116in_cache txt top1.flushtest.example || ret=1
117$RNDC $RNDCOPTS flushname top1.flushtest.example
118in_cache txt top1.flushtest.example && ret=1
119if [ $ret != 0 ]; then echo "I:failed"; fi
120status=`expr $status + $ret`
121
122echo "I:check flushing of individual nodes (leaf node, under the interior node)"
123ret=0
124# leaf node, under the interior node (should still exist)
125in_cache txt third2.second1.top1.flushtest.example || ret=1
126$RNDC $RNDCOPTS flushname third2.second1.top1.flushtest.example
127in_cache txt third2.second1.top1.flushtest.example && ret=1
128if [ $ret != 0 ]; then echo "I:failed"; fi
129status=`expr $status + $ret`
130
131echo "I:check flushing of individual nodes (another leaf node, with both positive and negative cache entries)"
132ret=0
133# another leaf node, with both positive and negative cache entries
134in_cache a third1.second1.top1.flushtest.example || ret=1
135in_cache txt third1.second1.top1.flushtest.example || ret=1
136$RNDC $RNDCOPTS flushname third1.second1.top1.flushtest.example
137in_cache a third1.second1.top1.flushtest.example && ret=1
138in_cache txt third1.second1.top1.flushtest.example && ret=1
139if [ $ret != 0 ]; then echo "I:failed"; fi
140status=`expr $status + $ret`
141
142echo "I:check flushing a nonexistent name"
143ret=0
144$RNDC $RNDCOPTS flushname fake.flushtest.example || ret=1
145if [ $ret != 0 ]; then echo "I:failed"; fi
146status=`expr $status + $ret`
147
148echo "I:check flushing of namespaces"
149ret=0
150clear_cache
151load_cache
152# flushing leaf node should leave the interior node:
153in_cache txt third1.second1.top1.flushtest.example || ret=1
154in_cache txt top1.flushtest.example || ret=1
155$RNDC $RNDCOPTS flushtree third1.second1.top1.flushtest.example
156in_cache txt third1.second1.top1.flushtest.example && ret=1
157in_cache txt top1.flushtest.example || ret=1
158in_cache txt second1.top1.flushtest.example || ret=1
159in_cache txt third2.second1.top1.flushtest.example || ret=1
160$RNDC $RNDCOPTS flushtree second1.top1.flushtest.example
161in_cache txt top1.flushtest.example || ret=1
162in_cache txt second1.top1.flushtest.example && ret=1
163in_cache txt third2.second1.top1.flushtest.example && ret=1
164
165# flushing from an empty node should still remove all its children
166in_cache txt second1.top2.flushtest.example || ret=1
167$RNDC $RNDCOPTS flushtree top2.flushtest.example
168in_cache txt second1.top2.flushtest.example && ret=1
169in_cache txt second2.top2.flushtest.example && ret=1
170in_cache txt second3.top2.flushtest.example && ret=1
171if [ $ret != 0 ]; then echo "I:failed"; fi
172status=`expr $status + $ret`
173
174echo "I:check flushing a nonexistent namespace"
175ret=0
176$RNDC $RNDCOPTS flushtree fake.flushtest.example || ret=1
177if [ $ret != 0 ]; then echo "I:failed"; fi
178status=`expr $status + $ret`
179
180echo "I:check the number of cached records remaining"
181ret=0
182dump_cache
183nrecords=`grep flushtest.example ns2/named_dump.db | grep -v '^;' | wc -l`
184[ $nrecords -eq 19 ] || ret=1
185if [ $ret != 0 ]; then echo "I:failed"; fi
186status=`expr $status + $ret`
187
188echo "I:exit status: $status"
189exit $status
190