forked from dsccommunity/xWinEventLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinEvent.Tests.ps1
215 lines (165 loc) · 8.08 KB
/
WinEvent.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
<#
.NOTES
#>
Import-Module $PSScriptRoot\DSCResources\MSFT_xWinEventLog\MSFT_xWinEventLog.psm1 -Prefix WinEventLog -Force
#Getting initial Value for Capi2 Log so we can test the ability to set Isenabled to False
#and then set it back to its original value when we're done
$Capi2Log = Get-WinEvent -ListLog 'Microsoft-Windows-CAPI2/Operational'
if($Capi2Log.IsEnabled){
$Capi2Log.IsEnabled = $false
$Capi2Log.SaveChanges()
}
Describe 'WinEventLog Get-TargetResource'{
Mock Get-WinEvent -ModuleName MSFT_xWinEventLog {
$properties = @{
MaximumSizeInBytes = '999'
IsEnabled = $true
LogMode = 'Test'
LogFilePath = 'c:\logs\test.evtx'
SecurityDescriptor = 'TestDescriptor'
}
Write-Output (New-Object -TypeName PSObject -Property $properties)
}
$results = Get-WinEventLogTargetResource 'Application'
It 'Should return an hashtable'{
$results.GetType().Name | Should Be 'HashTable'
}
It 'Should return a Hashtable name is Application'{
$results.LogName = 'Application'
}
It 'Should return a Hashatable with the MaximumSizeInBytes is 999'{
$results.MaximumSizeInBytes | Should Be '999'
}
It 'Should return a Hashtable where IsEnabled is true'{
$results.IsEnabled | should Be $true
}
It 'Should return a HashTable where LogMode is Test' {
$results.LogMode | Should Be 'Test'
}
It 'Should return a HashTable where LogFilePath is c:\logs\test.evtx' {
$results.LogFilePath | Should Be 'c:\logs\test.evtx'
}
It 'Should return a HashTable where SecurityDescriptor is TestDescriptor'{
$results.SecurityDescriptor | Should Be 'TestDescriptor'
}
}
Describe 'WinEventLog Test-TargetResource'{
Mock Get-WinEvent -ModuleName MSFT_xWinEventLog {
$properties = @{
MaximumSizeInBytes = '5111808'
IsEnabled = $true
LogMode = 'Circular'
LogFilePath = 'c:\logs\test.evtx'
SecurityDescriptor = 'TestDescriptor'
}
Write-Output (New-Object -TypeName PSObject -Property $properties)
}
$params = @{
LogName = 'Application'
MaximumSizeInBytes = '5111808'
LogMode = 'Circular'
IsEnabled = $true
LogFilePath = 'c:\logs\test.evtx'
SecurityDescriptor = 'TestDescriptor'
}
It 'should return true when all properties match does not match'{
$testResults = Test-WinEventLogTargetResource @params
$testResults | Should Be $True
}
It 'should return false when MaximumSizeInBytes does not match'{
$testResults = Test-WinEventLogTargetResource -LogName 'Application' -MaximumSizeInBytes '1' -IsEnabled $true -SecurityDescriptor 'TestDescriptor' -LogMode 'Circular' -LogFilePath 'c:\logs\test.evtx'
$testResults | Should Be $False
}
It 'should return false when LogMode does not match'{
$testResults = Test-WinEventLogTargetResource -LogName 'Application' -MaximumSizeInBytes '5111808' -IsEnabled $true -SecurityDescriptor 'TestDescriptor' -LogMode 'AutoBackup' -LogFilePath 'c:\logs\test.evtx'
$testResults | Should Be $false
}
It 'should return false when IsEnabled does not match'{
$testResults = Test-WinEventLogTargetResource -LogName 'Application' -MaximumSizeInBytes '5111808' -IsEnabled $false -SecurityDescriptor 'TestDescriptor' -LogMode 'Circular' -LogFilePath 'c:\logs\test.evtx'
$testResults | Should Be $false
}
It 'Should return false when SecurityDescriptor does not match'{
$testResults = Test-WinEventLogTargetResource -LogName 'Application' -MaximumSizeInBytes '5111808' -IsEnabled $true -SecurityDescriptor 'TestDescriptorFail' -LogMode 'Circular' -LogFilePath 'c:\logs\test.evtx'
$testResults | Should Be $false
}
It 'Should return false when LogFilePath does not match'{
$testResults = Test-WinEventLogTargetResource -LogName 'Application' -MaximumSizeInBytes '5111808' -IsEnabled $true -SecurityDescriptor 'TestDescriptor' -LogMode 'Circular' -LogFilePath 'c:\logs\wrongfile.evtx'
$testResults | Should Be $false
}
It 'Should call Get-WinEventLog' {
Assert-MockCalled Get-WinEvent -ModuleName MSFT_xWinEventLog -Exactly 6
}
}
Describe 'WinEventLog Set-TargetResource'{
BeforeAll {
New-EventLog -LogName 'Pester' -Source 'PesterTest'
$Log = Get-WinEvent -ListLog 'Pester'
$Log.LogMode = 'Circular'
$Log.SaveChanges()
New-Item -Path 'c:\tmp' -ItemType Directory -Force | Out-Null
}
Context 'When set is called and actual value does not match expected value'{
It 'Should update MaximumSizeInBytes' {
Set-WinEventLogTargetResource -LogName 'Pester' -MaximumSizeInBytes '5111800'
(Get-WinEvent -ListLog 'Pester').MaximumSizeInBytes | Should Be '5111800'
}
It 'Should update the LogMode'{
Set-WinEventLogTargetResource -LogName 'Pester' -LogMode 'AutoBackup'
(Get-WinEvent -ListLog 'Pester').LogMode | Should Be 'AutoBackup'
}
It 'Should update IsEnabled to false' {
Set-WinEventLogTargetResource -LogName 'Microsoft-Windows-CAPI2/Operational' -IsEnabled $false
(Get-WinEvent -ListLog 'Microsoft-Windows-CAPI2/Operational').IsEnabled | Should Be $false
}
It 'Should update SecurityDescriptor' {
Set-WinEventLogTargetResource -LogName 'Pester' -SecurityDescriptor 'O:BAG:SYD:(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)'
(Get-WinEvent -ListLog 'Pester').SecurityDescriptor = 'O:BAG:SYD:(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)'
}
It 'Should update the LogFilePath'{
Set-WinEventLogTargetResource -LogName 'Pester' -LogFilePath 'c:\tmp\test.evtx'
(Get-WinEvent -ListLog 'Pester').LogFilePath | Should Be 'c:\tmp\test.evtx'
}
}
#Setting up mocks to validate code is never called... not sure if this is good practice
Mock -CommandName Set-MaximumSizeInBytes -ModuleName MSFT_xWinEventLog -MockWith {
return $true
}
Mock -CommandName Set-LogMode -ModuleName MSFT_xWinEventLog -MockWith {
return $true
}
Mock -CommandName Set-SecurityDescriptor -ModuleName MSFT_xWinEventLog -MockWith {
return $true
}
Mock -CommandName Set-IsEnabled -ModuleName MSFT_xWinEventLog -MockWith {
return $true
}
Mock -CommandName Set-LogFilePath -ModuleName MSFT_xWinEventLog -MockWith {
return $true
}
Context 'When desired value matches property'{
$Log = Get-WinEvent -ListLog 'Pester'
Set-WinEventLogTargetResource -LogName $Log.LogName -SecurityDescriptor $log.SecurityDescriptor -LogMode $log.LogMode -IsEnabled $log.IsEnabled
It 'Should not call Set-MaximumSizeInBytes'{
Assert-MockCalled -CommandName Set-MaximumSizeInBytes -ModuleName MSFT_xWinEventLog -Exactly 0
}
It 'Should not call Set-LogMode' {
Assert-MockCalled -CommandName Set-LogMode -ModuleName MSFT_xWinEventLog -Exactly 0
}
It 'Should not call Set-SecurityDescriptor'{
Assert-MockCalled -CommandName Set-SecurityDescriptor -ModuleName MSFT_xWinEventLog -Exactly 0
}
It 'Should not call Set-IsEnabled'{
Assert-MockCalled -CommandName Set-IsEnabled -ModuleName MSFT_xWinEventLog -Exactly 0
}
It 'Should not call Set-LogFilePath'{
Assert-MockCalled -CommandName Set-LogFilePath -ModuleName MSFT_xWinEventLog -Exactly 0
}
}
AfterAll {
Remove-EventLog -LogName 'Pester'
$log = Get-WinEvent -ListLog 'Microsoft-Windows-CAPI2/Operational'
$log.IsEnabled = $Capi2Log.IsEnabled
$log.SaveChanges()
Remove-Item -Path 'c:\tmp' -Recurse -Force -ErrorAction SilentlyContinue
}
}