MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]5param()6 7Describe "SecureString conversion tests" -Tags "CI" {8 BeforeAll {9 $string = "ABCD"10 $secureString = [System.Security.SecureString]::New()11 $string.ToCharArray() | ForEach-Object { $securestring.AppendChar($_) }12 }13 14 It "Using null arguments to ConvertFrom-SecureString produces an exception" {15 { ConvertFrom-SecureString -SecureString $null -Key $null } |16 Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"17 }18 19 It "Using a bad key produces an exception" {20 $badkey = [byte[]]@(1,2)21 { ConvertFrom-SecureString -SecureString $secureString -Key $badkey } |22 Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand"23 }24 25 It "Can convert to a secure string" {26 $ss = ConvertTo-SecureString -AsPlainText -Force abcd27 $ss | Should -BeOfType SecureString28 }29 30 It "Can convert back from a secure string" {31 $ss1 = ConvertTo-SecureString -AsPlainText -Force $string32 $ss2 = ConvertFrom-SecureString $ss1 | ConvertTo-SecureString33 $ss2 | ConvertFrom-SecureString -AsPlainText | Should -Be $string34 }35 36 It "Can encode secure string with key" {37 $testString = '[8Chars][8Chars][Not8]'38 $key = [System.Text.Encoding]::UTF8.GetBytes("1234"*8)39 $ss1 = $testString | ConvertTo-SecureString -AsPlainText -Force40 $encodedStr = $ss1 | ConvertFrom-SecureString -Key $key41 $ss2 = $encodedStr | ConvertTo-SecureString -Key $key42 $ss2 | ConvertFrom-SecureString -AsPlainText | Should -BeExactly $testString43 }44 45 It "Using invalid secure string with ConvertFrom-SecureString produces an exception message without value" {46 $ex = { ConvertFrom-SecureString "1234" } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand" -PassThru47 $ex.Exception.Message | Should -Not -Match "1234"48 }49 50 It "Using invalid securestring with cast produces an exception message without value" {51 $ex = { [securestring]"1234" } | Should -Throw -ErrorId "ConvertToFinalInvalidCastException" -PassThru52 $ex.Exception.Message | Should -Not -Match "1234"53 }54}55 