testsAndMisc/linux_configuration/tests/test_setup_dns_blocker.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

74 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Regression tests for setup_dns_blocker.sh (LAN DNS blocker).
#
# Covers the pure, root-free logic:
# - command dispatch (help lists all subcommands, unknown -> exit 1)
# - firewall_is_loaded: the pipefail/SIGPIPE regression -- 'nft | grep -q'
# used to false-negative under 'set -o pipefail' because grep closed the
# pipe early and nft died on SIGPIPE. Detection must survive large output.
set -euo pipefail
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
REPO_DIR=$(cd -- "$SCRIPT_DIR/.." && pwd)
TARGET_SCRIPT="$REPO_DIR/./features/setup_dns_blocker.sh"
fail() {
printf 'FAIL: %s\n' "$1" >&2
exit 1
}
TMP_DIR=$(mktemp -d)
BIN_DIR="$TMP_DIR/bin"
mkdir -p "$BIN_DIR"
cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT
# --- Dispatch tests (real script, no root needed) --------------------------
help_out=$("$TARGET_SCRIPT" help)
for cmd in setup status refresh dhcp dhcp-off; do
[[ "$help_out" == *"$cmd"* ]] || fail "help output missing '$cmd' subcommand"
done
# Unknown command must exit non-zero.
if "$TARGET_SCRIPT" bogus-cmd >/dev/null 2>&1; then
fail "unknown command should exit non-zero"
fi
# --- firewall_is_loaded regression (pipefail/SIGPIPE) ----------------------
# Mock 'nft' so the function runs without root. Emit a LARGE ruleset so that a
# reintroduced 'grep -q' pipe would SIGPIPE nft and false-negative under pipefail.
make_nft() {
local mode="$1" # drop | accept
cat >"$BIN_DIR/nft" <<EOF
#!/bin/bash
# Large output to stress the early-close case.
echo "table inet filter {"
echo " chain input {"
echo " type filter hook input priority filter; policy ${mode};"
for i in \$(seq 1 500); do echo " ip saddr 10.0.0.\$i tcp dport 22 accept"; done
echo " }"
echo "}"
EOF
chmod +x "$BIN_DIR/nft"
}
# Source the target with main() suppressed so we can call its functions.
run_fw_check() {
local mode="$1"
make_nft "$mode"
PATH="$BIN_DIR:$PATH" SETUP_DNS_BLOCKER_SKIP_MAIN=1 bash -c '
set -euo pipefail
source "$1"
if firewall_is_loaded; then echo LOADED; else echo NOT_LOADED; fi
' _ "$TARGET_SCRIPT"
}
got=$(run_fw_check drop)
[[ "$got" == "LOADED" ]] || fail "firewall_is_loaded should be true for a policy-drop chain (got: $got)"
got=$(run_fw_check accept)
[[ "$got" == "NOT_LOADED" ]] || fail "firewall_is_loaded should be false for a policy-accept chain (got: $got)"
printf 'PASS: %s\n' "$(basename "$0")"