#!/usr/bin/env bats load test_helper # system/pacman-guard reads a fake /proc: process $1's command line and # environment, and its parent setup() { common_setup export PACMAN_GUARD_PROC="$BATS_TEST_TMPDIR/proc" export PACMAN_GUARD_PID=300 } # process PID PARENT "ARGS..." [ENV...] process() { local dir="$PACMAN_GUARD_PROC/$1" mkdir -p "$dir" # shellcheck disable=SC2086 printf '%s\0' $3 > "$dir/cmdline" printf 'Name:\tx\nPPid:\t%s\n' "$2" > "$dir/status" if (( $# > 3 )); then printf '%s\0' "${@:4}"; fi > "$dir/environ" } # pacman 300, started by sudo 200, started by a shell 100 processes() { process 300 200 "$1" PATH=/usr/bin process 200 100 "sudo $1" "${@:2}" process 100 1 "bash" HOME=/home/me } @test "the guard stops pacman -Syu" { processes "pacman -Syu" run bash "$REPO/system/pacman-guard" [ "$status" -eq 1 ] [[ "$output" == *"COREDESKTOP_ALLOW_PACMAN=1 sudo pacman -Syu"* ]] } @test "the guard stops every spelling of a full upgrade" { for args in "pacman -Su" "pacman -Syyu --noconfirm" "pacman -S -u" "pacman --sync --sysupgrade" "pacman -Suu"; do rm -rf "$PACMAN_GUARD_PROC" processes "$args" run bash "$REPO/system/pacman-guard" [ "$status" -eq 1 ] done } @test "the guard lets installs, removals and partial syncs through" { for args in "pacman -S git" "pacman -Sy git" "pacman -U /tmp/x.pkg.tar.zst" "pacman -Rns git" "pacman -D --asexplicit git" "pacman -S -- -u"; do rm -rf "$PACMAN_GUARD_PROC" processes "$args" run bash "$REPO/system/pacman-guard" [ "$status" -eq 0 ] done } @test "the guard lets update's upgrade through: sudo still has the variable" { processes "pacman -Su --noconfirm" COREDESKTOP_ALLOW_PACMAN=1 run bash "$REPO/system/pacman-guard" [ "$status" -eq 0 ] } @test "the guard lets an upgrade through when any ancestor has the variable" { process 300 200 "pacman -Su" PATH=/usr/bin process 200 150 "sudo pacman -Su" process 150 100 "yay -Syu" process 100 1 "bash" COREDESKTOP_ALLOW_PACMAN=1 run bash "$REPO/system/pacman-guard" [ "$status" -eq 0 ] } @test "the guard isn't fooled by a variable with another value" { processes "pacman -Syu" COREDESKTOP_ALLOW_PACMAN=0 XCOREDESKTOP_ALLOW_PACMAN=1 run bash "$REPO/system/pacman-guard" [ "$status" -eq 1 ] } @test "pacman-guard.sh installs the guard and its hook once" { export PACMAN_GUARD_ROOT="$BATS_TEST_TMPDIR/root" export COREDESKTOP="$REPO" stub sudo '"$@"' run bash "$REPO/system/pacman-guard.sh" [ "$status" -eq 0 ] cmp "$REPO/system/pacman-guard" "$PACMAN_GUARD_ROOT/usr/local/lib/coredesktop/pacman-guard" [ -x "$PACMAN_GUARD_ROOT/usr/local/lib/coredesktop/pacman-guard" ] grep -qx 'Exec = /usr/local/lib/coredesktop/pacman-guard' "$PACMAN_GUARD_ROOT/etc/pacman.d/hooks/00-coredesktop-guard.hook" grep -qx 'AbortOnFail' "$PACMAN_GUARD_ROOT/etc/pacman.d/hooks/00-coredesktop-guard.hook" : > "$STUB_LOG" run bash "$REPO/system/pacman-guard.sh" [ "$status" -eq 0 ] not_called sudo }