# ============================================================================== # Test-HarnessSmokeV3.ps1 - Complete Smoke V3 with Real Process & Chaos Proofs # ============================================================================== $ErrorActionPreference = "Continue" $root = $PSScriptRoot if (-not $root) { $root = Get-Location } Import-Module (Join-Path $root "modules\NetworkChaos.psm1") -Force Import-Module (Join-Path $root "modules\ProcessChaos.psm1") -Force Import-Module (Join-Path $root "modules\Performance.psm1") -Force Import-Module (Join-Path $root "modules\UpdateChaos.psm1") -Force Import-Module (Join-Path $root "modules\Assertions.psm1") -Force Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " Starting ClassNotify Test Harness Smoke Test V3 " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $smokePassed = $true # Track specific proof points requested by ChatGPT $proofResults = @{ RealNetDisconnect = $false RealNetReconnect = $false WatchdogExecuted = $false FirewallAutoReverted = $false WpfKilled = $false WpfRecovered = $false IpcAfterWpfRecovery = $false ServiceCrash = $false ServiceRecovery = $false SignalRReconnected = $false ServiceHealthStatus = $false EnvironmentClean = $false } # ------------------------------------------------------------------------------ # Test 1: Safe Startup Cleanup # ------------------------------------------------------------------------------ Write-Host "`n[1/10] Verifying Safe Startup Cleanup Invariant..." -ForegroundColor Yellow Clear-AllTestFirewallRules $activeRules = netsh advfirewall firewall show rule name=all | Select-String "ClassNotify-Chaos-" if ($null -eq $activeRules) { Write-Host " [PASS] Startup cleanup invariant: 0 stale rules." -ForegroundColor Green } else { Write-Host " [FAIL] Stale firewall rules remain after cleanup!" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 2 (Action B): Durable Watchdog Autonomous Execution # ------------------------------------------------------------------------------ Write-Host "`n[2/10] Action B: Testing Durable Watchdog Autonomous Execution..." -ForegroundColor Yellow $watchdogTask = "ClassNotifyChaos-NetWatchdog" $testRuleName = "ClassNotify-Chaos-WatchdogAutoTest" try { # 1. Add temporary test firewall rule netsh advfirewall firewall add rule name="$testRuleName" dir=out action=block program="C:\Windows\System32\cmd.exe" enable=yes | Out-Null $ruleExistsBefore = @(netsh advfirewall firewall show rule name=all | Select-String $testRuleName).Count -gt 0 Write-Host " Watchdog Test Rule Created: $ruleExistsBefore" # 2. Register SYSTEM Scheduled Task watchdog scheduled for +5 seconds $triggerTime = (Get-Date).AddSeconds(5) $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"netsh advfirewall firewall delete rule name='$testRuleName'`"" $trigger = New-ScheduledTaskTrigger -Once -At $triggerTime $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest Register-ScheduledTask -TaskName $watchdogTask -Action $action -Trigger $trigger -Principal $principal -Force -ErrorAction Stop | Out-Null Write-Host " Watchdog Task Scheduled for +5 seconds: $((Get-ScheduledTask -TaskName $watchdogTask).TaskName)" # 3. Wait 10 seconds for task to fire without manual intervention Write-Host " Waiting 10s for watchdog task to execute autonomously..." Start-Sleep -Seconds 10 # 4. Verify rule was removed by the watchdog $ruleExistsAfter = @(netsh advfirewall firewall show rule name=all | Select-String $testRuleName).Count -gt 0 Write-Host " Rule Exists After Watchdog: $ruleExistsAfter (Expected: False)" Unregister-ScheduledTask -TaskName $watchdogTask -Confirm:$false -ErrorAction SilentlyContinue | Out-Null if ($ruleExistsBefore -and (-not $ruleExistsAfter)) { Write-Host " [PASS] Action B: Watchdog task fired and auto-deleted rule with 0 manual intervention." -ForegroundColor Green $proofResults.WatchdogExecuted = $true $proofResults.FirewallAutoReverted = $true } else { Write-Host " [FAIL] Action B: Rule was not deleted by watchdog!" -ForegroundColor Red $smokePassed = $false } } catch { Write-Host " [FAIL] Watchdog execution error: $($_.Exception.Message)" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 3 (Action A): Real Service Network Chaos Self-Proof (Before -> During -> After) # ------------------------------------------------------------------------------ Write-Host "`n[3/10] Action A: Real Service Network Chaos Self-Proof (Before -> During -> After)..." -ForegroundColor Yellow $svcPath = Get-ClassNotifyServiceExePath $svcProc = Get-Process -Name "ClassNotify.Service" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($svcProc -and $svcPath) { Write-Host " Target Service: $svcPath (PID: $($svcProc.Id))" # 3A. Before: Check if Service has established TCP connection to Cloud Server $connsBefore = @(Get-NetTCPConnection -OwningProcess $svcProc.Id -RemoteAddress "158.179.179.29" -ErrorAction SilentlyContinue | Where-Object { $_.State -eq "Established" }) $beforeConnected = ($connsBefore.Count -gt 0) Write-Host " Service Network Before: Connected = $beforeConnected ($($connsBefore.Count) established conns)" # 3B. During: Block outbound traffic for ClassNotify.Service.exe $chaosRuleName = "ClassNotify-Chaos-SmokeV3-Svc" netsh advfirewall firewall add rule name="$chaosRuleName" dir=out action=block program="$svcPath" enable=yes | Out-Null # Wait for TCP connection to drop $duringBlocked = $false $swDuring = [System.Diagnostics.Stopwatch]::StartNew() while ($swDuring.Elapsed.TotalSeconds -lt 15) { Start-Sleep -Seconds 1 $connsDuring = @(Get-NetTCPConnection -OwningProcess $svcProc.Id -RemoteAddress "158.179.179.29" -ErrorAction SilentlyContinue | Where-Object { $_.State -eq "Established" }) if ($connsDuring.Count -eq 0) { $duringBlocked = $true break } } Write-Host " Service Network During: Blocked = $duringBlocked (TCP conns dropped to 0 in $([math]::Round($swDuring.Elapsed.TotalSeconds, 1))s)" # 3C. After: Remove block rule and verify reconnection netsh advfirewall firewall delete rule name="$chaosRuleName" | Out-Null $afterReconnected = $false $swAfter = [System.Diagnostics.Stopwatch]::StartNew() while ($swAfter.Elapsed.TotalSeconds -lt 25) { Start-Sleep -Seconds 1 $connsAfter = @(Get-NetTCPConnection -OwningProcess $svcProc.Id -RemoteAddress "158.179.179.29" -ErrorAction SilentlyContinue | Where-Object { $_.State -eq "Established" }) if ($connsAfter.Count -gt 0) { $afterReconnected = $true break } } Write-Host " Service Network After: Reconnected = $afterReconnected (Re-established in $([math]::Round($swAfter.Elapsed.TotalSeconds, 1))s)" if ($beforeConnected -and $duringBlocked -and $afterReconnected) { Write-Host " [PASS] Action A: Real Service connection transitioned Connected -> Disconnected -> Reconnected." -ForegroundColor Green $proofResults.RealNetDisconnect = $true $proofResults.RealNetReconnect = $true } else { Write-Host " [FAIL] Action A: Real Service network transition incomplete!" -ForegroundColor Red $smokePassed = $false } } else { Write-Host " [FAIL] ClassNotify.Service not running on this machine!" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 4: Negative Test 1 - Forced Watchdog Failure # ------------------------------------------------------------------------------ Write-Host "`n[4/10] Negative Test 1: Forced Watchdog Registration Failure..." -ForegroundColor Yellow Clear-AllTestFirewallRules $negative1Caught = $false try { $pwshExe = (Get-Process -Id $PID).Path Set-ClassNotifyNetworkBlock -SessionId "negtest1" -TargetServiceExe $pwshExe -SimulateWatchdogFailure } catch { if ($_.Exception.Message -match "NetworkChaosSafetyWatchdogNotArmed") { $negative1Caught = $true } } $rulesCount = @(netsh advfirewall firewall show rule name=all | Select-String "ClassNotify-Chaos-").Count if ($negative1Caught -and $rulesCount -eq 0) { Write-Host " [PASS] Force watchdog failure: 0 firewall rules created (Fail-Closed Verified)." -ForegroundColor Green } else { Write-Host " [FAIL] Fail-Closed violation! Rules created: $rulesCount" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 5: Negative Test 2 - Target Service EXE Not Found # ------------------------------------------------------------------------------ Write-Host "`n[5/10] Negative Test 2: Target Service EXE Not Found (No Fallback)..." -ForegroundColor Yellow Clear-AllTestFirewallRules $negative2Caught = $false try { Set-ClassNotifyNetworkBlock -SessionId "negtest2" -TargetServiceExe "C:\NonExistent\FakeService.exe" } catch { if ($_.Exception.Message -match "TargetServiceExecutableNotFound") { $negative2Caught = $true } } $rulesCount2 = @(netsh advfirewall firewall show rule name=all | Select-String "ClassNotify-Chaos-").Count if ($negative2Caught -and $rulesCount2 -eq 0) { Write-Host " [PASS] Target EXE not found: Network Chaos aborted with 0 rules, no fallback." -ForegroundColor Green } else { Write-Host " [FAIL] Target EXE not found failed to abort cleanly! Rules created: $rulesCount2" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 6 (Action C): Real WPF Client Kill & Auto-Recovery # ------------------------------------------------------------------------------ Write-Host "`n[6/10] Action C: Real WPF Client Kill & Service Auto-Recovery..." -ForegroundColor Yellow $wpfRes = Kill-WpfClientAndMeasureRecovery -MaxAllowedRecoverySeconds 15 if ($wpfRes.RecoveredSuccessfully) { $proofResults.WpfKilled = $true $proofResults.WpfRecovered = $true # Wait for IPC handshake and verify active communication $ipcHealthy = $false $clientLog = "C:\ProgramData\ClassNotify\logs\client.log" $swIpc = [System.Diagnostics.Stopwatch]::StartNew() while ($swIpc.Elapsed.TotalSeconds -lt 12) { Start-Sleep -Seconds 1 if (Test-Path $clientLog) { $lastWriteAge = ((Get-Date) - (Get-Item $clientLog).LastWriteTime).TotalSeconds $recent = Get-Content $clientLog -Tail 10 -ErrorAction SilentlyContinue if ($lastWriteAge -lt 6 -and ($recent | Select-String "type=Ping|TtsCheck")) { $ipcHealthy = $true break } } } $proofResults.IpcAfterWpfRecovery = $ipcHealthy Write-Host " [PASS] Action C: WPF killed (PID: $($wpfRes.OldPid)) and auto-recovered (New PID: $($wpfRes.NewPid)) in $($wpfRes.RecoveryDurationSeconds)s." -ForegroundColor Green Write-Host " [PASS] Action C IPC: Active Ping/Pong verified (ClientLog updated $([math]::Round($lastWriteAge, 1))s ago)." -ForegroundColor Green } else { Write-Host " [FAIL] Action C: WPF auto-recovery failed! Error: $($wpfRes.Error)" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 7 (Action D): Real Windows Service Kill & SCM Auto-Recovery # ------------------------------------------------------------------------------ Write-Host "`n[7/10] Action D: Real Windows Service Kill & SCM Auto-Recovery..." -ForegroundColor Yellow $svcRes = Kill-WindowsServiceAndMeasureRecovery -MaxAllowedRecoverySeconds 25 if ($svcRes.RecoveredSuccessfully) { $proofResults.ServiceCrash = $true $proofResults.ServiceRecovery = $true # Verify SignalR connection re-established $swSvcConn = [System.Diagnostics.Stopwatch]::StartNew() $svcSignalROk = $false while ($swSvcConn.Elapsed.TotalSeconds -lt 25) { Start-Sleep -Seconds 1 $svcConns = @(Get-NetTCPConnection -OwningProcess $svcRes.NewPid -RemoteAddress "158.179.179.29" -ErrorAction SilentlyContinue | Where-Object { $_.State -eq "Established" }) if ($svcConns.Count -gt 0) { $svcSignalROk = $true break } } # Verify service health and active IPC ping $clientLog = "C:\ProgramData\ClassNotify\logs\client.log" $serviceHealthy = $false $swHealth = [System.Diagnostics.Stopwatch]::StartNew() while ($swHealth.Elapsed.TotalSeconds -lt 15) { Start-Sleep -Seconds 1 if (Test-Path $clientLog) { $age = ((Get-Date) - (Get-Item $clientLog).LastWriteTime).TotalSeconds $recent = Get-Content $clientLog -Tail 10 -ErrorAction SilentlyContinue if ($age -lt 6 -and ($recent | Select-String "type=Ping|TtsCheck") -and $svcSignalROk) { $serviceHealthy = $true break } } } $proofResults.SignalRReconnected = $svcSignalROk $proofResults.ServiceHealthStatus = $serviceHealthy if ($svcSignalROk -and $serviceHealthy) { Write-Host " [PASS] Action D: Service killed (PID: $($svcRes.OldPid)), SCM auto-recovered (New PID: $($svcRes.NewPid)) in $($svcRes.RecoveryDurationSeconds)s." -ForegroundColor Green Write-Host " [PASS] Action D SignalR: Reconnected to 158.179.179.29:443. IPC Ping active." -ForegroundColor Green } else { Write-Host " [FAIL] Action D: Service restarted but SignalR/IPC did not recover within deadline!" -ForegroundColor Red $smokePassed = $false } } else { Write-Host " [FAIL] Action D: Service SCM recovery failed! Error: $($svcRes.Error)" -ForegroundColor Red $smokePassed = $false } # ------------------------------------------------------------------------------ # Test 8: Process Status & Interactive Desktop Awareness # ------------------------------------------------------------------------------ Write-Host "`n[8/10] Testing Process & Interactive Session Awareness..." -ForegroundColor Yellow $procStatus = Get-ClientProcessStatus $explorers = Get-Process -Name "explorer" -ErrorAction SilentlyContinue $activeInteractive = $false if ($explorers) { foreach ($e in $explorers) { if ($e.SessionId -gt 0) { $activeInteractive = $true; break } } } Write-Host " [PASS] Interactive Session Awareness: Active = $activeInteractive (Session 0 truthfully isolated)." -ForegroundColor Green # ------------------------------------------------------------------------------ # Test 9: Telemetry Sampling & HTML Report Generation # ------------------------------------------------------------------------------ Write-Host "`n[9/10] Testing Telemetry Sampling & HTML Report Generation..." -ForegroundColor Yellow $samples = Sample-ClientPerformanceMetrics -Stage "SmokeV3" Write-Host " [PASS] Telemetry: $($samples.Count) metrics samples captured." -ForegroundColor Green $smokeStateFile = "$env:TEMP\smokev3-state.json" $smokeMetricsFile = "$env:TEMP\smokev3-metrics.jsonl" $smokeHtmlFile = "$env:TEMP\smokev3-report.html" $mockSession = @{ sessionId = "smokev3-" + (Get-Date -Format "yyyyMMdd-HHmmss") scenarioId = "smoke-v3" startedUtc = (Get-Date).ToUniversalTime().ToString("o") endedUtc = (Get-Date).ToUniversalTime().ToString("o") duration = "00:00:15" currentPhase = "Completed" stats = @{ NotificationsSent = 1 NotificationsAcked = 1 NotificationsLost = 0 WpfKilled = 1 WpfRecovered = 1 ServiceKilled = 1 ServiceRecovered = 1 NetworkCuts = 1 NetworkRecovered = 1 UpdatesSucceeded = 1 RollbacksSucceeded = 1 ManualActionsRequired = 0 } eventLog = @( @{ TimestampUtc = (Get-Date).ToUniversalTime().ToString("o"); Level = "Success"; Message = "Smoke V3 Complete" } ) evaluation = @{ Verdict = "PASS" Checks = @( @{ Rule = "Smoke V3 Real Proof Verification"; Expected = "PASS"; Actual = "PASS"; Passed = $true } ) } serviceMemoryAnalysis = @{ SlopeMbPerHour = 0.1 } wpfMemoryAnalysis = @{ SlopeMbPerHour = 0.2 } } Set-Content -Path $smokeStateFile -Value ($mockSession | ConvertTo-Json -Depth 6) -Force Set-Content -Path $smokeMetricsFile -Value "{}" -Force $outReport = & (Join-Path $root "reporting\New-StabilityReport.ps1") -SessionStateJsonPath $smokeStateFile -MetricsJsonlPath $smokeMetricsFile -OutputHtmlPath $smokeHtmlFile if (Test-Path $outReport) { Write-Host " [PASS] HTML report generated successfully ($((Get-Item $outReport).Length) bytes)." -ForegroundColor Green } else { Write-Host " [FAIL] Failed to generate HTML report!" -ForegroundColor Red $smokePassed = $false } Remove-Item -Path $smokeStateFile, $smokeMetricsFile, $smokeHtmlFile -Force -ErrorAction SilentlyContinue # ------------------------------------------------------------------------------ # Test 10: Final Environment Cleanup Invariant # ------------------------------------------------------------------------------ Write-Host "`n[10/10] Verifying Final Environment Cleanup..." -ForegroundColor Yellow Clear-AllTestFirewallRules $remainingRules = netsh advfirewall firewall show rule name=all | Select-String "ClassNotify-Chaos-" $watchdogTaskLeft = Get-ScheduledTask -TaskName $watchdogTask -ErrorAction SilentlyContinue $clean = ($null -eq $remainingRules -and $null -eq $watchdogTaskLeft) if ($clean) { Write-Host " [PASS] Watchdog removed: True" -ForegroundColor Green Write-Host " [PASS] Firewall rules remaining = 0" -ForegroundColor Green Write-Host " [PASS] EnvironmentClean = true" -ForegroundColor Green $proofResults.EnvironmentClean = $true } else { Write-Host " [FAIL] Environment not clean after smoke test!" -ForegroundColor Red $smokePassed = $false } Write-Host "`n==========================================================" -ForegroundColor Cyan Write-Host " FINAL PROOF SUMMARY REQUESTED BY CHATGPT BRAIN " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " [$(if ($proofResults.RealNetDisconnect) {'PASS'} else {'FAIL'})] Real Service Network Disconnect" Write-Host " [$(if ($proofResults.RealNetReconnect) {'PASS'} else {'FAIL'})] Real Service Network Reconnect" Write-Host "" Write-Host " [$(if ($proofResults.WatchdogExecuted) {'PASS'} else {'FAIL'})] Durable Watchdog Executed" Write-Host " [$(if ($proofResults.FirewallAutoReverted) {'PASS'} else {'FAIL'})] Firewall Auto-Reverted" Write-Host "" Write-Host " [$(if ($proofResults.WpfKilled) {'PASS'} else {'FAIL'})] WPF Kill" Write-Host " [$(if ($proofResults.WpfRecovered) {'PASS'} else {'FAIL'})] WPF Recovery" Write-Host " [$(if ($proofResults.IpcAfterWpfRecovery) {'PASS'} else {'FAIL'})] IPC After WPF Recovery" Write-Host "" Write-Host " [$(if ($proofResults.ServiceCrash) {'PASS'} else {'FAIL'})] Service Crash" Write-Host " [$(if ($proofResults.ServiceRecovery) {'PASS'} else {'FAIL'})] Service Recovery" Write-Host " [$(if ($proofResults.SignalRReconnected) {'PASS'} else {'FAIL'})] SignalR Reconnected" Write-Host " [$(if ($proofResults.ServiceHealthStatus) {'PASS'} else {'FAIL'})] Notification / Health Status Healthy" Write-Host "" Write-Host " [$(if ($proofResults.EnvironmentClean) {'PASS'} else {'FAIL'})] EnvironmentClean = true" Write-Host "" $allProofsPassed = ($proofResults.Values -notcontains $false) if ($smokePassed -and $allProofsPassed) { Write-Host "HARNESS-SMOKE-V3: PASS" -ForegroundColor Green Write-Host "==========================================================" -ForegroundColor Cyan exit 0 } else { Write-Host "HARNESS-SMOKE-V3: FAIL" -ForegroundColor Red Write-Host "==========================================================" -ForegroundColor Cyan exit 1 }