MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]5param()6 7Describe "CliXml test" -Tags "CI" {8 9 BeforeAll {10 $testFilePath = Join-Path "testdrive:\" "testCliXml"11 $subFilePath = Join-Path $testFilePath ".test"12 13 if(Test-Path $testFilePath)14 {15 Remove-Item $testFilePath -Force -Recurse16 }17 18 # Create the test File and push the location into specified path19 New-Item -Path $testFilePath -ItemType Directory | Out-Null20 New-Item -Path $subFilePath -ItemType Directory | Out-Null21 Push-Location $testFilePath22 23 class TestData24 {25 [string] $testName26 [object] $inputObject27 [string] $expectedError28 [string] $testFile29 30 TestData($name, $file, $inputObj, $errorId)31 {32 $this.testName = $name33 $this.inputObject = $inputObj34 $this.expectedError = $errorId35 $this.testFile = $file36 }37 }38 }39 40 AfterAll {41 Pop-Location42 }43 44 Context "Export-CliXml" {45 BeforeAll {46 $gpsList = Get-Process pwsh47 $gps = $gpsList | Select-Object -First 148 $filePath = Join-Path $subFilePath 'gps.xml'49 50 $testData = @()51 $testData += [TestData]::new("with path as Null", [NullString]::Value, $gps, "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportClixmlCommand")52 $testData += [TestData]::new("with path as Empty string", "", $gps, "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ExportClixmlCommand")53 $testData += [TestData]::new("with path as non filesystem provider", "env:\", $gps, "ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportClixmlCommand")54 }55 56 AfterEach {57 Remove-Item $filePath -Force -ErrorAction SilentlyContinue58 }59 60 $testData | ForEach-Object {61 62 It "$($_.testName)" {63 $test = $_64 { Export-Clixml -Depth 1 -LiteralPath $test.testFile -InputObject $test.inputObject -Force } | Should -Throw -ErrorId $test.expectedError65 }66 }67 68 It "can be created with literal path" {69 70 $filePath = Join-Path $subFilePath 'gps.xml'71 Export-Clixml -Depth 1 -LiteralPath $filePath -InputObject ($gpsList | Select-Object -First 1)72 73 $filePath | Should -Exist74 75 $fileContent = Get-Content $filePath76 $isExisted = $false77 78 foreach($item in $fileContent)79 {80 foreach($gpsItem in $gpsList)81 {82 $checkId = $gpsItem.Id83 if (($null -ne $(Select-String -InputObject $item -SimpleMatch $checkId)) -and ($null -ne $(Select-String -InputObject $item -SimpleMatch "Id")))84 {85 $isExisted = $true86 break;87 }88 }89 }90 91 $isExisted | Should -BeTrue92 }93 94 It "can be created with literal path using pipeline" {95 96 $filePath = Join-Path $subFilePath 'gps.xml'97 ($gpsList | Select-Object -First 1) | Export-Clixml -Depth 1 -LiteralPath $filePath98 99 $filePath | Should -Exist100 101 $fileContent = Get-Content $filePath102 $isExisted = $false103 104 foreach($item in $fileContent)105 {106 foreach($gpsItem in $gpsList)107 {108 $checkId = $gpsItem.Id109 if (($null -ne $(Select-String -InputObject $item -SimpleMatch $checkId)) -and ($null -ne $(Select-String -InputObject $item -SimpleMatch "Id")))110 {111 $isExisted = $true112 break;113 }114 }115 }116 117 $isExisted | Should -BeTrue118 }119 }120 121 Context "Import-CliXML" {122 BeforeAll {123 $gpsList = Get-Process pwsh124 $gps = $gpsList | Select-Object -First 1125 $filePath = Join-Path $subFilePath 'gps.xml'126 127 $testData = @()128 $testData += [TestData]::new("with path as Null", [NullString]::Value, $null, "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ImportClixmlCommand")129 $testData += [TestData]::new("with path as Empty string", "", $null, "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ImportClixmlCommand")130 $testData += [TestData]::new("with path as non filesystem provider", "env:\", $null, "ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ImportClixmlCommand")131 }132 133 $testData | ForEach-Object {134 135 It "$($_.testName)" {136 $test = $_137 138 { Import-Clixml -LiteralPath $test.testFile } | Should -Throw -ErrorId $test.expectedError139 }140 }141 142 It "can import from a literal path" {143 Export-Clixml -Depth 1 -LiteralPath $filePath -InputObject $gps144 $filePath | Should -Exist145 146 $fileContent = Get-Content $filePath147 $fileContent | Should -Not -Be $null148 149 $importedProcess = Import-Clixml $filePath150 $importedProcess.ProcessName | Should -Not -BeNullOrEmpty151 $gps.ProcessName | Should -Be $importedProcess.ProcessName152 $importedProcess.Id | Should -Not -BeNullOrEmpty153 $gps.Id | Should -Be $importedProcess.Id154 }155 156 It "can import from a literal path using pipeline" {157 $gps | Export-Clixml -Depth 1 -LiteralPath $filePath158 $filePath | Should -Exist159 160 $fileContent = Get-Content $filePath161 $fileContent | Should -Not -Be $null162 163 $importedProcess = Import-Clixml $filePath164 $importedProcess.ProcessName | Should -Not -BeNullOrEmpty165 $gps.ProcessName | Should -Be $importedProcess.ProcessName166 $importedProcess.Id | Should -Not -BeNullOrEmpty167 $gps.Id | Should -Be $importedProcess.Id168 }169 170 It "test follow-up for WinBlue: 161470 - Export-CliXml errors in WhatIf scenarios" {171 172 $testPath = "testdrive:\Bug161470NonExistPath.txt"173 Export-Clixml -Path $testPath -InputObject "string" -WhatIf174 $testPath | Should -Not -Exist175 }176 177 It "should import PSCredential" {178 $UserName = "Foo"179 $pass = ConvertTo-SecureString (New-RandomHexString) -AsPlainText -Force180 $cred = [PSCredential]::new($UserName, $pass)181 $path = "$testdrive/cred.xml"182 $cred | Export-Clixml -Path $path183 $cred = Import-Clixml -Path $path184 $cred.UserName | Should -BeExactly "Foo"185 $cred.Password | Should -BeOfType System.Security.SecureString186 }187 }188 189 Context "ConvertTo-CliXml"{190 BeforeAll {191 $gpsList = Get-Process pwsh192 $gps = $gpsList | Select-Object -First 1193 }194 195 It "Create by passing as parameter" {196 $content = ConvertTo-CliXml -Depth 1 -InputObject ($gpsList | Select-Object -First 1)197 $isExisted = $false198 199 foreach($item in $content)200 {201 foreach($gpsItem in $gpsList)202 {203 $checkId = $gpsItem.Id204 if (($null -ne $(Select-String -InputObject $item -SimpleMatch $checkId)) -and ($null -ne $(Select-String -InputObject $item -SimpleMatch "Id")))205 {206 $isExisted = $true207 break;208 }209 }210 }211 212 $isExisted | Should -BeTrue213 }214 215 It "Create by passing as pipeline" {216 $content = ($gpsList | Select-Object -First 1) | ConvertTo-CliXml -Depth 1217 218 $isExisted = $false219 220 foreach($item in $content)221 {222 foreach($gpsItem in $gpsList)223 {224 $checkId = $gpsItem.Id225 if (($null -ne $(Select-String -InputObject $item -SimpleMatch $checkId)) -and ($null -ne $(Select-String -InputObject $item -SimpleMatch "Id")))226 {227 $isExisted = $true228 break;229 }230 }231 }232 233 $isExisted | Should -BeTrue234 }235 }236 237 Context "ConvertFrom-CliXml" {238 BeforeAll {239 $gpsList = Get-Process pwsh240 $gps = $gpsList | Select-Object -First 1241 }242 243 It "Create by passing as parameter" {244 $content = ConvertTo-CliXml -Depth 1 -InputObject $gps245 246 $content | Should -Not -Be $null247 248 $importedProcess = ConvertFrom-CliXml -InputObject $content249 $importedProcess.ProcessName | Should -Not -BeNullOrEmpty250 $gps.ProcessName | Should -Be $importedProcess.ProcessName251 $importedProcess.Id | Should -Not -BeNullOrEmpty252 $gps.Id | Should -Be $importedProcess.Id253 }254 255 It "Create by passing as pipeline" {256 $content = $gps | ConvertTo-CliXml -Depth 1257 258 $content | Should -Not -Be $null259 260 $importedProcess = $content | ConvertFrom-CliXml261 $importedProcess.ProcessName | Should -Not -BeNullOrEmpty262 $gps.ProcessName | Should -Be $importedProcess.ProcessName263 $importedProcess.Id | Should -Not -BeNullOrEmpty264 $gps.Id | Should -Be $importedProcess.Id265 }266 267 It "Should import PSCredential" {268 $UserName = "Foo"269 $pass = ConvertTo-SecureString (New-RandomHexString) -AsPlainText -Force270 $cred = [PSCredential]::new($UserName, $pass)271 272 $content = $cred | ConvertTo-CliXml273 $cred2 = ConvertFrom-CliXml -InputObject $content274 $cred2.UserName | Should -BeExactly $cred.UserName275 $cred2.Password | Should -BeOfType System.Security.SecureString276 $cred2.GetNetworkCredential().Password | Should -BeExactly $cred.GetNetworkCredential().Password277 }278 }279}280 281##282## CIM deserialization security vulnerability283##284Describe "Deserializing corrupted Cim classes should not instantiate non-Cim types" -Tags "Feature","Slow" {285 286 BeforeAll {287 288 # Only run on Windows platform.289 # Ensure calc.exe is available for test.290 $shouldRunTest = $IsWindows -and ((Get-Command calc.exe -ErrorAction SilentlyContinue) -ne $null)291 $skipNotWindows = ! $shouldRunTest292 if ( $shouldRunTest )293 {294 (Get-Process -Name 'win32calc','calculator' 2> $null) | Stop-Process -Force -ErrorAction SilentlyContinue295 }296 }297 298 AfterAll {299 if ( $shouldRunTest )300 {301 (Get-Process -Name 'win32calc','calculator' 2> $null) | Stop-Process -Force -ErrorAction SilentlyContinue302 }303 }304 305 It "Verifies that importing the corrupted Cim class does not launch calc.exe" -Skip:$skipNotWindows {306 307 Import-Clixml -Path (Join-Path $PSScriptRoot "assets\CorruptedCim.clixml")308 309 # Wait up to 10 seconds for calc.exe to run310 $calcProc = $null311 $count = 0312 while (!$calcProc -and ($count++ -lt 20))313 {314 $calcProc = Get-Process -Name 'win32calc','calculator' 2> $null315 Start-Sleep -Milliseconds 500316 }317 318 $calcProc | Should -BeNullOrEmpty319 }320}321 