Tests/Integration/MSFT_xRegistryResource.Integration.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
<#
    WARNING: DO NOT RUN THESE TESTS ON A VALUABLE MACHINE!
    Running on a disposable VM or AppVeyor is strongly recommended.
    If these tests go awry, your machine's registry could be corrupted which will brick your machine!
    If this happens to you, it is fixable, but the fix is difficult and time-consuming.
#>


$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'

# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
$script:testsFolderFilePath = Split-Path $PSScriptRoot -Parent
$script:commonTestHelperFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'CommonTestHelper.psm1'
Import-Module -Name $commonTestHelperFilePath

$script:testEnvironment = Enter-DscResourceTestEnvironment `
    -DscResourceModuleName 'xPSDesiredStateConfiguration' `
    -DscResourceName 'MSFT_xRegistryResource' `
    -TestType 'Integration'

try
{
    Describe 'xRegistry Integration Tests' {
        BeforeAll {
            # Import Registry test helper
            $registryTestHelperFilePath = Join-Path -Path $script:testsFolderFilePath -ChildPath 'MSFT_xRegistryResource.TestHelper.psm1'
            Import-Module -Name $registryTestHelperFilePath -Force

            # Import Registry resource module for Get-TargetResource, Test-TargetResource, Set-TargetResource
            $moduleRootFilePath = Split-Path -Path $script:testsFolderFilePath -Parent
            $dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
            $registryResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_xRegistryResource'
            $registryResourceModuleFilePath = Join-Path -Path $registryResourceFolderFilePath -ChildPath 'MSFT_xRegistryResource.psm1'
            Import-Module -Name $registryResourceModuleFilePath -Force

            $baseRegistryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TestKey'
            $script:registryKeyPath = $baseRegistryKeyPath

            $script:doNotDeleteRegistryKey = $false
            $script:registryDriveOriginallyMounted = $true

            $loopTimeoutMinutes = 1

            $startLoopTime = Get-Date
            while ((Test-RegistryKeyExists -KeyPath $script:registryKeyPath) -and $loopMinutes -lt $loopTimeoutMinutes)
            {
                $randomNumber = Get-Random
                $script:registryKeyPath = $baseRegistryKeyPath + $randomNumber
                $loopMinutes = ((Get-Date) - $startLoopTime).Minutes
            }

            if (Test-RegistryKeyExists -KeyPath $script:registryKeyPath)
            {
                $script:doNotDeleteRegistryKey = $true
                throw "Timed out while attempting to set up a non-destructive registry key for testing. Last testing key attempted: $script:registryKeyPath"
                return
            }
        }

        BeforeEach {
            # Remove the test registry key if it already exists
            if (Test-RegistryKeyExists -KeyPath $script:registryKeyPath)
            {
                Remove-RegistryKey -KeyPath $script:registryKeyPath
            }
        }

        AfterAll {
            # Remove the test registry key if it already exists
            if ((Test-RegistryKeyExists -KeyPath $script:registryKeyPath) -and -not $script:doNotDeleteRegistryKey)
            {
                Remove-RegistryKey -KeyPath $script:registryKeyPath
            }
        }

        Context 'Old tests' {
            # Get-TargetResource
            It 'Should return Present when retrieving a blank value from an existing registry key' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName ''
                $getTargetResourceResult.Ensure | Should Be 'Present'
            }

            It 'Should return Absent when retrieving a blank value from a registry key that does not exist' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmental'
                $getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName ''
                $getTargetResourceResult.Ensure | Should Be 'Absent'
            }

            It 'Should return Present when retrieving an existing value from an existing registry key' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $registryValueName = 'Path'
                $getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName $registryValueName
                $getTargetResourceResult.Ensure | Should Be 'Present'
            }

            It 'Should return Absent when retrieving a nonexistant value from an existing registry key' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $registryValueName = 'PsychoPath'
                $getTargetResourceResult = Get-TargetResource -Key $registryKeyPath -ValueName $registryValueName
                $getTargetResourceResult.Ensure | Should Be 'Absent'
            }

            $commonRegistryKeys = @( 'HKEY_CURRENT_USER', 'HKEY_CLASSES_ROOT', 'HKEY_USERS', 'HKEY_CURRENT_CONFIG' )
            foreach ($commonRegistryKey in $commonRegistryKeys)
            {
                It "Should return Present when retrieving a blank value from $commonRegistryKey" {
                    $getTargetResourceResult = Get-TargetResource -Key $commonRegistryKey -ValueName ''
                    $getTargetResourceResult.Ensure | Should Be 'Present'
                }
            }

            # Set-TargetResource
            It 'Should create a new registry key' {
                Set-TargetResource -Key $script:registryKeyPath -ValueName ''

                # Verify that the registry key has been created
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
                $registryKeyExists | Should Be $true
            }

            It 'Should create a new registry key tree' {
                $registryKeyTreePath = Join-Path -Path (Join-Path -Path (Join-Path -Path $script:registryKeyPath -ChildPath 'A') -ChildPath 'B') -ChildPath 'C'

                Set-TargetResource -Key $registryKeyTreePath -ValueName ''

                # Verify that the registry key has been created
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
                $registryKeyExists | Should Be $true
            }

            It 'Should remove a registry key' {
                # Create the test registry key
                New-TestRegistryKey -KeyPath $script:registryKeyPath

                # Verify that the registry key exists before removal
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
                $registryKeyExists | Should Be $true

                # Now remove the TestKey
                Set-TargetResource -Key $script:registryKeyPath -ValueName '' -Ensure 'Absent'

                # Verify that the registry key has been removed
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $script:registryKeyPath
                $registryKeyExists | Should Be $false
            }

            It 'Should remove a registry key tree' {
                $registryKeyTreePath = Join-Path -Path (Join-Path -Path (Join-Path -Path $script:registryKeyPath -ChildPath 'A') -ChildPath 'B') -ChildPath 'C'

                # Create the test registry key
                New-TestRegistryKey -KeyPath $registryKeyTreePath

                # Verify that the registry key tree exists before removal
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
                $registryKeyExists | Should Be $true

                # Remove the test registry key tree
                Set-TargetResource -Key $registryKeyTreePath -ValueName '' -Ensure 'Absent'

                # Verify that the registry key tree has been removed
                $registryKeyExists = Test-RegistryKeyExists -KeyPath $registryKeyTreePath
                $registryKeyExists | Should Be $false
            }

            It 'Should create a new string registry key value' {
                $valueName = 'TestValue'
                $valueData = 'TestData'
                $valueType = 'String'

                # Create the new registry key value
                Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                # Verify that the registry key value has been created with the correct data and type
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
                $registryValueExists | Should Be $true
            }

            It 'Should create a new binary registry key value' {
                $valueName = 'TestValue'
                $valueData = 'aabbcc'
                $valueType = 'Binary'

                # Create the new registry key value
                Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType -Hex $true

                # Verify that the registry key value has been created with the correct data and type
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType
                $registryValueExists | Should Be $true
            }

            It 'Should set the default value of a registry key' {
                $valueName = ''
                $valueData = 'DefaultValue'

                # Create the new registry key value
                Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData

                # Verify that the registry key value has been created with the correct data and type
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)' -ValueData $valueData -ValueType 'String'
                $registryValueExists | Should Be $true
            }

            It 'Should remove a registry key value' {
                $valueName = 'TestValue'
                $valueData = 'TestData'
                $valueType = 'String'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                # Verify that the registry key value exists before removal
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName
                $registryValueExists | Should Be $true

                # Remove the registry value
                Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -Ensure 'Absent'

                # Verify that the registry key value has been removed
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName
                $registryValueExists | Should Be $false
            }

            It 'Should remove the default value for a registry key' {
                $valueName = ''
                $valueData = 'DefaultValue'
                $valueType = 'String'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName '(default)' -ValueData $valueData -ValueType $valueType

                # Verify that the registry key value exists before removal
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)'
                $registryValueExists | Should Be $true

                # Remove the registry value
                Set-TargetResource -Key $script:registryKeyPath -ValueName $valueName -Ensure 'Absent'

                # Verify that the registry key value has been removed
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName '(default)'
                $registryValueExists | Should Be $false
            }

            It 'Should create a new key and value with path containing forward slashes' {
                $registryKeyPathWithForwardSlashes = $script:registryKeyPath + '/Test/Key'
                $valueName = 'Testing'
                $valueData = 'TestValue'

                # Create the new registry key value
                Set-TargetResource -Key $registryKeyPathWithForwardSlashes -ValueName $valueName -ValueData $valueData

                # Verify that the registry key value has been created with the correct data and type
                $registryValueExists = Test-RegistryValueExists -KeyPath $registryKeyPathWithForwardSlashes -ValueName $valueName  -ValueData $valueData
                $registryValueExists | Should Be $true
            }

            # Test-TargetResource
            It 'Should return true for an existing registry key' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName ''
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return false for a registry key that does not exist' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentally'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName ''
                $testTargetResourceResult | Should Be $false
            }

            It 'Should return true for an existing registry value' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $valueName = 'path'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return false for a registry value that does not exist' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $valueName = 'NonExisting'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName
                $testTargetResourceResult | Should Be $false
            }

            It 'Should return true when Ensure is Absent and registry key does not exist' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environmentally'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName '' -Ensure 'Absent'
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return false when Ensure is Absent and registry key exists' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName '' -Ensure 'Absent'
                $testTargetResourceResult | Should Be $false
            }

            It 'Should return false when Ensure is Absent and registry value exists with invalid data' {
                $registryKeyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
                $valueName = 'path'
                $valueData = 'FakePath'

                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName -ValueData $valueData -Ensure 'Absent'
                $testTargetResourceResult | Should Be $false
            }

            It 'Should return true for a multi-string registry value' {
                $valueName = 'TestValue'
                $valueData = @('a', 'b', 'c')
                $valueType = 'MultiString'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                $testTargetResourceResult = Test-TargetResource -Key $registryKeyPath -ValueName $valueName -ValueData $valueData
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return true for a binary registry value' {
                $valueName = 'TestValue'
                $valueData = 'abcd123'
                $valueType = 'Binary'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                $testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return true for an empty binary registry value' {
                $valueName = 'TestValue'
                $valueData = ''
                $valueType = 'Binary'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                # Verify that the registry key value has been created with the correct data and type
                $registryValueExists = Test-RegistryValueExists -KeyPath $script:registryKeyPath -ValueName $valueName  -ValueData $valueData -ValueType $valueType
                $registryValueExists | Should Be $true

                $testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
                $testTargetResourceResult | Should Be $true
            }

            It 'Should return true for binary registry value with zeroes' {
                $valueName = 'TestValue'
                $valueData = 'abcd0123'
                $valueType = 'Binary'

                # Create the test registry value
                New-RegistryValue -KeyPath $script:registryKeyPath -ValueName $valueName -ValueData $valueData -ValueType $valueType

                $testTargetResourceResult = Test-TargetResource -Key $script:registryKeyPath -ValueName $valueName -ValueData $valueData
                $testTargetResourceResult | Should Be $true
            }
        }
    }
}
finally
{
    Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}