feat: tests, demo, JSON output, VPN kill-switch, more issue responses
- tests/: bats test suite for 8 modules (suppress, heal, firewall, detect, validate, colors, dscl, backup) - demo: simulated full bypass flow for showcasing (no real changes) - --json flag for report and audit commands (machine-readable output) - vpn-kill: pf-based kill-switch that blocks MDM outside VPN tunnel - vpn-kill-remove / vpn-kill-status: manage the kill-switch - Responded to 5 more bypass-mdm issues - CI now includes all new lib files in ShellCheck - Bump 1.4.0 -> 1.5.0
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for backup.sh - run with: bats tests/test_backup.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/backup.sh'
|
||||
SCRIPT_DIR=$(mktemp -d)
|
||||
TEST_DIR=$(mktemp -d)
|
||||
mkdir -p "$TEST_DIR/private/etc"
|
||||
mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings"
|
||||
echo "test" > "$TEST_DIR/private/etc/hosts"
|
||||
BACKUP_DIR="$SCRIPT_DIR/.unleash-backup"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "$SCRIPT_DIR" "$TEST_DIR"
|
||||
}
|
||||
|
||||
@test "backup_state creates backup directory" {
|
||||
run backup_state "$TEST_DIR" 2>/dev/null || true
|
||||
[ -d "$BACKUP_DIR" ]
|
||||
}
|
||||
|
||||
@test "backup_state saves hosts file" {
|
||||
run backup_state "$TEST_DIR" 2>/dev/null || true
|
||||
[ -f "$BACKUP_DIR/hosts.backup" ]
|
||||
run cat "$BACKUP_DIR/hosts.backup"
|
||||
[ "$output" = "test" ]
|
||||
}
|
||||
|
||||
@test "has_backup returns false initially" {
|
||||
run has_backup
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "has_backup returns true after backup" {
|
||||
backup_state "$TEST_DIR" 2>/dev/null || true
|
||||
run has_backup
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for colors.sh - run with: bats tests/test_colors.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
TEST_LOG=$(mktemp)
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -f "$TEST_LOG"
|
||||
}
|
||||
|
||||
@test "log writes to LOG_FILE when set" {
|
||||
LOG_FILE="$TEST_LOG"
|
||||
log "INFO" "test message"
|
||||
run grep "test message" "$TEST_LOG"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "log respects VERBOSE for DEBUG" {
|
||||
VERBOSE=false
|
||||
run log "DEBUG" "hidden message"
|
||||
[ -z "$output" ]
|
||||
}
|
||||
|
||||
@test "log shows DEBUG when VERBOSE is true" {
|
||||
VERBOSE=true
|
||||
run log "DEBUG" "visible message"
|
||||
echo "$output" | grep -q "visible"
|
||||
}
|
||||
|
||||
@test "error_exit exits with non-zero" {
|
||||
run error_exit "test error" 2>/dev/null
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "header outputs formatted title" {
|
||||
run header "Test Title"
|
||||
[ "$status" -eq 0 ]
|
||||
echo "$output" | grep -q "Test Title"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for detect.sh - run with: bats tests/test_detect.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/detect.sh'
|
||||
}
|
||||
|
||||
@test "is_root returns true when EUID is 0" {
|
||||
EUID=0
|
||||
run is_root
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "is_root returns false when EUID is not 0" {
|
||||
EUID=501
|
||||
run is_root
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "is_recovery returns false in normal environment" {
|
||||
run is_recovery
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "detect_system_volume returns empty when no sys vol found" {
|
||||
run detect_system_volume "/nonexistent"
|
||||
[ -z "$output" ] || [ "$output" = "" ]
|
||||
}
|
||||
|
||||
@test "resolve_data_volume errors on non-existent device" {
|
||||
run resolve_data_volume 2>/dev/null || true
|
||||
[ "$status" -ne 0 ]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for dscl.sh - run with: bats tests/test_dscl.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/dscl.sh'
|
||||
TEST_DIR=$(mktemp -d)
|
||||
mkdir -p "$TEST_DIR/private/var/db/dslocal/nodes/Default"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "$TEST_DIR"
|
||||
}
|
||||
|
||||
@test "dscl_node returns correct path" {
|
||||
run dscl_node "$TEST_DIR"
|
||||
[ "$output" = "$TEST_DIR/private/var/db/dslocal/nodes/Default" ]
|
||||
}
|
||||
|
||||
@test "find_available_uid returns 501 for empty node" {
|
||||
local node
|
||||
node=$(dscl_node "$TEST_DIR")
|
||||
run find_available_uid "$node"
|
||||
[ "$output" = "501" ]
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for firewall.sh - run with: bats tests/test_firewall.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/firewall.sh'
|
||||
TEST_DIR=$(mktemp -d)
|
||||
mkdir -p "$TEST_DIR/etc/pf.anchors/com.unleash"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "$TEST_DIR"
|
||||
}
|
||||
|
||||
@test "install_pf_mdm_block creates anchor file" {
|
||||
FIREWALL_ANCHOR_DIR="/etc/pf.anchors"
|
||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||
[ -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ]
|
||||
}
|
||||
|
||||
@test "install_pf_mdm_block contains Apple IP ranges" {
|
||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||
run grep -c "17.0.0.0/8" "$TEST_DIR/etc/pf.anchors/com.unleash/mdm"
|
||||
[ "$output" -gt 0 ]
|
||||
}
|
||||
|
||||
@test "install_pf_mdm_block updates pf.conf" {
|
||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf"
|
||||
[ "$output" -gt 0 ]
|
||||
}
|
||||
|
||||
@test "remove_pf_mdm_block removes anchor and pf.conf entries" {
|
||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||
remove_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||
[ ! -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ]
|
||||
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf" 2>/dev/null || echo "0"
|
||||
[ "$output" = "0" ]
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for heal.sh - run with: bats tests/test_heal.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/suppress.sh'
|
||||
load '../lib/heal.sh'
|
||||
TEST_DIR=$(mktemp -d)
|
||||
mkdir -p "$TEST_DIR/private/etc"
|
||||
mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings"
|
||||
mkdir -p "$TEST_DIR/private/var/db/com.apple.xpc.launchd"
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "$TEST_DIR"
|
||||
}
|
||||
|
||||
@test "heal detects clean system" {
|
||||
run heal_suppress "$TEST_DIR" 2>/dev/null || true
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "heal re-applies when DEP record reappears" {
|
||||
touch "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound"
|
||||
heal_suppress "$TEST_DIR" 2>/dev/null || true
|
||||
[ ! -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]
|
||||
}
|
||||
|
||||
@test "heal re-applies when hosts block missing" {
|
||||
> "$TEST_DIR/private/etc/hosts"
|
||||
heal_suppress "$TEST_DIR" 2>/dev/null || true
|
||||
run grep -c "iprofiles.apple.com" "$TEST_DIR/private/etc/hosts"
|
||||
[ "$output" -gt 0 ]
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for suppress.sh - run with: bats tests/test_suppress.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/detect.sh'
|
||||
load '../lib/suppress.sh'
|
||||
TEST_DIR=$(mktemp -d)
|
||||
mkdir -p "$TEST_DIR/private/etc"
|
||||
mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings"
|
||||
mkdir -p "$TEST_DIR/private/var/db/com.apple.xpc.launchd"
|
||||
mkdir -p "$TEST_DIR/Users/testuser/Library/Preferences"
|
||||
mkdir -p "$TEST_DIR/Users/testuser/Library/Application Support"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "$TEST_DIR"
|
||||
}
|
||||
|
||||
@test "suppress_enrollment creates hosts entries" {
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
run grep -c "iprofiles.apple.com" "$TEST_DIR/private/etc/hosts"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" -gt 0 ]
|
||||
}
|
||||
|
||||
@test "suppress_enrollment clears DEP markers" {
|
||||
touch "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound"
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
[ ! -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]
|
||||
[ -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordNotFound" ]
|
||||
}
|
||||
|
||||
@test "suppress_enrollment disables daemons" {
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
local ldp="$TEST_DIR/private/var/db/com.apple.xpc.launchd/disabled.plist"
|
||||
[ -f "$ldp" ]
|
||||
run /usr/libexec/PlistBuddy -c "Print :com.apple.ManagedClient.enroll" "$ldp" 2>/dev/null
|
||||
[ "$output" = "true" ]
|
||||
}
|
||||
|
||||
@test "dry-run mode does not write files" {
|
||||
DRY_RUN=true
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
[ ! -f "$TEST_DIR/private/etc/hosts" ]
|
||||
[ ! -f "$TEST_DIR/private/var/db/com.apple.xpc.launchd/disabled.plist" ]
|
||||
}
|
||||
|
||||
@test "suppress_enrollment cleans user artifacts" {
|
||||
touch "$TEST_DIR/Users/testuser/Library/Preferences/com.apple.mdm.test"
|
||||
touch "$TEST_DIR/Users/testuser/Library/Application Support/com.apple.ManagedClient.cache"
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
[ ! -f "$TEST_DIR/Users/testuser/Library/Preferences/com.apple.mdm.test" ]
|
||||
[ ! -f "$TEST_DIR/Users/testuser/Library/Application Support/com.apple.ManagedClient.cache" ]
|
||||
}
|
||||
|
||||
@test "suppress_enrollment blocks org MDM host" {
|
||||
mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings"
|
||||
cat > "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" <<'XML'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0"><dict>
|
||||
<key>OrganizationName</key><string>ACME Corp</string>
|
||||
<key>URL</key><string>https://mdm.acme.com/enroll</string>
|
||||
</dict></plist>
|
||||
XML
|
||||
DRY_RUN=false
|
||||
suppress_enrollment "$TEST_DIR" 2>/dev/null || true
|
||||
run grep -c "mdm.acme.com" "$TEST_DIR/private/etc/hosts"
|
||||
[ "$output" -gt 0 ]
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for validate.sh - run with: bats tests/test_validate.bats
|
||||
|
||||
setup() {
|
||||
load '../lib/colors.sh'
|
||||
load '../lib/validate.sh'
|
||||
}
|
||||
|
||||
@test "validate_username accepts valid usernames" {
|
||||
run validate_username "apple"
|
||||
[ "$status" -eq 0 ]
|
||||
run validate_username "test-user"
|
||||
[ "$status" -eq 0 ]
|
||||
run validate_username "test_user"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "validate_username rejects empty" {
|
||||
run validate_username ""
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "validate_username rejects long names" {
|
||||
local long="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
run validate_username "$long"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "validate_username rejects special chars" {
|
||||
run validate_username "user name"
|
||||
[ "$status" -eq 1 ]
|
||||
run validate_username "user.name"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "validate_password accepts valid passwords" {
|
||||
run validate_password "1234"
|
||||
[ "$status" -eq 0 ]
|
||||
run validate_password "correct horse battery staple"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "validate_password rejects empty" {
|
||||
run validate_password ""
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "validate_password rejects short" {
|
||||
run validate_password "ab"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
Reference in New Issue
Block a user