# ============================================================================== # UpdateChaos.psm1 - Automated Update, Rollback & Disaster Interruption Verifier # ============================================================================== function Verify-DeviceIdentityPreserved { [CmdletBinding()] param( [string]$ProgramDataDir = "C:\ProgramData\ClassNotify" ) $idFile = Join-Path $ProgramDataDir "device-identity.json" if (Test-Path $idFile) { try { $content = Get-Content -Path $idFile -Raw | ConvertFrom-Json return ($null -ne $content.DeviceId -and $content.DeviceId.Length -gt 0) } catch { return $false } } return $true # If identity in registry/DB, assume preserved if no exception } function Verify-CleanManagedDirectory { [CmdletBinding()] param( [string]$InstallDir = "C:\Program Files\ClassNotify" ) if (-not (Test-Path $InstallDir)) { return $true } # Assert no leftover bad artifact files $badFile = Join-Path $InstallDir "BadOnly.dll" if (Test-Path $badFile) { Write-Warning "[UpdateChaos] Unmanifested file BadOnly.dll found in install directory!" return $false } return $true } function Invoke-AutomatedUpdateTestCycle { [CmdletBinding()] param( [string]$ServerBaseUrl = "http://127.0.0.1:5000", [string]$DeviceId, [string]$AdminToken, [bool]$InjectBadVersion = $false, [int]$TimeoutSeconds = 120 ) $result = [PSCustomObject]@{ TimestampUtc = (Get-Date).ToUniversalTime().ToString("o") Type = if ($InjectBadVersion) { "RollbackTest" } else { "NormalUpdate" } TriggeredSuccessfully = $false TerminalStatus = "Unknown" IdentityPreserved = $false DirectoryClean = $false DurationSeconds = 0 Error = $null } $sw = [System.Diagnostics.Stopwatch]::StartNew() try { $headers = @{ "Authorization" = "Bearer $AdminToken" "Content-Type" = "application/json" } # 1. Trigger Update Run via Admin API $body = @{ DeviceId = $DeviceId TargetVersion = if ($InjectBadVersion) { "2.6.0-corrupt" } else { "2.6.0" } Force = $true } | ConvertTo-Json $initUrl = "$ServerBaseUrl/api/admin/devices/$DeviceId/updates" $initResp = Invoke-RestMethod -Uri $initUrl -Method Post -Headers $headers -Body $body -ErrorAction Stop $runId = $initResp.UpdateRunId $result.TriggeredSuccessfully = $true # 2. Poll for terminal status $statusUrl = "$ServerBaseUrl/api/admin/devices/$DeviceId/updates/$runId" $expectedTerminal = if ($InjectBadVersion) { "RolledBack" } else { "Succeeded" } while ($sw.Elapsed.TotalSeconds -lt $TimeoutSeconds) { Start-Sleep -Seconds 2 try { $pollResp = Invoke-RestMethod -Uri $statusUrl -Method Get -Headers $headers -ErrorAction Stop $currentStatus = $pollResp.Status if ($currentStatus -in @("Succeeded", "RolledBack", "Failed", "RollbackFailed", "TimedOut")) { $result.TerminalStatus = $currentStatus break } } catch { # transient network retry } } $result.DurationSeconds = [math]::Round($sw.Elapsed.TotalSeconds, 2) $result.IdentityPreserved = Verify-DeviceIdentityPreserved $result.DirectoryClean = Verify-CleanManagedDirectory if ($result.TerminalStatus -ne $expectedTerminal) { $result.Error = "Expected terminal state '$expectedTerminal', but got '$($result.TerminalStatus)'" } } catch { $result.Error = $_.Exception.Message } return $result } Export-ModuleMember -Function Verify-DeviceIdentityPreserved, Verify-CleanManagedDirectory, Invoke-AutomatedUpdateTestCycle