BeforeAll { $messageTokens = $null; $messageErrors = $null $messageAst = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../tools/TemporarySsh.ps1", [ref]$messageTokens, [ref]$messageErrors) $messageFn = $messageAst.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Write-ConfigCentralMessage' }, $true) . ([scriptblock]::Create($messageFn.Extent.Text)) $script:temporarySshPath = "$PSScriptRoot/../tools/TemporarySsh.ps1" $tokens = $null; $errors = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile($temporarySshPath, [ref]$tokens, [ref]$errors) foreach ($name in @('Disable-TemporarySsh', 'Invoke-ElevatedCopy')) { $node = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name }, $true) . ([scriptblock]::Create($node.Extent.Text)) } function Get-NetFirewallRule { param($Name) } function Remove-NetFirewallRule { param($Name) } function Set-NetFirewallRule { param($Name, $Enabled) } function Disable-NetFirewallRule { param($Name) } } Describe 'Temporary SSH helper' { It 'parses under the PowerShell language parser' { $tokens = $null $errors = $null [void][System.Management.Automation.Language.Parser]::ParseFile($temporarySshPath, [ref]$tokens, [ref]$errors) $errors.Count | Should -Be 0 } } Describe 'Temporary SSH disable' { BeforeEach { $script:stateFile = Join-Path $TestDrive 'temporary-ssh.json' '{"standardRuleExisted":true,"standardRuleEnabled":false,"serviceWasRunning":true}' | Set-Content $stateFile Mock Get-NetFirewallRule { [pscustomobject]@{ Name = $Name } } Mock Remove-NetFirewallRule {} Mock Set-NetFirewallRule {} Mock Disable-NetFirewallRule {} Mock Start-Process {} } It 'restores a disabled rule using its enum name and keeps a previously running service' { Disable-TemporarySsh -statePath $stateFile -temporaryFirewallRule 'temporary' Should -Invoke Set-NetFirewallRule -Times 1 -ParameterFilter { $Enabled -is [string] -and $Enabled -eq 'False' } Should -Invoke Start-Process -Times 0 Test-Path $stateFile | Should -BeFalse } It 'retains state and the original error when firewall restoration fails' { Mock Set-NetFirewallRule { throw 'Firewall access denied' } { Disable-TemporarySsh -statePath $stateFile -temporaryFirewallRule 'temporary' } | Should -Throw '*Firewall access denied*' Test-Path $stateFile | Should -BeTrue Should -Invoke Start-Process -Times 0 } It 'retains state if it cannot launch the delayed service stop' { '{"standardRuleExisted":true,"standardRuleEnabled":true,"serviceWasRunning":false}' | Set-Content $stateFile Mock Start-Process { throw 'Could not launch service stop' } { Disable-TemporarySsh -statePath $stateFile -temporaryFirewallRule 'temporary' } | Should -Throw '*Could not launch service stop*' Test-Path $stateFile | Should -BeTrue Should -Invoke Set-NetFirewallRule -Times 1 -ParameterFilter { $Enabled -eq 'True' } } It 'does not modify the standard service or rule when no saved state exists' { Remove-Item $stateFile Disable-TemporarySsh -statePath $stateFile -temporaryFirewallRule 'temporary' Should -Invoke Set-NetFirewallRule -Times 0 Should -Invoke Start-Process -Times 0 } } Describe 'Elevated SSH failure details' { It 'returns the worker reason and removes temporary transport files' { $previousTemp = $env:TEMP $env:TEMP = $TestDrive $scriptUrl = 'https://example.test/helper.ps1' $Action = 'Disable'; $InstallMethod = 'Fast' Mock Invoke-RestMethod { 'script' | Set-Content $OutFile } Mock Start-Process { $resultFile = [regex]::Match($ArgumentList, '-ResultPath "([^"]+)"').Groups[1].Value '{"message":"Firewall access denied"}' | Set-Content $resultFile [pscustomobject]@{ ExitCode = 1 } } try { { Invoke-ElevatedCopy } | Should -Throw '*Firewall access denied*' @(Get-ChildItem $TestDrive -Filter 'config-central-temporary-ssh-*').Count | Should -Be 0 } finally { $env:TEMP = $previousTemp } } }