interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / dx / clean_user_cleanup_test.sh

99 lines · 3903 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# Host-only orchestration tests. All privilege, network and GPU-entry commands
4# are mocked; no real user account, sudo, network request or device is used.
5set -euo pipefail
6root=$(cd "$(dirname "$0")/../.." && pwd)
7script=${1:-$root/tools/runtime/clean-user-hello.sh}
8work=$(mktemp -d)
9trap 'rm -rf "$work"' EXIT
10mkdir "$work/bin"
11cat > "$work/bin/sudo" <<'MOCK'
12#!/usr/bin/env bash
13set -eu
14printf '%s\n' "$*" >> "$MOCK_CALLS"
15case "$1" in
16 useradd) exit "$MOCK_USERADD_RC" ;;
17 -u) exit 0 ;; # Never execute the requested login shell.
18 test) exit 1 ;; # No real trace was produced.
19 pkill|userdel|rm) exit 0 ;; # Record only; never execute.
20 *) echo UNEXPECTED_PRIVILEGE >> "$MOCK_CALLS"; echo "unexpected privileged command: $1" >&2; exit 99 ;;
21esac
22MOCK
23cat > "$work/bin/curl" <<'MOCK'
24#!/usr/bin/env bash
25echo curl >> "$MOCK_CALLS"
26exit 7 # Stop after account creation, before README/GPU execution.
27MOCK
28cat > "$work/bin/flock" <<'MOCK'
29#!/usr/bin/env bash
30echo UNEXPECTED_GPU_ENTRY >> "$MOCK_CALLS"
31exit 99
32MOCK
33cat > "$work/bin/getent" <<'MOCK'
34#!/usr/bin/env bash
35exit 2 # The mock account is absent after successful mock deletion.
36MOCK
37cat > "$work/bin/date" <<'MOCK'
38#!/usr/bin/env bash
39printf '%s\n' 010203 # Make clock-based names collide deterministically.
40MOCK
41real_mktemp=$(command -v mktemp)
42cat > "$work/bin/mktemp" <<'MOCK'
43#!/usr/bin/env bash
44if [ "${MOCK_MKTEMP_FAIL:-0}" = 1 ]; then exit 1; fi
45exec "$REAL_MKTEMP" "$@"
46MOCK
47chmod +x "$work/bin/"*
48pass=0; fail=0
49check() {
50 local name=$1; shift
51 if "$@"; then echo "PASS $name"; pass=$((pass + 1))
52 else echo "FAIL $name"; fail=$((fail + 1)); fi
53}
54no_account_cleanup() { ! grep -Eq '^(pkill|userdel) ' "$1"; }
55no_privilege() { [ ! -s "$1" ]; }
56run_case() {
57 local name=$1 add_rc=$2 temp_fail=${3:-0}
58 mkdir -p "$work/$name/tmp"
59 : > "$work/$name/calls"
60 set +e
61 env PATH="$work/bin:/usr/bin:/bin" \
62 REAL_MKTEMP="$real_mktemp" MOCK_CALLS="$work/$name/calls" \
63 MOCK_USERADD_RC="$add_rc" MOCK_MKTEMP_FAIL="$temp_fail" \
64 TMPDIR="$work/$name/tmp" CUDAPP_BOARD= \
65 bash "$script" main "$work/$name/run.log" > "$work/$name/output" 2>&1
66 rc=$?
67 set -e
68 check "$name-exit-3" test "$rc" -eq 3
69 check "$name-never-enters-gpu" test "$(grep -c UNEXPECTED_GPU_ENTRY "$work/$name/calls" || true)" -eq 0
70 check "$name-only-expected-privilege" test "$(grep -c UNEXPECTED_PRIVILEGE "$work/$name/calls" || true)" -eq 0
71}
72
73# useradd exit 9 means the candidate account already exists. Its processes and
74# home belong to someone else; the cleanup must never issue deletion commands.
75run_case collision 9
76check existing-account-untouched no_account_cleanup "$work/collision/calls"
77run_case failure 1
78check failed-creation-untouched no_account_cleanup "$work/failure/calls"
79
80# Successful creation followed by a network/setup failure must still remove
81# precisely the owned temporary account, not leak it or use a different name.
82run_case created 0
83created=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created/calls")
84check created-name-nonempty test -n "$created"
85check owned-process-cleanup grep -Fxq "pkill -u $created" "$work/created/calls"
86check owned-account-cleanup grep -Fxq "userdel -r $created" "$work/created/calls"
87check one-owned-account-deletion test "$(grep -c '^userdel ' "$work/created/calls" || true)" -eq 1
88
89# Even with the same UTC clock value, simultaneous/repeated candidates differ.
90run_case created_again 0
91again=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created_again/calls")
92check distinct-candidate-names test "$created" != "$again"
93
94# A missing temporary directory must abort before creating any account.
95run_case temp_failure 0 1
96check temp-failure-no-privilege no_privilege "$work/temp_failure/calls"
97
98echo "clean-user cleanup tests: $pass passed, $fail failed (mocked commands only)"
99[ "$fail" -eq 0 ]