-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiddenString.Tests.ps1
108 lines (76 loc) · 2.97 KB
/
HiddenString.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#Requires -Modules @{ModuleName="Pester"; ModuleVersion="5.0.0"}
Describe 'Join-Object' {
BeforeAll {
Set-StrictMode -Version Latest
. $PSScriptRoot\HiddenString.ps1
$Secret = 'Confidential information'
}
Context 'General tests' {
BeforeEach {
$HiddenSecret = [HiddenString]$Secret
}
It 'Hide' {
$HiddenSecret | Should -Be HiddenString
}
It 'Reveal' {
$HiddenSecret.Reveal() | Should -Be $Secret
}
It 'Clear' {
$HiddenSecret.Clear()
$HiddenSecret.Reveal() | Should -BeNullOrEmpty
}
It 'Add' {
$HiddenSecret = [HiddenString]::new()
$HiddenSecret.Add($Secret)
$HiddenSecret.Reveal() | Should -Be $Secret
}
It 'Equals' {
$HiddenSecret -eq $HiddenSecret | Should -be $True
$HiddenString = [HiddenString]'Something else'
$HiddenSecret -eq $HiddenString | Should -be $False
}
It 'Length' {
$HiddenSecret.Length | Should -be $Secret.Length
}
}
Context 'Use cases' {
It 'Hidden parameter' {
$Actual = $Null
function BadApp([String]$Username, [String]$Password) {
([ref]$Actual).Value = $Password
}
function MyScript {
[CmdletBinding()] param(
[String]$Username,
[HiddenString]$Password
)
Write-Host "Credentials: $Username/$Password" # Write-Log ...
BadApp $Username $Password.Reveal()
}
Start-Transcript -Path $PSScriptRoot\Transcript.txt
MyScript JohnDoe $Secret -WarningAction SilentlyContinue
Stop-Transcript
$Actual | Should -Be $Secret
Get-Content -Path $PSScriptRoot\Transcript.txt | Should -Not -Contain $Secret
}
It 'From base64 cypher' {
$HiddenSecret = [HiddenString]$Secret
if ($IsWindows) {
$Cypher = $HiddenSecret.ToBase64Cypher()
$HiddenSecret = [HiddenString][System.Convert]::FromBase64String($Cypher)
$HiddenSecret.Reveal() | Should -Be $Secret
$HiddenSecret = [HiddenString]::FromBase64Cypher($Cypher)
$HiddenSecret.Reveal() | Should -Be $Secret
}
else {
{ $Cypher = $HiddenSecret.ToBase64Cypher() } | Should -Throw
}
}
it 'Convert to SecureString' {
$HiddenPassword = [HiddenString]$Secret
$Credential = New-Object System.Management.Automation.PSCredential ('UserName', $HiddenPassword)
$Password = ([HiddenString]$Credential.Password).Reveal()
$Password | Should -Be $Secret
}
}
}