| 1 | #requires -Version 5.1 |
| 2 | # Run on any platform with PowerShell. The tests never call Windows deployment APIs. |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | . (Join-Path $PSScriptRoot 'claude-desktop-windows.ps1') |
| 5 | |
| 6 | function Expect-Failure([scriptblock]$action, [string]$reason) { |
| 7 | try { & $action } catch { |
| 8 | if ($_.Exception.Message.Contains($reason)) { return } |
| 9 | throw |
| 10 | } |
| 11 | throw "Expected rejection: $reason" |
| 12 | } |
| 13 | |
| 14 | $subject = [Security.Cryptography.X509Certificates.X500DistinguishedName]::new($publisher) |
| 15 | $signature = [pscustomobject]@{ Status = 'Valid'; SignerCertificate = @{ SubjectName = $subject } } |
| 16 | Assert-ClaudeSignature $signature |
| 17 | $signature.Status = 'HashMismatch' |
| 18 | Expect-Failure { Assert-ClaudeSignature $signature } 'did not validate' |
| 19 | $signature.Status = 'Valid' |
| 20 | $lookalike = $publisher.Replace('4860621', '1234567') |
| 21 | $signature.SignerCertificate.SubjectName = [Security.Cryptography.X509Certificates.X500DistinguishedName]::new($lookalike) |
| 22 | Expect-Failure { Assert-ClaudeSignature $signature } 'does not match' |
| 23 | |
| 24 | $manifest = [xml]@' |
| 25 | <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"> |
| 26 | <Identity Name="Claude" Version="4.23118.0.0" ProcessorArchitecture="x64"/> |
| 27 | <Applications> |
| 28 | <Application Id="SshProxy" Executable="app\resources\claude-ssh-proxy.exe"/> |
| 29 | <Application Id="Claude" Executable="app\Claude.exe" EntryPoint="Windows.FullTrustApplication"/> |
| 30 | </Applications> |
| 31 | </Package> |
| 32 | '@ |
| 33 | $manifest.Package.Identity.SetAttribute('Publisher', $publisher) |
| 34 | if ((Assert-ClaudeManifest $manifest) -ne [version]'4.23118.0.0') { throw 'Version mismatch.' } |
| 35 | $wrongApp = $manifest.Clone() |
| 36 | $wrongApp.Package.Applications.Application[1].SetAttribute('Executable', 'app\helper.exe') |
| 37 | Expect-Failure { Assert-ClaudeManifest $wrongApp } 'entry point' |
| 38 | |
| 39 | function Get-AppxPackage { $pkg } |
| 40 | function Get-AppxPackageManifest { $manifest } |
| 41 | $pkg = [pscustomobject]@{ |
| 42 | PackageFamilyName = 'OtherFamily'; IsDevelopmentMode = $false; IsPartiallyStaged = $false |
| 43 | SignatureKind = 'Developer'; Status = 'Ok'; Publisher = $publisher |
| 44 | PackageFullName = 'test-fixture'; InstallLocation = $PSScriptRoot |
| 45 | } |
| 46 | if (Get-RegisteredClaude) { throw 'An unrelated package was accepted.' } |
| 47 | $pkg.PackageFamilyName = $packageFamily |
| 48 | if ((Get-RegisteredClaude).Aumid -cne 'Claude_pzs8sxrjxfjjc!Claude') { throw 'Incorrect AUMID.' } |
| 49 | $pkg.IsDevelopmentMode = $true |
| 50 | Expect-Failure { Get-RegisteredClaude } 'registered Claude package' |
| 51 | |
| 52 | # Execute the actual install path against a local archive. Mock just the |
| 53 | # operating-system signature and registration results, never deploy this fixture. |
| 54 | $fixture = Join-Path ([IO.Path]::GetTempPath()) (([Guid]::NewGuid().ToString()) + '.msix') |
| 55 | $installedCount = 0 |
| 56 | function Get-AuthenticodeSignature { $signature } |
| 57 | function Add-AppxPackage { |
| 58 | if ($deploymentError) { throw $deploymentError } |
| 59 | $script:installedCount++ |
| 60 | $script:pkg.PackageFamilyName = $packageFamily |
| 61 | } |
| 62 | try { |
| 63 | Add-Type -AssemblyName System.IO.Compression |
| 64 | $handle = [IO.File]::Open($fixture, 'CreateNew', 'Write', 'None') |
| 65 | try { |
| 66 | $zip = [IO.Compression.ZipArchive]::new($handle, 'Create', $true) |
| 67 | try { |
| 68 | $stream = $zip.CreateEntry('AppxManifest.xml').Open() |
| 69 | try { $manifest.Save($stream) } finally { $stream.Dispose() } |
| 70 | } finally { $zip.Dispose() } |
| 71 | } finally { $handle.Dispose() } |
| 72 | $signature.Status = 'NotSigned' |
| 73 | Expect-Failure { Install-ClaudeMsix $fixture } 'did not validate' |
| 74 | $signature.Status = 'Valid' |
| 75 | Expect-Failure { Install-ClaudeMsix $fixture } 'does not match' |
| 76 | if ($installedCount -ne 0) { throw 'An untrusted package reached Add-AppxPackage.' } |
| 77 | $signature.SignerCertificate.SubjectName = $subject |
| 78 | $pkg.IsDevelopmentMode = $false |
| 79 | $pkg.PackageFamilyName = 'absent' |
| 80 | $deploymentError = 'Deployment failed: 0x80073D28' |
| 81 | Expect-Failure { Install-ClaudeMsix $fixture } 'requires guest administrator rights' |
| 82 | $deploymentError = 'Deployment failed: 0x80073CF6' |
| 83 | Expect-Failure { Install-ClaudeMsix $fixture } '0x80073CF6' |
| 84 | $deploymentError = $null |
| 85 | $first = Install-ClaudeMsix $fixture |
| 86 | $again = Install-ClaudeMsix $fixture |
| 87 | if (!$first.Registered -or !$again.Registered -or $installedCount -ne 1) { |
| 88 | throw 'Expected exactly one registration after two Install actions.' |
| 89 | } |
| 90 | } finally { if ([IO.File]::Exists($fixture)) { [IO.File]::Delete($fixture) } } |
| 91 | |
| 92 | $target = [Guid]::NewGuid() |
| 93 | $kvpId = $target.ToString() |
| 94 | $computer = @{ Manufacturer = 'Microsoft Corporation'; Model = 'Virtual Machine' } |
| 95 | function Get-CimInstance { $computer } |
| 96 | function Get-ItemProperty { @{ VirtualMachineId = $kvpId } } |
| 97 | Assert-TargetGuest $target |
| 98 | Expect-Failure { Assert-TargetGuest ([Guid]::NewGuid()) } 'does not match' |
| 99 | $computer.Model = 'Surface' |
| 100 | Expect-Failure { Assert-TargetGuest $target } 'requires a Hyper-V guest' |
| 101 | Write-Output 'Passed Claude manifest, signing, repeat install, registration and Hyper-V guest identity checks (simulated).' |