File size: 23,212 Bytes
8c763fb | 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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
try {
#skip all tests on non-windows platform
$defaultParamValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = !$IsWindows
Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") {
BeforeAll {
if ($IsWindows) {
$restoreLocation = Get-Location
$registryBase = "HKLM:\software\Microsoft\PowerShell\3\"
$parentKey = "TestKeyThatWillNotConflict"
$testKey = "TestKey"
$testKey2 = "TestKey2"
$testPropertyName = "TestEntry"
$testPropertyValue = 1
$defaultPropertyName = "(Default)"
$defaultPropertyValue = "something"
$otherPropertyValue = "other"
}
}
AfterAll {
if ($IsWindows) {
#restore the previous environment
Set-Location -Path $restoreLocation
}
}
BeforeEach {
if ($IsWindows) {
#create a parent key that can be easily removed and will not conflict
Set-Location $registryBase
New-Item -Path $parentKey > $null
#create the test keys/test properties for the tests to manipulate
Set-Location $parentKey
New-Item -Path $testKey > $null
New-Item -Path $testKey2 > $null
New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue > $null
}
}
AfterEach {
if ($IsWindows) {
Set-Location $registryBase
Remove-Item -Path $parentKey -Recurse -Force -ErrorAction SilentlyContinue
}
}
Context "Validate basic registry provider Cmdlets" {
It "Verify Test-Path" {
Test-Path -IsValid Registry::HKCU/Software | Should -BeTrue
Test-Path -IsValid Registry::foo/Softare | Should -BeFalse
}
It "Verify Get-Item" {
$item = Get-Item $testKey
$item.PSChildName | Should -BeExactly $testKey
}
It "Verify Get-Item on inaccessible path" {
{ Get-Item HKLM:\SAM\SAM -ErrorAction Stop } | Should -Throw -ErrorId "System.Security.SecurityException,Microsoft.PowerShell.Commands.GetItemCommand"
}
It "Verify Get-ChildItem" {
$items = Get-ChildItem
$items.Count | Should -BeExactly 2
$Items.PSChildName -contains $testKey | Should -BeTrue
$Items.PSChildName -contains $testKey2 | Should -BeTrue
}
It "Verify Get-ChildItem can get subkey names" {
$items = Get-ChildItem -Name
$items.Count | Should -BeExactly 2
$items -contains $testKey | Should -BeTrue
$items -contains $testKey2 | Should -BeTrue
}
It "Verify New-Item" {
$newKey = New-Item -Path "NewItemTest"
Test-Path "NewItemTest" | Should -BeTrue
Split-Path $newKey.Name -Leaf | Should -BeExactly "NewItemTest"
}
It "Verify Copy-Item" {
$copyKey = Copy-Item -Path $testKey -Destination "CopiedKey" -PassThru
Test-Path "CopiedKey" | Should -BeTrue
Split-Path $copyKey.Name -Leaf | Should -BeExactly "CopiedKey"
}
It "Verify Move-Item" {
$movedKey = Move-Item -Path $testKey -Destination "MovedKey" -PassThru
Test-Path "MovedKey" | Should -BeTrue
Split-Path $movedKey.Name -Leaf | Should -BeExactly "MovedKey"
}
It "Verify Rename-Item" {
$existBefore = Test-Path $testKey
$renamedKey = Rename-Item -Path $testKey -NewName "RenamedKey" -PassThru
$existAfter = Test-Path $testKey
$existBefore | Should -BeTrue
$existAfter | Should -BeFalse
Test-Path "RenamedKey" | Should -BeTrue
Split-Path $renamedKey.Name -Leaf | Should -BeExactly "RenamedKey"
}
}
Context "Valdiate basic registry property Cmdlets" {
It "Verify New-ItemProperty" {
New-ItemProperty -Path $testKey -Name "NewTestEntry" -Value 99 > $null
$property = Get-ItemProperty -Path $testKey -Name "NewTestEntry"
$property.NewTestEntry | Should -Be 99
$property.PSChildName | Should -BeExactly $testKey
}
It "Verify Set-ItemProperty" {
Set-ItemProperty -Path $testKey -Name $testPropertyName -Value 2
$property = Get-ItemProperty -Path $testKey -Name $testPropertyName
$property."$testPropertyName" | Should -Be 2
}
It "Verify Set-Item" {
Set-Item -Path $testKey -Value $defaultPropertyValue
$property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName
$property."$defaultPropertyName" | Should -BeExactly $defaultPropertyValue
}
It "Verify Set-Item with -WhatIf" {
Set-Item -Path $testKey -Value $defaultPropertyValue
Set-Item -Path $testKey -Value $otherPropertyValue -WhatIf
$property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName
$property."$defaultPropertyName" | Should -BeExactly $defaultPropertyValue
}
It "Verify Get-ItemPropertyValue" {
$propertyValue = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName
$propertyValue | Should -Be $testPropertyValue
}
It "Verify Copy-ItemProperty" {
Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property2."$testPropertyName" | Should -BeExactly $property1."$testPropertyName"
$property1.PSChildName | Should -BeExactly $testKey
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify Move-ItemProperty" {
Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property1 | Should -BeNullOrEmpty
$property2."$testPropertyName" | Should -BeExactly $testPropertyValue
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify Rename-ItemProperty" {
Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName "RenamedProperty"
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey -Name "RenamedProperty" -ErrorAction SilentlyContinue
$property1 | Should -BeNullOrEmpty
$property2.RenamedProperty | Should -BeExactly $testPropertyValue
$property2.PSChildName | Should -BeExactly $testKey
}
It "Verify Clear-ItemProperty" {
Clear-ItemProperty -Path $testKey -Name $testPropertyName
$property = Get-ItemProperty -Path $testKey -Name $testPropertyName
$property."$testPropertyName" | Should -Be 0
}
It "Verify Clear-Item" {
Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue
Set-Item -Path $testKey -Value $defaultPropertyValue
Clear-Item -Path $testKey
$key = Get-Item -Path $testKey
$key.Property.Length | Should -BeExactly 0
}
It "Verify Clear-Item with -WhatIf" {
Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue
Set-Item -Path $testKey -Value $defaultPropertyValue
Clear-Item -Path $testKey -WhatIf
$key = Get-Item -Path $testKey
$key.Property.Length | Should -BeExactly 2
}
It "Verify Remove-ItemProperty" {
Remove-ItemProperty -Path $testKey -Name $testPropertyName
$properties = @(Get-ItemProperty -Path $testKey)
$properties.Count | Should -Be 0
}
}
}
Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWindows") {
BeforeAll {
if ($IsWindows) {
$restoreLocation = Get-Location
$registryBase = "HKLM:\software\Microsoft\PowerShell\3\"
$parentKey = "TestKeyThatWillNotConflict"
$testKey = "TestKey"
$testKey2 = "TestKey2"
$testPropertyName = "TestEntry"
$testPropertyValue = 1
}
}
AfterAll {
if ($IsWindows) {
#restore the previous environment
Set-Location -Path $restoreLocation
}
}
BeforeEach {
if ($IsWindows) {
#create a parent key that can be easily removed and will not conflict
Set-Location $registryBase
New-Item -Path $parentKey > $null
#create the test keys/test properties for the tests to manipulate
Set-Location $parentKey
New-Item -Path $testKey > $null
New-Item -Path $testKey2 > $null
New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue > $null
New-ItemProperty -Path $testKey2 -Name $testPropertyName -Value $testPropertyValue > $null
}
}
AfterEach {
if ($IsWindows) {
Set-Location $registryBase
Remove-Item -Path $parentKey -Recurse -Force -ErrorAction SilentlyContinue
}
}
Context "Valdiate New-ItemProperty Parameters" {
BeforeEach {
#remove the current properties so that we can remake them with different parameters
Remove-ItemProperty -Path $testKey -Name $testPropertyName -Force -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue
}
It "Verify Filter" {
{ $result = New-ItemProperty -Path ".\*" -Filter "Test*" -Name $testPropertyName -Value $testPropertyValue -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.NewItemPropertyCommand"
}
It "Verify Include" {
$result = New-ItemProperty -Path ".\*" -Include "*2" -Name $testPropertyName -Value $testPropertyValue
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey2
}
It "Verify Exclude" {
$result = New-ItemProperty -Path ".\*" -Exclude "*2" -Name $testPropertyName -Value $testPropertyValue
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey
}
It "Verify Confirm can be bypassed" {
$result = New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue -Force -Confirm:$false
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey
}
It "Verify WhatIf" {
$result = New-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue -WhatIf
$result | Should -BeNullOrEmpty
}
}
Context "Valdiate Get-ItemProperty Parameters" {
It "Verify Name" {
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey
}
It "Verify Path but no Name" {
$result = Get-ItemProperty -Path $testKey
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey
}
It "Verify Filter" {
{ $result = Get-ItemProperty -Path ".\*" -Filter "*Test*" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyCommand"
}
It "Verify Include" {
$result = Get-ItemProperty -Path ".\*" -Include "*2"
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey2
}
It "Verify Exclude" {
$result = Get-ItemProperty -Path ".\*" -Exclude "*2"
$result."$testPropertyName" | Should -Be $testPropertyValue
$result.PSChildName | Should -BeExactly $testKey
}
}
Context "Valdiate Get-ItemPropertyValue Parameters" {
It "Verify Name" {
$result = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName
$result | Should -Be $testPropertyValue
}
}
Context "Valdiate Set-ItemPropertyValue Parameters" {
BeforeAll {
$newPropertyValue = 2
}
It "Verify Name" {
Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $newPropertyValue
}
It "Verify PassThru" {
$result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru
$result."$testPropertyName" | Should -Be $newPropertyValue
}
It "Verify Piped Default Parameter" {
$prop = Get-ItemProperty -Path $testKey -Name $testPropertyName
$prop | Set-ItemProperty -Name $testPropertyName -Value $newPropertyValue
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $newPropertyValue
}
It "Verify WhatIf" {
$result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru -WhatIf
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $testPropertyValue
}
It "Verify Confirm can be bypassed" {
$result = Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $newPropertyValue -PassThru -Confirm:$false
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $newPropertyValue
}
}
Context "Valdiate Copy-ItemProperty Parameters" {
BeforeEach {
#remove the current property so that we have a place to copy to
Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue
}
It "Verify PassThru" {
#passthru returns the property on testKey not testKey2
$property1 = Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -PassThru
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property2."$testPropertyName" | Should -Be $property1."$testPropertyName"
$property1.PSChildName | Should -BeExactly $testKey
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify Confirm can be bypassed" {
Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -Confirm:$false
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property2."$testPropertyName" | Should -Be $property1."$testPropertyName"
$property1.PSChildName | Should -BeExactly $testKey
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify WhatIf" {
Copy-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -WhatIf
{ Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction Stop } | Should -Throw -ErrorId "System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand"
}
}
Context "Valdiate Move-ItemProperty Parameters" {
BeforeEach {
#remove the current property so that we have a place to move to
Remove-ItemProperty -Path $testKey2 -Name $testPropertyName -Force -ErrorAction SilentlyContinue
}
It "Verify PassThru" {
$property2 = Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -PassThru
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property1 | Should -BeNullOrEmpty
$property2."$testPropertyName" | Should -Be $testPropertyValue
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify Confirm can be bypassed" {
Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -Confirm:$false
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property1 | Should -BeNullOrEmpty
$property2."$testPropertyName" | Should -Be $testPropertyValue
$property2.PSChildName | Should -BeExactly $testKey2
}
It "Verify WhatIf" {
Move-ItemProperty -Path $testKey -Name $testPropertyName -Destination $testKey2 -WhatIf
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey2 -Name $testPropertyName -ErrorAction SilentlyContinue
$property1."$testPropertyName" | Should -Be $testPropertyValue
$property1.PSChildName | Should -BeExactly $testKey
$property2 | Should -BeNullOrEmpty
}
}
Context "Valdiate Rename-ItemProperty Parameters" {
BeforeAll {
$newPropertyName = "NewEntry"
}
It "Verify Confirm can be bypassed" {
Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName $newPropertyName -Confirm:$false
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey -Name $newPropertyName -ErrorAction SilentlyContinue
$property1 | Should -BeNullOrEmpty
$property2."$newPropertyName" | Should -Be $testPropertyValue
}
It "Verify WhatIf" {
Rename-ItemProperty -Path $testKey -Name $testPropertyName -NewName $newPropertyName -WhatIf
$property1 = Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction SilentlyContinue
$property2 = Get-ItemProperty -Path $testKey -Name $newPropertyName -ErrorAction SilentlyContinue
$property1."$testPropertyName" | Should -Be $testPropertyValue
$property2 | Should -BeNullOrEmpty
}
}
Context "Valdiate Clear-ItemProperty Parameters" {
It "Verify Confirm can be bypassed" {
Clear-ItemProperty -Path $testKey -Name $testPropertyName -Confirm:$false
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be 0
}
It "Verify WhatIf" {
Clear-ItemProperty -Path $testKey -Name $testPropertyName -WhatIf
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $testPropertyValue
}
}
Context "Valdiate Remove-ItemProperty Parameters" {
It "Verify Confirm can be bypassed" {
Remove-ItemProperty -Path $testKey -Name $testPropertyName -Confirm:$false
{ Get-ItemProperty -Path $testKey -Name $testPropertyName -ErrorAction Stop } | Should -Throw -ErrorId "System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand"
}
It "Verify WhatIf" {
Remove-ItemProperty -Path $testKey -Name $testPropertyName -WhatIf
$result = Get-ItemProperty -Path $testKey -Name $testPropertyName
$result."$testPropertyName" | Should -Be $testPropertyValue
}
}
Context "Validate -LiteralPath" {
It "Verify New-Item and Remove-Item work with asterisk" {
try {
$tempPath = "HKCU:\_tmp"
$testPath = "$tempPath\*\sub"
$null = New-Item -Force $testPath
$testPath | Should -Exist
Remove-Item -LiteralPath $testPath
$testPath | Should -Not -Exist
}
finally {
Remove-Item -Recurse $tempPath -ErrorAction SilentlyContinue
}
}
}
Context "Validate Get-ItemProperty Cast Exception" {
BeforeAll {
if ($IsWindows) {
$registrySubkeyPath = 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'
# Below will import .reg file with 64 bit integer in 32 bit DWORD
$badRegistryContent = @"
Windows Registry Editor Version 5.00
[$registrySubkeyPath]
"NoModify"=hex(4):01,00,00,00,00,00,00,00
"@
$badRegistryPath = Join-Path -Path $TestDrive -ChildPath badreg.reg
$badRegistryContent | Set-Content -Path $badRegistryPath
reg.exe import $badRegistryPath
$registryProviderSubkeyPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg'
}
}
It "Validate non-terminating error for cast" {
Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorVariable err -ErrorAction SilentlyContinue
$err | Should -HaveCount 1
$err[0].Exception | Should -BeOfType [System.InvalidCastException]
$err[0].TargetObject | Should -BeExactly $registrySubkeyPath
$err[0].CategoryInfo.Category | Should -BeExactly 'ReadError'
$err[0].FullyQualifiedErrorId | Should -BeExactly 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'
}
It "Validate terminating error for cast" {
{ Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorAction Stop } | Should -Throw -ErrorId 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand'
}
AfterAll {
if ($IsWindows) {
reg.exe delete $registrySubkeyPath /f
}
}
}
}
} finally {
$global:PSdefaultParameterValues = $defaultParamValues
}
|