PowerShell/test/powershell/engine/Remoting/RoleCapabilityFiles.Tests.ps1
Steve Lee c1c5344a88 Update copyright and license headers (#6134)
Based on standard practices, we need to have a copyright and license notice at the top of each source file. Removed existing copyrights and updated/added copyright notices for .h, .cpp, .cs, .ps1, and .psm1 files.

Updated module manifests for consistency to have Author = "PowerShell" and Company = "Microsoft Corporation". Removed multiple line breaks.

Separate PR coming to update contribution document for new source files: #6140

Manually reviewed each change.

Fix #6073
2018-02-13 09:23:53 -08:00

114 lines
3.9 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
##
## 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()
}
}