-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_OperatorOverloading.Tests.ps1
More file actions
203 lines (151 loc) · 6.95 KB
/
Copy path21_OperatorOverloading.Tests.ps1
File metadata and controls
203 lines (151 loc) · 6.95 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
BeforeAll {
. "$PSScriptRoot/21_OperatorOverloading.ps1"
}
Describe "CryptoKeyPair Operator Overloading" {
Context "IEquatable Implementation" {
It "Should return true when KeyIds match" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-001", 128, 15)
$k1 -eq $k2 | Should -Be $true
}
It "Should return false when KeyIds differ" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 256, 30)
$k1 -eq $k2 | Should -Be $false
}
It "Should return false when comparing to null" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k1 -eq $null | Should -Be $false
}
It "Should return false when comparing to non-CryptoKeyPair object" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k1.Equals("key-001") | Should -Be $false
}
}
Context "IComparable Implementation" {
It "Should sort by expiry date using Sort-Object" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 10)
$k2 = [CryptoKeyPair]::new("key-002", 128, 20)
$k3 = [CryptoKeyPair]::new("key-003", 512, 5)
$sorted = @($k1, $k2, $k3) | Sort-Object
$sorted[0].KeyId | Should -Be "key-003"
$sorted[1].KeyId | Should -Be "key-001"
$sorted[2].KeyId | Should -Be "key-002"
}
It "Should return 1 when comparing to null" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k1.CompareTo($null) | Should -Be 1
}
It "Should compare expiry dates correctly" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 10)
$k2 = [CryptoKeyPair]::new("key-002", 128, 20)
$k1.CompareTo($k2) | Should -BeLessThan 0
$k2.CompareTo($k1) | Should -BeGreaterThan 0
}
}
Context "GetHashCode for Hashtable Keying" {
It "Should deduplicate identical KeyIds in hashtable" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-001", 128, 15)
$k3 = [CryptoKeyPair]::new("key-002", 512, 20)
$hash = @{}
$hash[$k1] = "first"
$hash[$k2] = "second"
$hash[$k3] = "third"
$hash.Count | Should -Be 2
$hash[$k1] | Should -Be "second"
}
It "Should generate consistent hash codes for same KeyId" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-001", 128, 15)
$k1.GetHashCode() | Should -Be $k2.GetHashCode()
}
It "Should allow different KeyIds as separate hashtable keys" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 128, 15)
$hash = @{}
$hash[$k1] = "value1"
$hash[$k2] = "value2"
$hash.Count | Should -Be 2
$hash[$k1] | Should -Be "value1"
$hash[$k2] | Should -Be "value2"
}
}
Context "Static Helper Methods" {
It "IsStrongerThan should compare strength correctly" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 128, 30)
[CryptoKeyPair]::IsStrongerThan($k1, $k2) | Should -Be $true
[CryptoKeyPair]::IsStrongerThan($k2, $k1) | Should -Be $false
}
It "SelectStronger should return stronger key" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 128, 30)
$result = [CryptoKeyPair]::SelectStronger($k1, $k2)
$result.KeyId | Should -Be "key-001"
$result.Strength | Should -Be 256
}
It "SelectStronger should return first when strengths are equal" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 256, 30)
$result = [CryptoKeyPair]::SelectStronger($k1, $k2)
$result.KeyId | Should -Be "key-001"
}
}
Context "IsExpired Method" {
It "Should return false for non-expired key" {
$k = [CryptoKeyPair]::new("key-001", 256, 30)
$k.IsExpired() | Should -Be $false
}
It "Should return true for expired key" {
$k = [CryptoKeyPair]::new("key-001", 256, -1)
$k.IsExpired() | Should -Be $true
}
}
Context "Static Merge Method" {
It "Should return strongest non-expired key" {
$k1 = [CryptoKeyPair]::new("key-001", 128, 30)
$k2 = [CryptoKeyPair]::new("key-002", 256, 30)
$k3 = [CryptoKeyPair]::new("key-003", 512, 30)
$result = [CryptoKeyPair]::Merge(@($k1, $k2, $k3))
$result.KeyId | Should -Be "key-003"
$result.Strength | Should -Be 512
}
It "Should exclude expired keys" {
$k1 = [CryptoKeyPair]::new("key-001", 128, 30)
$k2 = [CryptoKeyPair]::new("key-002", 512, -1) # expired
$k3 = [CryptoKeyPair]::new("key-003", 256, 30)
$result = [CryptoKeyPair]::Merge(@($k1, $k2, $k3))
$result.KeyId | Should -Be "key-003"
$result.Strength | Should -Be 256
}
It "Should return null when all keys are expired" {
$k1 = [CryptoKeyPair]::new("key-001", 128, -1)
$k2 = [CryptoKeyPair]::new("key-002", 256, -1)
$result = [CryptoKeyPair]::Merge(@($k1, $k2))
$result | Should -BeNullOrEmpty
}
It "Should return null when array is empty" {
$result = [CryptoKeyPair]::Merge(@())
$result | Should -BeNullOrEmpty
}
It "Should return null when array is null" {
$result = [CryptoKeyPair]::Merge($null)
$result | Should -BeNullOrEmpty
}
It "Should handle single valid key" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$result = [CryptoKeyPair]::Merge(@($k1))
$result.KeyId | Should -Be "key-001"
$result.Strength | Should -Be 256
}
It "Should select strongest when multiple keys have same strength" {
$k1 = [CryptoKeyPair]::new("key-001", 256, 30)
$k2 = [CryptoKeyPair]::new("key-002", 256, 30)
$k3 = [CryptoKeyPair]::new("key-003", 128, 30)
$result = [CryptoKeyPair]::Merge(@($k1, $k2, $k3))
$result.Strength | Should -Be 256
$result.KeyId | Should -BeIn @("key-001", "key-002")
}
}
}