utils/nfulnl_test.sh was a single monolithic script covering one hook (prerouting) on loopback only. Add a small nftables-style shell test instead. tests/shell/run-tests.sh discovers and runs executable scripts under tests/shell/testcases/, each a self-contained test exiting 0 (pass), 77 (skip) or non-zero (fail). tests/shell/lib.sh holds the common nfulnl_test-capture/cleanup helpers. Every test runs inside its own `unshare -n` network namespace. Testcases added: - 0001-prerouting-loopback: baseline, port of the old script, now also asserting the HWTYPE/HWLEN/HWHEADER triplet - 0002-output-no-hwheader: hwheader must be absent past LOCAL_OUT - 0003-forward-hwheader: hwheader present at NF_INET_FORWARD - 0004-netdev-ingress-hwheader: NFPROTO_NETDEV ingress hook - 0005-arp-hwheader: NFPROTO_ARP, NF_ARP_IN, NF_ARP_OUT - 0006-bridge-forward-indev: the actual regression test - bridges two netns together and asserts indev/hw_addr in the log follow the real ingress port per direction at NF_BR_FORWARD, not whatever skb->dev was last repointed to Not wired into "make check": testcases need root (netns, nft). Signed-off-by: Florian Westphal --- tests/shell/lib.sh | 140 ++++++++++++++++++ tests/shell/run-tests.sh | 66 +++++++++ .../testcases/nfulnl/0001-prerouting-loopback | 36 +++++ .../testcases/nfulnl/0002-output-no-hwheader | 43 ++++++ .../testcases/nfulnl/0003-forward-hwheader | 74 +++++++++ .../nfulnl/0004-netdev-ingress-hwheader | 29 ++++ .../shell/testcases/nfulnl/0005-arp-hwheader | 69 +++++++++ .../nfulnl/0006-bridge-forward-indev | 88 +++++++++++ 8 files changed, 545 insertions(+) create mode 100644 tests/shell/lib.sh create mode 100755 tests/shell/run-tests.sh create mode 100755 tests/shell/testcases/nfulnl/0001-prerouting-loopback create mode 100755 tests/shell/testcases/nfulnl/0002-output-no-hwheader create mode 100755 tests/shell/testcases/nfulnl/0003-forward-hwheader create mode 100755 tests/shell/testcases/nfulnl/0004-netdev-ingress-hwheader create mode 100755 tests/shell/testcases/nfulnl/0005-arp-hwheader create mode 100755 tests/shell/testcases/nfulnl/0006-bridge-forward-indev diff --git a/tests/shell/lib.sh b/tests/shell/lib.sh new file mode 100644 index 000000000000..28995c24f421 --- /dev/null +++ b/tests/shell/lib.sh @@ -0,0 +1,140 @@ +# Common helpers sourced by testcases/nfulnl/*. +# +# A testcase is expected to: +# source "$(dirname "$0")/../../lib.sh" +# trap nfulnl_cleanup EXIT # extend, don't replace, if the testcase has +# # its own cleanup (netns teardown etc.) +# nfulnl_capture [netns] +# ... generate traffic, use nft log group 0 ... +# nfulnl_wait +# and then grep/assert on $NFULNL_OUT. +# +# nfulnl_test(1) always binds group 0 (and 100), so every nft ruleset in +# these testcases must use "log group 0". + +SKIP_RC=77 +NFULNL_TEST_NAME="${0##*/}" + +# Make sure a failure inside a helper function (nfulnl_capture, nfulnl_wait, +# ...) still runs the ERR trap below, and print a command trace when +# NFULNL_TEST_VERBOSE is set, so a standalone run gives more to go on than a +# bare exit code. +set -E +[ -n "${NFULNL_TEST_VERBOSE:-}" ] && set -x + +# Locate the nfulnl_test helper binary built under utils/. +: "${NFULNL_TEST_BIN:=}" +if [ -z "$NFULNL_TEST_BIN" ]; then + for candidate in \ + "$(dirname "$0")/../../../../utils/nfulnl_test" \ + "$(pwd)/utils/nfulnl_test"; do + if [ -x "$candidate" ]; then + NFULNL_TEST_BIN="$candidate" + break + fi + done +fi + +# nfulnl_dump_out: print whatever nfulnl_test captured so far, for +# post-mortem debugging. Safe to call even if capture never started. +nfulnl_dump_out() { + if [ -n "$NFULNL_OUT" ] && [ -s "$NFULNL_OUT" ]; then + echo "---- $NFULNL_TEST_NAME: nfulnl_test output ($NFULNL_OUT) ----" >&2 + cat "$NFULNL_OUT" >&2 + echo "---- end nfulnl_test output ----" >&2 + fi +} + +die() { + echo "FAIL: $NFULNL_TEST_NAME: $@" >&2 + nfulnl_dump_out + exit 1 +} + +skip() { + echo "SKIP: $NFULNL_TEST_NAME: $@" >&2 + exit $SKIP_RC +} + +# Reports the failing command/line whenever `set -e` aborts a testcase, eg. +# a setup command (ip, nft, ping, ...) failing - those currently exit with +# no message at all since their own stdout is usually redirected away. +# Deliberately a no-op once a testcase does `set +e` for its assertion +# section: bash still runs the ERR trap there for things like +# `cnt=$(grep -c ...)` even though -e is off, and those failures are +# expected/handled by the `die` calls that follow, not errors in themselves. +nfulnl_on_err() { + local rc=$? line="$1" cmd="$2" + case $- in + *e*) ;; + *) return 0 ;; + esac + # nfulnl_wait already reports the process it ran and its output before + # propagating failure via a plain "return $rc"; skip the generic message + # for that case, it would only point at the "return" statement itself, + # not anything a test author wrote. + case "$cmd" in + "return "*) return 0 ;; + esac + echo "FAIL: $NFULNL_TEST_NAME: line $line: '$cmd' exited with status $rc" >&2 + nfulnl_dump_out +} +trap 'nfulnl_on_err "$LINENO" "$BASH_COMMAND"' ERR + +[ -x "$NFULNL_TEST_BIN" ] || skip "nfulnl_test binary not found/built (looked for \$NFULNL_TEST_BIN, utils/nfulnl_test)" +command -v nft >/dev/null || skip "nft not found" + +NFULNL_PID= +NFULNL_OUT= + +# nfulnl_capture [netns] +# +# Starts nfulnl_test in the background (optionally inside netns ), +# redirecting its output to a tempfile ($NFULNL_OUT). The caller is +# responsible for calling `nfulnl_wait` once traffic generation is done. +nfulnl_capture() { + local ns="$1" + NFULNL_OUT=$(mktemp) + + if [ -n "$ns" ]; then + ip netns exec "$ns" timeout 5 "$NFULNL_TEST_BIN" > "$NFULNL_OUT" & + else + timeout 5 "$NFULNL_TEST_BIN" > "$NFULNL_OUT" & + fi + NFULNL_PID=$! + + # give nflog_bind_group()/nft a moment to come up before traffic flows + sleep 1 +} + +# Waits for the backgrounded nfulnl_test (started by nfulnl_capture) to +# finish. Under normal operation that's `timeout 5` killing it once the +# capture window elapses, which `wait` reports as rc 124 - that is success, +# not a failure, so it must not be returned as-is: under `set -e` it is the +# right-most (ie. checked) element of a `&&` list, so returning it verbatim +# would silently abort the testcase right here, before any of its assertions +# ever run. +nfulnl_wait() { + local rc=0 + [ -n "$NFULNL_PID" ] || return 0 + wait "$NFULNL_PID" || rc=$? + if [ "$rc" -ne 0 ] && [ "$rc" -ne 124 ]; then + echo "FAIL: $NFULNL_TEST_NAME: nfulnl_test exited with status $rc (expected 124 from the timeout, or 0)" >&2 + nfulnl_dump_out + return "$rc" + fi + return 0 +} + +# Runs as an EXIT trap, so under `set -e` any command here that fails (eg. +# `kill` on a PID that has already exited on its own - the common case, +# since nfulnl_wait() only returns after the capture has ended) would abort +# the rest of the trap and silently replace the testcase's real exit status +# with the failed command's, turning a PASS into an unexplained FAIL. `set +# +e` so a failure here can't do that; stdout/stderr are left alone so a +# genuine problem (eg. permission denied) is still visible. +nfulnl_cleanup() { + set +e + [ -n "$NFULNL_PID" ] && kill "$NFULNL_PID" + [ -n "$NFULNL_OUT" ] && rm -f "$NFULNL_OUT" +} diff --git a/tests/shell/run-tests.sh b/tests/shell/run-tests.sh new file mode 100755 index 000000000000..9bcd43e5fa55 --- /dev/null +++ b/tests/shell/run-tests.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# +# Minimal test runner for tests/shell/testcases/, modeled after nftables' +# tests/shell/run-tests.sh and ipset's tests/ harness, but scaled down to +# this project's size: each testcase is run inside its own network +# namespace (via `unshare -n`), sets up whatever topology it needs on top +# of that (plain interfaces, or further `ip netns` peers for multi-host +# setups), drives nfulnl_test, and exits 0 (pass), 77 (skip) or anything +# else (fail). +# +# Usage: tests/shell/run-tests.sh [testcase ...] +# With no arguments, runs every executable file under testcases/. + +set -u + +SKIP_RC=77 +TESTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [ "$(id -u)" -ne 0 ]; then + echo "Most testcases require root (netns/nft support) - re-run with sudo." >&2 +fi + +if [ "$#" -gt 0 ]; then + tests=("$@") +else + tests=() + while IFS= read -r -d '' f; do + tests+=("$f") + done < <(find "$TESTDIR/testcases" -type f -perm -u+x -print0 | sort -z) +fi + +# Each testcase's combined stdout+stderr is captured to its own file under +# here rather than just a shell variable, so a failure can be inspected +# after the fact (full output, not just whatever fit on screen) - the whole +# directory is removed at the end unless some test failed. +LOGDIR=$(mktemp -d "${TMPDIR:-/tmp}/nfulnl-tests.XXXXXX") + +npass=0 +nfail=0 +nskip=0 + +for t in "${tests[@]}"; do + name="${t#"$TESTDIR"/testcases/}" + log="$LOGDIR/${name//\//_}.log" + (cd "$TESTDIR" && NFULNL_TEST_BIN="${NFULNL_TEST_BIN:-}" unshare -n -- "$t") > "$log" 2>&1 + rc=$? + if [ "$rc" -eq 0 ]; then + echo "PASS: $name" + npass=$((npass + 1)) + elif [ "$rc" -eq "$SKIP_RC" ]; then + echo "SKIP: $name" + nskip=$((nskip + 1)) + else + echo "FAIL: $name (rc=$rc)" + nfail=$((nfail + 1)) + fi +done + +echo +echo "Results: $npass passed, $nfail failed, $nskip skipped" +if [ "$nfail" -ne 0 ]; then + echo "Full logs for this run kept in $LOGDIR" + exit 1 +fi +rm -rf "$LOGDIR" +exit 0 diff --git a/tests/shell/testcases/nfulnl/0001-prerouting-loopback b/tests/shell/testcases/nfulnl/0001-prerouting-loopback new file mode 100755 index 000000000000..abdd70656407 --- /dev/null +++ b/tests/shell/testcases/nfulnl/0001-prerouting-loopback @@ -0,0 +1,36 @@ +#!/bin/bash +# +# Baseline: log ICMP/ICMPv6 in the prerouting chain on loopback. +# NF_INET_PRE_ROUTING is the simplest nfulnl_pre_egress() case (hooknum <= +# NF_INET_FORWARD for pf IPv4/IPv6) and indev == skb->dev always holds here, +# so NFULA_HWADDR and the NFULA_HWTYPE/HWLEN/HWHEADER triplet must both be +# present. + +source "$(dirname "$0")/../../lib.sh" +trap nfulnl_cleanup EXIT +set -e + +nfulnl_capture +ip link set lo up + +nft -f - </dev/null || skip "ip(8) not found" + +rnd=$(mktemp -u XXXXXX) +nsa="nfulnl-a-$rnd" +nsb="nfulnl-b-$rnd" + +cleanup() { + set +e + nfulnl_cleanup + ip netns del "$nsa" + ip netns del "$nsb" +} +trap cleanup EXIT +set -e + +ip netns add "$nsa" +ip netns add "$nsb" + +ip link add veth0 netns "$nsa" type veth peer name veth0 netns "$nsb" + +ip -net "$nsa" link set lo up +ip -net "$nsb" link set lo up +ip -net "$nsa" link set veth0 up +ip -net "$nsb" link set veth0 up +ip -net "$nsa" addr add 10.9.9.1/24 dev veth0 +ip -net "$nsb" addr add 10.9.9.2/24 dev veth0 + +ip netns exec "$nsa" nft -f - <