2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
2020-09-28 09:22:40 -07:00
|
|
|
|
|
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
|
|
|
|
|
param()
|
|
|
|
|
|
2016-08-24 14:20:07 -07:00
|
|
|
Describe "SecureString conversion tests" -Tags "CI" {
|
|
|
|
|
BeforeAll {
|
|
|
|
|
$string = "ABCD"
|
|
|
|
|
$secureString = [System.Security.SecureString]::New()
|
2020-05-07 13:00:30 +01:00
|
|
|
$string.ToCharArray() | ForEach-Object { $securestring.AppendChar($_) }
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-09 10:36:05 -07:00
|
|
|
It "Using null arguments to ConvertFrom-SecureString produces an exception" {
|
2020-05-07 13:00:30 +01:00
|
|
|
{ ConvertFrom-SecureString -SecureString $null -Key $null } |
|
2018-05-17 14:42:04 -07:00
|
|
|
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2021-09-09 10:36:05 -07:00
|
|
|
It "Using a bad key produces an exception" {
|
2018-05-17 14:42:04 -07:00
|
|
|
$badkey = [byte[]]@(1,2)
|
2020-05-07 13:00:30 +01:00
|
|
|
{ ConvertFrom-SecureString -SecureString $secureString -Key $badkey } |
|
2018-05-17 14:42:04 -07:00
|
|
|
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
|
|
|
}
|
2019-12-11 15:40:41 -08:00
|
|
|
|
2021-09-09 10:36:05 -07:00
|
|
|
It "Can convert back from a secure string" {
|
2020-09-28 09:22:40 -07:00
|
|
|
$ss1 = ConvertTo-SecureString -AsPlainText -Force $string
|
2019-12-11 15:40:41 -08:00
|
|
|
$ss2 = ConvertFrom-SecureString $ss1 | ConvertTo-SecureString
|
2020-09-28 09:22:40 -07:00
|
|
|
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -Be $string
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|
2021-09-09 10:36:05 -07:00
|
|
|
|
|
|
|
|
It "Can encode secure string with key" {
|
|
|
|
|
$testString = '[8Chars][8Chars][Not8]'
|
|
|
|
|
$key = [System.Text.Encoding]::UTF8.GetBytes("1234"*8)
|
|
|
|
|
$ss1 = $testString | ConvertTo-SecureString -AsPlainText -Force
|
|
|
|
|
$encodedStr = $ss1 | ConvertFrom-SecureString -Key $key
|
|
|
|
|
$ss2 = $encodedStr | ConvertTo-SecureString -Key $key
|
|
|
|
|
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -BeExactly $testString
|
|
|
|
|
}
|
2023-07-18 03:30:10 +10:00
|
|
|
|
|
|
|
|
It "Using invalid secure string with ConvertFrom-SecureString produces an exception message without value" {
|
|
|
|
|
$ex = { ConvertFrom-SecureString "1234" } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand" -PassThru
|
|
|
|
|
$ex.Exception.Message | Should -Not -Match "1234"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Using invalid securestring with cast produces an exception message without value" {
|
|
|
|
|
$ex = { [securestring]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru
|
|
|
|
|
$ex.Exception.Message | Should -Not -Match "1234"
|
|
|
|
|
}
|
2016-08-24 14:20:07 -07:00
|
|
|
}
|