PS: Port existing tests from internal repo.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-08-16 17:29:41 +01:00
parent 71349afae7
commit c30feab8ac
30 changed files with 259 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
$array1 = 1,2,"a",$true,$false,$null # 1-D array
$array1[1] = 3
$array1[2] = "b"
$array2 = New-Object 'object[,]' 2,2 # 2-D array
$array2[0,0] = "key1"
$array2[1,0] = "key1"
$array2[0,1] = "value1"
$array2[1,1] = $null
$array3 = @("a","b","c")
$array3.count
$array4 = [System.Collections.ArrayList]@()
$array4.Add(1)

View File

@@ -0,0 +1,23 @@
arrayExpr
| Arrays.ps1:11:11:11:25 | ArrayExpression at: Arrays.ps1:11:11:11:25 | Arrays.ps1:11:13:11:24 | StatementBlock at: Arrays.ps1:11:13:11:24 |
| Arrays.ps1:14:41:14:44 | ArrayExpression at: Arrays.ps1:14:41:14:44 | Arrays.ps1:0:0:0:0 | StatementBlock at: Arrays.ps1:0:0:0:0 |
arrayLiteral
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 0 | Arrays.ps1:1:11:1:12 | ConstantExpression at: Arrays.ps1:1:11:1:12 |
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 1 | Arrays.ps1:1:13:1:14 | ConstantExpression at: Arrays.ps1:1:13:1:14 |
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 2 | Arrays.ps1:1:15:1:18 | a |
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 3 | Arrays.ps1:1:19:1:24 | true |
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 4 | Arrays.ps1:1:25:1:31 | false |
| Arrays.ps1:1:11:1:37 | ArrayLiteral at: Arrays.ps1:1:11:1:37 | 5 | Arrays.ps1:1:32:1:37 | null |
| Arrays.ps1:5:34:5:37 | ArrayLiteral at: Arrays.ps1:5:34:5:37 | 0 | Arrays.ps1:5:34:5:35 | ConstantExpression at: Arrays.ps1:5:34:5:35 |
| Arrays.ps1:5:34:5:37 | ArrayLiteral at: Arrays.ps1:5:34:5:37 | 1 | Arrays.ps1:5:36:5:37 | ConstantExpression at: Arrays.ps1:5:36:5:37 |
| Arrays.ps1:6:9:6:12 | ArrayLiteral at: Arrays.ps1:6:9:6:12 | 0 | Arrays.ps1:6:9:6:10 | ConstantExpression at: Arrays.ps1:6:9:6:10 |
| Arrays.ps1:6:9:6:12 | ArrayLiteral at: Arrays.ps1:6:9:6:12 | 1 | Arrays.ps1:6:11:6:12 | ConstantExpression at: Arrays.ps1:6:11:6:12 |
| Arrays.ps1:7:9:7:12 | ArrayLiteral at: Arrays.ps1:7:9:7:12 | 0 | Arrays.ps1:7:9:7:10 | ConstantExpression at: Arrays.ps1:7:9:7:10 |
| Arrays.ps1:7:9:7:12 | ArrayLiteral at: Arrays.ps1:7:9:7:12 | 1 | Arrays.ps1:7:11:7:12 | ConstantExpression at: Arrays.ps1:7:11:7:12 |
| Arrays.ps1:8:9:8:12 | ArrayLiteral at: Arrays.ps1:8:9:8:12 | 0 | Arrays.ps1:8:9:8:10 | ConstantExpression at: Arrays.ps1:8:9:8:10 |
| Arrays.ps1:8:9:8:12 | ArrayLiteral at: Arrays.ps1:8:9:8:12 | 1 | Arrays.ps1:8:11:8:12 | ConstantExpression at: Arrays.ps1:8:11:8:12 |
| Arrays.ps1:9:9:9:12 | ArrayLiteral at: Arrays.ps1:9:9:9:12 | 0 | Arrays.ps1:9:9:9:10 | ConstantExpression at: Arrays.ps1:9:9:9:10 |
| Arrays.ps1:9:9:9:12 | ArrayLiteral at: Arrays.ps1:9:9:9:12 | 1 | Arrays.ps1:9:11:9:12 | ConstantExpression at: Arrays.ps1:9:11:9:12 |
| Arrays.ps1:11:13:11:24 | ArrayLiteral at: Arrays.ps1:11:13:11:24 | 0 | Arrays.ps1:11:13:11:16 | a |
| Arrays.ps1:11:13:11:24 | ArrayLiteral at: Arrays.ps1:11:13:11:24 | 1 | Arrays.ps1:11:17:11:20 | b |
| Arrays.ps1:11:13:11:24 | ArrayLiteral at: Arrays.ps1:11:13:11:24 | 2 | Arrays.ps1:11:21:11:24 | c |

View File

@@ -0,0 +1,7 @@
import powershell
query predicate arrayExpr(ArrayExpr arrayExpr, StmtBlock subExpr) { subExpr = arrayExpr.getStatementBlock() }
query predicate arrayLiteral(ArrayLiteral arrayLiteral, int i, Expr e) {
e = arrayLiteral.getElement(i)
}

View File

@@ -0,0 +1,5 @@
[CmdletBinding()]
param(
[Parameter()]
[string]$Parameter
)

View File

@@ -0,0 +1 @@
| ParamBlock.ps1:2:1:5:2 | ParamBlock | 0 | ParamBlock.ps1:3:5:4:23 | Parameter |

View File

@@ -0,0 +1,5 @@
import powershell
query predicate paramBlockHasParam(ParamBlock block, int i, Parameter p) {
p = block.getParameter(i)
}

View File

@@ -0,0 +1,5 @@
$foo = 'cmd.exe'
Invoke-Expression $foo
[scriptblock]::Create($foo)
& ([scriptblock]::Create($foo))
&"$foo"

View File

@@ -0,0 +1,11 @@
function ExecuteAThing {
param (
$userInput
)
$foo = 'cmd.exe' + $userInput;
Invoke-Expression $foo
[scriptblock]::Create($foo)
& ([scriptblock]::Create($foo))
&"$foo"
& 'cmd.exe' @($userInput)
}

View File

@@ -0,0 +1,4 @@
$val1 = 1
$val2 = 2
$result = $val1 + $val2
$result

View File

@@ -0,0 +1,2 @@
$UserInput = Read-Host "Please enter your secure code"
$EncryptedInput = ConvertTo-SecureString -String $UserInput -AsPlainText -Force

View File

@@ -0,0 +1 @@
"Name: $name`nDate: $([DateTime]::Now)"

View File

@@ -0,0 +1,2 @@
$(Get-Date).AddDays(10)
$(Get-Date).AddDays()

View File

@@ -0,0 +1 @@
$var = (6 -gt 7) ? 1:2

View File

@@ -0,0 +1,19 @@
binaryExpr
| BinaryExpression.ps1:3:11:3:24 | ...+... | BinaryExpression.ps1:3:11:3:16 | val1 | BinaryExpression.ps1:3:19:3:24 | val2 |
| TernaryExpression.ps1:1:9:1:16 | ...+... | TernaryExpression.ps1:1:9:1:10 | ConstantExpression at: TernaryExpression.ps1:1:9:1:10 | TernaryExpression.ps1:1:15:1:16 | ConstantExpression at: TernaryExpression.ps1:1:15:1:16 |
cmdExpr
| BinaryExpression.ps1:1:9:1:10 | CommandExpression at: BinaryExpression.ps1:1:9:1:10 | BinaryExpression.ps1:1:9:1:10 | ConstantExpression at: BinaryExpression.ps1:1:9:1:10 |
| BinaryExpression.ps1:2:9:2:10 | CommandExpression at: BinaryExpression.ps1:2:9:2:10 | BinaryExpression.ps1:2:9:2:10 | ConstantExpression at: BinaryExpression.ps1:2:9:2:10 |
| BinaryExpression.ps1:3:11:3:24 | CommandExpression at: BinaryExpression.ps1:3:11:3:24 | BinaryExpression.ps1:3:11:3:24 | ...+... |
| BinaryExpression.ps1:4:1:4:8 | CommandExpression at: BinaryExpression.ps1:4:1:4:8 | BinaryExpression.ps1:4:1:4:8 | result |
| ExpandableString.ps1:1:1:1:40 | CommandExpression at: ExpandableString.ps1:1:1:1:40 | ExpandableString.ps1:1:1:1:40 | ExpandableStringExpression at: ExpandableString.ps1:1:1:1:40 |
| ExpandableString.ps1:1:23:1:38 | CommandExpression at: ExpandableString.ps1:1:23:1:38 | file://:0:0:0:0 | (no string representation) |
| SubExpression.ps1:1:1:1:24 | CommandExpression at: SubExpression.ps1:1:1:1:24 | SubExpression.ps1:1:1:1:24 | ArrayExpression at: SubExpression.ps1:1:1:1:24 |
| SubExpression.ps1:2:1:2:22 | CommandExpression at: SubExpression.ps1:2:1:2:22 | SubExpression.ps1:2:1:2:22 | ArrayExpression at: SubExpression.ps1:2:1:2:22 |
| TernaryExpression.ps1:1:8:1:23 | CommandExpression at: TernaryExpression.ps1:1:8:1:23 | TernaryExpression.ps1:1:8:1:23 | ...?...:... |
| TernaryExpression.ps1:1:9:1:16 | CommandExpression at: TernaryExpression.ps1:1:9:1:16 | TernaryExpression.ps1:1:9:1:16 | ...+... |
invokeMemoryExpression
| SubExpression.ps1:1:1:1:24 | ArrayExpression at: SubExpression.ps1:1:1:1:24 | file://:0:0:0:0 | (no string representation) | 0 | SubExpression.ps1:1:21:1:23 | ConstantExpression at: SubExpression.ps1:1:21:1:23 |
expandableString
| ExpandableString.ps1:1:1:1:40 | ExpandableStringExpression at: ExpandableString.ps1:1:1:1:40 | 0 | ExpandableString.ps1:1:8:1:13 | name |
| ExpandableString.ps1:1:1:1:40 | ExpandableStringExpression at: ExpandableString.ps1:1:1:1:40 | 1 | file://:0:0:0:0 | (no string representation) |

View File

@@ -0,0 +1,19 @@
import powershell
query predicate binaryExpr(BinaryExpr e, Expr e1, Expr e2) {
e1 = e.getLeft() and
e2 = e.getRight()
}
query predicate cmdExpr(CmdExpr cmd, Expr e) {
e = cmd.getExpression()
}
query predicate invokeMemoryExpression(InvokeMemberExpression invoke, Expr e, int i, Expr arg) {
e = invoke.getExpression() and
arg = invoke.getArgument(i)
}
query predicate expandableString(ExpandableStringExpression expandable, int i, Expr e) {
e = expandable.getExpr(i)
}

View File

@@ -0,0 +1,7 @@
DO
{
Starting Loop $a
$a
$a++
Now `$a is $a
} Until ($a -le 5)

View File

@@ -0,0 +1,7 @@
DO
{
Starting Loop $a
$a
$a++
Now `$a is $a
} While ($a -le 5)

View File

@@ -0,0 +1,13 @@
$var = 1
while ($var -le 5)
{
Write-Host The value of Var is: $var
$var++
if ($var -le 3){
continue;
}
else
{
break;
}
}

View File

@@ -0,0 +1,16 @@
import powershell
query predicate doUntil(DoUntilStmt s, PipelineBase e, StmtBlock body) {
e = s.getCondition() and
body = s.getBody()
}
query predicate doWhile(DoWhileStmt s, PipelineBase e, StmtBlock body) {
e = s.getCondition() and
body = s.getBody()
}
query predicate while(WhileStmt s, PipelineBase e, StmtBlock body) {
e = s.getCondition() and
body = s.getBody()
}

View File

@@ -0,0 +1,3 @@
$(
Here is your current script
) *>&1 > output.txt

View File

@@ -0,0 +1,2 @@
| FileRedirection.ps1:3:3:3:7 | MergingRedirection |
| FileRedirection.ps1:3:8:3:20 | FileRedirection |

View File

@@ -0,0 +1,3 @@
import powershell
query predicate redirection(Redirection r) { any() }

View File

@@ -0,0 +1 @@
exit -1

View File

@@ -0,0 +1,8 @@
$x = 4
if ($x -ge 3) {
"$x is greater than or equal to 3"
}
else {
"$x is less than 3"
}

View File

@@ -0,0 +1,6 @@
function TrapTest {
trap {"Error found."}
nonsenseString
}
TrapTest

View File

@@ -0,0 +1,13 @@
try {
$Exception = New-Object System.Xaml.XamlException -ArgumentList ("Bad XAML!", $null, 10, 2)
throw $Exception
}
catch [System.Net.WebException],[System.IO.IOException] {
"Unable to download MyDoc.doc from http://www.contoso.com."
}
catch {
"An error occurred that could not be resolved."
}
finally {
"The finally block is executed."
}

View File

@@ -0,0 +1,11 @@
Function Get-Number
{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)]
[int]
$Number
)
$Number
}

View File

@@ -0,0 +1,38 @@
| ExitStatement.ps1:1:1:1:8 | exit ... |
| ExitStatement.ps1:1:6:1:8 | ...\|... |
| ExitStatement.ps1:1:6:1:8 | CommandExpression at: ExitStatement.ps1:1:6:1:8 |
| IfStatement.ps1:1:1:1:7 | AssignmentStatement at: IfStatement.ps1:1:1:1:7 |
| IfStatement.ps1:1:6:1:7 | CommandExpression at: IfStatement.ps1:1:6:1:7 |
| IfStatement.ps1:3:1:8:2 | if (...) {...} else {...} |
| IfStatement.ps1:3:5:3:13 | ...\|... |
| IfStatement.ps1:3:5:3:13 | CommandExpression at: IfStatement.ps1:3:5:3:13 |
| IfStatement.ps1:4:2:4:36 | ...\|... |
| IfStatement.ps1:4:2:4:36 | CommandExpression at: IfStatement.ps1:4:2:4:36 |
| IfStatement.ps1:7:2:7:21 | ...\|... |
| IfStatement.ps1:7:2:7:21 | CommandExpression at: IfStatement.ps1:7:2:7:21 |
| TrapStatement.ps1:1:1:4:2 | FunctionDefinition at: TrapStatement.ps1:1:1:4:2 |
| TrapStatement.ps1:2:5:2:26 | TrapStatement at: TrapStatement.ps1:2:5:2:26 |
| TrapStatement.ps1:2:11:2:25 | ...\|... |
| TrapStatement.ps1:2:11:2:25 | CommandExpression at: TrapStatement.ps1:2:11:2:25 |
| TrapStatement.ps1:3:5:3:19 | ...\|... |
| TrapStatement.ps1:3:5:3:19 | nonsenseString |
| TrapStatement.ps1:6:1:6:9 | ...\|... |
| TrapStatement.ps1:6:1:6:9 | TrapTest |
| Try.ps1:1:1:13:2 | try {...} |
| Try.ps1:2:4:2:95 | AssignmentStatement at: Try.ps1:2:4:2:95 |
| Try.ps1:2:17:2:95 | ...\|... |
| Try.ps1:2:17:2:95 | New-Object |
| Try.ps1:2:69:2:94 | ...\|... |
| Try.ps1:2:69:2:94 | CommandExpression at: Try.ps1:2:69:2:94 |
| Try.ps1:3:11:3:21 | ...\|... |
| Try.ps1:3:11:3:21 | CommandExpression at: Try.ps1:3:11:3:21 |
| Try.ps1:6:5:6:64 | ...\|... |
| Try.ps1:6:5:6:64 | CommandExpression at: Try.ps1:6:5:6:64 |
| Try.ps1:9:5:9:52 | ...\|... |
| Try.ps1:9:5:9:52 | CommandExpression at: Try.ps1:9:5:9:52 |
| Try.ps1:12:5:12:37 | ...\|... |
| Try.ps1:12:5:12:37 | CommandExpression at: Try.ps1:12:5:12:37 |
| UseProcessBlockForPipelineCommand.ps1:1:1:11:2 | FunctionDefinition at: UseProcessBlockForPipelineCommand.ps1:1:1:11:2 |
| UseProcessBlockForPipelineCommand.ps1:10:5:10:12 | ...\|... |
| UseProcessBlockForPipelineCommand.ps1:10:5:10:12 | CommandExpression at: UseProcessBlockForPipelineCommand.ps1:10:5:10:12 |
| file://:0:0:0:0 | (no string representation) |

View File

@@ -0,0 +1,3 @@
import powershell
query predicate stmt(Stmt s) { any() }

View File

@@ -0,0 +1,8 @@
name: microsoft-sdl/powershell-tests
groups:
- powershell
- test
dependencies:
microsoft-sdl/powershell-all: ${workspace}
extractor: powershell
tests: .