| 1 | #requires -Version 5.1 |
| 2 | # Exercise the actual Setup command with an isolated filesystem and registry stand-in. |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | $file = Join-Path $PSScriptRoot 'install-guest-powershell.cmd' |
| 5 | $lines = @(Get-Content -LiteralPath $file | Where-Object { $_.StartsWith(' "') }) |
| 6 | $parts = for ($i = 0; $i -lt $lines.Count; $i++) { |
| 7 | $suffix = if ($i -lt $lines.Count - 1) { '" ^' } else { '"' } |
| 8 | if (!$lines[$i].EndsWith($suffix)) { throw 'Broken CMD line continuation.' } |
| 9 | $lines[$i].Substring(3, $lines[$i].Length - 3 - $suffix.Length) |
| 10 | } |
| 11 | $command = $parts -join ' ' |
| 12 | if ($command.Length -gt 7700 -or $command.Contains('"') -or $command.Contains('%')) { |
| 13 | throw 'The guest installer command may be truncated or expanded by cmd.exe.' |
| 14 | } |
| 15 | $errors = $null |
| 16 | $ast = [Management.Automation.Language.Parser]::ParseInput($command, [ref]$null, [ref]$errors) |
| 17 | if ($errors.Count) { throw $errors[0] } |
| 18 | $steps = $ast.EndBlock.Statements |
| 19 | $installStart = @($steps | Where-Object { $_.Extent.Text.StartsWith('if ((Get-FileHash ') })[0] |
| 20 | $regStart = @($steps | Where-Object { $_.Extent.Text.StartsWith('$key = ') })[0] |
| 21 | $install = [scriptblock]::Create($command.Substring($installStart.Extent.StartOffset, $regStart.Extent.StartOffset - $installStart.Extent.StartOffset)) |
| 22 | $updatePath = [scriptblock]::Create($steps[-1].Extent.Text) |
| 23 | |
| 24 | function Assert-Failure([scriptblock]$run, [string]$message) { |
| 25 | try { & $run } catch { |
| 26 | if ($_.Exception.Message -like "*$message*") { return } |
| 27 | throw |
| 28 | } |
| 29 | throw "Expected failure: $message" |
| 30 | } |
| 31 | |
| 32 | # The archive, hash checks, staging, publication, cleanup and retry are real. |
| 33 | # Simulate the version response of a Windows executable on non-Windows hosts. |
| 34 | function Test-Runtime([string]$directory, [string]$expectedVersion) { |
| 35 | $version = [IO.File]::ReadAllText((Join-Path $directory 'pwsh.exe')) |
| 36 | if ($version -notmatch '^7\.[0-9]+\.[0-9]+$' -or ($expectedVersion -and $expectedVersion -ne $version)) { |
| 37 | throw 'Simulated incompatible runtime' |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | $oldHash = $env:CAGE_PWSH_HASH |
| 42 | $oldVersion = $env:CAGE_PWSH_VERSION |
| 43 | $temp = [IO.Directory]::CreateDirectory((Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString('N')))) |
| 44 | try { |
| 45 | $version = '7.6.0' |
| 46 | $env:CAGE_PWSH_VERSION = $version |
| 47 | $files = [IO.Directory]::CreateDirectory((Join-Path $temp 'source')) |
| 48 | [IO.File]::WriteAllText((Join-Path $files.FullName 'pwsh.exe'), $version) |
| 49 | $zip = Join-Path $temp.FullName 'PowerShell.zip' |
| 50 | Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 51 | [IO.Compression.ZipFile]::CreateFromDirectory($files.FullName, $zip) |
| 52 | $base = Join-Path $temp.FullName 'Program Files' |
| 53 | $target = Join-Path $base '7' |
| 54 | $env:CAGE_PWSH_HASH = '00' * 32 |
| 55 | Assert-Failure { & $install } 'pinned SHA-256' |
| 56 | if ([IO.Directory]::Exists($base)) { throw 'A hash mismatch changed the destination.' } |
| 57 | |
| 58 | $env:CAGE_PWSH_HASH = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash |
| 59 | $env:CAGE_PWSH_VERSION = '7.5.0' |
| 60 | Assert-Failure { & $install } 'incompatible runtime' |
| 61 | if (@([IO.Directory]::EnumerateFileSystemEntries($base)).Count) { throw 'Failed runtime left partial files.' } |
| 62 | $env:CAGE_PWSH_VERSION = $version |
| 63 | & $install |
| 64 | $exe = Join-Path $target 'pwsh.exe' |
| 65 | if ([IO.File]::ReadAllText($exe) -ne $version) { throw 'Verified shell was not published.' } |
| 66 | [IO.File]::WriteAllText($exe, '7.8.0') |
| 67 | & $install |
| 68 | if ([IO.File]::ReadAllText($exe) -ne '7.8.0') { throw 'A later installed release was overwritten.' } |
| 69 | [IO.File]::WriteAllText($exe, 'unexpected') |
| 70 | Assert-Failure { & $install } 'incompatible runtime' |
| 71 | if ([IO.File]::ReadAllText($exe) -ne 'unexpected') { throw 'Existing installation was modified on failure.' } |
| 72 | |
| 73 | $key = [pscustomobject]@{ Raw = '%SystemRoot%\System32'; Writes = 0 } |
| 74 | $key | Add-Member ScriptMethod GetValue { param($name,$fallback,$flags) $this.Raw } |
| 75 | $key | Add-Member ScriptMethod GetValueNames { @('Path') } |
| 76 | $key | Add-Member ScriptMethod GetValueKind { param($name) [Microsoft.Win32.RegistryValueKind]::ExpandString } |
| 77 | $key | Add-Member ScriptMethod SetValue { |
| 78 | param($name,$value,$kind) |
| 79 | if ($kind -ne [Microsoft.Win32.RegistryValueKind]::ExpandString) { throw 'Changed registry value kind.' } |
| 80 | $this.Raw = $value; $this.Writes++ |
| 81 | } |
| 82 | $key | Add-Member ScriptMethod Close {} |
| 83 | & $updatePath |
| 84 | & $updatePath |
| 85 | if ($key.Writes -ne 1 -or $key.Raw -cne ($target + ';%SystemRoot%\System32')) { |
| 86 | throw 'PATH retry duplicated the entry or rewrote existing variables.' |
| 87 | } |
| 88 | } finally { |
| 89 | $env:CAGE_PWSH_HASH = $oldHash |
| 90 | $env:CAGE_PWSH_VERSION = $oldVersion |
| 91 | $temp.Delete($true) |
| 92 | } |
| 93 | Write-Output 'Passed offline ZIP verification, publication, retry, existing shell preservation and machine PATH checks (simulated guest).' |