summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/dnstap/tests.sh
blob: ab449227a85477c6205e6f0b40aa6d1d86b9ee3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#!/bin/sh

# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

SYSTEMTESTTOP=..
. $SYSTEMTESTTOP/conf.sh

DIGOPTS="+short -p ${PORT}"
RNDCCMD="$RNDC -p ${CONTROLPORT} -c ../common/rndc.conf"

status=0

# dnstap_data_ready <fstrm_capture_PID> <capture_file> <min_file_size>
# Flushes capture_file and checks wheter its size is >= min_file_size.
dnstap_data_ready() {
  # Process id of running fstrm_capture.
  fstrm_capture_pid=$1
  # Output file provided to fstrm_capture via -w switch.
  capture_file=$2
  # Minimum expected file size.
  min_size_expected=$3

  kill -HUP $fstrm_capture_pid
  file_size=$(wc -c <"$capture_file" | tr -d ' ')
  if [ $file_size -lt $min_size_expected ]; then
    return 1
  fi
}

for bad in bad-*.conf; do
  ret=0
  echo_i "checking that named-checkconf detects error in $bad"
  $CHECKCONF $bad >/dev/null 2>&1
  if [ $? != 1 ]; then
    echo_i "failed"
    ret=1
  fi
  status=$(expr $status + $ret)
done

for good in good-*.conf; do
  ret=0
  echo_i "checking that named-checkconf detects no error in $good"
  $CHECKCONF $good >/dev/null 2>&1
  if [ $? != 0 ]; then
    echo_i "failed"
    ret=1
  fi
  status=$(expr $status + $ret)
done

echo_i "wait for servers to finish loading"
ret=0
wait_for_log 20 "all zones loaded" ns1/named.run || ret=1
wait_for_log 20 "all zones loaded" ns2/named.run || ret=1
wait_for_log 20 "all zones loaded" ns3/named.run || ret=1
wait_for_log 20 "all zones loaded" ns4/named.run || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

# both the 'a.example/A' lookup and the './NS' lookup to ns1
# need tocomplete before reopening/rolling for the counts to
# be correct.

$DIG $DIGOPTS @10.53.0.3 a.example >dig.out
wait_for_log 20 "(./NS): query_reset" ns1/named.run || true

# check three different dnstap reopen/roll methods:
# ns1: dnstap-reopen; ns2: dnstap -reopen; ns3: dnstap -roll
mv ns1/dnstap.out ns1/dnstap.out.save
mv ns2/dnstap.out ns2/dnstap.out.save

if [ -n "$FSTRM_CAPTURE" ]; then
  ret=0
  echo_i "starting fstrm_capture"
  $FSTRM_CAPTURE -t protobuf:dnstap.Dnstap -u ns4/dnstap.out \
    -w dnstap.out >fstrm_capture.out.1 2>&1 &
  fstrm_capture_pid=$!
  wait_for_log 10 "socket path ns4/dnstap.out" fstrm_capture.out.1 || ret=1
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)
fi

$RNDCCMD -s 10.53.0.1 dnstap-reopen | sed 's/^/ns1 /' | cat_i
$RNDCCMD -s 10.53.0.2 dnstap -reopen | sed 's/^/ns2 /' | cat_i
$RNDCCMD -s 10.53.0.3 dnstap -roll | sed 's/^/ns3 /' | cat_i
$RNDCCMD -s 10.53.0.4 dnstap -reopen | sed 's/^/ns4 /' | cat_i

$DIG $DIGOPTS @10.53.0.3 a.example >dig.out

# send an UPDATE to ns2
$NSUPDATE <<-EOF
server 10.53.0.2 ${PORT}
zone example
update add b.example 3600 in a 10.10.10.10
send
EOF

# XXX: file output should be flushed once a second according
# to the libfstrm source, but it doesn't seem to happen until
# enough data has accumulated. to get all the output, we stop
# the name servers, forcing a flush on shutdown. it would be
# nice to find a better way to do this.
$RNDCCMD -s 10.53.0.1 stop | sed 's/^/ns1 /' | cat_i
$RNDCCMD -s 10.53.0.2 stop | sed 's/^/ns2 /' | cat_i
$RNDCCMD -s 10.53.0.3 stop | sed 's/^/ns3 /' | cat_i

sleep 1

echo_i "checking initial message counts"

udp1=$($DNSTAPREAD ns1/dnstap.out.save | grep "UDP " | wc -l)
tcp1=$($DNSTAPREAD ns1/dnstap.out.save | grep "TCP " | wc -l)
aq1=$($DNSTAPREAD ns1/dnstap.out.save | grep "AQ " | wc -l)
ar1=$($DNSTAPREAD ns1/dnstap.out.save | grep "AR " | wc -l)
cq1=$($DNSTAPREAD ns1/dnstap.out.save | grep "CQ " | wc -l)
cr1=$($DNSTAPREAD ns1/dnstap.out.save | grep "CR " | wc -l)
rq1=$($DNSTAPREAD ns1/dnstap.out.save | grep "RQ " | wc -l)
rr1=$($DNSTAPREAD ns1/dnstap.out.save | grep "RR " | wc -l)
uq1=$($DNSTAPREAD ns1/dnstap.out.save | grep "UQ " | wc -l)
ur1=$($DNSTAPREAD ns1/dnstap.out.save | grep "UR " | wc -l)

udp2=$($DNSTAPREAD ns2/dnstap.out.save | grep "UDP " | wc -l)
tcp2=$($DNSTAPREAD ns2/dnstap.out.save | grep "TCP " | wc -l)
aq2=$($DNSTAPREAD ns2/dnstap.out.save | grep "AQ " | wc -l)
ar2=$($DNSTAPREAD ns2/dnstap.out.save | grep "AR " | wc -l)
cq2=$($DNSTAPREAD ns2/dnstap.out.save | grep "CQ " | wc -l)
cr2=$($DNSTAPREAD ns2/dnstap.out.save | grep "CR " | wc -l)
rq2=$($DNSTAPREAD ns2/dnstap.out.save | grep "RQ " | wc -l)
rr2=$($DNSTAPREAD ns2/dnstap.out.save | grep "RR " | wc -l)
uq2=$($DNSTAPREAD ns2/dnstap.out.save | grep "UQ " | wc -l)
ur2=$($DNSTAPREAD ns2/dnstap.out.save | grep "UR " | wc -l)

mv ns3/dnstap.out.0 ns3/dnstap.out.save
udp3=$($DNSTAPREAD ns3/dnstap.out.save | grep "UDP " | wc -l)
tcp3=$($DNSTAPREAD ns3/dnstap.out.save | grep "TCP " | wc -l)
aq3=$($DNSTAPREAD ns3/dnstap.out.save | grep "AQ " | wc -l)
ar3=$($DNSTAPREAD ns3/dnstap.out.save | grep "AR " | wc -l)
cq3=$($DNSTAPREAD ns3/dnstap.out.save | grep "CQ " | wc -l)
cr3=$($DNSTAPREAD ns3/dnstap.out.save | grep "CR " | wc -l)
rq3=$($DNSTAPREAD ns3/dnstap.out.save | grep "RQ " | wc -l)
rr3=$($DNSTAPREAD ns3/dnstap.out.save | grep "RR " | wc -l)
uq3=$($DNSTAPREAD ns3/dnstap.out.save | grep "UQ " | wc -l)
ur3=$($DNSTAPREAD ns3/dnstap.out.save | grep "UR " | wc -l)

echo_i "checking UDP message counts"
ret=0
[ $udp1 -eq 0 ] || {
  echo_i "ns1 $udp1 expected 0"
  ret=1
}
[ $udp2 -eq 2 ] || {
  echo_i "ns2 $udp2 expected 2"
  ret=1
}
[ $udp3 -eq 4 ] || {
  echo_i "ns3 $udp3 expected 4"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking TCP message counts"
ret=0
[ $tcp1 -eq 6 ] || {
  echo_i "ns1 $tcp1 expected 6"
  ret=1
}
[ $tcp2 -eq 2 ] || {
  echo_i "ns2 $tcp2 expected 2"
  ret=1
}
[ $tcp3 -eq 6 ] || {
  echo_i "ns3 $tcp3 expected 6"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking AUTH_QUERY message counts"
ret=0
[ $aq1 -eq 3 ] || {
  echo_i "ns1 $aq1 exepcted 3"
  ret=1
}
[ $aq2 -eq 2 ] || {
  echo_i "ns2 $aq2 expected 2"
  ret=1
}
[ $aq3 -eq 1 ] || {
  echo_i "ns3 $aq3 expected 1"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking AUTH_RESPONSE message counts"
ret=0
[ $ar1 -eq 2 ] || {
  echo_i "ns1 $ar1 expected 2"
  ret=1
}
[ $ar2 -eq 1 ] || {
  echo_i "ns2 $ar2 expected 1"
  ret=1
}
[ $ar3 -eq 0 ] || {
  echo_i "ns3 $ar3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking CLIENT_QUERY message counts"
ret=0
[ $cq1 -eq 0 ] || {
  echo_i "ns1 $cq1 expected 0"
  ret=1
}
[ $cq2 -eq 0 ] || {
  echo_i "ns2 $cq2 expected 0"
  ret=1
}
[ $cq3 -eq 1 ] || {
  echo_i "ns3 $cq3 expected 1"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking CLIENT_RESPONSE message counts"
ret=0
[ $cr1 -eq 1 ] || {
  echo_i "ns1 $cr1 expected 1"
  ret=1
}
[ $cr2 -eq 1 ] || {
  echo_i "ns2 $cr2 expected 1"
  ret=1
}
[ $cr3 -eq 2 ] || {
  echo_i "ns3 $cr3 expected 2"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking RESOLVER_QUERY message counts"
ret=0
[ $rq1 -eq 0 ] || {
  echo_i "ns1 $rq1 expected 0"
  ret=1
}
[ $rq2 -eq 0 ] || {
  echo_i "ns2 $rq2 expected 0"
  ret=1
}
[ $rq3 -eq 3 ] || {
  echo_i "ns3 $rq3 expected 3"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking RESOLVER_RESPONSE message counts"
ret=0
[ $rr1 -eq 0 ] || {
  echo_i "ns1 $rr1 expected 0"
  ret=1
}
[ $rr2 -eq 0 ] || {
  echo_i "ns2 $rr2 expected 0"
  ret=1
}
[ $rr3 -eq 3 ] || {
  echo_i "ns3 $rr3 expected 3"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking UPDATE_QUERY message counts"
ret=0
[ $uq1 -eq 0 ] || {
  echo_i "ns1 $uq1 expected 0"
  ret=1
}
[ $uq2 -eq 0 ] || {
  echo_i "ns2 $uq2 expected 0"
  ret=1
}
[ $uq3 -eq 0 ] || {
  echo_i "ns3 $uq3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking UPDATE_RESPONSE message counts"
ret=0
[ $ur1 -eq 0 ] || {
  echo_i "ns1 $ur1 expected 0"
  ret=1
}
[ $ur2 -eq 0 ] || {
  echo_i "ns2 $ur2 expected 0"
  ret=1
}
[ $ur3 -eq 0 ] || {
  echo_i "ns3 $ur3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking reopened message counts"

udp1=$($DNSTAPREAD ns1/dnstap.out | grep "UDP " | wc -l)
tcp1=$($DNSTAPREAD ns1/dnstap.out | grep "TCP " | wc -l)
aq1=$($DNSTAPREAD ns1/dnstap.out | grep "AQ " | wc -l)
ar1=$($DNSTAPREAD ns1/dnstap.out | grep "AR " | wc -l)
cq1=$($DNSTAPREAD ns1/dnstap.out | grep "CQ " | wc -l)
cr1=$($DNSTAPREAD ns1/dnstap.out | grep "CR " | wc -l)
rq1=$($DNSTAPREAD ns1/dnstap.out | grep "RQ " | wc -l)
rr1=$($DNSTAPREAD ns1/dnstap.out | grep "RR " | wc -l)
uq1=$($DNSTAPREAD ns1/dnstap.out | grep "UQ " | wc -l)
ur1=$($DNSTAPREAD ns1/dnstap.out | grep "UR " | wc -l)

udp2=$($DNSTAPREAD ns2/dnstap.out | grep "UDP " | wc -l)
tcp2=$($DNSTAPREAD ns2/dnstap.out | grep "TCP " | wc -l)
aq2=$($DNSTAPREAD ns2/dnstap.out | grep "AQ " | wc -l)
ar2=$($DNSTAPREAD ns2/dnstap.out | grep "AR " | wc -l)
cq2=$($DNSTAPREAD ns2/dnstap.out | grep "CQ " | wc -l)
cr2=$($DNSTAPREAD ns2/dnstap.out | grep "CR " | wc -l)
rq2=$($DNSTAPREAD ns2/dnstap.out | grep "RQ " | wc -l)
rr2=$($DNSTAPREAD ns2/dnstap.out | grep "RR " | wc -l)
uq2=$($DNSTAPREAD ns2/dnstap.out | grep "UQ " | wc -l)
ur2=$($DNSTAPREAD ns2/dnstap.out | grep "UR " | wc -l)

udp3=$($DNSTAPREAD ns3/dnstap.out | grep "UDP " | wc -l)
tcp3=$($DNSTAPREAD ns3/dnstap.out | grep "TCP " | wc -l)
aq3=$($DNSTAPREAD ns3/dnstap.out | grep "AQ " | wc -l)
ar3=$($DNSTAPREAD ns3/dnstap.out | grep "AR " | wc -l)
cq3=$($DNSTAPREAD ns3/dnstap.out | grep "CQ " | wc -l)
cr3=$($DNSTAPREAD ns3/dnstap.out | grep "CR " | wc -l)
rq3=$($DNSTAPREAD ns3/dnstap.out | grep "RQ " | wc -l)
rr3=$($DNSTAPREAD ns3/dnstap.out | grep "RR " | wc -l)
uq3=$($DNSTAPREAD ns3/dnstap.out | grep "UQ " | wc -l)
ur3=$($DNSTAPREAD ns3/dnstap.out | grep "UR " | wc -l)

echo_i "checking UDP message counts"
ret=0
[ $udp1 -eq 0 ] || {
  echo_i "ns1 $udp1 expected 0"
  ret=1
}
[ $udp2 -eq 2 ] || {
  echo_i "ns2 $udp2 expected 2"
  ret=1
}
[ $udp3 -eq 2 ] || {
  echo_i "ns3 $udp3 expected 2"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking TCP message counts"
ret=0
[ $tcp1 -eq 0 ] || {
  echo_i "ns1 $tcp1 expected 0"
  ret=1
}
[ $tcp2 -eq 0 ] || {
  echo_i "ns2 $tcp2 expected 0"
  ret=1
}
[ $tcp3 -eq 0 ] || {
  echo_i "ns3 $tcp3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking AUTH_QUERY message counts"
ret=0
[ $aq1 -eq 0 ] || {
  echo_i "ns1 $aq1 exepcted 0"
  ret=1
}
[ $aq2 -eq 0 ] || {
  echo_i "ns2 $aq2 expected 0"
  ret=1
}
[ $aq3 -eq 0 ] || {
  echo_i "ns3 $aq3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking AUTH_RESPONSE message counts"
ret=0
[ $ar1 -eq 0 ] || {
  echo_i "ns1 $ar1 expected 0"
  ret=1
}
[ $ar2 -eq 0 ] || {
  echo_i "ns2 $ar2 expected 0"
  ret=1
}
[ $ar3 -eq 0 ] || {
  echo_i "ns3 $ar3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking CLIENT_QUERY message counts"
ret=0
[ $cq1 -eq 0 ] || {
  echo_i "ns1 $cq1 expected 0"
  ret=1
}
[ $cq2 -eq 0 ] || {
  echo_i "ns2 $cq2 expected 0"
  ret=1
}
[ $cq3 -eq 1 ] || {
  echo_i "ns3 $cq3 expected 1"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking CLIENT_RESPONSE message counts"
ret=0
[ $cr1 -eq 0 ] || {
  echo_i "ns1 $cr1 expected 0"
  ret=1
}
[ $cr2 -eq 0 ] || {
  echo_i "ns2 $cr2 expected 0"
  ret=1
}
[ $cr3 -eq 1 ] || {
  echo_i "ns3 $cr3 expected 1"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking RESOLVER_QUERY message counts"
ret=0
[ $rq1 -eq 0 ] || {
  echo_i "ns1 $rq1 expected 0"
  ret=1
}
[ $rq2 -eq 0 ] || {
  echo_i "ns2 $rq2 expected 0"
  ret=1
}
[ $rq3 -eq 0 ] || {
  echo_i "ns3 $rq3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking RESOLVER_RESPONSE message counts"
ret=0
[ $rr1 -eq 0 ] || {
  echo_i "ns1 $rr1 expected 0"
  ret=1
}
[ $rr2 -eq 0 ] || {
  echo_i "ns2 $rr2 expected 0"
  ret=1
}
[ $rr3 -eq 0 ] || {
  echo_i "ns3 $rr3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking UPDATE_QUERY message counts"
ret=0
[ $uq1 -eq 0 ] || {
  echo_i "ns1 $uq1 expected 0"
  ret=1
}
[ $uq2 -eq 1 ] || {
  echo_i "ns2 $uq2 expected 1"
  ret=1
}
[ $uq3 -eq 0 ] || {
  echo_i "ns3 $uq3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking UPDATE_RESPONSE message counts"
ret=0
[ $ur1 -eq 0 ] || {
  echo_i "ns1 $ur1 expected 0"
  ret=1
}
[ $ur2 -eq 1 ] || {
  echo_i "ns2 $ur2 expected 1"
  ret=1
}
[ $ur3 -eq 0 ] || {
  echo_i "ns3 $ur3 expected 0"
  ret=1
}
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

echo_i "checking whether destination UDP port is logged for client queries"
ret=0
$DNSTAPREAD ns3/dnstap.out.save | grep -Eq "CQ [0-9:.]+ -> 10.53.0.3:${PORT} UDP" || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

HAS_PYYAML=0
if [ -n "$PYTHON" ]; then
  $PYTHON -c "import yaml" 2>/dev/null && HAS_PYYAML=1
fi

if [ $HAS_PYYAML -ne 0 ]; then
  echo_i "checking dnstap-read YAML output"
  ret=0
  {
    $PYTHON ydump.py "$DNSTAPREAD" "ns3/dnstap.out.save" >ydump.out || ret=1
  } | cat_i
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)
fi

echo_i "checking dnstap-read hex output"
ret=0
hex=$($DNSTAPREAD -x ns3/dnstap.out | tail -1)
echo $hex | $WIRETEST >dnstap.hex
grep 'status: NOERROR' dnstap.hex >/dev/null 2>&1 || ret=1
grep 'ANSWER: 3, AUTHORITY: 1' dnstap.hex >/dev/null 2>&1 || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

if [ -n "$FSTRM_CAPTURE" ]; then
  $DIG $DIGOPTS @10.53.0.4 a.example >dig.out

  # send an UPDATE to ns4
  $NSUPDATE <<-EOF >nsupdate.out 2>&1
	server 10.53.0.4 ${PORT}
	zone example
	update add b.example 3600 in a 10.10.10.10
	send
EOF
  grep "update failed: NOTAUTH" nsupdate.out >/dev/null || ret=1

  echo_i "checking unix socket message counts"
  sleep 2
  retry_quiet 5 dnstap_data_ready $fstrm_capture_pid dnstap.out 450 || {
    echo_i "dnstap output file smaller than expected"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)
  kill $fstrm_capture_pid
  wait
  udp4=$($DNSTAPREAD dnstap.out | grep "UDP " | wc -l)
  tcp4=$($DNSTAPREAD dnstap.out | grep "TCP " | wc -l)
  aq4=$($DNSTAPREAD dnstap.out | grep "AQ " | wc -l)
  ar4=$($DNSTAPREAD dnstap.out | grep "AR " | wc -l)
  cq4=$($DNSTAPREAD dnstap.out | grep "CQ " | wc -l)
  cr4=$($DNSTAPREAD dnstap.out | grep "CR " | wc -l)
  rq4=$($DNSTAPREAD dnstap.out | grep "RQ " | wc -l)
  rr4=$($DNSTAPREAD dnstap.out | grep "RR " | wc -l)
  uq4=$($DNSTAPREAD dnstap.out | grep "UQ " | wc -l)
  ur4=$($DNSTAPREAD dnstap.out | grep "UR " | wc -l)

  echo_i "checking UDP message counts"
  ret=0
  [ $udp4 -eq 4 ] || {
    echo_i "ns4 $udp4 expected 4"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking TCP message counts"
  ret=0
  [ $tcp4 -eq 0 ] || {
    echo_i "ns4 $tcp4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking AUTH_QUERY message counts"
  ret=0
  [ $aq4 -eq 0 ] || {
    echo_i "ns4 $aq4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking AUTH_RESPONSE message counts"
  ret=0
  [ $ar4 -eq 0 ] || {
    echo_i "ns4 $ar4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking CLIENT_QUERY message counts"
  ret=0
  [ $cq4 -eq 1 ] || {
    echo_i "ns4 $cq4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking CLIENT_RESPONSE message counts"
  ret=0
  [ $cr4 -eq 1 ] || {
    echo_i "ns4 $cr4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking RESOLVER_QUERY message counts"
  ret=0
  [ $rq4 -eq 0 ] || {
    echo_i "ns4 $rq4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking RESOLVER_RESPONSE message counts"
  ret=0
  [ $rr4 -eq 0 ] || {
    echo_i "ns4 $rr4 expected 0"
    ret=1
  }

  echo_i "checking UPDATE_QUERY message counts"
  ret=0
  [ $uq4 -eq 1 ] || {
    echo_i "ns4 $uq4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking UPDATE_RESPONSE message counts"
  ret=0
  [ $ur4 -eq 1 ] || {
    echo_i "ns4 $ur4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  mv dnstap.out dnstap.out.save

  echo_i "restarting fstrm_capture"
  $FSTRM_CAPTURE -t protobuf:dnstap.Dnstap -u ns4/dnstap.out \
    -w dnstap.out >fstrm_capture.out.2 2>&1 &
  fstrm_capture_pid=$!
  wait_for_log 10 "socket path ns4/dnstap.out" fstrm_capture.out.2 || {
    echo_i "failed"
    ret=1
  }
  $RNDCCMD -s 10.53.0.4 dnstap -reopen | sed 's/^/ns4 /' | cat_i
  $DIG $DIGOPTS @10.53.0.4 a.example >dig.out

  echo_i "checking reopened unix socket message counts"
  sleep 2
  retry_quiet 5 dnstap_data_ready $fstrm_capture_pid dnstap.out 270 || {
    echo_i "dnstap output file smaller than expected"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$((status + ret))
  kill $fstrm_capture_pid
  wait
  udp4=$($DNSTAPREAD dnstap.out | grep "UDP " | wc -l)
  tcp4=$($DNSTAPREAD dnstap.out | grep "TCP " | wc -l)
  aq4=$($DNSTAPREAD dnstap.out | grep "AQ " | wc -l)
  ar4=$($DNSTAPREAD dnstap.out | grep "AR " | wc -l)
  cq4=$($DNSTAPREAD dnstap.out | grep "CQ " | wc -l)
  cr4=$($DNSTAPREAD dnstap.out | grep "CR " | wc -l)
  rq4=$($DNSTAPREAD dnstap.out | grep "RQ " | wc -l)
  rr4=$($DNSTAPREAD dnstap.out | grep "RR " | wc -l)
  uq4=$($DNSTAPREAD dnstap.out | grep "UQ " | wc -l)
  ur4=$($DNSTAPREAD dnstap.out | grep "UR " | wc -l)

  echo_i "checking UDP message counts"
  ret=0
  [ $udp4 -eq 2 ] || {
    echo_i "ns4 $udp4 expected 2"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking TCP message counts"
  ret=0
  [ $tcp4 -eq 0 ] || {
    echo_i "ns4 $tcp4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking AUTH_QUERY message counts"
  ret=0
  [ $aq4 -eq 0 ] || {
    echo_i "ns4 $aq4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking AUTH_RESPONSE message counts"
  ret=0
  [ $ar4 -eq 0 ] || {
    echo_i "ns4 $ar4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking CLIENT_QUERY message counts"
  ret=0
  [ $cq4 -eq 1 ] || {
    echo_i "ns4 $cq4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking CLIENT_RESPONSE message counts"
  ret=0
  [ $cr4 -eq 1 ] || {
    echo_i "ns4 $cr4 expected 1"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking RESOLVER_QUERY message counts"
  ret=0
  [ $rq4 -eq 0 ] || {
    echo_i "ns4 $rq4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking RESOLVER_RESPONSE message counts"
  ret=0
  [ $rr4 -eq 0 ] || {
    echo_i "ns4 $rr4 expected 0"
    ret=1
  }

  echo_i "checking UPDATE_QUERY message counts"
  ret=0
  [ $uq4 -eq 0 ] || {
    echo_i "ns4 $uq4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)

  echo_i "checking UPDATE_RESPONSE message counts"
  ret=0
  [ $ur4 -eq 0 ] || {
    echo_i "ns4 $ur4 expected 0"
    ret=1
  }
  if [ $ret != 0 ]; then echo_i "failed"; fi
  status=$(expr $status + $ret)
fi

echo_i "checking large packet printing"
ret=0
# Expect one occurrence of "opcode: QUERY" below "reponse_message_data" and
# another one below "response_message".
lines=$($DNSTAPREAD -y large-answer.fstrm | grep -c "opcode: QUERY")
[ $lines -eq 2 ] || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$(expr $status + $ret)

test_dnstap_roll() (
  ip="$1"
  ns="$2"
  n="$3"
  $RNDCCMD -s "${ip}" dnstap -roll "${n}" | sed "s/^/${ns} /" | cat_i \
    && files=$(find "$ns" -name "dnstap.out.[0-9]" | wc -l) \
    && test "$files" -le "${n}" && test "$files" -ge "1"
)

echo_i "checking 'rndc -roll <value>' (no versions)"
ret=0
start_server --noclean --restart --port "${PORT}" ns3
_repeat 5 test_dnstap_roll 10.53.0.3 ns3 3 || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$((status + ret))

echo_i "checking 'rndc -roll <value>' (versions)"
ret=0
start_server --noclean --restart --port "${PORT}" ns2
_repeat 5 test_dnstap_roll 10.53.0.2 ns2 3 || ret=1
if [ $ret != 0 ]; then echo_i "failed"; fi
status=$((status + ret))

echo_i "exit status: $status"
[ "$status" -eq 0 ] || exit 1