-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_TemplateMethod.Tests.ps1
More file actions
306 lines (240 loc) · 11.8 KB
/
13_TemplateMethod.Tests.ps1
File metadata and controls
306 lines (240 loc) · 11.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
Describe 'Template Method Pattern - CryptoWorkflow' {
Context 'Base class CryptoWorkflow' {
It 'Should enforce step sequence order' {
# Create a test subclass that tracks order
$testClass = @'
class TestWorkflow : CryptoWorkflow {
hidden [byte[]] Encrypt([byte[]]$data) { return $data }
hidden [byte[]] Sign([byte[]]$data) { return $data }
}
'@
Invoke-Expression $testClass
$workflow = [TestWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test data')
$result = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
$log | Should -HaveCount 6
$log[0] | Should -Be 'Validate'
$log[1] | Should -Be 'Compress'
$log[2] | Should -Be 'Encrypt'
$log[3] | Should -Be 'Sign'
$log[4] | Should -Be 'Package'
$log[5] | Should -Be 'Audit'
}
It 'Should reject empty payload in Validate step' {
$testClass = @'
class TestWorkflow2 : CryptoWorkflow {
hidden [byte[]] Encrypt([byte[]]$data) { return $data }
hidden [byte[]] Sign([byte[]]$data) { return $data }
}
'@
Invoke-Expression $testClass
$workflow = [TestWorkflow2]::new()
$emptyData = [byte[]]::new(0)
{ $workflow.Execute($emptyData) } | Should -Throw '*Empty payload*'
}
It 'Should return hashtable with required keys from Package' {
$testClass = @'
class TestWorkflow3 : CryptoWorkflow {
hidden [byte[]] Encrypt([byte[]]$data) { return $data }
hidden [byte[]] Sign([byte[]]$data) { return $data }
}
'@
Invoke-Expression $testClass
$workflow = [TestWorkflow3]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$result = $workflow.Execute($plaintext)
$result | Should -BeOfType [hashtable]
$result.ContainsKey('Payload') | Should -Be $true
$result.ContainsKey('Timestamp') | Should -Be $true
$result.ContainsKey('Version') | Should -Be $true
$result.Version | Should -Be 1
}
It 'Should throw NotImplementedException for abstract Encrypt' {
$testClass = @'
class IncompleteWorkflow : CryptoWorkflow {
hidden [byte[]] Sign([byte[]]$data) { return $data }
}
'@
Invoke-Expression $testClass
$workflow = [IncompleteWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
{ $workflow.Execute($plaintext) } | Should -Throw '*Encrypt*'
}
It 'Should throw NotImplementedException for abstract Sign' {
$testClass = @'
class IncompleteWorkflow2 : CryptoWorkflow {
hidden [byte[]] Encrypt([byte[]]$data) { return $data }
}
'@
Invoke-Expression $testClass
$workflow = [IncompleteWorkflow2]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
{ $workflow.Execute($plaintext) } | Should -Throw '*Sign*'
}
}
Context 'GzipAesWorkflow implementation' {
It 'Should execute complete workflow without errors' {
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('Hello, Template Method!')
$result = $workflow.Execute($plaintext)
$result | Should -Not -BeNullOrEmpty
$result.Payload | Should -Not -BeNullOrEmpty
$result.Payload.Length | Should -BeGreaterThan 0
}
It 'Should record all steps in correct order' {
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test data')
$null = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
$log | Should -Be @('Validate', 'Compress', 'Encrypt', 'Sign', 'Package', 'Audit')
}
It 'Should compress data (Gzip produces different length)' {
$workflow = [GzipAesWorkflow]::new()
# Use compressible data
$plaintext = [System.Text.Encoding]::UTF8.GetBytes(('A' * 1000))
$result = $workflow.Execute($plaintext)
# After compression, encryption, signing, the length should differ
$result.Payload.Length | Should -Not -Be $plaintext.Length
}
It 'Should encrypt with AES-GCM (nonce + tag + ciphertext)' {
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$result = $workflow.Execute($plaintext)
# Should have HMAC (32) + nonce (12) + tag (16) + ciphertext
$result.Payload.Length | Should -BeGreaterThan 60
}
It 'Should sign with HMAC (32 bytes prepended)' {
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$result = $workflow.Execute($plaintext)
# HMAC adds 32 bytes at the front
$result.Payload.Length | Should -BeGreaterThan 32
}
It 'Should use unique key per instance' {
$workflow1 = [GzipAesWorkflow]::new()
$workflow2 = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('same data')
$result1 = $workflow1.Execute($plaintext)
$result2 = $workflow2.Execute($plaintext)
# Different keys should produce different outputs
[System.BitConverter]::ToString($result1.Payload) | Should -Not -Be ([System.BitConverter]::ToString($result2.Payload))
}
}
Context 'ZstdAesWorkflow implementation' {
It 'Should execute complete workflow without errors' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('Hello, ZLib compression!')
$result = $workflow.Execute($plaintext)
$result | Should -Not -BeNullOrEmpty
$result.Payload | Should -Not -BeNullOrEmpty
$result.Payload.Length | Should -BeGreaterThan 0
}
It 'Should record all steps in correct order' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test data')
$null = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
$log | Should -Be @('Validate', 'Compress', 'Encrypt', 'Sign', 'Package', 'Audit')
}
It 'Should compress data with ZLib' {
$workflow = [ZstdAesWorkflow]::new()
# Use compressible data
$plaintext = [System.Text.Encoding]::UTF8.GetBytes(('B' * 1000))
$result = $workflow.Execute($plaintext)
# After compression, encryption, signing, the length should differ
$result.Payload.Length | Should -Not -Be $plaintext.Length
}
It 'Should encrypt with AES-GCM' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('zlib test')
$result = $workflow.Execute($plaintext)
# Should have HMAC (32) + nonce (12) + tag (16) + ciphertext
$result.Payload.Length | Should -BeGreaterThan 60
}
It 'Should sign with HMAC' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$result = $workflow.Execute($plaintext)
# HMAC adds 32 bytes at the front
$result.Payload.Length | Should -BeGreaterThan 32
}
It 'Should use unique key per instance' {
$workflow1 = [ZstdAesWorkflow]::new()
$workflow2 = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('same data')
$result1 = $workflow1.Execute($plaintext)
$result2 = $workflow2.Execute($plaintext)
# Different keys should produce different outputs
[System.BitConverter]::ToString($result1.Payload) | Should -Not -Be ([System.BitConverter]::ToString($result2.Payload))
}
It 'Should produce different output than GzipAesWorkflow for same input' {
$gzipWorkflow = [GzipAesWorkflow]::new()
$zlibWorkflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('compare compression')
$gzipResult = $gzipWorkflow.Execute($plaintext)
$zlibResult = $zlibWorkflow.Execute($plaintext)
# Different compression algorithms should yield different results
[System.BitConverter]::ToString($gzipResult.Payload) | Should -Not -Be ([System.BitConverter]::ToString($zlibResult.Payload))
}
}
Context 'Template Method invariance' {
It 'Should prevent subclass from reordering steps' {
# The Execute method is sealed by design - subclasses cannot override it
# They can only override the individual step methods
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('invariance test')
$result = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
# Verify exact order every time
$log[0] | Should -Be 'Validate'
$log[1] | Should -Be 'Compress'
$log[2] | Should -Be 'Encrypt'
$log[3] | Should -Be 'Sign'
$log[4] | Should -Be 'Package'
$log[5] | Should -Be 'Audit'
}
It 'Should execute steps sequentially on multiple calls' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('first')
$null = $workflow.Execute($plaintext)
$null = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
# Should have two complete sequences
$log | Should -HaveCount 12
# First sequence
$log[0..5] | Should -Be @('Validate', 'Compress', 'Encrypt', 'Sign', 'Package', 'Audit')
# Second sequence
$log[6..11] | Should -Be @('Validate', 'Compress', 'Encrypt', 'Sign', 'Package', 'Audit')
}
}
Context 'Step log functionality' {
It 'Should return empty log before execution' {
$workflow = [GzipAesWorkflow]::new()
$log = $workflow.GetStepLog()
$log | Should -HaveCount 0
}
It 'Should maintain log across multiple executions' {
$workflow = [ZstdAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$null = $workflow.Execute($plaintext)
$null = $workflow.Execute($plaintext)
$null = $workflow.Execute($plaintext)
$log = $workflow.GetStepLog()
$log | Should -HaveCount 18 # 6 steps × 3 executions
}
It 'Should return array copy, not reference' {
$workflow = [GzipAesWorkflow]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$null = $workflow.Execute($plaintext)
$log1 = $workflow.GetStepLog()
$null = $workflow.Execute($plaintext)
$log2 = $workflow.GetStepLog()
$log1 | Should -HaveCount 6
$log2 | Should -HaveCount 12
}
}
}