opensnitch/utils/packaging/test_install.sh
trvon 60cd4311a8
build: daemon-only fork build + Arch packaging, kernel-matched eBPF
Add a daemon-only build path (no Python GUI) that builds the eBPF objects
against the running kernel's headers, deriving the BPF DataLayout from
clang and finalizing with opt; a structural unprivileged check target; an
install path that never clobbers existing units/config/rules; an Arch
PKGBUILD.in + prepare.sh and a test_install.sh harness. Document the
workflow and the event-loss diagnostics in README.md and ebpf_prog/README.
Gitignore the compiled ebpf_prog/*.o artifacts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-05 16:58:22 -07:00

132 lines
5.8 KiB
Bash

#!/bin/sh
# Unprivileged install/build-contract tests. All toolchain/service calls are
# stubs and all destinations are private temporary directories; no sudo/GUI.
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
work=$(mktemp -d "${TMPDIR:-/tmp}/opensnitch-install-test.XXXXXXXX")
trap 'rm -rf "$work"' 0
trap 'exit 130' INT TERM HUP
mkdir -p "$work/bin" "$work/daemon" "$work/ebpf"
cp "$root/daemon/Makefile" "$work/daemon/Makefile"
cp -R "$root/daemon/data" "$work/daemon/data"
cp "$root/ebpf_prog/Makefile" "$work/ebpf/Makefile"
export GO_CALLS="$work/go.calls" SYSTEMCTL_CALLS="$work/systemctl.calls"
: > "$GO_CALLS"
: > "$SYSTEMCTL_CALLS"
cat > "$work/bin/go" <<'EOF'
#!/bin/sh
printf '%s\n' "$*" >> "$GO_CALLS"
# A normal build must not resolve/update dependencies using go get.
[ "$*" = 'build -mod=readonly -o opensnitchd .' ] || exit 91
printf '#!/bin/sh\nexit 0\n' > opensnitchd
EOF
cat > "$work/bin/systemctl" <<'EOF'
#!/bin/sh
printf '%s\n' "$*" >> "$SYSTEMCTL_CALLS"
exit 0
EOF
cat > "$work/bin/readelf" <<'EOF'
#!/bin/sh
# Fixtures test staging, not ELF validity. Real ELF/section checks run separately.
printf 'Machine: Linux BPF\n'
printf 'kprobe/tcp_v4_connect\nuprobe/getaddrinfo\ntracepoint/syscalls/sys_enter_execve\n'
EOF
chmod 755 "$work/bin/go" "$work/bin/systemctl" "$work/bin/readelf"
PATH="$work/bin:$PATH"
export PATH
printf 'package main\nfunc main() {}\n' > "$work/daemon/main.go"
printf 'module example.invalid/install-test\n\ngo 1.23\n' > "$work/daemon/go.mod"
: > "$work/daemon/go.sum"
readonly_build() {
make -s -C "$work/daemon" > "$work/build.log" 2>&1 || return 1
grep -Fxq 'build -mod=readonly -o opensnitchd .' "$GO_CALLS" || return 1
! grep -Eq '^get( |$)' "$GO_CALLS" || return 1
}
daemon_staging() {
# Ensure an artifact exists even if the baseline readonly-build test failed.
printf '#!/bin/sh\nexit 0\n' > "$work/daemon/opensnitchd"
dest="$work/${1:-stage-daemon}"
conf="$dest/etc/opensnitchd"
mkdir -p "$conf/rules" "$conf/tasks"
printf 'operator configuration\n' > "$conf/default-config.json"
printf 'operator firewall\n' > "$conf/system-fw.json"
printf 'operator aliases\n' > "$conf/network_aliases.json"
printf 'operator rule\n' > "$conf/rules/000-allow-localhost.json"
printf 'operator tasks\n' > "$conf/tasks/tasks.json"
make -s -C "$work/daemon" install DESTDIR="$dest" PREFIX=/usr > "$work/install.log" 2>&1 || return 1
test -x "$dest/usr/bin/opensnitchd" || return 1
grep -Fxq 'operator configuration' "$conf/default-config.json" || return 1
grep -Fxq 'operator firewall' "$conf/system-fw.json" || return 1
grep -Fxq 'operator aliases' "$conf/network_aliases.json" || return 1
grep -Fxq 'operator rule' "$conf/rules/000-allow-localhost.json" || return 1
grep -Fxq 'operator tasks' "$conf/tasks/tasks.json" || return 1
test ! -s "$SYSTEMCTL_CALLS" || return 1
unit="$dest/etc/systemd/system/opensnitchd.service"
grep -Eq '^ExecStart="?/usr/bin/opensnitchd"?$' "$unit" || return 1
printf '# operator service override\n' > "$unit"
make -s -C "$work/daemon" install DESTDIR="$dest" PREFIX=/usr >> "$work/install.log" 2>&1 || return 1
grep -Fxq '# operator service override' "$unit" || return 1
test ! -s "$SYSTEMCTL_CALLS" || return 1
}
daemon_staging_spaces() {
daemon_staging 'stage daemon'
}
bpf_staging() {
for bpf_name in opensnitch opensnitch-dns opensnitch-procs; do
: > "$work/ebpf/$bpf_name.c"
printf 'test fixture: %s\n' "$bpf_name" > "$work/ebpf/$bpf_name.o"
done
make -s -C "$work/ebpf" install DESTDIR="$work/stage-bpf" PREFIX=/usr \
KERNEL_VER=test KERNEL_DIR="$work/headers" KERNEL_HEADERS="$work/headers" \
READELF="$work/bin/readelf" > "$work/bpf-install.log" 2>&1 || return 1
for bpf_name in opensnitch opensnitch-dns opensnitch-procs; do
cmp "$work/ebpf/$bpf_name.o" "$work/stage-bpf/usr/lib/opensnitchd/ebpf/$bpf_name.o" || return 1
done
test ! -s "$SYSTEMCTL_CALLS" || return 1
}
local_defaults() {
mkdir -p "$work/top/proto" "$work/top/daemon" "$work/top/ebpf_prog" "$work/top/ui"
cp "$root/Makefile" "$work/top/Makefile"
export RECURSIVE_CALLS="$work/recursive.calls"
: > "$RECURSIVE_CALLS"
cat > "$work/bin/submake" <<'EOF'
#!/bin/sh
printf '%s\n' "$*" >> "$RECURSIVE_CALLS"
EOF
chmod 755 "$work/bin/submake"
make -s -C "$work/top" MAKE="$work/bin/submake" > "$work/local.log" 2>&1 || return 1
grep -Fxq -- '-C proto ../daemon/ui/protocol/ui.pb.go' "$RECURSIVE_CALLS" || return 1
grep -Fxq -- '-C daemon' "$RECURSIVE_CALLS" || return 1
grep -Fxq -- '-B -C ebpf_prog check' "$RECURSIVE_CALLS" || return 1
make -s -C "$work/top" install MAKE="$work/bin/submake" >> "$work/local.log" 2>&1 || return 1
grep -Fxq -- '-C daemon install' "$RECURSIVE_CALLS" || return 1
grep -Fxq -- '-C ebpf_prog install' "$RECURSIVE_CALLS" || return 1
test "$(wc -l < "$RECURSIVE_CALLS")" -eq 5 || return 1
make -s -C "$work/top" clean MAKE="$work/bin/submake" >> "$work/local.log" 2>&1 || return 1
grep -Fxq -- '-C daemon clean' "$RECURSIVE_CALLS" || return 1
grep -Fxq -- '-C ebpf_prog clean' "$RECURSIVE_CALLS" || return 1
test "$(wc -l < "$RECURSIVE_CALLS")" -eq 7 || return 1
! grep -Eq '(^| )ui( |$)|python|pip' "$RECURSIVE_CALLS" || return 1
}
failed=0
for name in readonly_build daemon_staging daemon_staging_spaces bpf_staging local_defaults; do
if "$name"; then
printf 'PASS %s\n' "$name"
else
printf 'FAIL %s\n' "$name" >&2
case "$name" in
readonly_build) tail -n 6 "$work/build.log" "$GO_CALLS" >&2 ;;
daemon_staging*) tail -n 6 "$work/install.log" >&2 ;;
bpf_staging) tail -n 6 "$work/bpf-install.log" >&2 ;;
local_defaults) tail -n 6 "$work/local.log" >&2 ;;
esac
failed=$((failed + 1))
fi
done
test "$failed" -eq 0