|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Pester testing file')] |
| 5 | +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidOverwritingBuiltInCmdlets', '', Justification = 'Pester testing file')] |
| 6 | +[CmdletBinding()] |
| 7 | +param() |
| 8 | + |
| 9 | +BeforeAll { |
| 10 | + $Script:parentPath = (Split-Path -Parent $PSScriptRoot) |
| 11 | + . $Script:parentPath\Get-VulnerabilityStatus.ps1 |
| 12 | +} |
| 13 | + |
| 14 | +Describe "Testing Get-VulnerabilityStatus" { |
| 15 | + |
| 16 | + Context "Returns correct status when server is vulnerable and unmitigated" { |
| 17 | + |
| 18 | + It "Should report CodeFixApplied false and MitigationApplied false" { |
| 19 | + $definition = [PSCustomObject]@{ |
| 20 | + Id = "CVE-TEST-001" |
| 21 | + Description = "Test vulnerability" |
| 22 | + TestVulnerable = { return @{ MitigationApplied = $false; CodeFixApplied = $false } } |
| 23 | + } |
| 24 | + |
| 25 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 26 | + |
| 27 | + $result.CVE | Should -Be "CVE-TEST-001" |
| 28 | + $result.CodeFixApplied | Should -Be $false |
| 29 | + $result.MitigationApplied | Should -Be $false |
| 30 | + $result.Description | Should -Be "Test vulnerability" |
| 31 | + $result.CheckPerformed | Should -Not -BeNullOrEmpty |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + Context "Returns correct status when server is patched" { |
| 36 | + |
| 37 | + It "Should report CodeFixApplied true when security fix is installed" { |
| 38 | + $definition = [PSCustomObject]@{ |
| 39 | + Id = "CVE-TEST-002" |
| 40 | + Description = "Test patched" |
| 41 | + TestVulnerable = { return @{ MitigationApplied = $false; CodeFixApplied = $true } } |
| 42 | + } |
| 43 | + |
| 44 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 45 | + |
| 46 | + $result.CVE | Should -Be "CVE-TEST-002" |
| 47 | + $result.CodeFixApplied | Should -Be $true |
| 48 | + $result.MitigationApplied | Should -Be $false |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Context "Returns correct status when mitigation is applied but not patched" { |
| 53 | + |
| 54 | + It "Should report MitigationApplied true and CodeFixApplied false" { |
| 55 | + $definition = [PSCustomObject]@{ |
| 56 | + Id = "CVE-TEST-003" |
| 57 | + Description = "Test mitigated" |
| 58 | + TestVulnerable = { return @{ MitigationApplied = $true; CodeFixApplied = $false } } |
| 59 | + } |
| 60 | + |
| 61 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 62 | + |
| 63 | + $result.MitigationApplied | Should -Be $true |
| 64 | + $result.CodeFixApplied | Should -Be $false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Context "Returns correct status when both patched and mitigated" { |
| 69 | + |
| 70 | + It "Should report both CodeFixApplied and MitigationApplied as true" { |
| 71 | + $definition = [PSCustomObject]@{ |
| 72 | + Id = "CVE-TEST-004" |
| 73 | + Description = "Test both applied" |
| 74 | + TestVulnerable = { return @{ MitigationApplied = $true; CodeFixApplied = $true } } |
| 75 | + } |
| 76 | + |
| 77 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 78 | + |
| 79 | + $result.MitigationApplied | Should -Be $true |
| 80 | + $result.CodeFixApplied | Should -Be $true |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Context "Handles TestVulnerable exceptions gracefully" { |
| 85 | + |
| 86 | + It "Should default to both false when TestVulnerable throws" { |
| 87 | + Mock Write-Warning { } |
| 88 | + |
| 89 | + $definition = [PSCustomObject]@{ |
| 90 | + Id = "CVE-TEST-005" |
| 91 | + Description = "Test error" |
| 92 | + TestVulnerable = { throw "Cannot determine Exchange version" } |
| 93 | + } |
| 94 | + |
| 95 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 96 | + |
| 97 | + $result.MitigationApplied | Should -Be $false |
| 98 | + $result.CodeFixApplied | Should -Be $false |
| 99 | + $result.CVE | Should -Be "CVE-TEST-005" |
| 100 | + Should -Invoke Write-Warning -Times 1 -ParameterFilter { |
| 101 | + $Message -like "*Cannot determine Exchange version*" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Context "Result object has all expected properties" { |
| 107 | + |
| 108 | + It "Should return CVE, MitigationApplied, CodeFixApplied, Description, and CheckPerformed" { |
| 109 | + $definition = [PSCustomObject]@{ |
| 110 | + Id = "CVE-TEST-006" |
| 111 | + Description = "Property check" |
| 112 | + TestVulnerable = { return @{ MitigationApplied = $false; CodeFixApplied = $true } } |
| 113 | + } |
| 114 | + |
| 115 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 116 | + |
| 117 | + $result.PSObject.Properties.Name | Should -Contain "CVE" |
| 118 | + $result.PSObject.Properties.Name | Should -Contain "MitigationApplied" |
| 119 | + $result.PSObject.Properties.Name | Should -Contain "CodeFixApplied" |
| 120 | + $result.PSObject.Properties.Name | Should -Contain "Description" |
| 121 | + $result.PSObject.Properties.Name | Should -Contain "CheckPerformed" |
| 122 | + } |
| 123 | + |
| 124 | + It "Should have a CheckPerformed timestamp close to now" { |
| 125 | + $definition = [PSCustomObject]@{ |
| 126 | + Id = "CVE-TEST-007" |
| 127 | + Description = "Timestamp check" |
| 128 | + TestVulnerable = { return @{ MitigationApplied = $false; CodeFixApplied = $false } } |
| 129 | + } |
| 130 | + |
| 131 | + $before = [DateTime]::Now |
| 132 | + $result = Get-VulnerabilityStatus -MitigationDefinition $definition |
| 133 | + |
| 134 | + $result.CheckPerformed | Should -BeGreaterOrEqual $before |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments