config-central/tests/Elevation.Tests.ps1

76 lines
3.5 KiB
PowerShell

BeforeAll {
Import-Module "$PSScriptRoot/../src/Elevation.psm1" -Force
}
Describe 'Elevation host selection' {
It 'uses the current PowerShell installation instead of PATH' {
InModuleScope Elevation -Parameters @{ Working = $TestDrive } {
Mock Test-IsAdministrator { $false }
Mock Protect-ElevationFile {}
Mock Start-Process {
$script:startedFile = $FilePath
$match = [regex]::Match($ArgumentList, '-ResultPath\s+"([^"]+)"')
'{"succeeded":true,"results":[]}' | Set-Content -LiteralPath $match.Groups[1].Value
[pscustomobject]@{ ExitCode = 0 }
}
$paths = [pscustomobject]@{ Root = $Working }
$operation = [pscustomobject]@{
kind = 'package'; name = 'Example.App'; action = 'Install'; reason = 'Missing'
package = [pscustomobject]@{ id = 'Example.App'; source = 'winget'; scope = 'machine'; update = 'latest' }
}
[void](Invoke-ElevatedPackageBatch -Operations @($operation) -RepositoryRoot $Working -Paths $paths)
$script:startedFile | Should -Be (Join-Path $PSHOME 'pwsh.exe')
}
}
}
Describe 'Elevation request file protection' -Skip:(-not $IsWindows) {
It 'restricts a request file to the user, administrators, and SYSTEM' {
InModuleScope Elevation -Parameters @{ Root = $TestDrive } {
param($Root)
$path = Join-Path $Root 'request [test].json'
'{}' | Set-Content -LiteralPath $path
Protect-ElevationFile -Path $path
$acl = Get-Acl -LiteralPath $path
$acl.AreAccessRulesProtected | Should -BeTrue
$rules = @($acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier]))
$expected = @(
[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
'S-1-5-32-544'
'S-1-5-18'
) | Sort-Object -Unique
@($rules.IdentityReference.Value | Sort-Object -Unique) | Should -Be $expected
foreach ($rule in $rules) {
$rule.IsInherited | Should -BeFalse
$rule.AccessControlType | Should -Be 'Allow'
$rule.FileSystemRights | Should -Be 'FullControl'
}
Get-Content -LiteralPath $path -Raw | Should -Match '^{}'
}
}
It 'propagates ACL write failures' {
InModuleScope Elevation -Parameters @{ Root = $TestDrive } {
param($Root)
$path = Join-Path $Root 'failed-request.json'
'{}' | Set-Content -LiteralPath $path
Mock Set-Acl { throw 'ACL write denied' }
{ Protect-ElevationFile -Path $path } | Should -Throw '*ACL write denied*'
}
}
}
Describe 'Portable elevation boundary' {
It 'rejects portable requests before launching or using the administrator fast path' {
InModuleScope Elevation -Parameters @{ Working = $TestDrive } {
Mock Test-IsAdministrator { $true }
Mock Start-Process { throw 'Must not launch' }
$operation = [pscustomobject]@{ package = [pscustomobject]@{ provider = 'portable'; scope = 'machine' } }
{ Invoke-ElevatedPackageBatch -Operations @($operation) -RepositoryRoot $Working -Paths ([pscustomobject]@{ Root = $Working }) } | Should -Throw '*Only WinGet*'
Should -Invoke Test-IsAdministrator -Times 0
Should -Invoke Start-Process -Times 0
}
}
}