-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_ObjectPool.Tests.ps1
More file actions
334 lines (256 loc) · 10.2 KB
/
Copy path08_ObjectPool.Tests.ps1
File metadata and controls
334 lines (256 loc) · 10.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
BeforeAll {
. $PSScriptRoot/08_ObjectPool.ps1
}
Describe 'RsaPool' {
Context 'Construction and Warmup' {
It 'Pre-warms pool with correct number of instances' {
$pool = [RsaPool]::new(2048, 3)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 3
$stats.TotalCreated | Should -Be 3
$stats.TotalRented | Should -Be 0
$stats.TotalReturned | Should -Be 0
$stats.MaxSize | Should -Be 3
$pool.Drain()
}
It 'Creates RSA instances with correct key size' {
$pool = [RsaPool]::new(2048, 2)
$rsa = $pool.Rent()
try {
$rsa.KeySize | Should -Be 2048
} finally {
$pool.Return($rsa)
$pool.Drain()
}
}
}
Context 'Rent and Return Cycle' {
It 'Rents an RSA instance from pool' {
$pool = [RsaPool]::new(2048, 2)
$rsa = $pool.Rent()
$rsa | Should -Not -BeNullOrEmpty
$rsa | Should -BeOfType [System.Security.Cryptography.RSA]
$stats = $pool.Stats()
$stats.TotalRented | Should -Be 1
$stats.PoolDepth | Should -Be 1
$pool.Return($rsa)
$pool.Drain()
}
It 'Returns an RSA instance to pool' {
$pool = [RsaPool]::new(2048, 2)
$rsa = $pool.Rent()
$pool.Return($rsa)
$stats = $pool.Stats()
$stats.TotalReturned | Should -Be 1
$stats.PoolDepth | Should -Be 2
$pool.Drain()
}
It 'Reuses returned instances' {
$pool = [RsaPool]::new(2048, 1)
$rsa1 = $pool.Rent()
$pool.Return($rsa1)
$rsa2 = $pool.Rent()
# Should have reused the same instance
$stats = $pool.Stats()
$stats.TotalCreated | Should -Be 1
$stats.TotalRented | Should -Be 2
$pool.Return($rsa2)
$pool.Drain()
}
}
Context 'Pool Exhaustion' {
It 'Creates on-demand when pool is exhausted' {
$pool = [RsaPool]::new(2048, 2)
$rsa1 = $pool.Rent()
$rsa2 = $pool.Rent()
# Pool should be empty now
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 0
# Rent a third - should create on demand (and emit warning)
$rsa3 = $pool.Rent() 3> $null
$stats = $pool.Stats()
$stats.TotalCreated | Should -Be 3
$stats.PoolDepth | Should -Be 0
$pool.Return($rsa1)
$pool.Return($rsa2)
$pool.Return($rsa3)
$pool.Drain()
}
It 'Disposes excess instances on return' {
$pool = [RsaPool]::new(2048, 2)
# Rent all from pool
$rsa1 = $pool.Rent()
$rsa2 = $pool.Rent()
# Create on-demand (3rd instance)
$rsa3 = $pool.Rent()
# Return all three
$pool.Return($rsa1)
$pool.Return($rsa2)
# Pool is now full (2 instances)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 2
# Returning 3rd should dispose it (not add to pool)
$pool.Return($rsa3)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 2 # Still only 2 in pool
$stats.TotalReturned | Should -Be 3
$pool.Drain()
}
}
Context 'Rent All and Verify Empty Pool' {
It 'Drains pool completely when all instances are rented' {
$pool = [RsaPool]::new(2048, 5)
# Initial state
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 5
# Rent all 5
$instances = @()
for ($i = 0; $i -lt 5; $i++) {
$instances += $pool.Rent()
}
# Verify pool is empty
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 0
$stats.TotalRented | Should -Be 5
$stats.TotalCreated | Should -Be 5
# Return all
foreach ($rsa in $instances) {
$pool.Return($rsa)
}
# Verify pool is refilled
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 5
$stats.TotalReturned | Should -Be 5
$pool.Drain()
}
}
Context 'Drain' {
It 'Disposes all pooled instances' {
$pool = [RsaPool]::new(2048, 3)
$pool.Drain()
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 0
$stats.TotalCreated | Should -Be 3 # Still shows created count
}
It 'Works with partially rented pool' {
$pool = [RsaPool]::new(2048, 3)
# Rent one
$rsa = $pool.Rent()
# Drain remaining 2
$pool.Drain()
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 0
# Return the one we rented
$pool.Return($rsa)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 1
$pool.Drain()
}
}
Context 'Stats Method' {
It 'Returns correct statistics' {
$pool = [RsaPool]::new(2048, 3)
$stats = $pool.Stats()
$stats | Should -BeOfType [hashtable]
$stats.Keys | Should -Contain 'PoolDepth'
$stats.Keys | Should -Contain 'TotalCreated'
$stats.Keys | Should -Contain 'TotalRented'
$stats.Keys | Should -Contain 'TotalReturned'
$stats.Keys | Should -Contain 'MaxSize'
$pool.Drain()
}
It 'Tracks rent/return operations accurately' {
$pool = [RsaPool]::new(2048, 2)
$rsa1 = $pool.Rent()
$rsa2 = $pool.Rent()
$rsa3 = $pool.Rent() # On-demand
$pool.Return($rsa1)
$pool.Return($rsa2)
$stats = $pool.Stats()
$stats.TotalCreated | Should -Be 3
$stats.TotalRented | Should -Be 3
$stats.TotalReturned | Should -Be 2
$stats.PoolDepth | Should -Be 2
$pool.Return($rsa3)
$pool.Drain()
}
}
Context 'Real Crypto Operations' {
It 'Can sign data with rented RSA instance' {
$pool = [RsaPool]::new(2048, 1)
$rsa = $pool.Rent()
try {
$data = [byte[]]@(1, 2, 3, 4, 5)
$signature = $rsa.SignData(
$data,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss
)
$signature | Should -Not -BeNullOrEmpty
$signature.Length | Should -BeGreaterThan 0
# Verify the signature
$verified = $rsa.VerifyData(
$data,
$signature,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss
)
$verified | Should -Be $true
} finally {
$pool.Return($rsa)
$pool.Drain()
}
}
It 'Multiple rentals can perform independent operations' {
$pool = [RsaPool]::new(2048, 2)
$rsa1 = $pool.Rent()
$rsa2 = $pool.Rent()
try {
$data1 = [byte[]]@(10, 20, 30)
$data2 = [byte[]]@(40, 50, 60)
$sig1 = $rsa1.SignData(
$data1,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss
)
$sig2 = $rsa2.SignData(
$data2,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss
)
# Signatures should be different
$sig1 | Should -Not -Be $sig2
# Each can verify its own data
$rsa1.VerifyData($data1, $sig1,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss) | Should -Be $true
$rsa2.VerifyData($data2, $sig2,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss) | Should -Be $true
} finally {
$pool.Return($rsa1)
$pool.Return($rsa2)
$pool.Drain()
}
}
}
Context 'Edge Cases' {
It 'Handles single-instance pool correctly' {
$pool = [RsaPool]::new(2048, 1)
$rsa = $pool.Rent()
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 0
$pool.Return($rsa)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 1
$pool.Drain()
}
It 'Handles large pool size' {
$pool = [RsaPool]::new(2048, 10)
$stats = $pool.Stats()
$stats.PoolDepth | Should -Be 10
$stats.TotalCreated | Should -Be 10
$pool.Drain()
}
}
}