-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Inheritance.Tests.ps1
More file actions
284 lines (240 loc) · 9.01 KB
/
02_Inheritance.Tests.ps1
File metadata and controls
284 lines (240 loc) · 9.01 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
<#
.SYNOPSIS
Pester tests for 02_Inheritance.ps1
.DESCRIPTION
Tests covering:
- Three-level inheritance chain (CryptoBase -> AesService -> AuditedAesService -> GrandchildCrypto)
- Constructor chaining
- Method override behavior
- Type checking and is-a relationships
- Safe downcasting
#>
BeforeAll {
. $PSScriptRoot/02_Inheritance.ps1
}
Describe 'CryptoBase' {
It 'Cannot be instantiated directly (abstract-like base)' {
$base = [CryptoBase]::new('TEST', 128)
$base.Algorithm | Should -Be 'TEST'
$base.KeyBits | Should -Be 128
$base.CreatedAt | Should -BeOfType [datetime]
}
It 'Throws NotImplementedException on Encrypt if not overridden' {
$base = [CryptoBase]::new('TEST', 128)
{ $base.Encrypt([byte[]]::new(16)) } | Should -Throw -ExceptionType ([System.NotImplementedException])
}
It 'Describe() returns formatted string with type name' {
$base = [CryptoBase]::new('TEST', 128)
$desc = $base.Describe()
$desc | Should -Match 'CryptoBase'
$desc | Should -Match 'TEST'
$desc | Should -Match '128-bit'
}
}
Describe 'AesService (Level 1 inheritance)' {
It 'Instantiates with default constructor' {
$svc = [AesService]::new()
$svc | Should -Not -BeNullOrEmpty
$svc.Algorithm | Should -Be 'AES-256-GCM'
$svc.KeyBits | Should -Be 256
}
It 'Instantiates with 128-bit key' {
$key = [byte[]]::new(16)
$svc = [AesService]::new($key)
$svc.KeyBits | Should -Be 128
}
It 'Instantiates with 192-bit key' {
$key = [byte[]]::new(24)
$svc = [AesService]::new($key)
$svc.KeyBits | Should -Be 192
}
It 'Instantiates with 256-bit key' {
$key = [byte[]]::new(32)
$svc = [AesService]::new($key)
$svc.KeyBits | Should -Be 256
}
It 'Throws ArgumentException for invalid key length' {
$key = [byte[]]::new(15)
{ [AesService]::new($key) } | Should -Throw -ExceptionType ([System.ArgumentException])
}
It 'Implements Encrypt method' {
$svc = [AesService]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test data')
$ciphertext = $svc.Encrypt($plaintext)
$ciphertext | Should -Not -BeNullOrEmpty
$ciphertext.Length | Should -BeGreaterThan $plaintext.Length
}
It 'Is a CryptoBase' {
$svc = [AesService]::new()
$svc -is [CryptoBase] | Should -Be $true
}
It 'Is an AesService' {
$svc = [AesService]::new()
$svc -is [AesService] | Should -Be $true
}
It 'Base constructor was called (CreatedAt is set)' {
$svc = [AesService]::new()
$svc.CreatedAt | Should -Not -BeNullOrEmpty
$svc.CreatedAt | Should -BeOfType [datetime]
}
}
Describe 'AuditedAesService (Level 2 inheritance)' {
It 'Instantiates successfully' {
$svc = [AuditedAesService]::new()
$svc | Should -Not -BeNullOrEmpty
}
It 'Initializes AuditLog' {
$svc = [AuditedAesService]::new()
$null -eq $svc.AuditLog | Should -Be $false
$svc.AuditLog.Count | Should -Be 0
}
It 'Logs encryption operations' {
$svc = [AuditedAesService]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$svc.Encrypt($plaintext)
$svc.AuditLog.Count | Should -Be 1
$svc.AuditLog[0] | Should -Match 'ENCRYPT'
$svc.AuditLog[0] | Should -Match '4 bytes'
}
It 'Calls parent Encrypt implementation' {
$svc = [AuditedAesService]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$ciphertext = $svc.Encrypt($plaintext)
$ciphertext | Should -Not -BeNullOrEmpty
$ciphertext.Length | Should -BeGreaterThan $plaintext.Length
}
It 'Is a CryptoBase' {
$svc = [AuditedAesService]::new()
$svc -is [CryptoBase] | Should -Be $true
}
It 'Is an AesService' {
$svc = [AuditedAesService]::new()
$svc -is [AesService] | Should -Be $true
}
It 'Is an AuditedAesService' {
$svc = [AuditedAesService]::new()
$svc -is [AuditedAesService] | Should -Be $true
}
It 'Base constructor chain was called' {
$svc = [AuditedAesService]::new()
$svc.Algorithm | Should -Be 'AES-256-GCM'
$svc.KeyBits | Should -Be 256
$svc.CreatedAt | Should -Not -BeNullOrEmpty
}
}
Describe 'GrandchildCrypto (Level 3 inheritance)' {
It 'Instantiates with default constructor' {
$gc = [GrandchildCrypto]::new()
$gc | Should -Not -BeNullOrEmpty
$gc.Purpose | Should -Be 'Deep inheritance demonstration'
}
It 'Instantiates with purpose parameter' {
$gc = [GrandchildCrypto]::new('Custom purpose')
$gc.Purpose | Should -Be 'Custom purpose'
}
It 'Three-level chain loads without error' {
{ [GrandchildCrypto]::new() } | Should -Not -Throw
}
It 'Is a CryptoBase' {
$gc = [GrandchildCrypto]::new()
$gc -is [CryptoBase] | Should -Be $true
}
It 'Is an AesService' {
$gc = [GrandchildCrypto]::new()
$gc -is [AesService] | Should -Be $true
}
It 'Is an AuditedAesService' {
$gc = [GrandchildCrypto]::new()
$gc -is [AuditedAesService] | Should -Be $true
}
It 'Is a GrandchildCrypto' {
$gc = [GrandchildCrypto]::new()
$gc -is [GrandchildCrypto] | Should -Be $true
}
It 'Base constructor was called (all properties initialized)' {
$gc = [GrandchildCrypto]::new()
$gc.Algorithm | Should -Be 'AES-256-GCM'
$gc.KeyBits | Should -Be 256
$gc.CreatedAt | Should -Not -BeNullOrEmpty
$null -eq $gc.AuditLog | Should -Be $false
}
It 'Override Describe() fires and calls AesService.Describe()' {
$gc = [GrandchildCrypto]::new('Test purpose')
$desc = $gc.Describe()
# Should contain elements from AesService.Describe()
$desc | Should -Match 'GrandchildCrypto'
$desc | Should -Match 'AES-256-GCM'
$desc | Should -Match '256-bit'
# Should also contain Purpose from override
$desc | Should -Match 'Purpose: Test purpose'
}
It 'Safe downcast to AesService works' {
$gc = [GrandchildCrypto]::new()
$asAes = [AesService]$gc
$asAes | Should -Not -BeNullOrEmpty
$asAes.Algorithm | Should -Be 'AES-256-GCM'
}
It 'Safe downcast to AuditedAesService works' {
$gc = [GrandchildCrypto]::new()
$asAudited = [AuditedAesService]$gc
$asAudited | Should -Not -BeNullOrEmpty
$null -eq $asAudited.AuditLog | Should -Be $false
}
It 'Safe downcast to CryptoBase works' {
$gc = [GrandchildCrypto]::new()
$asBase = [CryptoBase]$gc
$asBase | Should -Not -BeNullOrEmpty
$asBase.Algorithm | Should -Be 'AES-256-GCM'
}
It 'Inherits Encrypt with audit logging' {
$gc = [GrandchildCrypto]::new()
$plaintext = [System.Text.Encoding]::UTF8.GetBytes('test')
$ciphertext = $gc.Encrypt($plaintext)
$ciphertext | Should -Not -BeNullOrEmpty
$gc.AuditLog.Count | Should -Be 1
}
It 'GetType() returns GrandchildCrypto' {
$gc = [GrandchildCrypto]::new()
$gc.GetType().Name | Should -Be 'GrandchildCrypto'
}
It 'BaseType chain is correct' {
$gc = [GrandchildCrypto]::new()
$gc.GetType().BaseType.Name | Should -Be 'AuditedAesService'
$gc.GetType().BaseType.BaseType.Name | Should -Be 'AesService'
$gc.GetType().BaseType.BaseType.BaseType.Name | Should -Be 'CryptoBase'
}
}
Describe 'Type hierarchy validation' {
It 'All levels maintain is-a relationship' {
$gc = [GrandchildCrypto]::new()
# Direct type
$gc -is [GrandchildCrypto] | Should -Be $true
# All ancestor types
$gc -is [AuditedAesService] | Should -Be $true
$gc -is [AesService] | Should -Be $true
$gc -is [CryptoBase] | Should -Be $true
}
It 'Type checking works at each level' {
$base = [CryptoBase]::new('TEST', 128)
$aes = [AesService]::new()
$audited = [AuditedAesService]::new()
$gc = [GrandchildCrypto]::new()
# Base is only base
$base -is [CryptoBase] | Should -Be $true
$base -is [AesService] | Should -Be $false
# AesService is base and AesService
$aes -is [CryptoBase] | Should -Be $true
$aes -is [AesService] | Should -Be $true
$aes -is [AuditedAesService] | Should -Be $false
# AuditedAesService is all three
$audited -is [CryptoBase] | Should -Be $true
$audited -is [AesService] | Should -Be $true
$audited -is [AuditedAesService] | Should -Be $true
$audited -is [GrandchildCrypto] | Should -Be $false
# GrandchildCrypto is all four
$gc -is [CryptoBase] | Should -Be $true
$gc -is [AesService] | Should -Be $true
$gc -is [AuditedAesService] | Should -Be $true
$gc -is [GrandchildCrypto] | Should -Be $true
}
}