-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_FactoryMethod.Tests.ps1
More file actions
256 lines (204 loc) · 9.74 KB
/
05_FactoryMethod.Tests.ps1
File metadata and controls
256 lines (204 loc) · 9.74 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
BeforeAll {
. $PSScriptRoot/05_FactoryMethod.ps1
}
Describe 'CryptoAlgorithmFactory' {
Context 'Create(string) - parameterless overload' {
It 'Returns AesCryptoAlgorithm for AES-256-GCM' {
$result = [CryptoAlgorithmFactory]::Create('AES-256-GCM')
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([AesCryptoAlgorithm])
$result.Name | Should -Be 'AES-256-GCM'
}
It 'Returns AesCryptoAlgorithm for case variations' {
$testCases = @(
@{ Input = 'aes-256-gcm' }
@{ Input = 'AES-256-GCM' }
@{ Input = 'Aes-256-Gcm' }
)
$testCases | ForEach-Object {
$result = [CryptoAlgorithmFactory]::Create($_.Input)
$result | Should -BeOfType ([AesCryptoAlgorithm])
}
}
It 'Generates a random 32-byte key for AES' {
$result = [CryptoAlgorithmFactory]::Create('AES-256-GCM')
$key = $result.GetKey()
$key | Should -Not -BeNullOrEmpty
$key.Length | Should -Be 32
# Verify it's not all zeros (random)
$nonZeroCount = ($key | Where-Object { $_ -ne 0 }).Count
$nonZeroCount | Should -BeGreaterThan 0
}
It 'Throws NotSupportedException for ChaCha20' {
{ [CryptoAlgorithmFactory]::Create('ChaCha20') } |
Should -Throw -ExceptionType ([System.NotSupportedException]) -ExpectedMessage '*ChaCha20*not yet implemented*'
}
It 'Throws NotSupportedException for ChaCha20 case variations' {
$testCases = @('ChaCha20', 'chacha20', 'CHACHA20', 'ChAcHa20')
$testCases | ForEach-Object {
{ [CryptoAlgorithmFactory]::Create($_) } |
Should -Throw -ExceptionType ([System.NotSupportedException])
}
}
It 'Throws ArgumentException for unknown algorithm' {
{ [CryptoAlgorithmFactory]::Create('TripleDES') } |
Should -Throw -ExceptionType ([System.ArgumentException]) -ExpectedMessage '*Unknown algorithm*'
}
It 'Throws ArgumentException for empty string' {
{ [CryptoAlgorithmFactory]::Create('') } |
Should -Throw -ExceptionType ([System.ArgumentException])
}
It 'Throws ArgumentException for invalid algorithm names' {
$testCases = @('RSA', 'DES', 'BlowFish', 'NotAnAlgorithm', 'SHA256')
$testCases | ForEach-Object {
{ [CryptoAlgorithmFactory]::Create($_) } |
Should -Throw -ExceptionType ([System.ArgumentException])
}
}
}
Context 'Create(string, byte[]) - keyed overload' {
It 'Returns AesCryptoAlgorithm with provided key' {
$key = [byte[]]::new(32)
for ($i = 0; $i -lt 32; $i++) { $key[$i] = $i }
$result = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key)
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType ([AesCryptoAlgorithm])
$result.Name | Should -Be 'AES-256-GCM'
}
It 'Sets the key correctly in the returned instance' {
$key = [byte[]]::new(32)
for ($i = 0; $i -lt 32; $i++) { $key[$i] = ($i * 2) % 256 }
$result = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key)
$actualKey = $result.GetKey()
$actualKey.Length | Should -Be $key.Length
for ($i = 0; $i -lt $key.Length; $i++) {
$actualKey[$i] | Should -Be $key[$i]
}
}
It 'Accepts different valid 32-byte keys' {
# All zeros
$key1 = [byte[]]::new(32)
$result1 = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key1)
$result1 | Should -BeOfType ([AesCryptoAlgorithm])
# All 255s
$key2 = [byte[]]::new(32)
for ($i = 0; $i -lt 32; $i++) { $key2[$i] = 255 }
$result2 = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key2)
$result2 | Should -BeOfType ([AesCryptoAlgorithm])
# Random
$key3 = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($key3)
$result3 = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key3)
$result3 | Should -BeOfType ([AesCryptoAlgorithm])
}
It 'Throws NotSupportedException for ChaCha20 with key' {
$key = [byte[]]::new(32)
{ [CryptoAlgorithmFactory]::Create('ChaCha20', $key) } |
Should -Throw -ExceptionType ([System.NotSupportedException]) -ExpectedMessage '*ChaCha20*not yet implemented*'
}
It 'Throws ArgumentException for unknown algorithm with key' {
$key = [byte[]]::new(32)
{ [CryptoAlgorithmFactory]::Create('TripleDES', $key) } |
Should -Throw -ExceptionType ([System.ArgumentException]) -ExpectedMessage '*Key overload not supported*'
}
It 'Handles case insensitivity for keyed overload' {
$key = [byte[]]::new(32)
$testCases = @('aes-256-gcm', 'AES-256-GCM', 'Aes-256-Gcm')
$testCases | ForEach-Object {
$result = [CryptoAlgorithmFactory]::Create($_, $key)
$result | Should -BeOfType ([AesCryptoAlgorithm])
}
}
}
Context 'Factory integration with encryption' {
It 'Created algorithm can encrypt data' {
$algo = [CryptoAlgorithmFactory]::Create('AES-256-GCM')
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('Test data')
$ciphertext = $algo.Encrypt($plaintext)
$ciphertext | Should -Not -BeNullOrEmpty
$ciphertext.Length | Should -BeGreaterThan $plaintext.Length
# AES-GCM adds 12-byte nonce + 16-byte tag = 28 bytes overhead
$ciphertext.Length | Should -Be ($plaintext.Length + 28)
}
It 'Keyed algorithm can encrypt data' {
$key = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($key)
$algo = [CryptoAlgorithmFactory]::Create('AES-256-GCM', $key)
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('Keyed encryption test')
$ciphertext = $algo.Encrypt($plaintext)
$ciphertext | Should -Not -BeNullOrEmpty
$ciphertext.Length | Should -Be ($plaintext.Length + 28)
}
It 'Different instances produce different ciphertexts for same plaintext' {
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('Same data')
$algo1 = [CryptoAlgorithmFactory]::Create('AES-256-GCM')
$algo2 = [CryptoAlgorithmFactory]::Create('AES-256-GCM')
$ct1 = $algo1.Encrypt($plaintext)
$ct2 = $algo2.Encrypt($plaintext)
# Different because different random keys and nonces
$ct1 | Should -Not -Be $ct2
}
}
Context 'Error handling edge cases' {
It 'Handles null algorithm name gracefully' {
# In PS 7.4.6 arm64, null may resolve to empty string rather than throw
$errorOccurred = $false
try {
[CryptoAlgorithmFactory]::Create($null)
}
catch {
$errorOccurred = $true
$_.Exception | Should -BeOfType ([System.ArgumentException])
}
$errorOccurred | Should -BeTrue
}
It 'Throws for whitespace-only algorithm name' {
{ [CryptoAlgorithmFactory]::Create(' ') } |
Should -Throw -ExceptionType ([System.ArgumentException])
}
}
}
Describe 'AesCryptoAlgorithm' {
Context 'Direct instantiation' {
It 'Can be created with a 32-byte key' {
$key = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($key)
$algo = [AesCryptoAlgorithm]::new($key)
$algo | Should -Not -BeNullOrEmpty
$algo.Name | Should -Be 'AES-256-GCM'
}
It 'GetKey returns the provided key' {
$key = [byte[]]::new(32)
for ($i = 0; $i -lt 32; $i++) { $key[$i] = $i }
$algo = [AesCryptoAlgorithm]::new($key)
$retrievedKey = $algo.GetKey()
$retrievedKey.Length | Should -Be 32
for ($i = 0; $i -lt 32; $i++) {
$retrievedKey[$i] | Should -Be $key[$i]
}
}
It 'Encrypts data successfully' {
$key = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($key)
$algo = [AesCryptoAlgorithm]::new($key)
$data = [System.Text.Encoding]::UTF8.GetBytes('Secret message')
$result = $algo.Encrypt($data)
$result | Should -Not -BeNullOrEmpty
$result.Length | Should -Be ($data.Length + 28)
}
}
}
Describe 'CryptoAlgorithm base class' {
Context 'Base class behavior' {
It 'Throws NotImplementedException when Encrypt is called on base class' {
$algo = [CryptoAlgorithm]::new('TestAlgo')
$data = [byte[]]::new(10)
{ $algo.Encrypt($data) } |
Should -Throw -ExceptionType ([System.NotImplementedException])
}
It 'Stores name correctly' {
$algo = [CryptoAlgorithm]::new('CustomName')
$algo.Name | Should -Be 'CustomName'
}
}
}