-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_GenericCollections.Tests.ps1
More file actions
197 lines (150 loc) · 6.71 KB
/
12_GenericCollections.Tests.ps1
File metadata and controls
197 lines (150 loc) · 6.71 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
BeforeAll {
. $PSScriptRoot/12_GenericCollections.ps1
}
Describe 'TypedKeyStore' {
Context 'Basic Operations' {
It 'Should store and retrieve byte array keys' {
$store = [TypedKeyStore]::new()
$key = [byte[]](1, 2, 3, 4)
$store.Set('testkey', $key)
$retrieved = $store.Get('testkey')
$retrieved | Should -Not -BeNullOrEmpty
$retrieved.Length | Should -Be 4
$retrieved[0] | Should -Be 1
}
It 'Should throw on Get with missing key' {
$store = [TypedKeyStore]::new()
{ $store.Get('nonexistent') } | Should -Throw -ExpectedMessage "*not found"
}
It 'Should return key names' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
$store.Set('key2', [byte[]](3,4))
$names = $store.GetNames()
$names.Count | Should -Be 2
$names | Should -Contain 'key1'
$names | Should -Contain 'key2'
}
}
Context 'TryGet Method' {
It 'Should return false on missing key' {
$store = [TypedKeyStore]::new()
$outKey = $null
$result = $store.TryGet('nonexistent', [ref]$outKey)
$result | Should -Be $false
# Note: PS 7.4.6 arm64 resolves null to byte[], so checking value assertion
if ($null -ne $outKey -and $outKey -is [byte[]]) {
$outKey.Length | Should -Be 0
}
}
It 'Should return true and populate output on existing key' {
$store = [TypedKeyStore]::new()
$key = [byte[]](10, 20, 30)
$store.Set('exists', $key)
$outKey = $null
$result = $store.TryGet('exists', [ref]$outKey)
$result | Should -Be $true
$outKey | Should -Not -BeNullOrEmpty
$outKey.Length | Should -Be 3
$outKey[0] | Should -Be 10
}
It 'Should not throw on missing key' {
$store = [TypedKeyStore]::new()
$outKey = $null
{ $store.TryGet('nonexistent', [ref]$outKey) } | Should -Not -Throw
}
}
Context 'GetExpiredKeys Method' {
It 'Should return empty array when no keys exist' {
$store = [TypedKeyStore]::new()
$expired = @($store.GetExpiredKeys(30))
$expired.Count | Should -Be 0
}
It 'Should return empty array when all keys are recent' {
$store = [TypedKeyStore]::new()
$store.Set('recent1', [byte[]](1,2))
$store.Set('recent2', [byte[]](3,4))
$expired = $store.GetExpiredKeys(1)
$expired.Count | Should -Be 0
}
It 'Should return keys not accessed within specified days' {
$store = [TypedKeyStore]::new()
# Set keys
$store.Set('key1', [byte[]](1,2))
$store.Set('key2', [byte[]](3,4))
$store.Set('key3', [byte[]](5,6))
# Manually update last-accessed to simulate old keys
$store._lastAccessed['key1'] = [datetime]::UtcNow.AddDays(-40)
$store._lastAccessed['key2'] = [datetime]::UtcNow.AddDays(-20)
# key3 stays recent
$expired = $store.GetExpiredKeys(30)
$expired.Count | Should -Be 1
$expired | Should -Contain 'key1'
$expired | Should -Not -Contain 'key2'
$expired | Should -Not -Contain 'key3'
}
It 'Should return correct keys with different thresholds' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
$store.Set('key2', [byte[]](3,4))
$store.Set('key3', [byte[]](5,6))
$store._lastAccessed['key1'] = [datetime]::UtcNow.AddDays(-100)
$store._lastAccessed['key2'] = [datetime]::UtcNow.AddDays(-50)
$store._lastAccessed['key3'] = [datetime]::UtcNow.AddDays(-25)
# 30 days threshold
$expired30 = $store.GetExpiredKeys(30)
$expired30.Count | Should -Be 2
$expired30 | Should -Contain 'key1'
$expired30 | Should -Contain 'key2'
# 60 days threshold
$expired60 = $store.GetExpiredKeys(60)
$expired60.Count | Should -Be 1
$expired60 | Should -Contain 'key1'
}
}
Context 'Last Accessed Timestamp Tracking' {
It 'Should update last accessed on Get' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
# Set old timestamp
$store._lastAccessed['key1'] = [datetime]::UtcNow.AddDays(-50)
# Get should update timestamp
$null = $store.Get('key1')
$expired = $store.GetExpiredKeys(30)
$expired | Should -Not -Contain 'key1'
}
It 'Should update last accessed on successful TryGet' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
# Set old timestamp
$store._lastAccessed['key1'] = [datetime]::UtcNow.AddDays(-50)
# TryGet should update timestamp
$outKey = $null
$null = $store.TryGet('key1', [ref]$outKey)
$expired = $store.GetExpiredKeys(30)
$expired | Should -Not -Contain 'key1'
}
It 'Should not update last accessed on failed TryGet' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
# Set old timestamp for existing key
$store._lastAccessed['key1'] = [datetime]::UtcNow.AddDays(-50)
# TryGet on different key should not affect key1
$outKey = $null
$null = $store.TryGet('nonexistent', [ref]$outKey)
$expired = $store.GetExpiredKeys(30)
$expired | Should -Contain 'key1'
}
}
Context 'Audit Log' {
It 'Should log Set operations' {
$store = [TypedKeyStore]::new()
$store.Set('key1', [byte[]](1,2))
$store.Set('key2', [byte[]](3,4))
$log = $store.GetAuditLog()
$log.Count | Should -BeGreaterOrEqual 2
$log[0] | Should -Match 'SET key1'
$log[1] | Should -Match 'SET key2'
}
}
}