albert
March 16, 2019, 10:10am
1
Hi experts,
I’m writing a PowerShell function that imports System.Security.SecureString from the pre-exported ‘Standard Encrypted String’ and ‘Encryption Key’, and I ran into an issue when I was writing Pester test for it. Here is the simplified example:
function Import-SecureString ($str, $key) {
ConvertTo-SecureString -String $str -Key $key
}
Describe 'Import-SecureString' {
BeforeAll {
$password = '123456'
$encryptionKey = New-Object -TypeName System.Byte[] -ArgumentList 24
$RNGCryptoServiceProvider = New-Object -TypeName System.Security.Cryptography.RNGCryptoServiceProvider
$RNGCryptoServiceProvider.GetBytes($encryptionKey)
$encryptedStandardString = ConvertTo-SecureString -String $password -AsPlainText -Force | ConvertFrom-SecureString -Key $encryptionKey
}
Context 'Unit Test' {
Mock ConvertTo-SecureString
Import-SecureString -str $encryptedStandardString -key $encryptionKey
It 'Should bind VAR $encryptedStandardString to PARAM str' {
$assert = @{
CommandName = 'ConvertTo-SecureString'
Times = 1
Exactly = $true
ParameterFilter = {$String -eq $encryptedStandardString}
}
Assert-MockCalled @assert
}
It 'Should bind VAR $encryptionKey to PARAM key' {
$assert = @{
CommandName = 'ConvertTo-SecureString'
Times = 1
Exactly = $true
ParameterFilter = {$Key -eq $encryptionKey}
}
Assert-MockCalled @assert
}
}
}
In this example, the first ‘It’ block passed and the second failed, how can I correctly assert the second one?
Can you share the pester output for the failed test case.
albert
March 18, 2019, 8:21pm
3
Hi kvprasoon,
Here is the output:
Describing Import-SecureString
Context Unit Test
[+] Should bind VAR $encryptedStandardString to PARAM str 5ms
[-] Should bind VAR $encryptionKey to PARAM key 8ms
Expected ConvertTo-SecureString to be called 1 times exactly but was called 0 times
36: Assert-MockCalled @assert
Also, if I replace the key with “123”, although the key length is invalid, the test will pass:
function Import-SecureString ($str, $key) {
ConvertTo-SecureString -String $str -Key $key
}
Describe 'Import-SecureString' {
BeforeAll {
$encryptedStandardString = ConvertTo-SecureString -String '123456' -AsPlainText -Force | ConvertFrom-SecureString
$encryptionKey = '123'
}
Context 'Unit Test' {
Mock ConvertTo-SecureString
Import-SecureString -str $encryptedStandardString -key $encryptionKey
It 'Should bind VAR $encryptedStandardString to PARAM str' {
$assert = @{
CommandName = 'ConvertTo-SecureString'
Times = 1
Exactly = $true
ParameterFilter = {$String -eq $encryptedStandardString}
}
Assert-MockCalled @assert
}
It 'Should bind VAR $encryptionKey to PARAM key' {
$assert = @{
CommandName = 'ConvertTo-SecureString'
Times = 1
Exactly = $true
ParameterFilter = {$Key -eq $encryptionKey}
}
Assert-MockCalled @assert
}
}
}
not getting any clue here, will include Jakub here.
albert
March 25, 2019, 7:38pm
5
Hi kvprasoon,
Should I submit an issue to GitHub?
Can you share the pester output for the failed test case.
nohwnd
June 19, 2019, 4:58am
9
Both tests pass on my computer (macOS / Pwsh 6.2.1 / Pester 4.8.1), can everybody else replicate it? What versions are you using?