forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckRestrictedlanguage.Tests.ps1
More file actions
110 lines (84 loc) · 3.56 KB
/
Copy pathCheckRestrictedlanguage.Tests.ps1
File metadata and controls
110 lines (84 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Describe "Test restricted language check method on scriptblocks" -Tags "CI" {
BeforeAll {
set-strictmode -v 2
function list {
$l = [System.Collections.Generic.List[String]]::new()
$args | ForEach-Object {$l.Add($_)}
, $l
}
}
AfterAll {
Set-StrictMode -Off
}
It 'Check basic expressions' {
{{2+2}.CheckRestrictedLanguage($null, $null, $false) } | Should Not Throw # Succeed with no variables
}
It 'Check default variables' {
{{ $PSCulture, $PSUICulture, $true, $false, $null}.CheckRestrictedLanguage($null, $null, $false) } | Should Not Throw
}
It 'Check default variables' {
try
{
{2+$a}.CheckRestrictedLanguage($null, $null, $false)
Throw "Exception expected, execution should not have reached here"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'ParseException'
}
}
It 'Check union of default + one allowed variables' {
{ { 2 + $a }.CheckRestrictedLanguage($null, (list a), $false) }| Should Not Throw # succeed
}
It 'Check union of default + two allowed variables' {
{ { $a + $b }.CheckRestrictedLanguage($null, (list a b), $false) } | Should Not Throw # succeed
}
It 'Check union of default + allowed variables' {
{ { $PSCulture, $PSUICulture, $true, $false, $null, $a, $b}.CheckRestrictedLanguage($null, (list a b), $false) }| Should Not Throw
}
It 'Check union of default + one disallowed variables' {
try
{
{ $a + $b + $c }.CheckRestrictedLanguage($null, (list a b), $false) # fail
Throw "Exception expected, execution should not have reached here"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'ParseException'
}
}
It 'Check union of default + one allowed variable and but not allow environment variable' {
try
{
{ 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $false) # fail
Throw "Exception expected, execution should not have reached here"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'ParseException'
}
}
It 'Check union of default + one allowed variable name and allow environment variable ' {
{{ 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $true)} | Should Not Throw # succeed
}
It 'Check that wildcard allows env even if the flag is set to false' {
{ { 2 + $a + $b + $c + $env:foo }.CheckRestrictedLanguage($null, (list *), $false)} | Should Not Throw # succeed
}
It 'Check for restricted commands' {
try
{
{get-date}.CheckRestrictedLangauge($null, $null, $false)
Throw "Exception expected, execution should not have reached here"
}
catch
{
$_.FullyQualifiedErrorId | Should Be 'MethodNotFound'
}
}
It 'Check for allowed commands and variables' {
{ { get-process | where name -match $pattern | foreach $prop }.CheckRestrictedLanguage(
(list get-process where foreach),
(list prop pattern)
, $false) }| Should Not Throw
}
}