MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# This is a Pester test suite to validate the New-FileCatalog & Test-FileCatalog cmdlets on PowerShell.5 6try {7 #skip all tests on non-windows platform8 $defaultParamValues = $PSDefaultParameterValues.Clone()9 $PSDefaultParameterValues["it:skip"] = !$IsWindows10 11$script:catalogPath = ""12 13Describe "Test suite for NewFileCatalogAndTestFileCatalogCmdlets" -Tags "CI" {14 15 #compare two hashtables16 function CompareHashTables17 {18 param19 (20 $hashTable1,21 $hashTable222 )23 24 foreach ($key in $hashTable1.keys)25 {26 $keyValue1 = $hashTable1["$key"]27 if($hashTable2.ContainsKey($key))28 {29 $keyValue2 = $hashTable2["$key"]30 $keyValue1 | Should -Be $keyValue231 }32 else33 {34 throw "Failed to find the file $keyValue1 for $key in Hashtable"35 }36 }37 }38 39 BeforeAll {40 $testDataPath = "$PSScriptRoot\TestData\CatalogTestData"41 }42 43 Context "NewAndTestCatalogTests PositiveTestCases when validation Succeeds" {44 45 It "NewFileCatalogWithSingleFile with WhatIf" {46 47 $sourcePath = Join-Path $testDataPath '\CatalogTestFile1.mof'48 # use existent Path for the directory when .cat file name is not specified49 $catalogPath = $testDataPath50 $catalogFile = $catalogPath + "\catalog.cat"51 52 try53 {54 $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -WhatIf55 $result = Test-Path -Path $catalogFile56 }57 finally58 {59 Remove-Item $catalogFile -Force -ErrorAction SilentlyContinue60 }61 62 # Validate result properties63 $result | Should -BeFalse64 }65 66 It "NewFileCatalogFolder" {67 68 $sourcePath = Join-Path $testDataPath 'UserConfigProv\DSCResources\scriptdsc'69 $catalogPath = "$testDataPath\NewFileCatalogFolder.cat"70 71 try72 {73 $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -CatalogVersion 174 $result = Test-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -Detailed75 }76 finally77 {78 Remove-Item $catalogPath -Force -ErrorAction SilentlyContinue79 }80 81 # Validate result properties82 $result.Status | Should -Be "Valid"83 $result.Signature.Status | Should -Be "NotSigned"84 $result.HashAlgorithm | Should -Be "SHA1"85 }86 87 It "NewFileCatalogFolderWithSubFolders" {88 89 $sourcePath = Join-Path $testDataPath 'UserConfigProv'90 # use non existent Path for the directory when .cat file name is specified91 $catalogPath = "$testDataPath\OutPutCatalog\NewFileCatalogFolderWithSubFolders.cat"92 93 try94 {95 $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath96 $result = Test-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -Detailed97 }98 finally99 {100 Remove-Item "$sourcePath\OutPutCatalog" -Force -ErrorAction SilentlyContinue -Recurse101 }102 103 # Validate result properties104 $result.Status | Should -Be "Valid"105 $result.Signature.Status | Should -Be "NotSigned"106 $result.HashAlgorithm | Should -Be "SHA256"107 }108 109 It "NewFileCatalogWithSingleFile" {110 111 $sourcePath = Join-Path $testDataPath '\CatalogTestFile1.mof'112 # use existent Path for the directory when .cat file name is not specified113 $catalogPath = $testDataPath114 try115 {116 $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath117 $result = Test-FileCatalog -Path $sourcePath -CatalogFilePath ($catalogPath + "\catalog.cat")118 }119 finally120 {121 Remove-Item "$catalogPath\catalog.cat" -Force -ErrorAction SilentlyContinue122 }123 124 # Validate result properties125 $result | Should -Be "Valid"126 }127 128 It "NewFileCatalogForFilesThatDoNotSupportEmbeddedSignatures" {129 130 $expectedPathsAndHashes = @{ "TestImage.gif" = "B0E4B9F0BB21284AA0AF0D525C913420AD73DA6A" ;131 "TestFileCatalog.txt" = "BA6A26C5F19AB50B0D5BE2A9D445B259998B0DD9" }132 133 # use non existent Path for the directory when .cat file name is not specified134 $catalogPath = "$testDataPath\OutPutCatalog"135 136 try137 {138 $null = New-FileCatalog -Path "$testDataPath\TestImage.gif","$testDataPath\TestFileCatalog.txt" -CatalogFilePath $catalogPath -CatalogVersion 1139 $result = Test-FileCatalog -Path "$testDataPath\TestImage.gif","$testDataPath\TestFileCatalog.txt" -CatalogFilePath ($catalogPath + "\catalog.cat") -Detailed140 }141 finally142 {143 Remove-Item "$catalogPath" -Force -ErrorAction SilentlyContinue -Recurse144 }145 146 $result.Status | Should -Be "Valid"147 $result.CatalogItems.Count | Should -Be 2148 $result.PathItems.Count | Should -Be 2149 CompareHashTables $result.CatalogItems $result.PathItems150 CompareHashTables $result.CatalogItems $expectedPathsAndHashes151 }152 153 It "NewFileCatalogWithMultipleFoldersAndFiles" -Pending {154 155 $expectedPathsAndHashes = @{156 "UserConfigProv.psd1" = "748E5486814051DA3DFB79FE8964152727213248" ;157 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.schema.mof" ="F7CAB050E32CF0C9B2AC2807C4F24D31EFCC8B61";158 "dscresources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.psm1" = "F9DD6B02C7BD0FB98A25BE0D41210B2A2333E139";159 "DSCResources\scriptdsc\scriptDSC.schema.psm1"= "CDBAF85FEDE2E0CD09B1AEA0532010CEFCECBC12";160 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1" = "7599777B85B60377B1F3E492C817190090A754A7"161 "DSCResources\scriptdsc\scriptdsc.psd1"= "CDDC68AF9B863760A14031772DC9ADDAFD209D80";162 "DSCResources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.schema.mof" ="AFEB46104F506FC64CAB4B0B2A9C6C50622B487A";163 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.psm1"= "60CB9C8AEDA7A64127D34361ED4F30DEAFE37022";164 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.schema.mof" = "E33FBFEA28E9A8FBA793FBC3D8015BCC9A10944B";165 "CatalogTestFile1.mof" = "083B0953D0D70FFF62710F0356FEB86BCE327FE7";166 "CatalogTestFile2.xml" = "E73BB7A0DD9FAC6A8182F67B750D9CA3094490F1" }167 168 $catalogPath = "$env:TEMP\NewFileCatalogWithMultipleFoldersAndFiles.cat"169 $catalogDataPath = @("$testDataPath\UserConfigProv\","$testDataPath\CatalogTestFile1.mof","$testDataPath\CatalogTestFile2.xml")170 171 try172 {173 $null =New-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -CatalogVersion 1174 $result = Test-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -Detailed175 }176 finally177 {178 Remove-Item "$catalogPath" -Force -ErrorAction SilentlyContinue179 }180 181 $result.Status | Should -Be "Valid"182 $result.Signature.Status | Should -Be "NotSigned"183 $result.HashAlgorithm | Should -Be "SHA1"184 $result.CatalogItems.Count | Should -Be 11185 $result.PathItems.Count | Should -Be 11186 187 CompareHashTables $result.CatalogItems $result.PathItems188 CompareHashTables $result.CatalogItems $expectedPathsAndHashes189 }190 191 It "NewFileCatalogVersion2WithMultipleFoldersAndFiles" -Pending {192 193 $expectedPathsAndHashes = @{194 "UserConfigProv.psd1" = "9FFE4CA2873CD91CDC9D71362526446ECACDA64D26DEA768E6CE489B84D888E4" ;195 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.schema.mof" ="517F625CB6C465928586F5C613F768B33C20F477DAF843C179071B8C74B992AA";196 "DSCResources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.psm1" = "0774A539E73B1A480E38CFFE2CF0B8AC46120A0B2E0377E0DE2630031BE83347";197 "DSCResources\scriptdsc\scriptdsc.schema.psm1"= "7DE80DED0F96FA7D34CF34089A1B088E91CD7B1D80251949FC7C78A6308D51C3";198 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1" = "EB0310C630EDFDFBDD1D993A636EC9B75BB1F04DF7E7FFE39CF6357679C852C7"199 "DSCResources\scriptdsc\scriptdsc.psd1"= "AB8E8D0840D4854CDCDE25058872413AF417FC016BD77FD5EC677BBB7393532B";200 "DSCResources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.schema.mof" ="7163E607F067A3C4F91D3AFF3C466ECA47C0CF84B5F0DDA22B2C0E99929B5E21";201 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.psm1"= "6591FE02528D7FB66F00E09D7F1A025D5D5BAF30A49C5FF1EC562FAE39B38F43";202 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.schema.mof" = "679318201B012CC5936B29C095956B2131FAF828C0CCA4342A5914F721480FB9";203 "CatalogTestFile1.mof" = "7C1885AE5F76F58DAA232A5E962875F90308C3CB8580400EE12F999B4E10F940";204 "CatalogTestFile2.xml" = "00B7DA28CD285F796660D36B77B2EC6054F21A44D5B329EB6BC4EC7687D70B13";205 "TestImage.gif" = "2D938D255D0D6D547747BD21447CF7295318D34D9B4105D04C1C27487D2FF402" }206 207 $catalogPath = "$env:TEMP\NewFileCatalogVersion2WithMultipleFoldersAndFiles.cat"208 $catalogDataPath = @("$testDataPath\UserConfigProv\","$testDataPath\CatalogTestFile1.mof","$testDataPath\CatalogTestFile2.xml", "$testDataPath\TestImage.gif")209 210 try211 {212 $null = New-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -CatalogVersion 2213 $result = Test-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -Detailed214 }215 finally216 {217 Remove-Item "$catalogPath" -Force -ErrorAction SilentlyContinue218 }219 220 $result.Status | Should -Be "Valid"221 $result.Signature.Status | Should -Be "NotSigned"222 $result.HashAlgorithm | Should -Be "SHA256"223 $result.CatalogItems.Count | Should -Be 12224 $result.PathItems.Count | Should -Be 12225 CompareHashTables $result.CatalogItems $result.PathItems226 CompareHashTables $result.CatalogItems $expectedPathsAndHashes227 }228 229 # This is failing saying the exact thing that it says is supposed to work does not230 It "Test-FileCatalog should pass when catalog is in the same folder as files being tested" -Pending {231 232 $catalogPath = "$env:TEMP\UserConfigProv\catalog.cat"233 try234 {235 Copy-Item "$testDataPath\UserConfigProv" $env:temp -Recurse -ErrorAction SilentlyContinue236 Push-Location "$env:TEMP\UserConfigProv"237 # When -Path is not specified, it should use current directory238 $null = New-FileCatalog -CatalogFilePath $catalogPath -CatalogVersion 1239 $result = Test-FileCatalog -CatalogFilePath $catalogPath240 241 if($result -ne 'Valid')242 {243 # We will fail, Write why.244 $detailResult = Test-FileCatalog -CatalogFilePath $catalogPath -Detailed245 $detailResult | ConvertTo-Json | Write-Verbose -Verbose246 }247 }248 finally249 {250 Pop-Location251 Remove-Item "$catalogPath" -Force -ErrorAction SilentlyContinue252 Remove-Item "$env:temp\UserConfigProv\" -Force -ErrorAction SilentlyContinue -Recurse253 }254 255 $result | Should -Be "Valid"256 }257 258 It "NewFileCatalogWithUnicodeCharactersInFileNames" -Pending {259 260 $expectedPathsAndHashes = @{261 "UserConfigProv.psd1" = "9FFE4CA2873CD91CDC9D71362526446ECACDA64D26DEA768E6CE489B84D888E4" ;262 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.schema.mof" ="517F625CB6C465928586F5C613F768B33C20F477DAF843C179071B8C74B992AA";263 "DSCResources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.psm1" = "0774A539E73B1A480E38CFFE2CF0B8AC46120A0B2E0377E0DE2630031BE83347";264 "DSCResources\scriptdsc\scriptdsc.schema.psm1"= "7DE80DED0F96FA7D34CF34089A1B088E91CD7B1D80251949FC7C78A6308D51C3";265 "DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1" = "EB0310C630EDFDFBDD1D993A636EC9B75BB1F04DF7E7FFE39CF6357679C852C7"266 "DSCResources\scriptdsc\scriptdsc.psd1"= "AB8E8D0840D4854CDCDE25058872413AF417FC016BD77FD5EC677BBB7393532B";267 "DSCResources\UserConfigProviderModVersion3\UserConfigProviderModVersion3.schema.mof" ="7163E607F067A3C4F91D3AFF3C466ECA47C0CF84B5F0DDA22B2C0E99929B5E21";268 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.psm1"= "6591FE02528D7FB66F00E09D7F1A025D5D5BAF30A49C5FF1EC562FAE39B38F43";269 "DSCResources\UserConfigProviderModVersion2\UserConfigProviderModVersion2.schema.mof" = "679318201B012CC5936B29C095956B2131FAF828C0CCA4342A5914F721480FB9";270 "ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ.txt" = "EFD0AE8FF12C7387D51FFC03259B60E06DA012BF7D3B7B9D3480FAB2864846CE";271 "ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ.txt" = "9FB57660EDD8DA898A9F1E7F5A36B8B760B4A21625F9968D87A32A55B3546BF9"}272 273 # Create Test Files with unicode characters in names and content274 $unicodeTempDir = Join-Path -Path $testDataPath -ChildPath "UnicodeTestDir"275 $null = New-Item -ItemType Directory -Path $unicodeTempDir -Force276 277 $null = New-Item -ItemType File -Path "$unicodeTempDir\ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ.txt" -Force -ErrorAction SilentlyContinue278 $null = Add-Content -Path "$unicodeTempDir\ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ.txt" -Value "Testing unicode"279 $null = Out-File -FilePath "$unicodeTempDir\ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ.txt" -Encoding unicode -InputObject "ɗ ɘ ə ɚ ɛ ɜ ɝ ɞ ɟ ɠ ɡ ɢ ɣ ɤ ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ ɭ ɮ ɯ ɰ ɱ ɲ ɳ ɴ ɵ ɶ ɷ ɸ ɹ ɺ ɻ ɼ ɽ ɾ ɿ ʀ ʁ ʂ ʃ ʄ ʅ" -Append280 $null = New-Item -ItemType File -Path "$unicodeTempDir\ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ.txt" -Force -ErrorAction SilentlyContinue281 $null = Out-File -FilePath "$unicodeTempDir\ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ.txt" -Encoding unicode -InputObject "ਅ ਆ ਇ ਈ ਉ ਊ ਏ ਐ ਓ ਔ ਕ ਖ ਗ ਘ ਙ ਚ ਛ ਜ ਝ ਞ ਟ ਠ ਡ ਢ ਣ ਤ ਥ ਦ ਧ ਨ ਪ ਫ ਬ ਭ ਮ ਯ ਰ ਲ ਲ਼ ਵ " -Append282 $null = Out-File -FilePath "$unicodeTempDir\ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ.txt" -Encoding unicode -InputObject "அ ஆ இ ஈ உ ஊ எ ஏ ஐ ஒ ஓ ஔ க ங ச ஜ ஞ ட ண த ந ன ப ம ய ர ற ல ள ழ வ ஷ ஸ ஹ " -Append283 284 $catalogPath = "$env:TEMP\క ఖ గ ఘ ఙ చ ఛ జ ఝ ఞ.cat"285 $catalogDataPath = @("$testDataPath\UserConfigProv\", "$unicodeTempDir\ٿ ڀ ځ ڂ ڃ ڄ څ چ ڇ ڈ ډ ڊ ڋ ڌ ڍ ڎ ڏ ڐ ڑ.txt" ,"$unicodeTempDir\ɥ ɦ ɧ ɨ ɩ ɪ ɫ ɬ.txt")286 287 try288 {289 $null = New-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -CatalogVersion 2290 $result = Test-FileCatalog -Path $catalogDataPath -CatalogFilePath $catalogPath -Detailed291 }292 finally293 {294 Remove-Item $unicodeTempDir -Recurse -Force -ErrorAction SilentlyContinue295 Remove-Item "$catalogPath" -Force -ErrorAction SilentlyContinue296 }297 298 $result.Status | Should -Be "Valid"299 $result.Signature.Status | Should -Be "NotSigned"300 $result.HashAlgorithm | Should -Be "SHA256"301 $result.CatalogItems.Count | Should -Be 11302 $result.PathItems.Count | Should -Be 11303 CompareHashTables $result.CatalogItems $result.PathItems304 CompareHashTables $result.CatalogItems $expectedPathsAndHashes305 }306 }307 308 Context "NewAndTestCatalogTests NegativeTestCases when creation or validation Fails"{309 310 AfterEach {311 Remove-Item "$script:catalogPath" -Force -ErrorAction SilentlyContinue312 Remove-Item "$env:temp\UserConfigProv" -Force -Recurse -ErrorAction SilentlyContinue313 }314 315 It "TestCatalogWhenNewFileAddedtoFolderBeforeValidation" {316 317 $script:catalogPath = "$env:TEMP\TestCatalogWhenNewFileAddedtoFolderBeforeValidation.cat"318 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 2319 $null = Copy-Item $testDataPath\UserConfigProv $env:temp -Recurse -ErrorAction SilentlyContinue320 $null = New-Item $env:temp\UserConfigProv\DSCResources\NewFile.txt -ItemType File321 Add-Content $env:temp\UserConfigProv\DSCResources\NewFile.txt -Value "More Data" -Force322 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed323 324 $result.Status | Should -Be "ValidationFailed"325 $result.CatalogItems.Count | Should -Be 9326 $result.PathItems.Count | Should -Be 10327 $result.CatalogItems.ContainsKey("DSCResources\NewFile.txt") | Should -BeFalse328 $result.PathItems.ContainsKey("DSCResources\NewFile.txt") | Should -BeTrue329 330 # By Skipping the new added file validation will pass331 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed -FilesToSkip "NewFile.txt"332 $result.Status | Should -Be "Valid"333 }334 335 It "TestCatalogWhenNewFileDeletedFromFolderBeforeValidation" {336 337 $script:catalogPath = "$env:TEMP\TestCatalogWhenNewFileDeletedFromFolderBeforeValidation.cat"338 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 1339 $null = Copy-Item $testDataPath\UserConfigProv $env:temp -Recurse -ErrorAction SilentlyContinue340 del $env:temp\UserConfigProv\DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1 -Force -ErrorAction SilentlyContinue341 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed342 343 $result.Status | Should -Be "ValidationFailed"344 $result.CatalogItems.Count | Should -Be 9345 $result.PathItems.Count | Should -Be 8346 $result.CatalogItems.ContainsKey("DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1") | Should -BeTrue347 $result.PathItems.ContainsKey("DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1") | Should -BeFalse348 349 # By Skipping the deleted file validation will pass350 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed -FilesToSkip "UserConfigProviderModVersion1.psm1"351 $result.Status | Should -Be "Valid"352 }353 354 It "TestCatalogWhenFileContentModifiedBeforeValidation" {355 356 $script:catalogPath = "$env:TEMP\TestCatalogWhenFileContentModifiedBeforeValidation.cat"357 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 1358 $null = Copy-Item $testDataPath\UserConfigProv $env:temp -Recurse -ErrorAction SilentlyContinue359 Add-Content $env:temp\UserConfigProv\DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1 -Value "More Data" -Force360 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed361 362 $result.Status | Should -Be "ValidationFailed"363 $result.CatalogItems.Count | Should -Be 9364 $result.PathItems.Count | Should -Be 9365 $catalogHashValue = $result.CatalogItems["DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1"]366 $pathHashValue = $result.PathItems["DSCResources\UserConfigProviderModVersion1\UserConfigProviderModVersion1.psm1"]367 ($catalogHashValue -eq $pathHashValue) | Should -BeFalse368 369 # By Skipping the file with modifed contents validation will pass370 $result = Test-FileCatalog -Path $env:temp\UserConfigProv -CatalogFilePath $script:catalogPath -Detailed -FilesToSkip "UserConfigProviderModVersion1.psm1"371 $result.Status | Should -Be "Valid"372 }373 }374 375 Context "TestCatalog Skip Validation Tests"{376 377 AfterEach {378 Remove-Item "$script:catalogPath" -Force -ErrorAction SilentlyContinue379 }380 381 It "TestCatalogSkipSingleFileDuringValidation" {382 383 $script:catalogPath = "$env:TEMP\TestCatalogSkipSingleFileDuringValidation.cat"384 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 2385 $result = Test-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -FilesToSkip "scriptdsc.schema"386 $result | Should -Be "Valid"387 }388 389 It "TestCatalogSkipCertainFileTypeDuringValidation" {390 391 $script:catalogPath = "$env:TEMP\TestCatalogSkipCertainFileTypeDuringValidation.cat"392 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 2393 $result = Test-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -FilesToSkip "*.mof"394 $result | Should -Be "Valid"395 }396 397 It "TestCatalogSkipWildCardPatternDuringValidation" {398 399 $script:catalogPath = "$env:TEMP\TestCatalogSkipWildCardPatternDuringValidation.cat"400 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 1401 $result = Test-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -FilesToSkip "UserConfigProvider*.psm1"402 $result | Should -Be "Valid"403 }404 405 It "TestCatalogSkipMultiplePattensDuringValidation" {406 407 $script:catalogPath = "$env:TEMP\TestCatalogSkipMultiplePattensDuringValidation.cat"408 $null = New-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -CatalogVersion 1409 $result = Test-FileCatalog -Path $testDataPath\UserConfigProv\ -CatalogFilePath $script:catalogPath -FilesToSkip "*.psd1","UserConfigProviderModVersion2.psm1","*ModVersion1.schema.mof"410 $result | Should -Be "Valid"411 }412 413 It "New-FileCatalog -WhatIf does not create file" {414 $catalogPath = Join-Path "TestDrive:" "TestCatalogWhatIfForNewFileCatalog.cat"415 New-FileCatalog -CatalogFilePath $catalogPath -WhatIf416 $catalogPath | Should -Not -Exist417 }418 }419 420 Context "TestCatalog Executing File Validation Tests"{421 422 AfterEach {423 Remove-Item "$script:catalogPath" -Force -ErrorAction SilentlyContinue424 }425 426 It "Test-FileCatalog should pass when target file is an executing process" {427 $processFolder = (Get-Process -Id $pid).Path428 $script:catalogPath = "$env:TEMP\TestCatalogExecutingFileValidation.cat"429 $null = New-FileCatalog -Path "$processFolder" -CatalogFilePath $script:catalogPath -CatalogVersion 2430 $result = Test-FileCatalog -Path "$processFolder" -CatalogFilePath $script:catalogPath431 $result | Should -Be "Valid"432 }433 }434 435 Context "TestCatalog File Open Validation Tests" {436 437 BeforeEach {438 $null = New-Item -ItemType Directory -Path "$env:TEMP\testCatalog" -Force -ErrorAction SilentlyContinue439 Set-Content -PassThru "$env:TEMP\testCatalog\test.txt" -Value "Test Data" | Out-Null440 }441 442 AfterEach {443 Remove-Item "$script:catalogPath" -Force -ErrorAction SilentlyContinue444 Remove-Item "$env:TEMP\testCatalog" -Recurse -Force -ErrorAction SilentlyContinue445 }446 447 it "Test-FileCatalog should pass when target file has an open reader with FileMode Open FileAccess Read and FileShare <name>" -TestCases @(448 @{ Name = "Read"; fileShareParameter = [System.IO.FileShare]::Read }449 @{ Name = "ReadWrite"; fileShareParameter = [System.IO.FileShare]::ReadWrite }450 ) {451 param($name, [System.IO.FileShare] $fileShareParameter)452 453 $script:catalogPath = "$env:TEMP\TestCatalogFileOpenValidation.cat"454 $null = New-FileCatalog -Path "$env:TEMP\testCatalog\" -CatalogFilePath $script:catalogPath -CatalogVersion 2455 $fileStream = [System.IO.File]::Open("$env:TEMP\testCatalog\test.txt", [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, $fileShareParameter)456 try {457 $result = Test-FileCatalog -Path "$env:TEMP\testCatalog\" -CatalogFilePath $script:catalogPath458 $result | Should -Be "Valid"459 } finally {460 $fileStream.Close()461 $fileStream.Dispose()462 }463 }464 }465}466 467} finally {468 $global:PSdefaultParameterValues = $defaultParamValues469}470 