PowerShell/test/powershell/engine/Remoting/RoleCapabilityFiles.Tests.ps1
bergmeister ffd39b2853 PSScriptAnalyzer fixes by category (#4261)
- Fix PSScriptAnalyzer warnings of type PSAvoidUsingCmdletAliases for 'ForEach-Object' (alias is '%' or 'foreach')
- Fix PSScriptAnalyzer warnings of type PSAvoidUsingCmdletAliases for 'Where-Object' (alias is '?' or 'where')
- Fix PSScriptAnalyzer warnings of type PSAvoidUsingCmdletAliases for 'Select-Object' (alias is 'select')
- Fix PSScriptAnalyzer warnings of type PSPossibleIncorrectComparisonWithNull. Essentially, $null has to be on the left-hand side when using it for comparison.
- A Test in ParameterBinding.Tests.ps1 needed adapting as this test used to rely on the wrong null comparison
- Replace a subset of tests of kind '($object -eq $null) | Should Be $true' with '$object | Should Be $null'
2017-07-21 21:03:49 -07:00

112 lines
3.8 KiB
PowerShell

##
## PowerShell Remoting Endpoint Role Capability Files Tests
##
Describe "Remote session configuration RoleDefintion RoleCapabilityFiles key tests" -Tags "Feature" {
BeforeAll {
if (!$IsWindows)
{
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
}
else
{
[string] $RoleCapDirectory = (New-Item -Path "$TestDrive\RoleCapability" -ItemType Directory -Force).FullName
[string] $GoodRoleCapFile = "$RoleCapDirectory\TestGoodRoleCap.psrc"
New-PSRoleCapabilityFile -Path $GoodRoleCapFile -VisibleCmdlets 'Get-Command','Get-Process','Clear-Host','Out-Default','Select-Object','Get-FormatData','Get-Help'
[string] $BadRoleCapFile = "$RoleCapDirectory\TestBadRoleCap.psrc"
New-PSRoleCapabilityFile -Path $BadRoleCapFile -VisibleCmdlets *
[string] $BadRoleCapFile = $BadRoleCapFile.Replace('.psrc', 'psbad')
[string] $PSSessionConfigFile = "$RoleCapDirectory\TestConfig.pssc"
}
}
AfterAll {
if (!$IsWindows)
{
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
}
It "Verifies missing role capability file error" {
New-PSSessionConfigurationFile -Path $PSSessionConfigFile -RoleDefinitions @{
Administrators = @{ RoleCapabilityFiles = "$RoleCapDirectory\NoFile.psrc" }
}
$fullyQualifiedErrorId = ""
try
{
$iss = [initialsessionstate]::CreateFromSessionConfigurationFile($PSSessionConfigFile, { $true })
throw 'No Exception!'
}
catch
{
$psioe = [System.Management.Automation.PSInvalidOperationException] ($_.Exception).InnerException
if ($null -ne $psioe)
{
$fullyQualifiedErrorId = $psioe.ErrorRecord.FullyQualifiedErrorId
}
$fullyQualifiedErrorId | Should Be 'CouldNotFindRoleCapabilityFile'
}
}
It "Verifies incorrect role capability file extenstion error" {
New-PSSessionConfigurationFile -Path $PSSessionConfigFile -RoleDefinitions @{
Administrators = @{ RoleCapabilityFiles = "$BadRoleCapFile" }
}
$fullyQualifiedErrorId = ""
try
{
$iss = [initialsessionstate]::CreateFromSessionConfigurationFile($PSSessionConfigFile, { $true })
throw 'No Exception!'
}
catch
{
$psioe = [System.Management.Automation.PSInvalidOperationException] ($_.Exception).InnerException
if ($null -ne $psioe)
{
$fullyQualifiedErrorId = $psioe.ErrorRecord.FullyQualifiedErrorId
}
$fullyQualifiedErrorId | Should Be 'InvalidRoleCapabilityFileExtension'
}
}
It "Verifies restriction on good role capability file" {
New-PSSessionConfigurationFile -Path $PSSessionConfigFile -RoleDefinitions @{
Administrators = @{ RoleCapabilityFiles = "$GoodRoleCapFile" }
}
# 'Get-Service' is not included in the session.
$iss = [initialsessionstate]::CreateFromSessionConfigurationFile($PSSessionConfigFile, { $true })
[powershell] $ps = [powershell]::Create($iss)
$null = $ps.AddCommand('Get-Service')
$exceptionTypeName = ""
try
{
$ps.Invoke()
throw 'No Exception!'
}
catch
{
if ($null -ne $_.Exception.InnerException)
{
$exceptionTypeName = $_.Exception.InnerException.GetType().FullName
}
$exceptionTypeName | Should Be 'System.Management.Automation.CommandNotFoundException'
}
$ps.Dispose()
}
}