-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16_NullObject.Tests.ps1
More file actions
185 lines (144 loc) · 5.91 KB
/
Copy path16_NullObject.Tests.ps1
File metadata and controls
185 lines (144 loc) · 5.91 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
BeforeAll {
. $PSScriptRoot/16_NullObject.ps1
}
Describe 'NullAuditLogger' {
It 'Never throws on LogEncrypt' {
$logger = [NullAuditLogger]::new()
{ $logger.LogEncrypt([byte[]]@(1,2,3)) } | Should -Not -Throw
}
It 'Never throws on LogDecrypt' {
$logger = [NullAuditLogger]::new()
{ $logger.LogDecrypt(100) } | Should -Not -Throw
}
It 'Never throws on LogFailure' {
$logger = [NullAuditLogger]::new()
{ $logger.LogFailure('encrypt', 'test error') } | Should -Not -Throw
}
It 'Handles null data without throwing' {
$logger = [NullAuditLogger]::new()
{ $logger.LogEncrypt($null) } | Should -Not -Throw
}
It 'Handles zero bytes without throwing' {
$logger = [NullAuditLogger]::new()
{ $logger.LogDecrypt(0) } | Should -Not -Throw
}
It 'Handles empty strings without throwing' {
$logger = [NullAuditLogger]::new()
{ $logger.LogFailure('', '') } | Should -Not -Throw
}
}
Describe 'FileAuditLogger' {
BeforeEach {
$script:testFile = Join-Path $TestDrive "audit.log"
}
It 'Logs encrypt operations' {
$logger = [FileAuditLogger]::new($script:testFile)
$logger.LogEncrypt([byte[]]@(1,2,3,4,5))
$content = Get-Content $script:testFile -Raw
$content | Should -Match 'ENCRYPT\|.*\|5b'
}
It 'Logs decrypt operations' {
$logger = [FileAuditLogger]::new($script:testFile)
$logger.LogDecrypt(128)
$content = Get-Content $script:testFile -Raw
$content | Should -Match 'DECRYPT\|.*\|128b'
}
It 'Logs failure operations' {
$logger = [FileAuditLogger]::new($script:testFile)
$logger.LogFailure('encrypt', 'key not found')
$content = Get-Content $script:testFile -Raw
$content | Should -Match 'FAIL\|.*\|encrypt\|key not found'
}
}
Describe 'BufferingAuditLogger' {
BeforeEach {
$script:testFile = Join-Path $TestDrive "buffered-$(Get-Random).log"
}
It 'Accumulates log entries in memory' {
$logger = [BufferingAuditLogger]::new()
$logger.LogEncrypt([byte[]]@(1,2,3))
$logger.LogDecrypt(64)
$logger.LogFailure('test', 'error')
# Should not have written to disk yet
Test-Path $script:testFile | Should -Be $false
}
It 'Writes all entries on Flush' {
$logger = [BufferingAuditLogger]::new()
$logger.LogEncrypt([byte[]]@(1,2,3))
$logger.LogDecrypt(64)
$logger.LogFailure('test', 'error')
$logger.Flush($script:testFile)
$content = Get-Content $script:testFile
$content.Count | Should -Be 3
$content[0] | Should -Match 'ENCRYPT\|.*\|3b'
$content[1] | Should -Match 'DECRYPT\|.*\|64b'
$content[2] | Should -Match 'FAIL\|.*\|test\|error'
}
It 'Clears buffer after Flush' {
$logger = [BufferingAuditLogger]::new()
$logger.LogEncrypt([byte[]]@(1,2,3))
$logger.Flush($script:testFile)
$firstContent = @(Get-Content $script:testFile | Where-Object { $_ -ne '' })
$firstContent.Count | Should -Be 1
# Log again and flush to same file
Remove-Item $script:testFile
$logger.LogDecrypt(100)
$logger.Flush($script:testFile)
$secondContent = @(Get-Content $script:testFile | Where-Object { $_ -ne '' })
$secondContent.Count | Should -Be 1
$secondContent[0] | Should -Match 'DECRYPT'
}
It 'Handles empty buffer flush' {
$logger = [BufferingAuditLogger]::new()
{ $logger.Flush($script:testFile) } | Should -Not -Throw
}
}
Describe 'CryptoServiceWithAudit' {
BeforeEach {
$script:testFile = Join-Path $TestDrive "crypto.log"
}
It 'Works with NullAuditLogger by default' {
$service = [CryptoServiceWithAudit]::new()
$data = [byte[]]@(1,2,3,4,5)
{ $result = $service.Encrypt($data) } | Should -Not -Throw
}
It 'Works with FileAuditLogger' {
$logger = [FileAuditLogger]::new($script:testFile)
$service = [CryptoServiceWithAudit]::new($logger)
$data = [byte[]]@(1,2,3,4,5)
$result = $service.Encrypt($data)
$result | Should -Not -BeNullOrEmpty
$content = Get-Content $script:testFile -Raw
$content | Should -Match 'ENCRYPT\|.*\|5b'
}
It 'Works with BufferingAuditLogger' {
$logger = [BufferingAuditLogger]::new()
$service = [CryptoServiceWithAudit]::new($logger)
$data = [byte[]]@(1,2,3,4,5)
$result = $service.Encrypt($data)
$result | Should -Not -BeNullOrEmpty
# Verify buffer has entry
$logger.Flush($script:testFile)
$content = Get-Content $script:testFile -Raw
$content | Should -Match 'ENCRYPT\|.*\|5b'
}
It 'Encrypts data correctly' {
$service = [CryptoServiceWithAudit]::new()
$data = [byte[]]@(72,101,108,108,111) # "Hello"
$encrypted = $service.Encrypt($data)
# AES-GCM output: 12 byte nonce + 16 byte tag + ciphertext
$encrypted.Length | Should -Be (12 + 16 + $data.Length)
}
It 'No null reference exceptions with any logger' {
$nullLogger = [NullAuditLogger]::new()
$fileLogger = [FileAuditLogger]::new($script:testFile)
$bufferLogger = [BufferingAuditLogger]::new()
$service1 = [CryptoServiceWithAudit]::new($nullLogger)
$service2 = [CryptoServiceWithAudit]::new($fileLogger)
$service3 = [CryptoServiceWithAudit]::new($bufferLogger)
$data = [byte[]]@(1,2,3)
{ $service1.Encrypt($data) } | Should -Not -Throw
{ $service2.Encrypt($data) } | Should -Not -Throw
{ $service3.Encrypt($data) } | Should -Not -Throw
}
}