#!/bin/sh # unbound-sec test suite. # # Runs fully inside Docker: this script is executed in a docker:cli container # with the Docker socket mounted; every assertion targets containers. # # Usage: # ./test/run.sh (host wrapper; recommended) # or directly in a docker:cli container with the socket + repo mounted # at the same absolute path as on the host (REPO_PATH set). set -u IMAGE="unbound-sec:test" C1="ubsec-t1" # main hardened container C2="ubsec-t2" # valid drop-in container C3="ubsec-t3" # broken drop-in container C4="ubsec-t4" # identity control container C5="ubsec-t5" # anchor persistence container C6="ubsec-t6" # negative-test: valid zone + trust anchor (expect AD) C7="ubsec-t7" # negative-test: broken signature + trust anchor (expect SERVFAIL) C8="ubsec-t8" # negative-test: broken zone, validator off (counter-test, expect NOERROR) RESP="ubsec-t6-resp" # static DNS responder, VALID zone (port 5353) RESP_BROKEN="ubsec-t7-resp" # static DNS responder, BROKEN zone (port 5354) NET="ubsec-testnet" # The compose template is tested under a DEDICATED project name so the test # never touches a production deployment of the same template on this daemon # (its own container, network, and volume unbound-sec-test_unbound_anchor). PROJ_TEST="unbound-sec-test" C6_CONT="${PROJ_TEST}-unbound-1" # compose auto-name for the test project # Fixtures must use HOST paths (bind mounts are resolved by the daemon). # run.sh sets REPO_PATH to the host repo path and pre-creates the fixtures. CFG1="${REPO_PATH:-$(pwd)}/test/fixture-config-ok" CFG2="${REPO_PATH:-$(pwd)}/test/fixture-config-bad" ANCHOR_DIR="${REPO_PATH:-$(pwd)}/test/fixture-anchor" PASS=0 FAIL=0 NOTVERIFIED=0 # checks that could not be run (infrastructure, e.g. timeout) say() { printf '%s\n' "$*"; } ok() { PASS=$((PASS+1)); say " [PASS] $1"; } bad() { FAIL=$((FAIL+1)); say " [FAIL] $1"; } # A security probe that could not be executed (timeout / network) — NOT a # content finding, but must be visible in the result. nv() { NOTVERIFIED=$((NOTVERIFIED+1)); say " [NOT VERIFIED] $1"; } # Resolve a container's IP on $NET so probes can address it directly # (avoids depending on Docker's embedded DNS inside the probe container). ip_of() { docker inspect -f '{{(index .NetworkSettings.Networks "'"$NET"').IPAddress}}' "$1" 2>/dev/null; } # Test-only compose invocation (dedicated project + test-image override; # see test/compose.test.yml). A function (not an array) for POSIX sh. compose_test() { (cd "${REPO_PATH:-$(pwd)}" && docker compose -p "$PROJ_TEST" \ -f docker-compose.yml -f test/compose.test.yml "$@") } cleanup() { # Remove ONLY resources this test run created. The fixed container name # "unbound" is gone from the template, so we no longer rm a name that a # production deployment might use. docker rm -f "$C1" "$C2" "$C3" "$C4" "$C5" "$C6" "$C7" "$C8" "$RESP" "$RESP_BROKEN" >/dev/null 2>&1 compose_test down --remove-orphans --volumes >/dev/null 2>&1 docker network rm "$NET" >/dev/null 2>&1 docker network rm "ubsec-negnet" >/dev/null 2>&1 rm -rf "$(pwd)/test/fixture-identity" 2>/dev/null rm -rf "$(pwd)/test/fixture-broken-valid" 2>/dev/null rm -rf "$(pwd)/test/fixture-broken-broken" 2>/dev/null rm -rf "$(pwd)/test/fixture-broken-noval" 2>/dev/null rm -f "$(pwd)/test/.broken-valid.bin" 2>/dev/null rm -f "$(pwd)/test/.broken-broken.bin" 2>/dev/null rm -f "$(pwd)/test/.broken-dnskey.bin" 2>/dev/null rm -f "$(pwd)/test/.broken-anchor.key" 2>/dev/null rm -rf "$ANCHOR_DIR" 2>/dev/null rm -f "$(pwd)/test/build.log" 2>/dev/null } trap cleanup EXIT say "== 0. cleanup old artifacts ==" cleanup say "== 1. build image ==" BUILDLOG="$(pwd)/test/build.log" # CACHEBUST forces the src stages (SHA256 + GPG verification) to re-run on # every test run so the build log always contains fresh evidence; the slow # compile stages stay cached. if docker build --build-arg CACHEBUST="$(date +%s)" -t "$IMAGE" . >"$BUILDLOG" 2>&1; then ok "docker build" # NOTE: we deliberately do NOT tag unbound-sec:latest here. The compose # template is exercised through test/compose.test.yml, which pins the test # image — so the production tag is left untouched. else bad "docker build" tail -15 "$BUILDLOG" say "ABORT: build failed"; exit 1 fi say "== 2. start hardened container ==" docker network create "$NET" >/dev/null docker run -d --name "$C1" --network "$NET" \ -p 5053:5053/udp -p 5053:5053/tcp \ --read-only \ --tmpfs /tmp --tmpfs "/run/unbound:uid=1500,gid=1500" \ --tmpfs "/var/unbound:uid=1500,gid=1500" \ --security-opt no-new-privileges:true \ --cap-drop ALL \ "$IMAGE" >/dev/null sleep 3 if [ "$(docker inspect -f '{{.State.Running}}' "$C1" 2>/dev/null)" = "true" ]; then ok "container running" else bad "container running"; docker logs "$C1" 2>&1 | tail -5 fi say "== 3. healthcheck ==" healthy="no" i=0 # WSL/9p container startup can be slow; give it up to 120s. while [ $i -lt 20 ]; do h="$(docker inspect -f '{{.State.Health.Status}}' "$C1" 2>/dev/null)" [ "$h" = "healthy" ] && healthy="yes" && break sleep 6 i=$((i+1)) done if [ "$healthy" = "yes" ]; then ok "healthcheck healthy" else bad "healthcheck healthy (status=$h)" fi say "== 4. version ==" v="$(docker exec "$C1" unbound -V 2>/dev/null | head -1)" case "$v" in *1.26.2*) ok "unbound version 1.26.2 ($v)";; *) bad "unbound version 1.26.2 (got: $v)";; esac say "== 5. DNS resolution ==" if docker exec "$C1" drill -p 5053 example.com @127.0.0.1 2>/dev/null | grep -q "example.com"; then ok "drill resolves example.com" else bad "drill resolves example.com" fi say "== 6. DNSSEC (correct EDNS-DO query) ==" # Primary check: drill -D sets the EDNS DO bit; a validating resolver must # answer with the AD (authenticated data) bit set. Loopback via docker exec # is reliable (no external overlay-network dependency). ad_out="$(docker exec "$C1" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)" if printf '%s' "$ad_out" | grep -q "rcode: NOERROR" \ && printf '%s' "$ad_out" | grep -qE 'flags:.*\bad\b' \ && printf '%s' "$ad_out" | grep -q "example.com"; then ok "drill -D: signed zone answers with AD bit (EDNS DO)" else bad "drill -D: signed zone answers with AD bit (EDNS DO)" printf '%s\n' "$ad_out" | sed 's/^/ /' fi # Secondary check: raw EDNS-0 DO probe from an external container (also # verifies the no-DO -> no-AD behavior). The probe distinguishes content # deviations (exit 1 -> FAIL) from infrastructure timeouts (exit 2 -> NOT # VERIFIED). The loopback check above is the deterministic primary proof. probe_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/dns-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 2>&1)" probe_rc=$? case $probe_rc in 0) ok "external EDNS-DO probe (AD=1 with DO, AD=0 without)";; 1) bad "external EDNS-DO probe: CONTENT deviation" printf '%s\n' "$probe_out" | sed 's/^/ /';; 2) nv "external EDNS-DO probe (timeout; loopback check is the proof)" printf '%s\n' "$probe_out" | sed 's/^/ /';; *) bad "external EDNS-DO probe: execution error (exit $probe_rc)" printf '%s\n' "$probe_out" | sed 's/^/ /';; esac say "== 7. non-root user ==" uid="$(docker exec "$C1" sh -c 'id -u' 2>/dev/null)" if [ "$uid" = "1500" ]; then ok "runs as uid 1500" else bad "runs as uid 1500 (got: $uid)" fi say "== 8. capabilities ==" capeff="$(docker exec "$C1" sh -c 'grep ^CapEff /proc/self/status' 2>/dev/null | awk '{print $2}')" if [ "$capeff" = "0000000000000000" ]; then ok "CapEff = 0" else bad "CapEff = 0 (got: $capeff)" fi say "== 9. no SUID binaries ==" suid="$(docker exec "$C1" sh -c 'find / -perm -4000 -type f 2>/dev/null' 2>/dev/null)" if [ -z "$suid" ]; then ok "no SUID-root binaries" else bad "no SUID-root binaries (found: $suid)" fi say "== 10. read-only root filesystem ==" if docker exec "$C1" sh -c 'touch /usr/writetest 2>/dev/null'; then bad "root fs is read-only (write to /usr succeeded)" else ok "root fs is read-only" fi if docker exec "$C1" sh -c 'touch /run/unbound/writetest && rm /run/unbound/writetest' 2>/dev/null; then ok "/run/unbound writable (tmpfs)" else bad "/run/unbound writable (tmpfs)" fi say "== 11. no-new-privileges + seccomp ==" nnp="$(docker exec "$C1" sh -c 'grep ^NoNewPrivs /proc/self/status' 2>/dev/null | awk '{print $2}')" if [ "$nnp" = "1" ]; then ok "NoNewPrivs = 1" else bad "NoNewPrivs = 1 (got: $nnp)" fi sc="$(docker exec "$C1" sh -c 'grep ^Seccomp: /proc/self/status' 2>/dev/null | awk '{print $2}')" if [ "$sc" = "2" ]; then ok "seccomp filter active" else bad "seccomp filter active (got: $sc)" fi say "== 12. hide-identity / hide-version (CHAOS) ==" # CHAOS id.server/version.server probes. The Python probe distinguishes # content deviations (exit 1 -> FAIL, e.g. an identity string leaked) from # infrastructure timeouts (exit 2 -> NOT VERIFIED). A leaked identity is a # real finding and MUST fail the run; a network hiccup is visible but not a # failure. The config value is asserted separately below (deterministic). id_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C1")" 5053 id.server. empty 2>&1)" id_rc=$? case $id_rc in 0) ok "hide-identity: id.server. answer is empty";; 1) bad "hide-identity: id.server. answer is empty (CONTENT deviation)" printf '%s\n' "$id_out" | sed 's/^/ /';; 2) nv "hide-identity CHAOS probe (timeout; config asserted below)" printf '%s\n' "$id_out" | sed 's/^/ /';; *) bad "hide-identity CHAOS probe: execution error (exit $id_rc)" printf '%s\n' "$id_out" | sed 's/^/ /';; esac # Config-level assertion (deterministic): the base config must set it. if docker exec "$C1" grep -q "hide-identity: yes" /etc/unbound/unbound.conf 2>/dev/null; then ok "config: hide-identity: yes in base config" else bad "config: hide-identity: yes in base config" fi # Control: same image with hide-identity switched off must reveal the identity. CFG_ID="${REPO_PATH:-$(pwd)}/test/fixture-identity" mkdir -p "$CFG_ID" printf 'server:\n hide-identity: no\n' > "$CFG_ID/00-identity.conf" docker run -d --name "$C4" --network "$NET" -v "$CFG_ID:/config:ro" "$IMAGE" >/dev/null sleep 3 id2_out="$(docker run --rm --network "$NET" -v "$(pwd)/test/chaos-check.py:/probe.py:ro" python:alpine python /probe.py "$(ip_of "$C4")" 5053 id.server. unbound-sec 2>&1)" id2_rc=$? case $id2_rc in 0) ok "identity control: hide-identity:no reveals identity";; 1) bad "identity control: hide-identity:no reveals identity (CONTENT deviation)" printf '%s\n' "$id2_out" | sed 's/^/ /';; 2) nv "identity-control CHAOS probe (timeout)" printf '%s\n' "$id2_out" | sed 's/^/ /';; *) bad "identity-control CHAOS probe: execution error (exit $id2_rc)" printf '%s\n' "$id2_out" | sed 's/^/ /';; esac say "== 13. drop-in config include ==" # Fixtures are created on the host by test/run.sh (host paths required). if [ ! -f "$CFG1/99-test.conf" ] || [ ! -f "$CFG2/99-broken.conf" ]; then bad "test fixtures missing (run via test/run.sh)" fi docker run -d --name "$C2" --network "$NET" -v "$CFG1:/config:ro" "$IMAGE" >/dev/null sleep 3 if [ "$(docker inspect -f '{{.State.Running}}' "$C2" 2>/dev/null)" = "true" ]; then ok "valid drop-in config accepted (include works)" else bad "valid drop-in config accepted" fi docker run -d --name "$C3" --network "$NET" -v "$CFG2:/config:ro" "$IMAGE" >/dev/null sleep 3 if [ "$(docker inspect -f '{{.State.Running}}' "$C3" 2>/dev/null)" = "false" ]; then ok "broken drop-in config rejects start (checkconf)" else bad "broken drop-in config rejects start" fi say "== 14. package attack surface ==" has_curl="$(docker exec "$C1" sh -c 'test -x /usr/bin/curl && echo yes' 2>/dev/null)" if [ "$has_curl" = "yes" ]; then bad "curl not in runtime image" else ok "no curl in runtime image" fi has_shadow="$(docker exec "$C1" sh -c 'test -x /usr/bin/chsh && echo yes' 2>/dev/null)" if [ "$has_shadow" = "yes" ]; then bad "shadow package not removed" else ok "shadow package removed" fi say "== 15. supply chain: GPG + SHA256 of source tarballs ==" # The build echoes "GPG-VALIDSIG signer=" after each successful # signature verification (the signer fingerprint is extracted from GnuPG's # machine-readable --status-fd output and must equal the pinned fingerprint). sigs="$(grep -c "GPG-VALIDSIG" "$BUILDLOG" 2>/dev/null)" if [ "$sigs" -ge 2 ]; then ok "GPG: signer bound via VALIDSIG x$sigs (unbound + ldns tarballs)" else bad "GPG: signer bound via VALIDSIG (found: $sigs, need >= 2)" fi if grep -E "unbound\.tar\.gz: OK|ldns\.tar\.gz: OK" "$BUILDLOG" >/dev/null 2>&1; then ok "SHA256: tarball checksums OK" else bad "SHA256: tarball checksums OK" fi say "== 16. binary hardening: PIE ==" # ELF e_type: 3 = ET_DYN (PIE/shared). Read from the final binary itself, # so this works even on cached builds. etype="$(docker exec "$C1" sh -c 'od -An -N32 -td2 /usr/sbin/unbound' 2>/dev/null | awk 'NR==2{print $1}')" if [ "$etype" = "3" ]; then ok "unbound binary is PIE (ELF e_type=ET_DYN)" else bad "unbound binary is PIE (ELF e_type=$etype, expected 3)" fi say "== 17. features compiled in (configure line) ==" confline="$(docker exec "$C1" unbound -V 2>/dev/null | grep -i 'configure line')" for flag in --enable-subnet --enable-tfo-server --enable-tfo-client --enable-pie --enable-relro-now; do if printf '%s' "$confline" | grep -q -- "$flag"; then ok "compiled with $flag" else bad "compiled with $flag" fi done say "== 18. RFC5011 anchor: initialized once, not reset on restart ==" # Deterministic entrypoint test (unbound stubbed so it cannot rewrite the # file): the entrypoint must (a) initialize /var/unbound/root.key from the # package anchor when it is missing, and (b) NOT overwrite an existing file. # The old bug (unconditional `cp -f`) reset learned RFC5011 state on every # start. STUB='mkdir -p /tmp/stub && printf "#!/bin/sh\nexit 0\n" > /tmp/stub/unbound && chmod +x /tmp/stub/unbound && PATH=/tmp/stub:$PATH sh /entrypoint.sh' rm -rf "$ANCHOR_DIR" mkdir -p "$ANCHOR_DIR" chmod 777 "$ANCHOR_DIR" h_pkg="$(docker run --rm --entrypoint cat "$IMAGE" /usr/share/dnssec-root/trusted-key.key 2>/dev/null | sha256sum | cut -d' ' -f1)" # (a) missing -> initialized from the package anchor docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1 h_init="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)" if [ -n "$h_init" ] && [ "$h_init" = "$h_pkg" ]; then ok "entrypoint initializes anchor from package when missing" else bad "entrypoint initializes anchor from package when missing (init=$h_init pkg=$h_pkg)" fi # (b) existing -> kept, not reset (RFC5011 state must survive) printf '\n; sentinel: entrypoint must not overwrite this file\n' >> "$ANCHOR_DIR/root.key" h_sentinel="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)" docker run --rm --entrypoint sh -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" -c "$STUB" >/dev/null 2>&1 h_now="$(sha256sum "$ANCHOR_DIR/root.key" 2>/dev/null | cut -d' ' -f1)" if [ "$h_now" = "$h_sentinel" ]; then ok "entrypoint keeps existing anchor (no cp -f reset)" else bad "entrypoint keeps existing anchor (now=$h_now sentinel=$h_sentinel pkg=$h_pkg)" fi # Integration: real unbound, DNSSEC validates, RFC5011 state persisted to # the file, and still validating after a restart. rm -rf "$ANCHOR_DIR"; mkdir -p "$ANCHOR_DIR"; chmod 777 "$ANCHOR_DIR" docker run -d --name "$C5" --network "$NET" \ -v "$ANCHOR_DIR:/var/unbound" "$IMAGE" >/dev/null ready="no"; i=0 while [ $i -lt 10 ]; do if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then ready="yes"; break fi sleep 2; i=$((i+1)) done [ "$ready" = "yes" ] || bad "resolver ready (initial)" # unbound writes its RFC5011 state into the anchor file (autotrust format) sleep 3 if grep -q "autotrust trust anchor file" "$ANCHOR_DIR/root.key" 2>/dev/null; then ok "unbound persists RFC5011 state into anchor file" else bad "unbound persists RFC5011 state into anchor file" fi p1_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)" if printf '%s' "$p1_out" | grep -q "rcode: NOERROR" \ && printf '%s' "$p1_out" | grep -qE 'flags:.*\bad\b'; then ok "DNSSEC AD=1 (initial, drill -D loopback)" else bad "DNSSEC AD=1 (initial, drill -D loopback)" printf '%s\n' "$p1_out" | sed 's/^/ /' fi docker restart "$C5" >/dev/null 2>&1 ready="no"; i=0 while [ $i -lt 10 ]; do if docker exec "$C5" drill -p 5053 unbound.net @127.0.0.1 >/dev/null 2>&1; then ready="yes"; break fi sleep 2; i=$((i+1)) done [ "$ready" = "yes" ] || bad "resolver ready after restart" if [ -f "$ANCHOR_DIR/root.key" ]; then ok "anchor file persists after restart" else bad "anchor file persists after restart" fi p2_out="$(docker exec "$C5" drill -p 5053 -D example.com @127.0.0.1 2>/dev/null)" if printf '%s' "$p2_out" | grep -q "rcode: NOERROR" \ && printf '%s' "$p2_out" | grep -qE 'flags:.*\bad\b'; then ok "DNSSEC AD=1 after restart (drill -D loopback)" else bad "DNSSEC AD=1 after restart (drill -D loopback)" printf '%s\n' "$p2_out" | sed 's/^/ /' fi say "== 19. compose template starts (no recursive include) ==" # Regression test for the audit P1 finding: the template must start cleanly # with the ACTUAL docker-compose.yml (old version mounted ./config over # itself -> recursive include -> start aborted). # # It runs under the dedicated test project (PROJ_TEST) with the test-image # override, so it creates only its own container/network/volume and never # touches a production deployment of the template. if compose_test up -d >/dev/null 2>&1; then hc="no" i=0 while [ $i -lt 20 ]; do h="$(docker inspect -f '{{.State.Health.Status}}' "$C6_CONT" 2>/dev/null)" [ "$h" = "healthy" ] && hc="yes" && break sleep 6 i=$((i+1)) done if [ "$hc" = "yes" ]; then ok "compose template starts + healthy (no ports, conf.d + anchor volume)" else bad "compose template starts + healthy (status=$h)" docker logs "$C6_CONT" 2>&1 | tail -5 | sed 's/^/ /' fi else bad "compose template starts" compose_test up -d 2>&1 | tail -5 | sed 's/^/ /' fi compose_test down --remove-orphans --volumes >/dev/null 2>&1 say "== 20. DNSSEC negative test: invalid signature -> SERVFAIL ==" # Controlled zone "broken.example" with an EXPLICIT trust anchor (the zone # DNSKEY), so validation is well-defined (RFC 4035 5.2 — no parent DS needed): # C6 valid : A + RRSIG (valid) -> NOERROR + AD # C7 broken : A (data modified) + RRSIG -> SERVFAIL (signature rejected) # C8 noval : broken zone, validator off -> NOERROR (resolvable, counter-test) # The counter-test (C8) separates SIGNATURE REJECTION from a generic # resolution failure: the broken answer resolves fine with validation off, # yet its signature is rejected with validation on. A static responder that # merely failed to serve the DNSKEY would not produce this pattern. # # RESP/RESP_BROKEN and C6/C7/C8 get their OWN network (not $NET), so they # can't be affected by the other test containers and the test stays # deterministic. REPO="${REPO_PATH:-$(pwd)}" NEG_NET="ubsec-negnet" docker network create "$NEG_NET" >/dev/null 2>&1 BZDIR="$(pwd)/test" # Remove stale zone files first: a DIRECTORY left there by a previous mount of # a missing file would make the generator fail with IsADirectoryError. rm -f "$BZDIR/.broken-valid.bin" "$BZDIR/.broken-broken.bin" \ "$BZDIR/.broken-dnskey.bin" "$BZDIR/.broken-anchor.key" docker run --rm -v "$REPO/test/make-broken-zone.py:/m.py:ro" -v "$BZDIR:/o" \ python:alpine sh -c "pip install -q dnspython cryptography 2>/dev/null; python /m.py /o" >/dev/null 2>&1 if [ ! -f "$BZDIR/.broken-valid.bin" ] || [ ! -f "$BZDIR/.broken-broken.bin" ] \ || [ ! -f "$BZDIR/.broken-dnskey.bin" ] || [ ! -f "$BZDIR/.broken-anchor.key" ]; then bad "generate broken.example zone packets" else ok "generate broken.example zone packets (valid + broken + anchor)" fi # Two static responders: VALID (5353) and BROKEN (5354). Both serve the same # DNSKEY packet (for trust-anchor priming); only the A packet differs. docker run -d --name "$RESP" --network "$NEG_NET" \ -v "$BZDIR/.broken-valid.bin:/a.bin:ro" -v "$BZDIR/.broken-dnskey.bin:/k.bin:ro" \ -v "$REPO/test/broken-zone-responder.py:/r.py:ro" \ python:alpine python /r.py /a.bin /k.bin 5353 >/dev/null docker run -d --name "$RESP_BROKEN" --network "$NEG_NET" \ -v "$BZDIR/.broken-broken.bin:/a.bin:ro" -v "$BZDIR/.broken-dnskey.bin:/k.bin:ro" \ -v "$REPO/test/broken-zone-responder.py:/r.py:ro" \ python:alpine python /r.py /a.bin /k.bin 5354 >/dev/null # Get a responder's IP from ITS OWN log (it prints its container IP); fall # back to docker inspect. This is more reliable than host-side inspect, which # can be empty while a container is starting on a slow WSL daemon. get_ip() { _c="$1"; _ip=""; _i=0 while [ $_i -lt 15 ]; do _ip="$(docker logs "$_c" 2>&1 | grep -oE 'responder ip=[0-9.]+' | head -1 | cut -d= -f2)" [ -n "$_ip" ] && [ "$_ip" != "0.0.0.0" ] && break sleep 2; _i=$((_i+1)) done if [ -z "$_ip" ] || [ "$_ip" = "0.0.0.0" ]; then _ip="$(docker inspect -f '{{(index .NetworkSettings.Networks "'"$NEG_NET"').IPAddress}}' "$_c" 2>/dev/null)" fi printf '%s' "$_ip" } RVALID="$(get_ip "$RESP")" RBROKEN="$(get_ip "$RESP_BROKEN")" [ -n "$RVALID" ] && [ -n "$RBROKEN" ] || { bad "negative-test responders have IPs (valid=$RVALID broken=$RBROKEN)" docker logs "$RESP" 2>&1 | tail -2 | sed 's/^/ /' docker logs "$RESP_BROKEN" 2>&1 | tail -2 | sed 's/^/ /' } # C6: trust anchor + forward to VALID + validator -> expect NOERROR + AD FWD_V="$(pwd)/test/fixture-broken-valid"; rm -rf "$FWD_V"; mkdir -p "$FWD_V"; chmod 755 "$FWD_V" printf 'trust-anchor-file: "/anchor.key"\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5353\n' "$RVALID" > "$FWD_V/99.conf" chmod 644 "$FWD_V/99.conf" docker run -d --name "$C6" --network "$NEG_NET" -v "$FWD_V:/config:ro" -v "$BZDIR/.broken-anchor.key:/anchor.key:ro" "$IMAGE" >/dev/null # C7: trust anchor + forward to BROKEN + validator -> expect SERVFAIL FWD_B="$(pwd)/test/fixture-broken-broken"; rm -rf "$FWD_B"; mkdir -p "$FWD_B"; chmod 755 "$FWD_B" printf 'trust-anchor-file: "/anchor.key"\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5354\n' "$RBROKEN" > "$FWD_B/99.conf" chmod 644 "$FWD_B/99.conf" docker run -d --name "$C7" --network "$NEG_NET" -v "$FWD_B:/config:ro" -v "$BZDIR/.broken-anchor.key:/anchor.key:ro" "$IMAGE" >/dev/null # C8: validator disabled (module-config: iterator) + forward to BROKEN -> # expect NOERROR (the broken answer is resolvable, just not validated). FWD_N="$(pwd)/test/fixture-broken-noval"; rm -rf "$FWD_N"; mkdir -p "$FWD_N"; chmod 755 "$FWD_N" printf 'module-config: iterator\nforward-zone:\n name: "broken.example"\n forward-addr: %s@5354\n' "$RBROKEN" > "$FWD_N/99.conf" chmod 644 "$FWD_N/99.conf" docker run -d --name "$C8" --network "$NEG_NET" -v "$FWD_N:/config:ro" "$IMAGE" >/dev/null # Readiness: unbound is ready when it answers the query at all — a SERVFAIL is # a valid response, so check for ANY rcode, not a successful (exit 0) drill. ready_c() { _c="$1"; _r="no"; _i=0 while [ $_i -lt 15 ]; do if docker exec "$_c" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null | grep -q "rcode:"; then _r="yes"; break fi sleep 2; _i=$((_i+1)) done [ "$_r" = "yes" ] } ready_c "$C6" && ok "negative-test: valid resolver ready" || bad "negative-test: valid resolver ready" ready_c "$C7" && ok "negative-test: broken resolver ready" || bad "negative-test: broken resolver ready" ready_c "$C8" && ok "negative-test: noval resolver ready" || bad "negative-test: noval resolver ready" # 1) VALID zone validates: NOERROR + AD v_out="$(docker exec "$C6" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null)" if printf '%s' "$v_out" | grep -q "rcode: NOERROR" && printf '%s' "$v_out" | grep -qE "flags:.*ad"; then ok "valid zone validates (NOERROR + AD with explicit trust anchor)" else bad "valid zone validates (got: $(printf '%s' "$v_out" | grep rcode | head -1))" printf '%s\n' "$v_out" | sed 's/^/ /' fi # 2) BROKEN signature rejected: SERVFAIL (no CD) b_out="$(docker exec "$C7" drill -p 5053 -D www.broken.example A @127.0.0.1 2>/dev/null)" if printf '%s' "$b_out" | grep -q "rcode: SERVFAIL"; then ok "broken signature -> SERVFAIL (no CD; validator rejects the invalid RRSIG)" else bad "broken signature -> SERVFAIL (got: $(printf '%s' "$b_out" | grep rcode | head -1))" printf '%s\n' "$b_out" | sed 's/^/ /' fi # 3) Counter-test: broken answer is RESOLVABLE with validation off -> NOERROR. # This proves step 2 is a signature rejection, not a resolution failure. n_out="$(docker exec "$C8" drill -p 5053 www.broken.example A @127.0.0.1 2>/dev/null)" if printf '%s' "$n_out" | grep -q "rcode: NOERROR" && printf '%s' "$n_out" | grep -qE "IN[[:space:]]+A[[:space:]]"; then ok "broken answer resolvable with validation off (counter-test: signature rejection, not resolution failure)" else bad "broken answer resolvable with validation off (got: $(printf '%s' "$n_out" | grep rcode | head -1))" printf '%s\n' "$n_out" | sed 's/^/ /' fi rm -rf "$FWD_V" "$FWD_B" "$FWD_N" say "" say "==================================================" say "RESULT: $PASS passed, $FAIL failed, $NOTVERIFIED not verified" if [ "$NOTVERIFIED" -gt 0 ]; then say "(not verified = infrastructure timeout, not a content finding; re-run to clear)" fi say "==================================================" # A content finding (FAIL) fails the run. Not-verified checks are visible but # do not fail the run on their own (they are infrastructure, re-run to clear). [ $FAIL -eq 0 ]