-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22_RecursiveTypes.Tests.ps1
More file actions
176 lines (141 loc) · 6.5 KB
/
22_RecursiveTypes.Tests.ps1
File metadata and controls
176 lines (141 loc) · 6.5 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
BeforeAll {
. $PSScriptRoot/22_RecursiveTypes.ps1
}
Describe "CertificateChainNode" {
Context "Basic Construction and Chain Methods" {
It "Should create a single node with depth 0" {
$node = [CertificateChainNode]::new("CN=Root", "Self-Signed")
$node.Depth() | Should -Be 0
}
It "Should calculate depth correctly for 2-level chain" {
$root = [CertificateChainNode]::new("CN=Root", "Self-Signed")
$child = [CertificateChainNode]::new("CN=Intermediate", "CN=Root")
$root.AddChild($child)
$child.Depth() | Should -Be 1
}
It "Should calculate depth correctly for 3-level chain" {
$root = [CertificateChainNode]::new("CN=Root", "Self-Signed")
$intermediate = [CertificateChainNode]::new("CN=Intermediate", "CN=Root")
$leaf = [CertificateChainNode]::new("CN=Leaf", "CN=Intermediate")
$root.AddChild($intermediate)
$intermediate.AddChild($leaf)
$leaf.Depth() | Should -Be 2
}
It "Should return subjects in leaf-to-root order" {
$root = [CertificateChainNode]::new("CN=Root", "Self-Signed")
$intermediate = [CertificateChainNode]::new("CN=Intermediate", "CN=Root")
$leaf = [CertificateChainNode]::new("CN=Leaf", "CN=Intermediate")
$root.AddChild($intermediate)
$intermediate.AddChild($leaf)
$subjects = $leaf.GetChainSubjects()
$subjects.Count | Should -Be 3
$subjects[0] | Should -Be "CN=Leaf"
$subjects[1] | Should -Be "CN=Intermediate"
$subjects[2] | Should -Be "CN=Root"
}
}
Context "BuildChain Static Factory Method" {
It "Should throw on null subjects array" {
{ [CertificateChainNode]::BuildChain($null) } | Should -Throw
}
It "Should throw on empty subjects array" {
{ [CertificateChainNode]::BuildChain(@()) } | Should -Throw
}
It "Should create single-node chain from 1-element array" {
$leaf = [CertificateChainNode]::BuildChain(@("CN=Root"))
$leaf.Subject | Should -Be "CN=Root"
$leaf.Issuer | Should -Be "Self-Signed"
$leaf.Depth() | Should -Be 0
$leaf.Parent | Should -BeNullOrEmpty
}
It "Should create 2-level chain from 2-element array" {
$leaf = [CertificateChainNode]::BuildChain(@("CN=Root", "CN=Leaf"))
$leaf.Subject | Should -Be "CN=Leaf"
$leaf.Issuer | Should -Be "CN=Root"
$leaf.Depth() | Should -Be 1
$leaf.Parent.Subject | Should -Be "CN=Root"
}
It "Should create 3-level chain from 3-element array" {
$leaf = [CertificateChainNode]::BuildChain(@("CN=Root", "CN=Intermediate", "CN=Leaf"))
$leaf.Subject | Should -Be "CN=Leaf"
$leaf.Depth() | Should -Be 2
}
It "Should return correct GetChainSubjects for BuildChain output (2 levels)" {
$leaf = [CertificateChainNode]::BuildChain(@("CN=Root", "CN=Leaf"))
$subjects = $leaf.GetChainSubjects()
$subjects.Count | Should -Be 2
$subjects[0] | Should -Be "CN=Leaf"
$subjects[1] | Should -Be "CN=Root"
}
It "Should return correct GetChainSubjects for BuildChain output (3 levels)" {
$leaf = [CertificateChainNode]::BuildChain(@("CN=Root", "CN=Intermediate", "CN=Leaf"))
$subjects = $leaf.GetChainSubjects()
$subjects.Count | Should -Be 3
$subjects[0] | Should -Be "CN=Leaf"
$subjects[1] | Should -Be "CN=Intermediate"
$subjects[2] | Should -Be "CN=Root"
}
It "Should return correct GetChainSubjects for BuildChain output (5 levels)" {
$leaf = [CertificateChainNode]::BuildChain(@(
"CN=RootCA",
"CN=IntermediateCA1",
"CN=IntermediateCA2",
"CN=IssuingCA",
"CN=EndEntity"
))
$subjects = $leaf.GetChainSubjects()
$subjects.Count | Should -Be 5
$subjects[0] | Should -Be "CN=EndEntity"
$subjects[1] | Should -Be "CN=IssuingCA"
$subjects[2] | Should -Be "CN=IntermediateCA2"
$subjects[3] | Should -Be "CN=IntermediateCA1"
$subjects[4] | Should -Be "CN=RootCA"
}
It "Should return correct Depth for BuildChain output (5 levels)" {
$leaf = [CertificateChainNode]::BuildChain(@(
"CN=RootCA",
"CN=IntermediateCA1",
"CN=IntermediateCA2",
"CN=IssuingCA",
"CN=EndEntity"
))
$leaf.Depth() | Should -Be 4
}
}
}
Describe "KeyHistoryNode" {
Context "Basic Construction and History" {
It "Should create node with key material" {
$key = [byte[]]::new(32)
$node = [KeyHistoryNode]::new($key)
$node.KeyMaterial | Should -Not -BeNullOrEmpty
$node.KeyMaterial.Length | Should -Be 32
$node.HistoryLength() | Should -Be 1
}
It "Should track history length correctly with previous node" {
$key1 = [byte[]]::new(16)
$key2 = [byte[]]::new(16)
$old = [KeyHistoryNode]::new($key1)
$current = [KeyHistoryNode]::new($key2)
$current.Previous = $old
$current.HistoryLength() | Should -Be 2
}
It "Should track history length correctly with 3-node chain" {
$old1 = [KeyHistoryNode]::new([byte[]]::new(16))
$old2 = [KeyHistoryNode]::new([byte[]]::new(16))
$current = [KeyHistoryNode]::new([byte[]]::new(16))
$old2.Previous = $old1
$current.Previous = $old2
$current.HistoryLength() | Should -Be 3
}
It "Should set ActiveFrom timestamp on creation" {
$before = [datetime]::UtcNow
Start-Sleep -Milliseconds 10
$node = [KeyHistoryNode]::new([byte[]]::new(16))
Start-Sleep -Milliseconds 10
$after = [datetime]::UtcNow
$node.ActiveFrom | Should -BeGreaterThan $before
$node.ActiveFrom | Should -BeLessThan $after
}
}
}