MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Unblock-File" -Tags "CI" {5 6 Context "Windows and macOS" {7 BeforeAll {8 9 if ($IsWindows) {10 function Test-UnblockFile {11 { Get-Content -Path $testfilepath -Stream Zone.Identifier -ErrorAction Stop | Out-Null } |12 Should -Throw -ErrorId "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand"13 }14 15 function Block-File {16 param($path)17 Set-Content -Value "[ZoneTransfer]`r`nZoneId=4" -Path $path -Stream Zone.Identifier18 }19 }20 else {21 function Test-UnblockFile {22 $result = (xattr $testfilepath | Select-String 'com.apple.com.quarantine')23 $result | Should -BeNullOrEmpty24 }25 26 function Block-File {27 param($path)28 Set-Content -Path $path -Value 'test'29 xattr -w com.apple.quarantine '0081;5dd5c373;Microsoft Edge;1A9A933D-619A-4036-BAF3-17A7966A1BA8' $path30 31 }32 }33 34 if ( $IsLinux )35 {36 $origDefaults = $PSDefaultParameterValues.Clone()37 $PSDefaultParameterValues['it:skip'] = $true38 39 } else {40 $testfilepath = Join-Path -Path $TestDrive -ChildPath testunblockfile.ttt41 }42 }43 44 AfterAll {45 if ( $IsLinux ){46 $global:PSDefaultParameterValues = $origDefaults47 }48 }49 50 BeforeEach {51 Block-File -Path $testfilepath52 }53 54 It "With '-Path': no file exist" {55 { Unblock-File -Path nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"56 }57 58 It "With '-LiteralPath': no file exist" {59 { Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"60 }61 62 It "With '-Path': file exist" {63 Unblock-File -Path $testfilepath64 Test-UnblockFile65 66 # If a file is not blocked we silently return without an error.67 { Unblock-File -Path $testfilepath -ErrorAction Stop } | Should -Not -Throw68 }69 70 It "With '-LiteralPath': file exist" {71 Unblock-File -LiteralPath $testfilepath72 Test-UnblockFile73 }74 75 It "Write an error if a file is read only" {76 $TestFile = Join-Path $TestDrive "testfileunlock.ps1"77 Block-File -Path $TestFile78 Set-ItemProperty -Path $TestFile -Name IsReadOnly -Value $true79 80 $TestFileCreated = Get-ChildItem $TestFile81 $TestFileCreated.IsReadOnly | Should -BeTrue82 if ($IsWindows) {83 $expectedError = "RemoveItemUnableToAccessFile,Microsoft.PowerShell.Commands.UnblockFileCommand"84 } else {85 $expectedError = "UnblockError,Microsoft.PowerShell.Commands.UnblockFileCommand"86 }87 88 { Unblock-File -LiteralPath $TestFile -ErrorAction Stop } | Should -Throw -ErrorId $expectedError89 }90 }91 92 Context "Linux" {93 BeforeAll {94 if ( ! $IsLinux )95 {96 $origDefaults = $PSDefaultParameterValues.Clone()97 $PSDefaultParameterValues['it:skip'] = $true98 99 } else {100 $testfilepath = Join-Path -Path $TestDrive -ChildPath testunblockfile.ttt101 $null = New-Item -Path $testfilepath -ItemType File102 }103 }104 105 AfterAll {106 if ( ! $IsLinux ){107 $global:PSDefaultParameterValues = $origDefaults108 }109 }110 111 112 It "With '-Path': no file exist" {113 { Unblock-File -Path nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"114 }115 116 It "With '-LiteralPath': no file exist" {117 { Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop } | Should -Throw -ErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"118 }119 120 It "With '-LiteralPath': file exist" {121 { Unblock-File -LiteralPath $testfilepath -ErrorAction Stop } | Should -Throw -ErrorId "LinuxNotSupported,Microsoft.PowerShell.Commands.UnblockFileCommand"122 }123 124 It "With '-Path': file exist" {125 { Unblock-File -Path $testfilepath -ErrorAction Stop } | Should -Throw -ErrorId "LinuxNotSupported,Microsoft.PowerShell.Commands.UnblockFileCommand"126 }127 }128}129 