2018-02-13 09:23:53 -08:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
# Licensed under the MIT License.
|
2016-08-24 14:20:07 -07:00
|
|
|
Describe "SecureString conversion tests" -Tags "CI" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$string = "ABCD"
|
|
|
|
|
$secureString = [System.Security.SecureString]::New()
|
|
|
|
|
$string.ToCharArray() | foreach-object { $securestring.AppendChar($_) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "using null arguments to ConvertFrom-SecureString produces an exception" {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ ConvertFrom-SecureString -secureString $null -key $null } |
|
|
|
|
|
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-08-24 14:20:07 -07:00
|
|
|
It "using a bad key produces an exception" {
|
2018-05-17 14:42:04 -07:00
|
|
|
$badkey = [byte[]]@(1,2)
|
|
|
|
|
{ ConvertFrom-SecureString -securestring $secureString -key $badkey } |
|
|
|
|
|
Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Can convert to a secure string" {
|
|
|
|
|
$ss = ConvertTo-SecureString -AsPlainText -Force abcd
|
2018-03-23 10:41:14 -07:00
|
|
|
$ss | Should -BeOfType SecureString
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
|
|
|
|
It "can convert back from a secure string" {
|
|
|
|
|
$secret = "abcd"
|
|
|
|
|
$ss1 = ConvertTo-SecureString -AsPlainText -Force $secret
|
|
|
|
|
$ss2 = convertfrom-securestring $ss1 | convertto-securestring
|
2018-03-23 10:41:14 -07:00
|
|
|
[pscredential]::New("user",$ss2).GetNetworkCredential().Password | Should -Be $secret
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
|
|
|
|
}
|