From 5a7c832b9e44fe404e10c1ffac5887026fdfe51a Mon Sep 17 00:00:00 2001 From: ruihan Date: Fri, 30 Oct 2026 11:34:53 -0300 Subject: [PATCH] fix(doctor): distinguish failed queries from missing Wi-Fi --- lib/doctor.sh | 34 ++++++++++++--- tests/test_network_states.bats | 76 ++++++++++++++++++++++++++++++++++ unleash-standalone.sh | 34 ++++++++++++--- 3 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 tests/test_network_states.bats diff --git a/lib/doctor.sh b/lib/doctor.sh index b754245..95e75e6 100644 --- a/lib/doctor.sh +++ b/lib/doctor.sh @@ -7,8 +7,16 @@ _network_diagnostics() { fi printf 'macOS: %s (build %s)\n' "$version" "$build" case "$version" in 27.*) warn "macOS 27 hardware compatibility remains unverified (issue #22)." ;; esac - local interface - interface=$(networksetup -listallhardwareports 2>/dev/null | awk ' + local interface ports + if ! command -v networksetup >/dev/null; then + warn "Wi-Fi diagnostics unavailable: networksetup is missing." + return 1 + fi + if ! ports=$(networksetup -listallhardwareports 2>/dev/null); then + warn "Could not query Wi-Fi hardware ports." + return 1 + fi + interface=$(printf '%s\n' "$ports" | awk ' /Hardware Port:/ { wifi=($0 ~ /: (Wi-Fi|AirPort)$/); next } wifi && /^[[:space:]]*Device:/ { print $2; exit } ' || true) @@ -16,11 +24,25 @@ _network_diagnostics() { info "No Wi-Fi hardware port reported." return 0 fi - local power status - power=$(networksetup -getairportpower "$interface" 2>/dev/null || true) + local power status details ipv4 + if ! power=$(networksetup -getairportpower "$interface" 2>/dev/null); then + warn "Could not query Wi-Fi power." + return 1 + fi + if ! details=$(ifconfig "$interface" 2>/dev/null); then + warn "Could not query Wi-Fi link state." + return 1 + fi case "$power" in *': On') power=on ;; *': Off') power=off ;; *) power=unavailable ;; esac - status=$(ifconfig "$interface" 2>/dev/null | awk '/^[[:space:]]*status:/ {print $2; exit}' || true) - printf 'Wi-Fi interface: %s\nWi-Fi power: %s\nLink status: %s\n' "$interface" "$power" "${status:-unavailable}" + status=$(printf '%s\n' "$details" | awk '/^[[:space:]]*status:/ {print $2; exit}' || true) + ipv4=$(printf '%s\n' "$details" | awk ' + /^[[:space:]]*inet / { + if ($2 ~ /^169[.]254[.]/) { state="link-local"; next } + if ($2 != "0.0.0.0") { state="configured"; exit } + } + END { print (state ? state : "none") } + ') + printf 'Wi-Fi interface: %s\nWi-Fi power: %s\nLink status: %s\nWi-Fi IPv4: %s\n' "$interface" "$power" "${status:-unavailable}" "$ipv4" } run_doctor() { diff --git a/tests/test_network_states.bats b/tests/test_network_states.bats new file mode 100644 index 0000000..16b5b01 --- /dev/null +++ b/tests/test_network_states.bats @@ -0,0 +1,76 @@ +#!/usr/bin/env bats + +run_network_state() { + local script="$1" mode="$2" + local definitions="$BATS_TEST_TMPDIR/network.functions" + sed '/^main "\$@"$/d' "$script" > "$definitions" + run "${UNLEASH_TEST_SHELL:-bash}" -u -c ' + source "$1" + mode="$2" + load_config() { :; } + sw_vers() { echo fixture; } + command() { + if [ "$mode" = missing ] && [ "$*" = "-v networksetup" ]; then return 1; fi + builtin command "$@" + } + networksetup() { + case "$*" in + -listallhardwareports) + case "$mode" in portsfail|missing) echo PRIVATE-DATA >&2; return 1 ;; esac + printf "Hardware Port: Ethernet\nDevice: en0\n" + [ "$mode" != absent ] || return 0 + printf "Hardware Port: Wi-Fi\nDevice: en7\nEthernet Address: PRIVATE-MAC\n" ;; + "-getairportpower en7") + [ "$mode" != powerfail ] || { echo PRIVATE-DATA >&2; return 1; } + echo "Wi-Fi Power (en7): On" ;; + *) return 97 ;; + esac + } + ifconfig() { + [ "$*" = en7 ] || return 97 + [ "$mode" != linkfail ] || { echo PRIVATE-DATA >&2; return 1; } + printf "en7: flags\n ether PRIVATE-MAC\n status: active\n" + case "$mode" in + configured) printf " inet 192.0.2.7 netmask 0xffffff00\n" ;; + linklocal) printf " inet 169.254.8.9 netmask 0xffff0000\n" ;; + esac + } + main doctor --network + ' "$script" "$definitions" "$mode" +} + +@test "network doctor distinguishes query failure from absent hardware" { + local entrypoint mode + for entrypoint in unleash unleash-standalone.sh; do + for mode in missing portsfail powerfail linkfail; do + run_network_state "$BATS_TEST_DIRNAME/../$entrypoint" "$mode" + [ "$status" -ne 0 ] + [[ "$output" != *"No Wi-Fi hardware port"* ]] + [[ "$output" != *"PRIVATE-"* ]] + done + done +} + +@test "network doctor classifies IPv4 state without exposing addresses" { + local entrypoint mode expected + for entrypoint in unleash unleash-standalone.sh; do + for mode in configured linklocal none; do + expected="$mode"; [ "$mode" != linklocal ] || expected=link-local + run_network_state "$BATS_TEST_DIRNAME/../$entrypoint" "$mode" + [ "$status" -eq 0 ] + [[ "$output" == *"Wi-Fi IPv4: $expected"* ]] + [[ "$output" != *"192.0.2.7"* ]] + [[ "$output" != *"169.254.8.9"* ]] + [[ "$output" != *"PRIVATE-"* ]] + done + done +} + +@test "network doctor reports absent Wi-Fi after a successful hardware query" { + local entrypoint + for entrypoint in unleash unleash-standalone.sh; do + run_network_state "$BATS_TEST_DIRNAME/../$entrypoint" absent + [ "$status" -eq 0 ] + [[ "$output" == *"No Wi-Fi hardware port reported"* ]] + done +} diff --git a/unleash-standalone.sh b/unleash-standalone.sh index 15e82b0..1c40249 100755 --- a/unleash-standalone.sh +++ b/unleash-standalone.sh @@ -1905,8 +1905,16 @@ _network_diagnostics() { fi printf 'macOS: %s (build %s)\n' "$version" "$build" case "$version" in 27.*) warn "macOS 27 hardware compatibility remains unverified (issue #22)." ;; esac - local interface - interface=$(networksetup -listallhardwareports 2>/dev/null | awk ' + local interface ports + if ! command -v networksetup >/dev/null; then + warn "Wi-Fi diagnostics unavailable: networksetup is missing." + return 1 + fi + if ! ports=$(networksetup -listallhardwareports 2>/dev/null); then + warn "Could not query Wi-Fi hardware ports." + return 1 + fi + interface=$(printf '%s\n' "$ports" | awk ' /Hardware Port:/ { wifi=($0 ~ /: (Wi-Fi|AirPort)$/); next } wifi && /^[[:space:]]*Device:/ { print $2; exit } ' || true) @@ -1914,11 +1922,25 @@ _network_diagnostics() { info "No Wi-Fi hardware port reported." return 0 fi - local power status - power=$(networksetup -getairportpower "$interface" 2>/dev/null || true) + local power status details ipv4 + if ! power=$(networksetup -getairportpower "$interface" 2>/dev/null); then + warn "Could not query Wi-Fi power." + return 1 + fi + if ! details=$(ifconfig "$interface" 2>/dev/null); then + warn "Could not query Wi-Fi link state." + return 1 + fi case "$power" in *': On') power=on ;; *': Off') power=off ;; *) power=unavailable ;; esac - status=$(ifconfig "$interface" 2>/dev/null | awk '/^[[:space:]]*status:/ {print $2; exit}' || true) - printf 'Wi-Fi interface: %s\nWi-Fi power: %s\nLink status: %s\n' "$interface" "$power" "${status:-unavailable}" + status=$(printf '%s\n' "$details" | awk '/^[[:space:]]*status:/ {print $2; exit}' || true) + ipv4=$(printf '%s\n' "$details" | awk ' + /^[[:space:]]*inet / { + if ($2 ~ /^169[.]254[.]/) { state="link-local"; next } + if ($2 != "0.0.0.0") { state="configured"; exit } + } + END { print (state ? state : "none") } + ') + printf 'Wi-Fi interface: %s\nWi-Fi power: %s\nLink status: %s\nWi-Fi IPv4: %s\n' "$interface" "$power" "${status:-unavailable}" "$ipv4" } run_doctor() {