1#!/bin/sh
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14set -e
15
16#shellcheck source=conf.sh
17. ../conf.sh
18
19dig_with_opts() {
20  "$DIG" -p "${PORT}" "$@"
21}
22
23wait_for_pid() (
24  for pid in "$@"; do
25    kill -0 "$pid" 2>/dev/null && return 1
26  done
27  return 0
28)
29
30status=0
31n=0
32
33n=$((n + 1))
34echo_i "check lookups against TTL=0 records ($n)"
35i=0
36ret=0
37passes=10
38dig_with_opts @10.53.0.2 axfr example | grep -v "^ds0" \
39  | awk '$2 == "0" { print "-q", $1, $4; print "-q", "zzz"$1, $4;}' >query.list
40
41# add 1/5 second per query
42timeout=$(($(wc -l <query.list) / 5))
43while [ $i -lt $passes ]; do
44  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.1.test$n" 2>&1) &
45  pid1="$!"
46  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.2.test$n" 2>&1) &
47  pid2="$!"
48  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.3.test$n" 2>&1) &
49  pid3="$!"
50  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.4.test$n" 2>&1) &
51  pid4="$!"
52  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.5.test$n" 2>&1) &
53  pid5="$!"
54  (dig_with_opts -d +qr @10.53.0.3 -f query.list >"dig.out$i.6.test$n" 2>&1) &
55  pid6="$!"
56
57  retry_quiet "$timeout" wait_for_pid "$pid1" "$pid2" "$pid3" "$pid4" "$pid5" "$pid6" || {
58    echo_i "wait_for_pid failed"
59    ret=1
60  }
61  kill -TERM "$pid1" "$pid2" "$pid3" "$pid4" "$pid5" "$pid6" 2>/dev/null || true
62
63  wait "$pid1" || {
64    echo_i "wait $pid1 (dig.out$i.1.test$n) failed with $?"
65    ret=1
66  }
67  wait "$pid2" || {
68    echo_i "wait $pid2 (dig.out$i.2.test$n) failed with $?"
69    ret=1
70  }
71  wait "$pid3" || {
72    echo_i "wait $pid3 (dig.out$i.3.test$n) failed with $?"
73    ret=1
74  }
75  wait "$pid4" || {
76    echo_i "wait $pid4 (dig.out$i.4.test$n) failed with $?"
77    ret=1
78  }
79  wait "$pid5" || {
80    echo_i "wait $pid5 (dig.out$i.5.test$n) failed with $?"
81    ret=1
82  }
83  wait "$pid6" || {
84    echo_i "wait $pid6 (dig.out$i.6.test$n) failed with $?"
85    ret=1
86  }
87
88  grep "status: SERVFAIL" "dig.out$i.1.test$n" >/dev/null && ret=1
89  grep "status: SERVFAIL" "dig.out$i.2.test$n" >/dev/null && ret=1
90  grep "status: SERVFAIL" "dig.out$i.3.test$n" >/dev/null && ret=1
91  grep "status: SERVFAIL" "dig.out$i.4.test$n" >/dev/null && ret=1
92  grep "status: SERVFAIL" "dig.out$i.5.test$n" >/dev/null && ret=1
93  grep "status: SERVFAIL" "dig.out$i.6.test$n" >/dev/null && ret=1
94  [ $ret = 1 ] && break
95  i=$((i + 1))
96  echo_i "successfully completed pass $i of $passes"
97done
98if [ $ret != 0 ]; then echo_i "failed"; fi
99status=$((status + ret))
100
101repeat_query() (
102  i=0
103  while [ "$i" -lt "$1" ]; do
104    dig_with_opts +short "@$2" "$3" | tee "dig.out$i.test$n" || return 1
105    i=$((i + 1))
106  done
107)
108
109count_unique() (
110  repeat_query "$@" | sort -u | wc -l
111)
112
113n=$((n + 1))
114echo_i "check repeated recursive lookups of non recurring TTL=0 responses get new values ($n)"
115ret=0
116repeats=9
117count=$(count_unique "$repeats" 10.53.0.3 foo.increment)
118if [ "$count" -ne "$repeats" ]; then
119  echo_i "failed (count=$count, repeats=$repeats)"
120  ret=1
121fi
122status=$((status + ret))
123
124n=$((n + 1))
125echo_i "check lookups against TTL=1 records ($n)"
126i=0
127passes=10
128ret=0
129while [ $i -lt $passes ]; do
130  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.1.test$n" || ret=1
131  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.2.test$n" || ret=1
132  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.3.test$n" || ret=1
133  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.4.test$n" || ret=1
134  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.5.test$n" || ret=1
135  dig_with_opts @10.53.0.3 www.one.tld >"dig.out$i.6.test$n" || ret=1
136  grep "status: SERVFAIL" "dig.out$i.1.test$n" >/dev/null && ret=1
137  grep "status: SERVFAIL" "dig.out$i.2.test$n" >/dev/null && ret=1
138  grep "status: SERVFAIL" "dig.out$i.3.test$n" >/dev/null && ret=1
139  grep "status: SERVFAIL" "dig.out$i.4.test$n" >/dev/null && ret=1
140  grep "status: SERVFAIL" "dig.out$i.5.test$n" >/dev/null && ret=1
141  grep "status: SERVFAIL" "dig.out$i.6.test$n" >/dev/null && ret=1
142  [ $ret = 1 ] && break
143  i=$((i + 1))
144  echo_i "successfully completed pass $i of $passes"
145  sleep 1
146done
147if [ $ret != 0 ]; then echo_i "failed"; fi
148status=$((status + ret))
149
150echo_i "exit status: $status"
151[ "$status" -eq 0 ] || exit 1
152