#!/usr/bin/env bash # tests/unit/05_policy.sh -- how allow entries are classified. source "$TESTS/lib.sh" PROJ="$WORK/proj"; mkdir -p "$PROJ"; cd "$PROJ" || exit 1 export AISB_CONFIG_DIR="$WORK/cfg-empty"; mkdir -p "$AISB_CONFIG_DIR" direct_line() { printf '%s' "$1" | grep '^direct=' | cut -d= -f2-; } t_name "domains go to the gate, not to netfilter" sb --print-policy --net allow --allow api.openai.com assert_contains "gate_domains=api.openai.com" "domain listed for the gate" assert_eq "" "$(direct_line "$OUT")" "no netfilter entry for a domain in gate mode" assert_contains "proxy=100.64.0.1:18081" "gate address is the proxy" t_name "IP and CIDR entries become direct rules" sb --print-policy --net allow --allow 10.0.0.5:5432 assert_eq "direct=10.0.0.5 5432" "$(printf '%s' "$OUT" | grep '^direct=')" "explicit host:port" sb --print-policy --net allow --allow 10.1.0.0/16 assert_eq "direct=10.1.0.0/16 80,443" "$(printf '%s' "$OUT" | grep '^direct=')" "CIDR gets the default ports" sb --print-policy --net allow --allow 10.0.0.5:5432 --allow 10.9.9.9 assert_eq "direct=10.0.0.5 5432;10.9.9.9 80,443" "$(printf '%s' "$OUT" | grep '^direct=')" "several direct entries" t_name "ipv6" sb --print-policy --net allow --allow "[::1]:8080" assert_eq "direct6=::1 8080" "$(printf '%s' "$OUT" | grep '^direct6=')" "bracketed [ipv6]:port parses" assert_eq "direct=" "$(printf '%s' "$OUT" | grep '^direct=')" "and stays out of the ipv4 rules" assert_contains "gate_domains=" "policy printed for an ipv6 entry" t_name "--no-gate resolves names up front" sb --print-policy --net allow --no-gate --allow localhost assert_contains "direct=127.0.0.1 80,443" "localhost resolves to a direct rule" assert_contains "proxy=none" "--no-gate has no proxy" sb --print-policy --net allow --no-gate --allow nope.invalid assert_rc 1 "an unresolvable name is an error, not a silent no-op" assert_contains "could not resolve" "and it says so" t_name "ports" sb --allow api.openai.com:not-a-port --print-policy assert_rc 1 "non-numeric port rejected" sb --print-policy --net allow --allow api.openai.com:8443 assert_contains "gate_domains=api.openai.com" "domain+port still routed through the gate" t_name "mixed list" sb --print-policy --net allow --allow api.openai.com --allow 10.0.0.5:5432 --allow files.pythonhosted.org assert_contains "api.openai.com" "domain 1" assert_contains "files.pythonhosted.org" "domain 2" assert_contains "10.0.0.5 5432" "ip entry"