From 3334fc32d6eed2e5c72147ce1bc348794b959e5a Mon Sep 17 00:00:00 2001 From: "Earl Tankard, Jr., Ph.D" <45021016+primetimetank21@users.noreply.github.com> Date: Fri, 25 Sep 2026 20:01:34 -0400 Subject: [PATCH] fix(aliases): route shutdown helpers safely on WSL Preserve native Linux/macOS commands, reject ambiguous platform signals, and validate Windows delays before conversion. Add isolated Bash/Zsh regression coverage and CI wiring without executing real power-control commands. Closes #514 Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> --- .github/workflows/validate.yml | 22 ++ CHANGELOG.md | 16 ++ README.md | 65 +++++- config/dotfiles/.aliases | 81 +++++-- tests/test_alias_parity.sh | 1 + tests/test_shutdown_helpers.sh | 401 +++++++++++++++++++++++++++++++++ 6 files changed, 571 insertions(+), 15 deletions(-) create mode 100644 tests/test_shutdown_helpers.sh diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 4043704..5ac0938 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -80,6 +80,15 @@ jobs: chmod +x tests/test_alias_parity.sh bash tests/test_alias_parity.sh + - name: Check shutdown helper syntax (Bash and Zsh) + run: | + bash --noprofile --norc -n config/dotfiles/.aliases + zsh -df -n config/dotfiles/.aliases + bash --noprofile --norc -n tests/test_shutdown_helpers.sh + + - name: Run isolated shutdown helper tests (Bash and Zsh) + run: bash tests/test_shutdown_helpers.sh --bash "$(command -v bash)" --zsh "$(command -v zsh)" + - name: Run idempotency test suite run: bash tests/test_idempotency.sh @@ -183,6 +192,16 @@ jobs: bash setup.sh echo "Second run completed without errors" + - name: Check shutdown helper syntax (system Bash 3.2 and Zsh) + run: | + /bin/bash --version + /bin/bash --noprofile --norc -n config/dotfiles/.aliases + /bin/zsh -df -n config/dotfiles/.aliases + /bin/bash --noprofile --norc -n tests/test_shutdown_helpers.sh + + - name: Run isolated shutdown helper tests (system Bash 3.2 and Zsh) + run: /bin/bash tests/test_shutdown_helpers.sh --bash /bin/bash --zsh /bin/zsh + - name: Run tool version tests run: bash tests/test_tool_versions.sh @@ -236,6 +255,9 @@ jobs: - name: Lint config/dotfiles/.aliases run: shellcheck -s bash config/dotfiles/.aliases + - name: Lint isolated shutdown helper harness + run: shellcheck tests/test_shutdown_helpers.sh + - name: Run Bash flag compatibility gates run: bash tests/test_setup_flags.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ca7e60..a3c184a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,13 +9,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Isolated shutdown-helper regression coverage under Bash/Zsh on Linux/macOS, + including macOS system Bash 3.2, with non-forwarding command stubs. (#514) + ### Changed - Interactive tool pickers use Quiet Accent semantic colors when a compatible console is available, while preserving ASCII/plain output for redirected sessions and `NO_COLOR`. (#507) +- Bash/Zsh `tsdn` now requires exactly one canonical positive decimal minute + argument, rejects WSL delays above 5,256,000 minutes before conversion, and + warns that positive Windows timeouts force application closure and risk + unsaved work. Native minute strings remain unconverted. (#514) ### Fixed +- Route Bash/Zsh `sdn`, `tsdn`, and `cancel_tsdn` to the Windows host when a + Microsoft kernel marker identifies WSL, without native fallback on interop + failure. Preserve native Linux/macOS commands; deliberately reject conflicting + WSL environment hints on non-Microsoft Linux kernels and indeterminate or + unsupported platforms. Document custom-kernel/container detection limits. (#514) +- Preserve actual cancellation diagnostics and failure status instead of + reporting every failure as "no pending shutdown" success. Cancellation remains + system-wide. (#514) + ### Removed - Embedded squad infrastructure: removed `.squad/` directory and all internal AI-agent coordination tooling from the repository. Project coordination now handled externally. (PR #475, PR #477) diff --git a/README.md b/README.md index 29a5cfc..349636a 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ Full definitions: ## Shell Functions -Three helper functions are available after setup: +The following helper functions are available after setup: ### Linux / macOS / WSL @@ -298,6 +298,69 @@ create_tmux start_up ``` +#### Shutdown helpers (Bash/Zsh) + +**`sdn`** requests immediate shutdown, **`tsdn `** schedules shutdown, +and **`cancel_tsdn`** cancels a pending shutdown system-wide (not just requests +created by these helpers). In WSL they target the **Windows host**, not just the +Linux distribution. Native Linux/macOS keep their existing commands: + +| Helper | WSL (Windows host) | Native Linux | macOS | +|--------|--------------------|--------------|-------| +| `sdn` | `shutdown.exe /s /t 0` | `sudo shutdown -h now` | `sudo shutdown -h now` | +| `tsdn M` | `shutdown.exe /s /t S` (`S = M * 60`) | `sudo shutdown -h +M` | `sudo shutdown -h +M` | +| `cancel_tsdn` | `shutdown.exe /a` | `sudo shutdown -c` | `sudo killall shutdown` | + +`tsdn` requires **exactly one positive decimal integer with no leading zeros** +(for example, `1` or `30`). Missing/extra arguments, zero, negatives, fractions, +and nonnumeric input fail without running a shutdown command. WSL accepts up to +**5,256,000 minutes** (315,360,000 seconds); larger values are rejected before +arithmetic. Native Linux/macOS receive the original minute string without +conversion or the Windows ceiling; their own command limits still apply. + +> **Windows data-loss warning:** A positive Windows `/t` timeout implicitly +> forces applications to close, even without `/f`, and unsaved work may be lost. +> WSL timed requests emit this warning to stderr. There is no extra confirmation. +> See Microsoft's [`shutdown` reference](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown). +> A zero-timeout request is not a guarantee of instantaneous power-off. + +Platform selection happens only when a helper is invoked, using `uname -s` and, +on Linux only, a successful nonempty read of `/proc/version`: + +| Detection | Result | +|-----------|--------| +| Linux + case-insensitive Microsoft kernel marker | WSL, even when both WSL environment variables are absent | +| Linux + non-Microsoft kernel + `WSL_DISTRO_NAME` and `WSL_INTEROP` both unset/empty | Native Linux, even if `shutdown.exe` is on PATH | +| Linux + non-Microsoft kernel + either WSL variable nonempty | Ambiguous: diagnostic, nonzero status, no operation | +| Linux + missing/unreadable/empty/failed kernel probe | Indeterminate: diagnostic, nonzero status, no operation, regardless of WSL variables | +| Darwin | macOS; inherited WSL variables are ignored and `/proc/version` is not read | +| Unsupported OS or failed OS probe | Diagnostic, nonzero status, no operation | + +Stale WSL variables on native Linux are **intentionally rejected** as ambiguous. +Establish the actual platform and correct the environment manually; the helpers +never clear variables or retry automatically. This is a **heuristic, not a +security boundary**: custom WSL kernels without the Microsoft marker are +ambiguous when WSL hints remain, but indistinguishable from native Linux when +all hints are removed. Custom kernels and nested containers are outside initial +coverage; do not assume universal or fail-safe detection. + +WSL requires `shutdown.exe` on PATH and Windows-process interop enabled. The +helpers check command availability without executing a probe. Missing interop +or execution failures **never fall back to Linux shutdown**, use a hard-coded +Windows drive path, or automatically abort/reschedule. Actual command output +and exit status are preserved, including permission and cancellation failures; +not every cancellation failure means "no pending shutdown." Shell exit codes +are not interpreted as Win32 error numbers. The native PowerShell profile is +unchanged by this Bash/Zsh routing contract. + +The isolated regression suite is `bash tests/test_shutdown_helpers.sh`; it +requires both Bash and Zsh (optional `--bash /absolute/path` and +`--zsh /absolute/path`). It extracts only the production helper definitions and +uses non-forwarding recording stubs, never real shutdown commands. Linux/macOS +CI includes both shells and macOS system Bash 3.2. These simulated tests do not +prove live WSL or OS-level shutdown behavior; do not validate with a live +schedule-then-cancel experiment. + ### Windows **`New-PsmuxSession`** -- Create or attach to the `tank_dev` psmux session (Windows equivalent of `create_tmux`). diff --git a/config/dotfiles/.aliases b/config/dotfiles/.aliases index 1e993fc..4bc371e 100644 --- a/config/dotfiles/.aliases +++ b/config/dotfiles/.aliases @@ -162,30 +162,83 @@ start_up() { create_tmux } -# ── Shutdown (requires sudo on Linux/macOS) ────────────────────────────────── +# ── Shutdown (Windows host on WSL; sudo on native Linux/macOS) ──────────────── + +# Heuristic detection, evaluated only on invocation. WSL environment variables +# are ambiguity hints, not authority; custom kernels/containers have limits. +_dev_setup_shutdown_platform() { + local os kernel + if ! os=$(uname -s); then + printf '%s\n' 'shutdown helpers: unable to detect OS with uname -s' >&2 + return 1 + fi + case "$os" in + Linux) + if ! kernel=$(cat /proc/version 2>/dev/null) || [[ -z "$kernel" ]]; then + printf '%s\n' 'shutdown helpers: unable to read a nonempty /proc/version; platform is indeterminate' >&2 + return 1 + fi + case "$kernel" in + *[Mm][Ii][Cc][Rr][Oo][Ss][Oo][Ff][Tt]*) + if ! command -v shutdown.exe >/dev/null 2>&1; then + printf '%s\n' 'shutdown helpers: WSL requires shutdown.exe on PATH and Windows interop enabled' >&2 + return 1 + fi + printf '%s\n' wsl + ;; + *) + if [[ -n "${WSL_DISTRO_NAME:-}" || -n "${WSL_INTEROP:-}" ]]; then + printf '%s\n' 'shutdown helpers: ambiguous platform: Linux kernel lacks Microsoft marker but WSL_DISTRO_NAME or WSL_INTEROP is set; verify the platform and correct the environment manually' >&2 + return 1 + fi + printf '%s\n' linux + ;; + esac + ;; + Darwin) printf '%s\n' macos ;; + *) printf 'shutdown helpers: unsupported OS: %s\n' "$os" >&2; return 1 ;; + esac +} -# Shut down immediately +# Shut down immediately. WSL interop failures never fall back to native shutdown. sdn() { - sudo shutdown -h now + local platform + platform=$(_dev_setup_shutdown_platform) || return $? + case "$platform" in + wsl) shutdown.exe /s /t 0 ;; + linux|macos) sudo shutdown -h now ;; + esac } # Timed shutdown — usage: tsdn tsdn() { - if [[ ! "$1" =~ ^[1-9][0-9]*$ ]]; then - echo "tsdn: minutes must be a positive integer" + if [[ $# -ne 1 || ! "${1:-}" =~ ^[1-9][0-9]*$ ]]; then + printf '%s\n' 'tsdn: usage: tsdn (one positive decimal integer, no leading zeros)' >&2 return 1 fi - sudo shutdown -h +"$1" + local platform minutes="$1" + platform=$(_dev_setup_shutdown_platform) || return $? + case "$platform" in + wsl) + # Bound the decimal string before arithmetic (also safe on Bash 3.2). + if [[ ${#minutes} -gt 7 ]] || [[ ${#minutes} -eq 7 && "$minutes" -gt 5256000 ]]; then + printf '%s\n' 'tsdn: WSL delay must not exceed 5256000 minutes' >&2 + return 1 + fi + printf '%s\n' 'tsdn: warning: a positive Windows timeout forces applications to close; unsaved work may be lost.' >&2 + shutdown.exe /s /t "$((minutes * 60))" + ;; + linux|macos) sudo shutdown -h +"$minutes" ;; + esac } -# Cancel a pending timed shutdown (OS-aware) +# Cancel a pending shutdown system-wide, preserving the command's diagnostics/status. cancel_tsdn() { - case "$(uname)" in - Linux*) cmd="sudo shutdown -c" ;; - Darwin*) cmd="sudo killall shutdown" ;; - *) echo "cancel_tsdn: unsupported OS"; return 1 ;; + local platform + platform=$(_dev_setup_shutdown_platform) || return $? + case "$platform" in + wsl) shutdown.exe /a ;; + linux) sudo shutdown -c ;; + macos) sudo killall shutdown ;; esac - if ! eval "$cmd" 2>/dev/null; then - echo "No pending shutdown to cancel." - fi } diff --git a/tests/test_alias_parity.sh b/tests/test_alias_parity.sh index 8673ffa..1649c76 100644 --- a/tests/test_alias_parity.sh +++ b/tests/test_alias_parity.sh @@ -68,6 +68,7 @@ ALLOWED_ALIAS_DRIFT=( "touch:windows" # Set-FileTimestamp -- Windows-specific override "start_up:linux" # shell startup function -- Linux-specific "create_tmux:linux" # tmux session create -- Linux uses tmux directly + "_dev_setup_shutdown_platform:linux" # Private Bash/Zsh WSL/native shutdown selector "New-PsmuxSession:windows" # Windows equivalent of create_tmux # v5.2 profile-path resolution helpers (issue #441/#442) -- Windows-only PS # internal functions; they query $PROFILE on PS hosts and have no Linux equiv. diff --git a/tests/test_shutdown_helpers.sh b/tests/test_shutdown_helpers.sh new file mode 100644 index 0000000..bff4798 --- /dev/null +++ b/tests/test_shutdown_helpers.sh @@ -0,0 +1,401 @@ +#!/usr/bin/env bash +# Isolated shutdown-helper tests. NEVER source the full aliases file or live aliases. +# Usage: bash tests/test_shutdown_helpers.sh [--bash /path/to/bash] [--zsh /path/to/zsh] +# Both shells are mandatory. The driver also supports macOS /bin/bash 3.2. +# Review this harness AND the extracted production functions before first execution. +# This isolates trusted, reviewed source; it is not a malicious-code sandbox. +set -uo pipefail + +PASS=0 +FAIL=0 +pass() { printf 'PASS: %s\n' "$1"; PASS=$((PASS + 1)); } +fail() { printf 'FAIL: %s\n' "$1"; FAIL=$((FAIL + 1)); } +die() { printf 'ERROR: %s\n' "$1" >&2; exit 1; } + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" || exit 1 +BASH_PATH=/bin/bash +ZSH_PATH="$(command -v zsh || :)" +while [[ $# -gt 0 ]]; do + case "$1" in + --bash|--zsh) + [[ $# -ge 2 ]] || die "$1 requires an absolute shell path" + case "$1" in + --bash) BASH_PATH=$2 ;; + --zsh) ZSH_PATH=$2 ;; + esac + shift 2 + ;; + *) die "unknown argument: $1" ;; + esac +done +for shell_path in "$BASH_PATH" "$ZSH_PATH"; do + [[ "$shell_path" == /* && -x "$shell_path" && ! -d "$shell_path" ]] || + die "Bash and Zsh are both required; supply executable absolute paths with --bash/--zsh" +done +[[ -x /bin/sh ]] || die '/bin/sh is required for non-forwarding stubs' +for tool in awk cat chmod cmp cp env mkdir mktemp rm; do + command -v "$tool" >/dev/null 2>&1 || die "required fixture tool missing: $tool" +done +ENV_PATH="$(command -v env)" +[[ "$ENV_PATH" == /* ]] || die 'env must resolve to an absolute executable' +mkdir -p "$REPO_ROOT/.pi-herdsman" || die 'cannot create fixture parent' +FIXTURE="$(mktemp -d "$REPO_ROOT/.pi-herdsman/shutdown-tests.XXXXXX")" || die 'mktemp failed' +[[ "$FIXTURE" == "$REPO_ROOT"/.pi-herdsman/shutdown-tests.* && -d "$FIXTURE" ]] || + die 'unexpected fixture directory' +trap 'rm -rf -- "$FIXTURE"' EXIT +mkdir "$FIXTURE/bin" "$FIXTURE/no-exe" "$FIXTURE/home" || die 'cannot create fixture directories' +DEFINITIONS="$FIXTURE/definitions.sh" + +# Copy only these actual definitions, verbatim. No substitution, eval, or whole-file +# sourcing. Fail closed on missing/duplicate functions or changed closing layout. +# The reviewed functions use column-zero closing braces and indented body lines. +awk ' + /^(_dev_setup_shutdown_platform|sdn|tsdn|cancel_tsdn)\(\) \{$/ { + if (copying) exit 1 + name = $0 + sub(/\(\).*/, "", name) + count[name]++ + copying = 1 + } + copying { print } + copying && /^}$/ { copying = 0 } + END { + if (copying || count["_dev_setup_shutdown_platform"] != 1 || + count["sdn"] != 1 || count["tsdn"] != 1 || count["cancel_tsdn"] != 1) exit 1 + } +' "$REPO_ROOT/config/dotfiles/.aliases" > "$DEFINITIONS" || die 'helper extraction failed' +[[ -s "$DEFINITIONS" ]] || die 'empty helper extraction' + +# Every executable on the child PATH is a copy of this stand-in. Even sudo only +# records; it NEVER executes its arguments. Probe reads are limited to the exact +# production cat /proc/version call and never consult the host kernel. +cat > "$FIXTURE/stand-in" <<'STUB' || die 'cannot write stand-in' +#!/bin/sh +set -u +name=${0##*/} +case "$name" in + uname|cat) + printf '%s\000' "$name" "$#" "$@" >> "$TEST_PROBES" || exit 98 + case "$name" in + uname) + [ "$#" -eq 1 ] && [ "$1" = -s ] || exit 98 + printf '%s' "$TEST_OS" + exit "$TEST_OS_STATUS" + ;; + cat) + [ "$#" -eq 1 ] && [ "$1" = /proc/version ] || exit 98 + case "$TEST_KERNEL_MODE" in + ok) printf '%s' "$TEST_KERNEL" ;; + empty) : ;; + missing) printf 'fixture: /proc/version missing\n' >&2; exit 1 ;; + unreadable) printf 'fixture: /proc/version permission denied\n' >&2; exit 1 ;; + error) printf '%s' "$TEST_KERNEL"; exit 74 ;; + *) exit 98 ;; + esac + ;; + esac + ;; + shutdown.exe|sudo|shutdown|killall) + # NUL fields retain exact argument boundaries, including empty arguments, + # whitespace and shell metacharacters. Count disambiguates multiple calls. + printf '%s\000' "$name" "$#" "$@" >> "$TEST_OPERATIONS" || exit 98 + [ -z "$TEST_OP_OUT" ] || printf '%s\n' "$TEST_OP_OUT" + [ -z "$TEST_OP_ERR" ] || printf '%s\n' "$TEST_OP_ERR" >&2 + exit "$TEST_OP_STATUS" + ;; + *) exit 98 ;; +esac +STUB +for name in uname cat sudo shutdown killall shutdown.exe; do + cp "$FIXTURE/stand-in" "$FIXTURE/bin/$name" || die "cannot copy recording stub: $name" + chmod +x "$FIXTURE/bin/$name" || die "cannot enable recording stub: $name" + if [[ "$name" != shutdown.exe ]]; then + cp "$FIXTURE/stand-in" "$FIXTURE/no-exe/$name" || die "cannot copy missing-exe fixture: $name" + chmod +x "$FIXTURE/no-exe/$name" || die "cannot enable missing-exe fixture: $name" + fi +done + +cat > "$FIXTURE/runner.sh" <<'RUNNER' || die 'cannot write child runner' +# This runner uses only Bash/Zsh builtins and the extracted definitions. +set -u +case "$TEST_SHELL_KIND" in + bash) [ -n "${BASH_VERSION:-}" ] || exit 98 ;; + zsh) [ -n "${ZSH_VERSION:-}" ] || exit 98 ;; + *) exit 98 ;; +esac +# env -i clears exported functions, WSL hints, ENV/BASH_ENV, shell options, etc. +# Bash uses --noprofile --norc; Zsh uses -df and an empty HOME/ZDOTDIR. Zsh still +# reads the host /etc/zshenv: that trusted system file must not defeat isolation. +# Abort if startup changed PATH or command resolution, before loading helpers. +unalias -a +assert_isolation() { + [ "$PATH" = "$TEST_BIN" ] || exit 98 + for required in sudo shutdown killall uname cat; do + [ "$(command -v "$required")" = "$TEST_BIN/$required" ] || exit 98 + [ -x "$TEST_BIN/$required" ] || exit 98 + done + if [ "$TEST_HAS_EXE" = yes ]; then + [ "$(command -v shutdown.exe)" = "$TEST_BIN/shutdown.exe" ] || exit 98 + [ -x "$TEST_BIN/shutdown.exe" ] || exit 98 + else + ! command -v shutdown.exe >/dev/null 2>&1 || exit 98 + fi +} +assert_isolation +unset WSL_DISTRO_NAME WSL_INTEROP +case "$TEST_HINTS" in + none) : ;; + empty) export WSL_DISTRO_NAME='' WSL_INTEROP='' ;; + distro) export WSL_DISTRO_NAME=fixture-distro ;; + interop) export WSL_INTEROP=/fixture/interop ;; + both) export WSL_DISTRO_NAME=fixture-distro WSL_INTEROP=/fixture/interop ;; + *) exit 98 ;; +esac +[ -f "$1" ] && [ -s "$1" ] || exit 98 +# shellcheck disable=SC1090 +. "$1" || exit 98 +shift +for required in _dev_setup_shutdown_platform sdn tsdn cancel_tsdn; do + typeset -f "$required" >/dev/null || exit 98 +done +assert_isolation +[ ! -s "$TEST_OPERATIONS" ] && [ ! -s "$TEST_PROBES" ] || exit 98 +printf 'loaded\n' > "$TEST_LOADED" || exit 98 +case "${1:-}" in + --load) exit 0 ;; + sdn|tsdn|cancel_tsdn) "$@"; exit $? ;; + *) exit 98 ;; +esac +RUNNER + +# Expected logs use the same documented NUL wire format, not the implementation. +expect_operation() { + : > "$FIXTURE/expected-operations" || die 'cannot reset expected operations' + if [[ $# -gt 0 ]]; then + local command_name=$1 + shift + printf '%s\000' "$command_name" "$#" "$@" > "$FIXTURE/expected-operations" || die 'cannot write expected operation' + fi +} +expect_probes() { + : > "$FIXTURE/expected-probes" || die 'cannot reset expected probes' + case "$1" in + linux) + printf '%s\000' uname 1 -s cat 1 /proc/version > "$FIXTURE/expected-probes" || die 'cannot write expected probes' + ;; + os) printf '%s\000' uname 1 -s > "$FIXTURE/expected-probes" || die 'cannot write expected probe' ;; + none) : ;; + *) die 'invalid expected probe kind' ;; + esac +} +reset_case() { + OS=Linux + OS_STATUS=0 + KERNEL='Linux version 6.6.0-native' + KERNEL_MODE=ok + HINTS=none + HAS_EXE=yes + OP_STATUS=0 + OP_OUT='' + OP_ERR='' + EXPECT_OUT='' + expect_operation + expect_probes linux +} +run_case() { + local label="$SHELL_KIND: $1" expected_status=$2 expected_error=$3 + shift 3 + local bin="$FIXTURE/bin" actual_status + [[ "$HAS_EXE" == yes ]] || bin="$FIXTURE/no-exe" + : > "$FIXTURE/operations" || die 'cannot reset operation log' + : > "$FIXTURE/probes" || die 'cannot reset probe log' + : > "$FIXTURE/loaded" || die 'cannot reset load marker' + "$ENV_PATH" -i PATH="$bin" HOME="$FIXTURE/home" ZDOTDIR="$FIXTURE/home" LC_ALL=C \ + TEST_BIN="$bin" TEST_HAS_EXE="$HAS_EXE" TEST_HINTS="$HINTS" TEST_SHELL_KIND="$SHELL_KIND" \ + TEST_OS="$OS" TEST_OS_STATUS="$OS_STATUS" TEST_KERNEL="$KERNEL" TEST_KERNEL_MODE="$KERNEL_MODE" \ + TEST_OP_STATUS="$OP_STATUS" TEST_OP_OUT="$OP_OUT" TEST_OP_ERR="$OP_ERR" \ + TEST_OPERATIONS="$FIXTURE/operations" TEST_PROBES="$FIXTURE/probes" TEST_LOADED="$FIXTURE/loaded" \ + "$SHELL_PATH" "${SHELL_OPTIONS[@]}" "$FIXTURE/runner.sh" "$DEFINITIONS" "$@" \ + > "$FIXTURE/stdout" 2> "$FIXTURE/stderr" + actual_status=$? + # An absent load marker is an isolation/setup failure, not an expected helper + # failure. Stop rather than proceeding with a possibly broken fixture. + [[ "$(cat "$FIXTURE/loaded")" == loaded ]] || die "$label: isolation/definition-load gate failed (status $actual_status)" + if [[ "$actual_status" -eq "$expected_status" ]]; then pass "$label status"; + else fail "$label status: expected $expected_status, got $actual_status"; fi + if cmp -s "$FIXTURE/expected-operations" "$FIXTURE/operations"; then pass "$label exact operations"; + else fail "$label exact operations"; fi + if cmp -s "$FIXTURE/expected-probes" "$FIXTURE/probes"; then pass "$label exact probes"; + else fail "$label exact probes"; fi + : > "$FIXTURE/expected-stdout" || die 'cannot reset expected stdout' + : > "$FIXTURE/expected-stderr" || die 'cannot reset expected stderr' + if [[ -n "$EXPECT_OUT" ]]; then printf '%s\n' "$EXPECT_OUT" > "$FIXTURE/expected-stdout" || die 'cannot write expected stdout'; fi + if [[ -n "$expected_error" ]]; then printf '%s\n' "$expected_error" > "$FIXTURE/expected-stderr" || die 'cannot write expected stderr'; fi + if cmp -s "$FIXTURE/expected-stdout" "$FIXTURE/stdout"; then pass "$label stdout"; + else fail "$label stdout"; fi + if cmp -s "$FIXTURE/expected-stderr" "$FIXTURE/stderr"; then pass "$label stderr"; + else fail "$label stderr"; fi +} + +WARNING='tsdn: warning: a positive Windows timeout forces applications to close; unsaved work may be lost.' +AMBIGUOUS='shutdown helpers: ambiguous platform: Linux kernel lacks Microsoft marker but WSL_DISTRO_NAME or WSL_INTEROP is set; verify the platform and correct the environment manually' +KERNEL_ERROR='shutdown helpers: unable to read a nonempty /proc/version; platform is indeterminate' +OS_ERROR='shutdown helpers: unable to detect OS with uname -s' +INTEROP_ERROR='shutdown helpers: WSL requires shutdown.exe on PATH and Windows interop enabled' +INPUT_ERROR='tsdn: usage: tsdn (one positive decimal integer, no leading zeros)' +LIMIT_ERROR='tsdn: WSL delay must not exceed 5256000 minutes' + +# Shared matrix assertions call only the public production helpers. The selector +# is exercised transitively, so routing must stay correct through each entrypoint. +check_route() { + local label=$1 route=$2 + case "$route" in + wsl) expect_operation shutdown.exe /s /t 0 ;; + linux|macos) expect_operation sudo shutdown -h now ;; + *) die 'unknown expected route' ;; + esac + run_case "$label immediate" 0 '' sdn + if [[ "$route" == wsl ]]; then + expect_operation shutdown.exe /s /t 120 + run_case "$label timed" 0 "$WARNING" tsdn 2 + else + expect_operation sudo shutdown -h +2 + run_case "$label timed" 0 '' tsdn 2 + fi + case "$route" in + wsl) expect_operation shutdown.exe /a ;; + linux) expect_operation sudo shutdown -c ;; + macos) expect_operation sudo killall shutdown ;; + esac + run_case "$label cancel" 0 '' cancel_tsdn +} +check_blocked() { + local label=$1 diagnostic=$2 + expect_operation + run_case "$label immediate" 1 "$diagnostic" sdn + run_case "$label timed" 1 "$diagnostic" tsdn 2 + run_case "$label cancel" 1 "$diagnostic" cancel_tsdn +} + +for SHELL_KIND in bash zsh; do + case "$SHELL_KIND" in + bash) SHELL_PATH=$BASH_PATH; SHELL_OPTIONS=(--noprofile --norc) ;; + zsh) SHELL_PATH=$ZSH_PATH; SHELL_OPTIONS=(-df) ;; + esac + reset_case + expect_probes none + run_case 'definition loading does nothing' 0 '' --load + + # Positive kernel evidence is sufficient even without interop environment. + for marker in 'Linux version 4.4.0-Microsoft' 'Linux version 5.15.0-microsoft-standard-WSL2' 'Linux version 6.6.0-MiCrOsOfT'; do + for hints in none empty distro interop both; do + reset_case; KERNEL=$marker; HINTS=$hints + check_route "WSL $marker hints=$hints" wsl + done + done + # Release-blocking native regression: Windows executable presence is irrelevant. + for hints in none empty; do + for executable in yes no; do + reset_case; HINTS=$hints; HAS_EXE=$executable + check_route "native Linux hints=$hints exe=$executable" linux + done + done + for hints in distro interop both; do + reset_case; HINTS=$hints + check_blocked "conflicting Linux hints=$hints" "$AMBIGUOUS" + done + for mode in missing unreadable empty error; do + for hints in none empty distro interop both; do + reset_case; KERNEL_MODE=$mode; HINTS=$hints + # Even partial Microsoft output from a failed cat is not evidence. + KERNEL='Linux version Microsoft' + check_blocked "kernel $mode hints=$hints" "$KERNEL_ERROR" + done + done + for hints in none empty distro interop both; do + reset_case; OS=Darwin; HINTS=$hints; KERNEL_MODE=error + expect_probes os + check_route "Darwin hints=$hints ignores kernel" macos + for unsupported in FreeBSD CYGWIN_NT-10.0 ''; do + reset_case; OS=$unsupported; HINTS=$hints + expect_probes os + check_blocked "unsupported OS=$unsupported hints=$hints" "shutdown helpers: unsupported OS: $unsupported" + done + # Failed uname must not trust even otherwise valid output. + for failed_os in Linux Darwin ''; do + reset_case; OS=$failed_os; OS_STATUS=71; HINTS=$hints + expect_probes os + check_blocked "failed uname output=$failed_os hints=$hints" "$OS_ERROR" + done + done + + # Input grammar is identical on all supported platforms, including set -u. + for route in wsl linux macos; do + reset_case + case "$route" in wsl) KERNEL='Linux Microsoft' ;; macos) OS=Darwin ;; esac + expect_probes none + run_case "$route missing minutes under nounset" 1 "$INPUT_ERROR" tsdn + run_case "$route extra minutes" 1 "$INPUT_ERROR" tsdn 1 2 + run_case "$route extra empty argument" 1 "$INPUT_ERROR" tsdn 1 '' + # Injection-shaped inputs must remain literal, never shell-expanded. + # shellcheck disable=SC2016 + for bad in '' 0 -1 01 00 +1 1.5 abc '1 2' ' 1' '1 ' $'1\n' '1;shutdown.exe /s /t 0' '$(shutdown.exe /s /t 0)' '`sudo shutdown -h now`' '1*2'; do + run_case "$route invalid [$bad]" 1 "$INPUT_ERROR" tsdn "$bad" + done + done + reset_case; KERNEL='Linux Microsoft' + expect_operation shutdown.exe /s /t 60 + run_case 'WSL one minute' 0 "$WARNING" tsdn 1 + expect_operation shutdown.exe /s /t 315360000 + run_case 'WSL maximum minutes' 0 "$WARNING" tsdn 5256000 + expect_operation + for oversized in 5256001 9999999 10000000 999999999999999999999999999999999999999999999999999999999999999999999999; do + run_case "WSL oversized $oversized" 1 "$LIMIT_ERROR" tsdn "$oversized" + done + for route in linux macos; do + reset_case + if [[ "$route" == macos ]]; then OS=Darwin; expect_probes os; fi + for minutes in 1 5256001 999999999999999999999999999999999999999999999999999999999999; do + expect_operation sudo shutdown -h "+$minutes" + run_case "$route unconverted minutes $minutes" 0 '' tsdn "$minutes" + done + done + + reset_case; KERNEL='Linux Microsoft'; HAS_EXE=no + check_blocked 'WSL executable absent: no native fallback' "$INTEROP_ERROR" + # Statuses are shell process results, NOT inferred Win32 error numbers. + # All commands preserve output and failures, including cancellation. sudo's + # non-forwarding behavior also means simulated native failures stay harmless. + for route in wsl linux macos; do + for result in 0 1 126 127; do + reset_case; OP_STATUS=$result + OP_OUT='fixture command stdout'; EXPECT_OUT=$OP_OUT + case "$result" in + 0) OP_ERR='fixture command diagnostic' ;; + 1) OP_ERR='fixture cancellation/command failure' ;; + 126) OP_ERR='fixture permission denied' ;; + 127) OP_ERR='fixture Windows interop disabled / command unavailable' ;; + esac + case "$route" in + wsl) KERNEL='Linux Microsoft'; expect_operation shutdown.exe /s /t 0 ;; + linux) expect_operation sudo shutdown -h now ;; + macos) OS=Darwin; expect_probes os; expect_operation sudo shutdown -h now ;; + esac + run_case "$route immediate command result=$result" "$result" "$OP_ERR" sdn + if [[ "$route" == wsl ]]; then + expect_operation shutdown.exe /s /t 120 + run_case "$route timed command result=$result" "$result" "$WARNING"$'\n'"$OP_ERR" tsdn 2 + expect_operation shutdown.exe /a + else + expect_operation sudo shutdown -h +2 + run_case "$route timed command result=$result" "$result" "$OP_ERR" tsdn 2 + if [[ "$route" == macos ]]; then expect_operation sudo killall shutdown; + else expect_operation sudo shutdown -c; fi + fi + run_case "$route cancellation result=$result" "$result" "$OP_ERR" cancel_tsdn + done + done +done + +printf '\nResults: %s passed, %s failed\n' "$PASS" "$FAIL" +[[ "$FAIL" -eq 0 ]]