Files
PowerShell-PowerShell/test/powershell/Modules/Microsoft.PowerShell.Security/SecureString.Tests.ps1
T
Klaudia Algiz 2df9aac811 Convert ShouldBeErrorId to Should -Throw -ErrorId in PowerShell tests (#6682)
Convert ShouldBeErrorId to Should -Throw -ErrorId in PowerShell tests.
Get rid of try { } catch { } formula to assert that errors were thrown.
Small fixes in tests to obey the new Pester -Parameter syntax.
2018-05-17 14:42:04 -07:00

38 lines
1.6 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "SecureString conversion tests" -Tags "CI" {
BeforeAll {
$string = "ABCD"
$secureString = [System.Security.SecureString]::New()
$string.ToCharArray() | foreach-object { $securestring.AppendChar($_) }
$defaultParamValues = $PSdefaultParameterValues.Clone()
$PSdefaultParameterValues = @{}
if ( ! $IsWindows ) { $PSdefaultParameterValues["it:pending"] = $true }
}
AfterAll {
$global:PSdefaultParameterValues = $defaultParamValues
}
It "using null arguments to ConvertFrom-SecureString produces an exception" {
{ ConvertFrom-SecureString -secureString $null -key $null } |
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"
}
It "using a bad key produces an exception" {
$badkey = [byte[]]@(1,2)
{ ConvertFrom-SecureString -securestring $secureString -key $badkey } |
Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"
}
It "Can convert to a secure string" {
$ss = ConvertTo-SecureString -AsPlainText -Force abcd
$ss | Should -BeOfType SecureString
}
It "can convert back from a secure string" {
$secret = "abcd"
$ss1 = ConvertTo-SecureString -AsPlainText -Force $secret
$ss2 = convertfrom-securestring $ss1 | convertto-securestring
[pscredential]::New("user",$ss2).GetNetworkCredential().Password | Should -Be $secret
}
}