| 1 | #requires -Version 5.1 |
| 2 | $ErrorActionPreference='Stop' |
| 3 | . (Join-Path $PSScriptRoot 'windows-host-image.ps1') |
| 4 | $credential=[pscredential]::new('cagesetup',(ConvertTo-SecureString 'Test-only_Account!123' -AsPlainText -Force)) |
| 5 | $answer=New-CageCloneAnswer $credential 'zh-CN' |
| 6 | $ns=[Xml.XmlNamespaceManager]::new($answer.NameTable) |
| 7 | $ns.AddNamespace('u','urn:schemas-microsoft-com:unattend') |
| 8 | if($answer.SelectSingleNode('//u:LocalAccount/u:Name',$ns).InnerText -cne 'cagesetup'){throw 'Wrong setup account.'} |
| 9 | if($answer.SelectSingleNode('//u:LocalAccount/u:Password/u:Value',$ns).InnerText -cne $credential.GetNetworkCredential().Password){throw 'Setup password changed in XML.'} |
| 10 | if($answer.SelectSingleNode('//u:UILanguage',$ns).InnerText -cne 'zh-CN'){throw 'Image language was not preserved.'} |
| 11 | if($answer.SelectNodes('//u:ProductKey | //u:AutoLogon',$ns).Count){throw 'Unexpected host key or automatic administrator logon.'} |
| 12 | function Get-WindowsImage { |
| 13 | param($ImagePath,$Index) |
| 14 | if(!$Index){return [pscustomobject]@{ImageIndex=1}} |
| 15 | return [pscustomobject]@{ImageIndex=1;EditionId='Professional';Architecture='amd64';Languages=@('zh-CN');Version='10.0.26200.6584'} |
| 16 | } |
| 17 | $script:mounted=$false |
| 18 | function Mount-VHD { param($Path,[switch]$Passthru); $script:mounted=$true; throw 'ReachedDiskPreparation' } |
| 19 | try { Install-CageWindowsImage 'test.esd' 'test.vhdx' 'Professional' 'zh-CN' $answer '26200.9999'; throw 'Wrong build accepted.' } |
| 20 | catch { if($_.Exception.Message -notlike '*build differs*'){throw} } |
| 21 | if($script:mounted){throw 'An unmatched image touched a disk.'} |
| 22 | try { Install-CageWindowsImage 'test.esd' 'test.vhdx' 'Professional' 'zh-CN' $answer '26200.6584'; throw 'Expected disk preparation.' } |
| 23 | catch { if($_.Exception.Message -cne 'ReachedDiskPreparation'){throw} } |
| 24 | if(!$script:mounted){throw 'Matching image did not reach disk preparation.'} |
| 25 | $profile = Join-Path ([IO.Path]::GetTempPath()) ('cage-setup-credential-'+[guid]::NewGuid().ToString('N')) |
| 26 | $null = New-Item -ItemType Directory -Path $profile |
| 27 | try { |
| 28 | $installation = [guid]::NewGuid() |
| 29 | $receipt = Join-Path $profile 'windows-install-vm.txt' |
| 30 | [IO.File]::WriteAllText($receipt, [guid]::NewGuid().ToString('D')) |
| 31 | try { $null = Get-CageSetupCredential $profile $installation; throw 'A retained guest received a new setup password.' } |
| 32 | catch { if ($_.Exception.Message -notlike '*saved setup credential*missing*') { throw } } |
| 33 | if (Test-Path -LiteralPath (Join-Path $profile ('windows-setup-'+$installation.ToString('N')+'.dpapi'))) { throw 'Created a replacement setup credential.' } |
| 34 | [IO.File]::Delete($receipt) |
| 35 | $credentialPath = Join-Path $profile ('windows-setup-'+$installation.ToString('N')+'.dpapi') |
| 36 | [IO.File]::WriteAllText($credentialPath, 'encrypted-test-fixture') |
| 37 | $script:disabled = $false |
| 38 | $script:sessionAttempts = 0 |
| 39 | function Test-CageSetupAccountDisabled { param($VMId,$Profile); return $script:disabled } |
| 40 | function New-PSSession { |
| 41 | param($VMId,$Credential) |
| 42 | $script:sessionAttempts++ |
| 43 | throw 'InjectedCleanupConnectionFailure' |
| 44 | } |
| 45 | try { Remove-CageSetupCredential ([guid]::NewGuid()) $credential $profile $installation; throw 'Cleanup failure was ignored.' } |
| 46 | catch { if ($_.Exception.Message -cne 'InjectedCleanupConnectionFailure') { throw } } |
| 47 | if (!(Test-Path -LiteralPath $credentialPath)) { throw 'Failed cleanup discarded the original credential.' } |
| 48 | $script:disabled = $true |
| 49 | Remove-CageSetupCredential ([guid]::NewGuid()) $credential $profile $installation |
| 50 | if ($script:sessionAttempts -ne 1 -or (Test-Path -LiteralPath $credentialPath)) { throw 'Retired setup account could not complete cleanup without logging in again.' } |
| 51 | $pair = Join-Path (Join-Path (Join-Path $profile 'state') ('windows-'+$installation.ToString('N'))) 'cage-workbench/pair' |
| 52 | $null = [IO.Directory]::CreateDirectory($pair) |
| 53 | [IO.File]::WriteAllText((Join-Path $pair 'workbench-components-ready'), 'ready') |
| 54 | [IO.File]::WriteAllText((Join-Path $pair 'windows-guest.json'), '{}') |
| 55 | [IO.File]::WriteAllText($receipt, [guid]::NewGuid().ToString('D')) |
| 56 | if ($null -ne (Get-CageSetupCredential $profile $installation) -or (Test-Path -LiteralPath $credentialPath)) { |
| 57 | throw 'Retirement followed by an interrupted publish regenerated a credential.' |
| 58 | } |
| 59 | } finally { [IO.Directory]::Delete($profile, $true) } |
| 60 | |
| 61 | $tokens = $null; $parseErrors = $null |
| 62 | $completion = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot 'complete-windows-guest.ps1'), [ref]$tokens, [ref]$parseErrors) |
| 63 | if ($parseErrors.Count) { throw 'The completion script has syntax errors.' } |
| 64 | $publisher = $completion.Find({param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -ceq 'Publish-ReadyBinding'}, $true) |
| 65 | # Publication needs a live relay and Windows ACLs. Keep the required ordering |
| 66 | # checked here; the whole native path remains part of Windows real testing. |
| 67 | $retireAt = $publisher.Body.Extent.Text.IndexOf('Complete-CloneSetup $stageBinding') |
| 68 | $publishAt = $publisher.Body.Extent.Text.IndexOf('Complete-CageHostEnrollment') |
| 69 | if ($retireAt -lt 0 -or $publishAt -le $retireAt) { throw 'Host enrollment can publish before clone retirement.' } |
| 70 | $retireAt = $publisher.Body.Extent.Text.LastIndexOf('Complete-CloneSetup $stageBinding') |
| 71 | $publishAt = $publisher.Body.Extent.Text.IndexOf('[IO.File]::Copy($stageBinding') |
| 72 | if ($retireAt -lt 0 -or $publishAt -le $retireAt) { throw 'The local host can publish before clone retirement.' } |
| 73 | |
| 74 | $manifestPublisher = Join-Path $PSScriptRoot 'publish-windows-component-manifest.ps1' |
| 75 | $manifestBytes = [IO.File]::ReadAllBytes($manifestPublisher) |
| 76 | if ($manifestBytes.Length -lt 3 -or $manifestBytes[0] -ne 0xEF -or $manifestBytes[1] -ne 0xBB -or $manifestBytes[2] -ne 0xBF) { |
| 77 | throw 'The component manifest publisher must start with a UTF-8 BOM for Windows PowerShell 5.1.' |
| 78 | } |
| 79 | $parseErrors = $null |
| 80 | $null = [Management.Automation.Language.Parser]::ParseFile($manifestPublisher, [ref]$null, [ref]$parseErrors) |
| 81 | if ($parseErrors.Count) { throw "The component manifest publisher failed Windows PowerShell parsing: $($parseErrors[0].Message)" } |
| 82 | Write-Output 'Passed isolated account XML, locale preservation, build validation, retained credential identity and interrupted retirement recovery.' |