-
Notifications
You must be signed in to change notification settings - Fork 67
/
CertificateDsc.Common.tests.ps1
496 lines (453 loc) · 21.6 KB
/
CertificateDsc.Common.tests.ps1
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
$script:ModuleName = 'CertificateDsc.Common'
#region HEADER
# Unit Test Template Version: 1.1.0
[string] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
Import-Module -Name (Join-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Modules' -ChildPath $script:ModuleName)) -ChildPath "$script:ModuleName.psm1") -Force
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
#endregion HEADER
# Begin Testing
try
{
InModuleScope $script:ModuleName {
$DSCResourceName = 'CertificateDsc.Common'
$invalidThumbprint = 'Zebra'
$validThumbprint = (
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | Where-Object {
$_.BaseType.BaseType -eq [System.Security.Cryptography.HashAlgorithm] -and
($_.Name -cmatch 'Managed$' -or $_.Name -cmatch 'Provider$')
} | Select-Object -First 1 | ForEach-Object {
(New-Object $_).ComputeHash([String]::Empty) | ForEach-Object {
'{0:x2}' -f $_
}
}
) -join ''
$testFile = 'test.pfx'
$invalidPath = 'TestDrive:'
$validPath = "TestDrive:\$testFile"
Describe "$DSCResourceName\Test-CertificatePath" {
$null | Set-Content -Path $validPath
Context 'a single existing file by parameter' {
$result = Test-CertificatePath -Path $validPath
It 'should return true' {
($result -is [bool]) | Should Be $true
$result | Should Be $true
}
}
Context 'a single missing file by parameter' {
It 'should throw an exception' {
# directories are not valid
{ Test-CertificatePath -Path $invalidPath } | Should Throw
}
}
Context 'a single missing file by parameter with -Quiet' {
$result = Test-CertificatePath -Path $invalidPath -Quiet
It 'should return false' {
($result -is [bool]) | Should Be $true
$result | Should Be $false
}
}
Context 'a single existing file by pipeline' {
$result = $validPath | Test-CertificatePath
It 'should return true' {
($result -is [bool]) | Should Be $true
$result | Should Be $true
}
}
Context 'a single missing file by pipeline' {
It 'should throw an exception' {
# directories are not valid
{ $invalidPath | Test-CertificatePath } | Should Throw
}
}
Context 'a single missing file by pipeline with -Quiet' {
$result = $invalidPath | Test-CertificatePath -Quiet
It 'should return false' {
($result -is [bool]) | Should Be $true
$result | Should Be $false
}
}
}
Describe "$DSCResourceName\Test-Thumbprint" {
Context 'a single valid thumbrpint by parameter' {
$result = Test-Thumbprint -Thumbprint $validThumbprint
It 'should return true' {
($result -is [bool]) | Should Be $true
$result | Should Be $true
}
}
Context 'a single invalid thumbprint by parameter' {
It 'should throw an exception' {
# directories are not valid
{ Test-Thumbprint -Thumbprint $invalidThumbprint } | Should Throw
}
}
Context 'a single invalid thumbprint by parameter with -Quiet' {
$result = Test-Thumbprint $invalidThumbprint -Quiet
It 'should return false' {
($result -is [bool]) | Should Be $true
$result | Should Be $false
}
}
Context 'a single valid thumbprint by pipeline' {
$result = $validThumbprint | Test-Thumbprint
It 'should return true' {
($result -is [bool]) | Should Be $true
$result | Should Be $true
}
}
Context 'a single invalid thumborint by pipeline' {
It 'should throw an exception' {
# directories are not valid
{ $invalidThumbprint | Test-Thumbprint } | Should Throw
}
}
Context 'a single invalid thumbprint by pipeline with -Quiet' {
$result = $invalidThumbprint | Test-Thumbprint -Quiet
It 'should return false' {
($result -is [bool]) | Should Be $true
$result | Should Be $false
}
}
}
Describe "$DSCResourceName\Find-Certificate" {
# Download and dot source the New-SelfSignedCertificateEx script
. (Install-NewSelfSignedCertificateExScript)
# Generate the Valid certificate for testing but remove it from the store straight away
$certDNSNames = @('www.fabrikam.com', 'www.contoso.com')
$certDNSNamesReverse = @('www.contoso.com', 'www.fabrikam.com')
$certDNSNamesNoMatch = $certDNSNames + @('www.nothere.com')
$certKeyUsage = @('DigitalSignature','DataEncipherment')
$certKeyUsageReverse = @('DataEncipherment','DigitalSignature')
$certKeyUsageNoMatch = $certKeyUsage + @('KeyEncipherment')
$certEKU = @('Server Authentication','Client authentication')
$certEKUReverse = @('Client authentication','Server Authentication')
$certEKUNoMatch = $certEKU + @('Encrypting File System')
$certSubject = 'CN=contoso, DC=com'
$certFriendlyName = 'Contoso Test Cert'
$validCert = New-SelfSignedCertificateEx `
-Subject $certSubject `
-KeyUsage $certKeyUsage `
-KeySpec 'Exchange' `
-EKU $certEKU `
-SubjectAlternativeName $certDNSNames `
-FriendlyName $certFriendlyName `
-StoreLocation 'CurrentUser' `
-Exportable
# Pull the generated certificate from the store so we have the friendlyname
$validThumbprint = $validCert.Thumbprint
$validCert = Get-Item -Path "cert:\CurrentUser\My\$validThumbprint"
Remove-Item -Path $validCert.PSPath -Force
# Generate the Expired certificate for testing but remove it from the store straight away
$expiredCert = New-SelfSignedCertificateEx `
-Subject $certSubject `
-KeyUsage $certKeyUsage `
-KeySpec 'Exchange' `
-EKU $certEKU `
-SubjectAlternativeName $certDNSNames `
-FriendlyName $certFriendlyName `
-NotBefore ((Get-Date) - (New-TimeSpan -Days 2)) `
-NotAfter ((Get-Date) - (New-TimeSpan -Days 1)) `
-StoreLocation 'CurrentUser' `
-Exportable
# Pull the generated certificate from the store so we have the friendlyname
$expiredThumbprint = $expiredCert.Thumbprint
$expiredCert = Get-Item -Path "cert:\CurrentUser\My\$expiredThumbprint"
Remove-Item -Path $expiredCert.PSPath -Force
$nocertThumbprint = '1111111111111111111111111111111111111111'
Mock `
-CommandName Get-ChildItem `
-MockWith { @( $validCert ) } `
-ParameterFilter { $Path -eq 'cert:\LocalMachine\My' } `
-Verifiable
Context 'Thumbprint only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Thumbprint $validThumbprint } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'Thumbprint only is passed and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Thumbprint $nocertThumbprint } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'FriendlyName only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -FriendlyName $certFriendlyName } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'FriendlyName only is passed and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -FriendlyName 'Does Not Exist' } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'Subject only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Subject $certSubject } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'Subject only is passed and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Subject 'CN=Does Not Exist' } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'Issuer only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Issuer $certSubject } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'Issuer only is passed and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Issuer 'CN=Does Not Exist' } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'DNSName only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -DnsName $certDNSNames } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'DNSName only is passed in reversed order and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -DnsName $certDNSNamesReverse } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'DNSName only is passed with only one matching DNS name and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -DnsName $certDNSNames[0] } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'DNSName only is passed but an entry is missing and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -DnsName $certDNSNamesNoMatch } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'KeyUsage only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -KeyUsage $certKeyUsage } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'KeyUsage only is passed in reversed order and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -KeyUsage $certKeyUsageReverse } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'KeyUsage only is passed with only one matching DNS name and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -KeyUsage $certKeyUsage[0] } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'KeyUsage only is passed but an entry is missing and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -KeyUsage $certKeyUsageNoMatch } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'EnhancedKeyUsage only is passed and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -EnhancedKeyUsage $certEKU } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'EnhancedKeyUsage only is passed in reversed order and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -EnhancedKeyUsage $certEKUReverse } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'EnhancedKeyUsage only is passed with only one matching DNS name and matching certificate exists' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -EnhancedKeyUsage $certEKU[0] } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'EnhancedKeyUsage only is passed but an entry is missing and matching certificate does not exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -EnhancedKeyUsage $certEKUNoMatch } | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Mock `
-CommandName Get-ChildItem `
-ParameterFilter { $Path -eq 'cert:\LocalMachine\CA' } `
-Verifiable
Context 'Thumbprint only is passed and matching certificate does not exist in CA store' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -Thumbprint $validThumbprint -Store 'CA'} | Should Not Throw
}
It 'should return null' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Mock `
-CommandName Get-ChildItem `
-MockWith { @( $expiredCert, $validCert ) } `
-ParameterFilter { $Path -eq 'cert:\LocalMachine\My' } `
-Verifiable
Context 'FriendlyName only is passed and both valid and expired certificates exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -FriendlyName $certFriendlyName } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $validThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Mock `
-CommandName Get-ChildItem `
-MockWith { @( $expiredCert ) } `
-ParameterFilter { $Path -eq 'cert:\LocalMachine\My' } `
-Verifiable
Context 'FriendlyName only is passed and only expired certificates exist' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -FriendlyName $certFriendlyName } | Should Not Throw
}
It 'should return expected certificate' {
$script:result | Should BeNullOrEmpty
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
Context 'FriendlyName only is passed and only expired certificates exist but allowexpired passed' {
It 'should not throw exception' {
{ $script:result = Find-Certificate -FriendlyName $certFriendlyName -AllowExpired:$true } | Should Not Throw
}
It 'should return expected certificate' {
$script:result.Thumbprint | Should Be $expiredThumbprint
}
It 'should call expected mocks' {
Assert-VerifiableMocks
}
}
}
}
}
finally
{
#region FOOTER
#endregion
}