2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
Describe "Tests for the Import-PowerShellDataFile cmdlet" -Tags "CI" {
|
2020-12-09 11:07:07 -08:00
|
|
|
BeforeAll {
|
|
|
|
|
$largePsd1Path = Join-Path -Path $TestDrive -ChildPath 'large.psd1'
|
|
|
|
|
$largePsd1Builder = [System.Text.StringBuilder]::new('@{')
|
|
|
|
|
1..501 | ForEach-Object {
|
|
|
|
|
$largePsd1Builder.Append("key$_ = $_;")
|
|
|
|
|
}
|
|
|
|
|
$largePsd1Builder.Append('}')
|
|
|
|
|
Set-Content -Path $largePsd1Path -Value $largePsd1Builder.ToString()
|
|
|
|
|
}
|
2016-07-25 11:06:55 -07:00
|
|
|
|
|
|
|
|
It "Validates error on a missing path" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Import-PowerShellDataFile -Path /SomeMissingDirectory -ErrorAction Stop } |
|
|
|
|
|
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Validates error on a directory" {
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Import-PowerShellDataFile ${TESTDRIVE} -ErrorAction Stop } |
|
|
|
|
|
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Generates a good error on an insecure file" {
|
|
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
$path = Setup -f insecure.psd1 -Content '@{ Foo = Get-Process }' -pass
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Import-PowerShellDataFile $path -ErrorAction Stop } |
|
|
|
|
|
Should -Throw -ErrorId "System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Generates a good error on a file that isn't a PowerShell Data File (missing the hashtable root)" {
|
2020-05-07 13:00:30 +01:00
|
|
|
$path = Setup -f NotAPSDataFile -Content '"Hello World"' -Pass
|
2018-03-23 10:26:23 -07:00
|
|
|
{ Import-PowerShellDataFile $path -ErrorAction Stop } |
|
|
|
|
|
Should -Throw -ErrorId "CouldNotParseAsPowerShellDataFileNoHashtableRoot,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Can parse a PowerShell Data File (detailed tests are in AST.SafeGetValue tests)" {
|
2020-05-07 13:00:30 +01:00
|
|
|
$path = Setup -F gooddatafile -Content '@{ "Hello" = "World" }' -pass
|
2016-07-25 11:06:55 -07:00
|
|
|
$result = Import-PowerShellDataFile $path -ErrorAction Stop
|
2018-03-23 10:26:23 -07:00
|
|
|
$result.Hello | Should -BeExactly "World"
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2020-12-09 11:07:07 -08:00
|
|
|
It 'Fails if psd1 file has more than 500 keys' {
|
|
|
|
|
{ Import-PowerShellDataFile $largePsd1Path } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand'
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 14:01:57 -07:00
|
|
|
It 'Succeeds if -NoLimit is used and has more than 500 keys' {
|
2020-12-09 11:07:07 -08:00
|
|
|
$result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck
|
|
|
|
|
$result.Keys.Count | Should -Be 501
|
|
|
|
|
}
|
2026-04-20 13:56:25 -04:00
|
|
|
|
|
|
|
|
It 'Fails if psd1 file is insecure while -SkipLimitCheck is used' {
|
|
|
|
|
$path = Setup -f insecure2.psd1 -Content '@{ Foo = [object] (calc.exe) }' -pass
|
|
|
|
|
{ Import-PowerShellDataFile $path -SkipLimitCheck -ErrorAction Stop } |
|
|
|
|
|
Should -Throw -ErrorId "System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
|
|
|
|
|
}
|
2016-07-25 11:06:55 -07:00
|
|
|
}
|