Files
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/Unblock-File.Tests.ps1
T
Steve Lee c1c5344a88 Update copyright and license headers (#6134)
Based on standard practices, we need to have a copyright and license notice at the top of each source file. Removed existing copyrights and updated/added copyright notices for .h, .cpp, .cs, .ps1, and .psm1 files.

Updated module manifests for consistency to have Author = "PowerShell" and Company = "Microsoft Corporation". Removed multiple line breaks.

Separate PR coming to update contribution document for new source files: #6140

Manually reviewed each change.

Fix #6073
2018-02-13 09:23:53 -08:00

78 lines
2.5 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Test-UnblockFile {
try {
Get-Content -Path $testfilepath -Stream Zone.Identifier -ErrorAction Stop | Out-Null
}
catch {
if ($_.FullyQualifiedErrorId -eq "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand") {
return $true
}
}
return $false
}
Describe "Unblock-File" -Tags "CI" {
BeforeAll {
if ( ! $IsWindows )
{
$origDefaults = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $true
} else {
$testfilepath = Join-Path -Path $TestDrive -ChildPath testunblockfile.ttt
}
}
AfterAll {
if ( ! $IsWindows ){
$global:PSDefaultParameterValues = $origDefaults
}
}
BeforeEach {
if ( $IsWindows ){
Set-Content -Value "[ZoneTransfer]`r`nZoneId=4" -Path $testfilepath -Stream Zone.Identifier
}
}
It "With '-Path': no file exist" {
{ Unblock-File -Path nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
}
It "With '-LiteralPath': no file exist" {
{ Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop } | ShouldBeErrorId "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
}
It "With '-Path': file exist" {
Unblock-File -Path $testfilepath
Test-UnblockFile | Should Be $true
# If a file is not blocked we silently return without an error.
{ Unblock-File -Path $testfilepath -ErrorAction Stop } | Should Not Throw
}
It "With '-LiteralPath': file exist" {
Unblock-File -LiteralPath $testfilepath
Test-UnblockFile | Should Be $true
}
It "Write an error if a file is read only" {
$TestFile = Join-Path $TestDrive "testfileunlock.ps1"
Set-Content -Path $TestFile -value 'test'
$ZoneIdentifier = {
[ZoneTransfer]
ZoneId=3
}
Set-Content -Path $TestFile -Value $ZoneIdentifier -Stream 'Zone.Identifier'
Set-ItemProperty -Path $TestFile -Name IsReadOnly -Value $True
$TestFileCreated = Get-ChildItem $TestFile
$TestFileCreated.IsReadOnly | Should Be $true
{ Unblock-File -LiteralPath $TestFile -ErrorAction Stop } | ShouldBeErrorId "RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.UnblockFileCommand"
}
}