Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed unit test according to styling guide.
- Corrected incorrectly located entries in `CHANGELOG.MD`.
- Fix bug `Find-Certificate` when invalid certificate path is passed - fixes
[Issue #208](https://github.com/PowerShell/CertificateDsc/issues/208).
Expand Down
2 changes: 2 additions & 0 deletions Tests/TestHelpers/CommonTestHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ function Get-InvalidOperationRecord
[CmdletBinding()]
param
(
[Parameter]
[ValidateNotNullOrEmpty()]
[String]
$Message,

[Parameter]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
Expand Down
162 changes: 128 additions & 34 deletions Tests/Unit/CertificateDsc.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ InModuleScope 'CertificateDsc.Common' {
Describe 'CertificateDsc.Common\Test-DscParameterState' -Tag TestDscParameterState {
Context -Name 'When passing values' -Fixture {
It 'Should return true for two identical tables' {
$mockDesiredValues = @{ Example = 'test' }
$mockDesiredValues = @{
Example = 'test'
}

$testParameters = @{
CurrentValues = $mockDesiredValues
Expand All @@ -30,8 +32,12 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is different for [System.String]' {
$mockCurrentValues = @{ Example = [System.String] 'something' }
$mockDesiredValues = @{ Example = [System.String] 'test' }
$mockCurrentValues = @{
Example = [System.String] 'something'
}
$mockDesiredValues = @{
Example = [System.String] 'test'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -42,8 +48,12 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is different for [System.Int32]' {
$mockCurrentValues = @{ Example = [System.Int32] 1 }
$mockDesiredValues = @{ Example = [System.Int32] 2 }
$mockCurrentValues = @{
Example = [System.Int32] 1
}
$mockDesiredValues = @{
Example = [System.Int32] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -54,8 +64,12 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is different for [Int16]' {
$mockCurrentValues = @{ Example = [System.Int16] 1 }
$mockDesiredValues = @{ Example = [System.Int16] 2 }
$mockCurrentValues = @{
Example = [System.Int16] 1
}
$mockDesiredValues = @{
Example = [System.Int16] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -66,8 +80,13 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is different for [UInt16]' {
$mockCurrentValues = @{ Example = [System.UInt16] 1 }
$mockDesiredValues = @{ Example = [System.UInt16] 2 }
$mockCurrentValues = @{
Example = [System.UInt16] 1
}

$mockDesiredValues = @{
Example = [System.UInt16] 2
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -78,8 +97,13 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is different for [Boolean]' {
$mockCurrentValues = @{ Example = [System.Boolean] $true }
$mockDesiredValues = @{ Example = [System.Boolean] $false }
$mockCurrentValues = @{
Example = [System.Boolean] $true
}

$mockDesiredValues = @{
Example = [System.Boolean] $false
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -90,8 +114,11 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when a value is missing' {
$mockCurrentValues = @{ }
$mockDesiredValues = @{ Example = 'test' }
$mockCurrentValues = @{}

$mockDesiredValues = @{
Example = 'test'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -102,8 +129,15 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return true when only a specified value matches, but other non-listed values do not' {
$mockCurrentValues = @{ Example = 'test'; SecondExample = 'true' }
$mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' }
$mockCurrentValues = @{
Example = 'test'
SecondExample = 'true'
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = 'false'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -115,8 +149,15 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when only specified values do not match, but other non-listed values do ' {
$mockCurrentValues = @{ Example = 'test'; SecondExample = 'true' }
$mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' }
$mockCurrentValues = @{
Example = 'test'
SecondExample = 'true'
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = 'false'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -128,8 +169,12 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when an empty hash table is used in the current values' {
$mockCurrentValues = @{ }
$mockDesiredValues = @{ Example = 'test'; SecondExample = 'false' }
$mockCurrentValues = @{}

$mockDesiredValues = @{
Example = 'test'
SecondExample = 'false'
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -140,7 +185,10 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return true when evaluating a table against a CimInstance' {
$mockCurrentValues = @{ Handle = '0'; ProcessId = '1000' }
$mockCurrentValues = @{
Handle = '0'
ProcessId = '1000'
}

$mockWin32ProcessProperties = @{
Handle = 0
Expand All @@ -166,7 +214,10 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when evaluating a table against a CimInstance and a value is wrong' {
$mockCurrentValues = @{ Handle = '1'; ProcessId = '1000' }
$mockCurrentValues = @{
Handle = '1'
ProcessId = '1000'
}

$mockWin32ProcessProperties = @{
Handle = 0
Expand All @@ -192,8 +243,15 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return true when evaluating a hash table containing an array' {
$mockCurrentValues = @{ Example = 'test'; SecondExample = @('1', '2') }
$mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') }
$mockCurrentValues = @{
Example = 'test'
SecondExample = @('1', '2')
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = @('1', '2')
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -204,8 +262,15 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when evaluating a hash table containing an array with wrong values' {
$mockCurrentValues = @{ Example = 'test'; SecondExample = @('A', 'B') }
$mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') }
$mockCurrentValues = @{
Example = 'test'
SecondExample = @('A', 'B')
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = @('1', '2')
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -216,8 +281,14 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when evaluating a hash table containing an array, but the CurrentValues are missing an array' {
$mockCurrentValues = @{ Example = 'test' }
$mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') }
$mockCurrentValues = @{
Example = 'test'
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = @('1', '2')
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -228,8 +299,15 @@ InModuleScope 'CertificateDsc.Common' {
}

It 'Should return false when evaluating a hash table containing an array, but the property i CurrentValues is $null' {
$mockCurrentValues = @{ Example = 'test'; SecondExample = $null }
$mockDesiredValues = @{ Example = 'test'; SecondExample = @('1', '2') }
$mockCurrentValues = @{
Example = 'test'
SecondExample = $null
}

$mockDesiredValues = @{
Example = 'test'
SecondExample = @('1', '2')
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -242,7 +320,9 @@ InModuleScope 'CertificateDsc.Common' {

Context -Name 'When passing invalid types for DesiredValues' -Fixture {
It 'Should throw the correct error when DesiredValues is of wrong type' {
$mockCurrentValues = @{ Example = 'something' }
$mockCurrentValues = @{
Example = 'something'
}
$mockDesiredValues = 'NotHashTable'

$testParameters = @{
Expand Down Expand Up @@ -273,8 +353,13 @@ InModuleScope 'CertificateDsc.Common' {
}
}

$mockCurrentValues = @{ Example = New-Object -TypeName MockUnknownType }
$mockDesiredValues = @{ Example = New-Object -TypeName MockUnknownType }
$mockCurrentValues = @{
Example = New-Object -TypeName MockUnknownType
}

$mockDesiredValues = @{
Example = New-Object -TypeName MockUnknownType
}

$testParameters = @{
CurrentValues = $mockCurrentValues
Expand All @@ -289,7 +374,9 @@ InModuleScope 'CertificateDsc.Common' {

Context -Name 'When passing an CimInstance as DesiredValue and ValuesToCheck is $null' -Fixture {
It 'Should throw the correct error' {
$mockCurrentValues = @{ Example = 'something' }
$mockCurrentValues = @{
Example = 'something'
}

$mockWin32ProcessProperties = @{
Handle = 0
Expand Down Expand Up @@ -769,7 +856,11 @@ InModuleScope 'CertificateDsc.Common' {
}

Context 'When FIPS is enabled' {
Mock -CommandName Get-ItemProperty -MockWith { @{ Enabled = 1 } }
Mock -CommandName Get-ItemProperty -MockWith {
@{
Enabled = 1
}
}

Context 'When a single valid FIPS thumbrpint by parameter is passed' {
$result = Test-Thumbprint -Thumbprint $validFipsThumbprint
Expand Down Expand Up @@ -1276,6 +1367,7 @@ InModuleScope 'CertificateDsc.Common' {
[CmdletBinding()]
param
(
[Parameter()]
$DomainName
)
return New-Object -TypeName psobject -Property @{
Expand Down Expand Up @@ -1317,6 +1409,7 @@ InModuleScope 'CertificateDsc.Common' {
[CmdletBinding()]
param
(
[Parameter()]
$DomainName
)
return New-Object -TypeName psobject -Property @{
Expand Down Expand Up @@ -1356,6 +1449,7 @@ InModuleScope 'CertificateDsc.Common' {
[CmdletBinding()]
param
(
[Parameter()]
$DomainName
)
New-InvalidOperationException `
Expand Down
21 changes: 16 additions & 5 deletions Tests/Unit/MSFT_CertReq.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ try
return $true
}

$sanOid = New-Object -TypeName System.Security.Cryptography.Oid -Property @{FriendlyName = 'Subject Alternative Name' }
$sanOid = New-Object -TypeName System.Security.Cryptography.Oid -Property @{
FriendlyName = 'Subject Alternative Name'
}

$sanExt = [PSCustomObject] @{
Oid = $sanOid
Critical = $false
Expand Down Expand Up @@ -1831,10 +1834,18 @@ OID = $oid
Describe 'MSFT_CertReq\ConvertTo-StringEnclosedInDoubleQuotes' {
Context 'When called with test values' {
$testValues = @(
@{ Value = 'test' },
@{ Value = '"test' },
@{ Value = 'test"' },
@{ Value = '"test"' }
@{
Value = 'test'
},
@{
Value = '"test'
},
@{
Value = 'test"'
},
@{
Value = '"test"'
}
)

It 'Should return ''"test"'' when called with ''<Value>''' -TestCases $testValues {
Expand Down
Loading