mirror of
https://github.com/LizardByte/libvirtualhid.git
synced 2026-12-04 14:11:46 +09:00
660 lines
22 KiB
PowerShell
660 lines
22 KiB
PowerShell
BeforeAll {
|
|
$sourcePath = Join-Path $PSScriptRoot "..\..\scripts\windows\install-driver.ps1"
|
|
. $sourcePath -InfPath "unused.inf"
|
|
|
|
function global:Invoke-LibVirtualHidTestCommand {
|
|
param([Parameter(ValueFromRemainingArguments)] $Arguments)
|
|
|
|
$null = $Arguments
|
|
$global:LASTEXITCODE = [int] $env:LIBVIRTUALHID_INSTALL_TEST_EXIT_CODE
|
|
}
|
|
}
|
|
|
|
AfterAll {
|
|
Remove-Item -LiteralPath Function:\Invoke-LibVirtualHidTestCommand -ErrorAction SilentlyContinue
|
|
Remove-Item Env:\LIBVIRTUALHID_INSTALL_TEST_EXIT_CODE -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
Describe "Invoke-CheckedCommand" {
|
|
It "accepts a configured success exit code" {
|
|
$env:LIBVIRTUALHID_INSTALL_TEST_EXIT_CODE = 5
|
|
|
|
{
|
|
Invoke-CheckedCommand `
|
|
-FilePath "Invoke-LibVirtualHidTestCommand" `
|
|
-Arguments @("one", "two") `
|
|
-SuccessExitCodes @(0, 5)
|
|
} | Should -Not -Throw
|
|
}
|
|
|
|
It "throws for an unexpected exit code" {
|
|
$env:LIBVIRTUALHID_INSTALL_TEST_EXIT_CODE = 12
|
|
|
|
{
|
|
Invoke-CheckedCommand `
|
|
-FilePath "Invoke-LibVirtualHidTestCommand" `
|
|
-Arguments @("one")
|
|
} | Should -Throw "*exited with code 12*"
|
|
}
|
|
}
|
|
|
|
Describe "install-driver.ps1 entry point" {
|
|
It "executes normal invocation and validates the INF path" {
|
|
{
|
|
& $sourcePath -InfPath (Join-Path $TestDrive "missing.inf") -StageOnly
|
|
} | Should -Throw "*Cannot find path*"
|
|
}
|
|
|
|
It "runs the complete orchestration path safely with WhatIf" {
|
|
$infPath = Join-Path $TestDrive "libvirtualhid.inf"
|
|
$setupPath = Join-Path $TestDrive "libvirtualhid_driver_setup.exe"
|
|
New-Item -ItemType File -Path $infPath, $setupPath | Out-Null
|
|
function pnputil.exe {
|
|
$global:LASTEXITCODE = 0
|
|
@()
|
|
}
|
|
Mock Get-CimInstance { @() }
|
|
Mock Get-ChildItem { @() }
|
|
Mock Get-Service { $null }
|
|
|
|
try {
|
|
{
|
|
& $sourcePath `
|
|
-InfPath $infPath `
|
|
-SetupPath $setupPath `
|
|
-HardwareId "ROOT\LIBVIRTUALHID_PESTER" `
|
|
-WhatIf
|
|
} | Should -Not -Throw
|
|
} finally {
|
|
Remove-Item -LiteralPath Function:\pnputil.exe
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe "Driver package path helpers" {
|
|
It "resolves an explicitly supplied broker" {
|
|
$path = Join-Path $TestDrive "libvirtualhid_broker.exe"
|
|
New-Item -ItemType File -Path $path | Out-Null
|
|
|
|
Resolve-LibVirtualHidBrokerPath -Path $path | Should -Be (Resolve-Path $path).Path
|
|
}
|
|
|
|
It "rejects a missing explicitly supplied broker" {
|
|
{
|
|
Resolve-LibVirtualHidBrokerPath -Path (Join-Path $TestDrive "missing.exe")
|
|
} | Should -Throw "The broker executable was not found*"
|
|
}
|
|
|
|
It "returns null when no packaged broker exists" {
|
|
Resolve-LibVirtualHidBrokerPath | Should -BeNullOrEmpty
|
|
}
|
|
|
|
It "resolves an explicitly supplied setup helper" {
|
|
$path = Join-Path $TestDrive "libvirtualhid_driver_setup.exe"
|
|
New-Item -ItemType File -Path $path | Out-Null
|
|
|
|
Resolve-LibVirtualHidDriverSetupPath -Path $path | Should -Be (Resolve-Path $path).Path
|
|
}
|
|
|
|
It "rejects a missing explicitly supplied setup helper" {
|
|
{
|
|
Resolve-LibVirtualHidDriverSetupPath -Path (Join-Path $TestDrive "missing.exe")
|
|
} | Should -Throw "The driver setup helper was not found*"
|
|
}
|
|
}
|
|
|
|
Describe "Service path quoting" {
|
|
It "quotes a broker path" {
|
|
Get-LibVirtualHidQuotedServiceBinaryPath -Path "C:\Program Files\libvirtualhid\broker.exe" |
|
|
Should -Be '"C:\Program Files\libvirtualhid\broker.exe"'
|
|
}
|
|
|
|
It "rejects quotation marks in a broker path" {
|
|
{
|
|
Get-LibVirtualHidQuotedServiceBinaryPath -Path 'C:\bad"path\broker.exe'
|
|
} | Should -Throw "*must not contain quotation marks*"
|
|
}
|
|
|
|
It "accepts an exactly quoted service ImagePath" {
|
|
Mock Get-ItemProperty {
|
|
[pscustomobject]@{ ImagePath = '"C:\Program Files\libvirtualhid\broker.exe"' }
|
|
}
|
|
|
|
{
|
|
Assert-LibVirtualHidBrokerServiceImagePath `
|
|
-Name "libvirtualhid_broker" `
|
|
-Path "C:\Program Files\libvirtualhid\broker.exe"
|
|
} | Should -Not -Throw
|
|
}
|
|
|
|
It "rejects an unquoted service ImagePath" {
|
|
Mock Get-ItemProperty {
|
|
[pscustomobject]@{ ImagePath = "C:\Program Files\libvirtualhid\broker.exe" }
|
|
}
|
|
|
|
{
|
|
Assert-LibVirtualHidBrokerServiceImagePath `
|
|
-Name "libvirtualhid_broker" `
|
|
-Path "C:\Program Files\libvirtualhid\broker.exe"
|
|
} | Should -Throw "*is not safely quoted*"
|
|
}
|
|
}
|
|
|
|
Describe "Broker service helpers" {
|
|
BeforeEach {
|
|
Mock Remove-ItemProperty {}
|
|
Mock Stop-Service {}
|
|
}
|
|
|
|
It "does not clear a missing service registry key" {
|
|
Mock Test-Path { $false }
|
|
|
|
Clear-LibVirtualHidBrokerServiceEnvironment -Name "libvirtualhid_broker"
|
|
|
|
Should -Invoke Remove-ItemProperty -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "clears a legacy service environment" {
|
|
Mock Test-Path { $true }
|
|
|
|
Clear-LibVirtualHidBrokerServiceEnvironment -Name "libvirtualhid_broker" -Confirm:$false
|
|
|
|
Should -Invoke Remove-ItemProperty -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$LiteralPath -eq "HKLM:\SYSTEM\CurrentControlSet\Services\libvirtualhid_broker" -and
|
|
$Name -eq "Environment"
|
|
}
|
|
}
|
|
|
|
It "does nothing when the broker service is absent" {
|
|
Mock Get-Service { $null }
|
|
|
|
Stop-LibVirtualHidBrokerService -Name "libvirtualhid_broker"
|
|
|
|
Should -Invoke Stop-Service -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "stops a running broker service" {
|
|
$service = [pscustomobject]@{
|
|
Status = "Running"
|
|
Waited = $false
|
|
}
|
|
$service | Add-Member -MemberType ScriptMethod -Name WaitForStatus -Value {
|
|
param($Status, $Timeout)
|
|
$null = $Status, $Timeout
|
|
$this.Waited = $true
|
|
}
|
|
Mock Get-Service { $service }
|
|
|
|
Stop-LibVirtualHidBrokerService -Name "libvirtualhid_broker" -Confirm:$false
|
|
|
|
$service.Waited | Should -BeTrue
|
|
Should -Invoke Stop-Service -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Name -eq "libvirtualhid_broker" -and $Force
|
|
}
|
|
}
|
|
|
|
It "skips service registration when no broker executable exists" {
|
|
Mock Write-Verbose {}
|
|
|
|
Install-LibVirtualHidBrokerService
|
|
|
|
Should -Invoke Write-Verbose -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -like "No libvirtualhid broker executable was found*"
|
|
}
|
|
}
|
|
|
|
It "registers and configures a new broker service" {
|
|
$path = Join-Path $TestDrive "libvirtualhid_broker.exe"
|
|
New-Item -ItemType File -Path $path | Out-Null
|
|
$resolvedPath = (Resolve-Path $path).Path
|
|
Mock Get-Service { $null }
|
|
Mock New-Service {}
|
|
Mock Assert-LibVirtualHidBrokerServiceImagePath {}
|
|
Mock Clear-LibVirtualHidBrokerServiceEnvironment {}
|
|
Mock Invoke-CheckedCommand {}
|
|
Mock Start-Service {}
|
|
|
|
Install-LibVirtualHidBrokerService -Path $path -Confirm:$false
|
|
|
|
Should -Invoke New-Service -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Name -eq "libvirtualhid_broker" -and
|
|
$BinaryPathName -eq ('"' + $resolvedPath + '"') -and
|
|
$StartupType -eq "Automatic"
|
|
}
|
|
Should -Invoke Assert-LibVirtualHidBrokerServiceImagePath -Times 1 -Exactly -Scope It
|
|
Should -Invoke Clear-LibVirtualHidBrokerServiceEnvironment -Times 1 -Exactly -Scope It
|
|
Should -Invoke Invoke-CheckedCommand -Times 2 -Exactly -Scope It
|
|
Should -Invoke Start-Service -Times 1 -Exactly -Scope It
|
|
}
|
|
|
|
It "updates and configures an existing broker service" {
|
|
$path = Join-Path $TestDrive "existing-libvirtualhid_broker.exe"
|
|
New-Item -ItemType File -Path $path | Out-Null
|
|
$resolvedExistingPath = (Resolve-Path $path).Path
|
|
Mock Get-Service { [pscustomobject]@{ Status = "Stopped" } }
|
|
Mock Stop-LibVirtualHidBrokerService {}
|
|
Mock Assert-LibVirtualHidBrokerServiceImagePath {}
|
|
Mock Clear-LibVirtualHidBrokerServiceEnvironment {}
|
|
Mock Invoke-CheckedCommand {}
|
|
Mock Set-LibVirtualHidBrokerServiceConfiguration { $true }
|
|
Mock Start-Service {}
|
|
|
|
Install-LibVirtualHidBrokerService -Path $path -Confirm:$false
|
|
|
|
Should -Invoke Stop-LibVirtualHidBrokerService -Times 1 -Exactly -Scope It
|
|
Should -Invoke Invoke-CheckedCommand -Times 2 -Exactly -Scope It
|
|
Should -Invoke Set-LibVirtualHidBrokerServiceConfiguration -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Path -eq $resolvedExistingPath
|
|
}
|
|
Should -Invoke Start-Service -Times 1 -Exactly -Scope It
|
|
}
|
|
}
|
|
|
|
Describe "Broker service reconfiguration" {
|
|
BeforeEach {
|
|
$cimService = New-CimInstance -ClassName Win32_Service -ClientOnly -Property @{ Name = "libvirtualhid_broker" }
|
|
Mock Get-CimInstance { $cimService }
|
|
Mock Invoke-CimMethod { [pscustomobject]@{ ReturnValue = 0 } }
|
|
}
|
|
|
|
It "passes a quoted executable path and service settings directly to CIM" {
|
|
Set-LibVirtualHidBrokerServiceConfiguration -Path "C:\Program Files\libvirtualhid\broker.exe" -Confirm:$false |
|
|
Should -BeTrue
|
|
|
|
Should -Invoke Get-CimInstance -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$ClassName -eq "Win32_Service" -and $Filter -eq "Name = 'libvirtualhid_broker'"
|
|
}
|
|
Should -Invoke Invoke-CimMethod -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$InputObject -eq $cimService -and $MethodName -eq "Change" -and
|
|
$Arguments.PathName -ceq '"C:\Program Files\libvirtualhid\broker.exe"' -and
|
|
$Arguments.DisplayName -eq "libvirtualhid Broker" -and $Arguments.StartMode -eq "Automatic"
|
|
}
|
|
}
|
|
|
|
It "rejects a failed service configuration change" {
|
|
Mock Invoke-CimMethod { [pscustomobject]@{ ReturnValue = 2 } }
|
|
|
|
{
|
|
Set-LibVirtualHidBrokerServiceConfiguration -Path "C:\broker.exe" -Confirm:$false
|
|
} | Should -Throw "*Win32_Service.Change return value 2*"
|
|
}
|
|
|
|
It "rejects a service that disappeared before reconfiguration" {
|
|
Mock Get-CimInstance { $null }
|
|
|
|
{
|
|
Set-LibVirtualHidBrokerServiceConfiguration -Path "C:\broker.exe" -Confirm:$false
|
|
} | Should -Throw "*service was not found for reconfiguration*"
|
|
|
|
Should -Invoke Invoke-CimMethod -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "does not reconfigure a service during WhatIf" {
|
|
Set-LibVirtualHidBrokerServiceConfiguration -Path "C:\broker.exe" -WhatIf | Should -BeFalse
|
|
|
|
Should -Invoke Get-CimInstance -Times 0 -Exactly -Scope It
|
|
Should -Invoke Invoke-CimMethod -Times 0 -Exactly -Scope It
|
|
}
|
|
}
|
|
|
|
Describe "Driver installation helpers" {
|
|
BeforeEach {
|
|
$script:LibVirtualHidRebootRequired = $false
|
|
}
|
|
|
|
It "imports an existing certificate into both required stores" {
|
|
$path = Join-Path $TestDrive "driver.cer"
|
|
New-Item -ItemType File -Path $path | Out-Null
|
|
Mock Import-Certificate {}
|
|
|
|
Import-DriverCertificate -Path $path -Confirm:$false
|
|
|
|
Should -Invoke Import-Certificate -Times 2 -Exactly -Scope It -ParameterFilter {
|
|
$FilePath -eq (Resolve-Path $path).Path -and
|
|
$CertStoreLocation -in @("Cert:\LocalMachine\Root", "Cert:\LocalMachine\TrustedPublisher")
|
|
}
|
|
}
|
|
|
|
It "ignores a missing certificate" {
|
|
Mock Import-Certificate {}
|
|
|
|
Import-DriverCertificate -Path (Join-Path $TestDrive "missing.cer")
|
|
|
|
Should -Invoke Import-Certificate -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "removes a stale device with pnputil" {
|
|
Mock Invoke-CheckedCommand {}
|
|
|
|
Remove-DeviceInstance -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
Should -Invoke Invoke-CheckedCommand -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$FilePath -eq "pnputil.exe" -and
|
|
$Arguments[0] -eq "/remove-device" -and
|
|
$Arguments[1] -eq "ROOT\LIBVIRTUALHID\0000"
|
|
}
|
|
}
|
|
|
|
It "sets VhfMode on an existing root device" {
|
|
Mock Test-Path { $true }
|
|
Mock New-ItemProperty {}
|
|
|
|
Set-RootDeviceVhfMode -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
Should -Invoke New-ItemProperty -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Name -eq "VhfMode" -and $Value -eq 1 -and $PropertyType -eq "DWord"
|
|
}
|
|
}
|
|
|
|
It "skips VhfMode when the registry device is absent" {
|
|
Mock Test-Path { $false }
|
|
Mock New-ItemProperty {}
|
|
Mock Write-Verbose {}
|
|
|
|
Set-RootDeviceVhfMode -InstanceId "ROOT\LIBVIRTUALHID\0000"
|
|
|
|
Should -Invoke New-ItemProperty -Times 0 -Exactly -Scope It
|
|
Should -Invoke Write-Verbose -Times 1 -Exactly -Scope It
|
|
}
|
|
|
|
It "warns when restarting the device requires a reboot" {
|
|
Mock pnputil.exe {
|
|
$global:LASTEXITCODE = 0
|
|
"A reboot is needed to complete the operation."
|
|
}
|
|
Mock Write-Warning {}
|
|
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -like "Windows reported that a reboot is required*"
|
|
}
|
|
$script:LibVirtualHidRebootRequired | Should -BeTrue
|
|
}
|
|
|
|
It "defers a restart with exit code <ExitCode> until reboot" -TestCases @(
|
|
@{ ExitCode = 50 },
|
|
@{ ExitCode = 3010 }
|
|
) {
|
|
param($ExitCode)
|
|
$script:restartExitCode = $ExitCode
|
|
Mock pnputil.exe { $global:LASTEXITCODE = $script:restartExitCode }
|
|
Mock Write-Warning {}
|
|
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
$script:LibVirtualHidRebootRequired | Should -BeTrue
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -like "*Restart Windows to complete*"
|
|
}
|
|
}
|
|
|
|
It "does not restart a device once a reboot is required" {
|
|
$script:LibVirtualHidRebootRequired = $true
|
|
Mock pnputil.exe { throw "A pending-reboot device must not be restarted" }
|
|
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
Should -Invoke pnputil.exe -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "handles native stderr before interpreting a reboot exit code" {
|
|
Mock pnputil.exe {
|
|
& $env:ComSpec /c "echo Device is pending system reboot. 1>&2 & exit /b 50"
|
|
}
|
|
Mock Write-Warning {}
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
$script:LibVirtualHidRebootRequired | Should -BeTrue
|
|
$ErrorActionPreference | Should -Be "Stop"
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It
|
|
}
|
|
|
|
It "preserves a command invocation failure and restores the error preference" {
|
|
Mock pnputil.exe { throw "Launch failed" }
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
{
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
} | Should -Throw "Launch failed"
|
|
|
|
$ErrorActionPreference | Should -Be "Stop"
|
|
}
|
|
|
|
It "restarts a device successfully without requiring a reboot" {
|
|
Mock pnputil.exe { $global:LASTEXITCODE = 0 }
|
|
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
|
|
$script:LibVirtualHidRebootRequired | Should -BeFalse
|
|
Should -Invoke pnputil.exe -Times 1 -Exactly -Scope It
|
|
}
|
|
|
|
It "throws when restarting the device fails" {
|
|
Mock pnputil.exe { $global:LASTEXITCODE = 7 }
|
|
|
|
{
|
|
Restart-RootDevice -InstanceId "ROOT\LIBVIRTUALHID\0000" -Confirm:$false
|
|
} | Should -Throw "*exited with code 7*"
|
|
}
|
|
|
|
It "updates a driver and permits SetupAPI reboot status" {
|
|
Mock Invoke-CheckedCommand { $global:LASTEXITCODE = 3010 }
|
|
Mock Write-Warning {}
|
|
|
|
Update-RootDeviceDriverWithSetupApi `
|
|
-Path "driver.inf" `
|
|
-TargetHardwareId "ROOT\LIBVIRTUALHID" `
|
|
-SetupHelperPath "setup.exe" `
|
|
-Confirm:$false
|
|
|
|
Should -Invoke Invoke-CheckedCommand -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$FilePath -eq "setup.exe" -and
|
|
$Arguments[0] -eq "update" -and
|
|
$SuccessExitCodes -contains 3010
|
|
}
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It
|
|
$script:LibVirtualHidRebootRequired | Should -BeTrue
|
|
}
|
|
|
|
It "installs a root device through the SetupAPI helper" {
|
|
Mock Invoke-CheckedCommand {}
|
|
|
|
Install-RootDeviceWithSetupApi `
|
|
-Path "driver.inf" `
|
|
-TargetHardwareId "ROOT\LIBVIRTUALHID" `
|
|
-SetupHelperPath "setup.exe"
|
|
|
|
Should -Invoke Invoke-CheckedCommand -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$FilePath -eq "setup.exe" -and $Arguments -join "," -eq "install,driver.inf,ROOT\LIBVIRTUALHID"
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe "Install-LibVirtualHidDriver" {
|
|
BeforeEach {
|
|
$infPath = Join-Path $TestDrive "libvirtualhid.inf"
|
|
Set-Content -LiteralPath $infPath -Value "test INF"
|
|
$script:installOperations = [System.Collections.Generic.List[string]]::new()
|
|
$script:brokerStatus = "Running"
|
|
Mock Import-DriverCertificate {}
|
|
Mock Invoke-CheckedCommand {
|
|
$script:installOperations.Add("stage")
|
|
}
|
|
Mock Resolve-LibVirtualHidDriverSetupPath { "setup.exe" }
|
|
Mock Resolve-LibVirtualHidBrokerPath { "broker.exe" }
|
|
Mock Get-Service { [pscustomobject]@{ Status = $script:brokerStatus } }
|
|
Mock Stop-LibVirtualHidBrokerService {
|
|
$script:installOperations.Add("stop")
|
|
$script:brokerStatus = "Stopped"
|
|
}
|
|
Mock Get-LibVirtualHidRegistryRootDevice { @() }
|
|
Mock Get-LibVirtualHidRootDeviceInstanceId { "ROOT\LIBVIRTUALHID\0000" }
|
|
Mock Install-RootDeviceWithSetupApi {
|
|
$script:installOperations.Add("create")
|
|
}
|
|
Mock Remove-DeviceInstance {
|
|
$script:installOperations.Add("remove")
|
|
}
|
|
Mock Set-RootDeviceVhfMode {
|
|
$script:installOperations.Add("vhf")
|
|
}
|
|
Mock Update-RootDeviceDriverWithSetupApi {
|
|
$script:installOperations.Add("update")
|
|
}
|
|
Mock Restart-RootDevice {
|
|
$script:installOperations.Add("restart")
|
|
}
|
|
Mock Install-LibVirtualHidBrokerService {
|
|
$script:installOperations.Add("broker")
|
|
$script:brokerStatus = "Running"
|
|
}
|
|
Mock Start-Service {
|
|
$script:installOperations.Add("restore")
|
|
}
|
|
Mock Write-Warning {}
|
|
}
|
|
|
|
It "stops the broker before updating an existing device and starts it last" {
|
|
Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false
|
|
|
|
$script:installOperations.ToArray() | Should -Be @("stage", "stop", "vhf", "update", "restart", "broker")
|
|
Should -Invoke Install-RootDeviceWithSetupApi -Times 0 -Exactly -Scope It
|
|
Should -Invoke Start-Service -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "stops the broker before removing a stale device and creating its replacement" {
|
|
Mock Get-LibVirtualHidRegistryRootDevice {
|
|
[pscustomobject]@{
|
|
InstanceId = "ROOT\LIBVIRTUALHID\0000"
|
|
HasCorruptHardwareId = $true
|
|
HasLegacyHidClass = $false
|
|
}
|
|
}
|
|
$script:deviceQueries = 0
|
|
Mock Get-LibVirtualHidRootDeviceInstanceId {
|
|
$script:deviceQueries += 1
|
|
if ($script:deviceQueries -gt 1) {
|
|
"ROOT\LIBVIRTUALHID\0001"
|
|
}
|
|
}
|
|
|
|
Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false
|
|
|
|
$script:installOperations.ToArray() | Should -Be @("stage", "stop", "remove", "create", "vhf", "update", "restart", "broker")
|
|
}
|
|
|
|
It "finishes broker installation without restarting a pending-reboot device" {
|
|
Mock Update-RootDeviceDriverWithSetupApi {
|
|
$script:installOperations.Add("update")
|
|
$script:LibVirtualHidRebootRequired = $true
|
|
}
|
|
|
|
Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false
|
|
|
|
$script:installOperations.ToArray() | Should -Be @("stage", "stop", "vhf", "update", "broker")
|
|
Should -Invoke Restart-RootDevice -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "does not stop the broker when only staging a driver" {
|
|
Install-LibVirtualHidDriver -InfPath $infPath -StageOnly -Confirm:$false
|
|
|
|
$script:installOperations.ToArray() | Should -Be @("stage")
|
|
Should -Invoke Get-Service -Times 0 -Exactly -Scope It
|
|
Should -Invoke Resolve-LibVirtualHidDriverSetupPath -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "validates the broker path before stopping the existing service" {
|
|
Mock Resolve-LibVirtualHidBrokerPath { throw "Missing broker" }
|
|
|
|
{ Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false } | Should -Throw "Missing broker"
|
|
|
|
Should -Invoke Stop-LibVirtualHidBrokerService -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "preserves a driver update failure and restores a previously running broker" {
|
|
Mock Update-RootDeviceDriverWithSetupApi { throw "Update failed" }
|
|
|
|
{ Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false } | Should -Throw "Update failed"
|
|
|
|
$script:installOperations.ToArray() | Should -Be @("stage", "stop", "vhf", "restore")
|
|
Should -Invoke Install-LibVirtualHidBrokerService -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "does not start a previously stopped broker after an update failure" {
|
|
$script:brokerStatus = "Stopped"
|
|
Mock Update-RootDeviceDriverWithSetupApi { throw "Update failed" }
|
|
|
|
{ Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false } | Should -Throw "Update failed"
|
|
|
|
Should -Invoke Start-Service -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "preserves an update failure if broker recovery also fails" {
|
|
Mock Update-RootDeviceDriverWithSetupApi { throw "Update failed" }
|
|
Mock Start-Service { throw "Recovery failed" }
|
|
|
|
{ Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false } | Should -Throw "Update failed"
|
|
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -eq "Unable to restore the libvirtualhid broker service: Recovery failed"
|
|
}
|
|
}
|
|
|
|
It "restores a running broker after an install without a broker executable" {
|
|
Mock Resolve-LibVirtualHidBrokerPath { $null }
|
|
Mock Install-LibVirtualHidBrokerService {}
|
|
|
|
Install-LibVirtualHidDriver -InfPath $infPath -Confirm:$false
|
|
|
|
$script:installOperations[-1] | Should -Be "restore"
|
|
}
|
|
}
|
|
|
|
Describe "Installation failure logging" {
|
|
BeforeEach {
|
|
$script:LibVirtualHidTranscriptStarted = $false
|
|
Mock Write-Warning {}
|
|
}
|
|
|
|
It "records the error if a transcript could not be started" {
|
|
$path = Join-Path $TestDrive "failure.log"
|
|
|
|
Write-LibVirtualHidInstallFailure -Path $path -Message "Driver update failed"
|
|
|
|
Get-Content -LiteralPath $path | Should -BeLike "*libvirtualhid driver installation failed: Driver update failed"
|
|
}
|
|
|
|
It "writes the error to an active transcript without appending to its file" {
|
|
$script:LibVirtualHidTranscriptStarted = $true
|
|
Mock Add-Content {}
|
|
|
|
Write-LibVirtualHidInstallFailure -Path "driver.log" -Message "Driver update failed"
|
|
|
|
Should -Invoke Add-Content -Times 0 -Exactly -Scope It
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -eq "libvirtualhid driver installation failed: Driver update failed"
|
|
}
|
|
}
|
|
|
|
It "does not require a log path" {
|
|
Mock Add-Content {}
|
|
|
|
Write-LibVirtualHidInstallFailure -Message "Driver update failed"
|
|
|
|
Should -Invoke Add-Content -Times 0 -Exactly -Scope It
|
|
}
|
|
|
|
It "does not replace the original error when writing the log also fails" {
|
|
Mock Add-Content { throw "Log is locked" }
|
|
|
|
{ Write-LibVirtualHidInstallFailure -Path "driver.log" -Message "Driver update failed" } | Should -Not -Throw
|
|
|
|
Should -Invoke Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
|
|
$Message -eq "Unable to write the libvirtualhid installation error log: Log is locked"
|
|
}
|
|
}
|
|
}
|