| 1 | # Exercise the bundled read-only query. No Hyper-V service or real VM is used. |
| 2 | param([Parameter(Mandatory)][string]$Query) |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | $targetId = [Guid]::NewGuid() |
| 5 | $global:hypervTestId = $targetId |
| 6 | $global:hypervCase = 'single' |
| 7 | function Get-CimInstance { |
| 8 | param($Namespace,$ClassName,$Filter) |
| 9 | if ($Namespace -ne 'root/virtualization/v2' -or $ClassName -ne 'Msvm_ComputerSystem' -or $Filter -ne "Name='$global:hypervTestId'") { throw 'Unscoped VM lookup.' } |
| 10 | if ($global:hypervCase -eq 'denied') { throw 'test management access denied' } |
| 11 | if ($global:hypervCase -ne 'absent') { [pscustomobject]@{Name=$global:hypervTestId} } |
| 12 | } |
| 13 | $fake = New-Module -Name Hyper-V -ScriptBlock { |
| 14 | function Get-VM { |
| 15 | param($Id) |
| 16 | if ($id -ne $global:hypervTestId) { throw 'Unscoped management lookup' } |
| 17 | $returned = if ($global:hypervCase -eq 'swapped') { [Guid]::NewGuid() } else { $id } |
| 18 | $generation = if ($global:hypervCase -eq 'legacy') { 1 } else { 2 } |
| 19 | $state = if ($global:hypervCase -eq 'connected') { 'Running' } else { 'Off' } |
| 20 | [pscustomobject]@{Id=$returned; Generation=$generation; State=$state} |
| 21 | } |
| 22 | function Get-VMHardDiskDrive { |
| 23 | param($VM) |
| 24 | [pscustomobject]@{Path='C:\private-qa\system.vhdx'} |
| 25 | if ($global:hypervCase -eq 'connected') { [pscustomobject]@{Path='C:\private-qa\second.vhdx'} } |
| 26 | } |
| 27 | function Get-VMNetworkAdapter { param($VM) if ($global:hypervCase -eq 'connected') { [pscustomobject]@{Name='adapter'} } } |
| 28 | function Get-VHD { |
| 29 | param($Path) |
| 30 | if ($global:hypervCase -eq 'disk-denied') { throw 'test disk metadata unavailable' } |
| 31 | if ($Path -cne 'C:\private-qa\system.vhdx') { throw 'Unexpected disk lookup' } |
| 32 | [pscustomobject]@{VhdFormat='VHDX'; VhdType='Dynamic'} |
| 33 | } |
| 34 | function Get-VMFirmware {param($VM) if ($VM.Generation -ne 2) { throw 'Legacy VM firmware query' }; [pscustomobject]@{SecureBoot='On'; SecureBootTemplate='MicrosoftWindows'} } |
| 35 | function Get-VMSecurity {param($VM) if ($VM.Generation -ne 2) { throw 'Legacy VM security query' }; [pscustomobject]@{TpmEnabled=$true} } |
| 36 | Export-ModuleMember -Function 'Get-*' |
| 37 | } |
| 38 | Import-Module $fake -Force |
| 39 | function Result { (& $Query) | ConvertFrom-Json } |
| 40 | $found = Result |
| 41 | if ($found.state -ne 'Off' -or $found.generation -ne 2 -or $found.diskCount -ne 1 -or !$found.singleLocalVhdx -or $found.diskType -ne 'Dynamic' -or !$found.secureBoot -or !$found.virtualTpm -or $found.networkAdapterCount -ne 0) { throw 'Invalid offline VM observation' } |
| 42 | if (($found | ConvertTo-Json -Compress).Contains('private-qa')) { throw 'Host disk path exposed' } |
| 43 | $global:hypervCase='connected'; $running=Result |
| 44 | if ($running.state -ne 'Running' -or $running.networkAdapterCount -ne 1 -or $running.diskCount -ne 2 -or $running.singleLocalVhdx) { throw 'Connected or multiple-disk VM misreported' } |
| 45 | $global:hypervCase='disk-denied' |
| 46 | if ($null -ne (Result).singleLocalVhdx) { throw 'Denied disk metadata reported as verified' } |
| 47 | $global:hypervCase='legacy'; $legacy = Result |
| 48 | if ($legacy.secureBoot -or $legacy.virtualTpm) { throw 'Legacy VM reported as secure boot/vTPM capable' } |
| 49 | $global:hypervCase='absent'; if ($null -ne (Result)) { throw 'Unexpected VM for missing ID' } |
| 50 | foreach ($testCase in @('denied', 'swapped')) { |
| 51 | $global:hypervCase = $testCase |
| 52 | $rejected = $false |
| 53 | try { $null = Result } catch { $rejected = $true } |
| 54 | if (!$rejected) { throw 'Access failure or changed VM identity treated as a valid lookup' } |
| 55 | } |