From 396a283da9e554d4fc4803dddf383a5b6fcf3afa Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 15 Apr 2025 17:03:14 +0100 Subject: [PATCH] PS: Add tests for flow sources. --- .../microsoft_powershell/test.expected | 0 .../frameworks/microsoft_powershell/test.ps1 | 8 +++++++ .../frameworks/microsoft_powershell/test.ql | 1 + .../frameworks/microsoft_win32/test.expected | 0 .../frameworks/microsoft_win32/test.ps1 | 16 ++++++++++++++ .../frameworks/microsoft_win32/test.ql | 1 + .../frameworks/system/test.expected | 0 .../library-tests/frameworks/system/test.ps1 | 11 ++++++++++ .../library-tests/frameworks/system/test.ql | 1 + .../frameworks/system_io/test.expected | 0 .../frameworks/system_io/test.ps1 | 21 +++++++++++++++++++ .../frameworks/system_io/test.ql | 1 + .../system_net_sockets/test.expected | 1 + .../frameworks/system_net_sockets/test.ps1 | 17 +++++++++++++++ .../frameworks/system_net_sockets/test.ql | 1 + 15 files changed, 79 insertions(+) create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.expected create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ps1 create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ql create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_win32/test.expected create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ps1 create mode 100644 powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ql create mode 100644 powershell/ql/test/library-tests/frameworks/system/test.expected create mode 100644 powershell/ql/test/library-tests/frameworks/system/test.ps1 create mode 100644 powershell/ql/test/library-tests/frameworks/system/test.ql create mode 100644 powershell/ql/test/library-tests/frameworks/system_io/test.expected create mode 100644 powershell/ql/test/library-tests/frameworks/system_io/test.ps1 create mode 100644 powershell/ql/test/library-tests/frameworks/system_io/test.ql create mode 100644 powershell/ql/test/library-tests/frameworks/system_net_sockets/test.expected create mode 100644 powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ps1 create mode 100644 powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ql diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.expected b/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ps1 b/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ps1 new file mode 100644 index 00000000000..ef2af8b62c9 --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ps1 @@ -0,0 +1,8 @@ +$data = Read-Host -Prompt "Enter your name" # $ type="read from stdin" + + +$xmlQuery = "/Users/User" +$path = "C:/Users/MyData.xml" +$xmldata = Select-Xml -Path $path -XPath $xmlQuery # $ type="file stream" + +$hexdata = Format-Hex -Path $path -Count 48 # $ type="file stream" \ No newline at end of file diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ql b/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ql new file mode 100644 index 00000000000..5f832831f9b --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/microsoft_powershell/test.ql @@ -0,0 +1 @@ +import TestUtilities.InlineFlowSourceTest \ No newline at end of file diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.expected b/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ps1 b/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ps1 new file mode 100644 index 00000000000..c26b611211e --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ps1 @@ -0,0 +1,16 @@ +$registryPath = "HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion" +$valueName = "ProductName" +$productName = [Microsoft.Win32.Registry]::GetValue($registryPath, $valueName, $null) # $ type="a value from the Windows registry" + + +$registryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($registryPath) + +# Get the value of a registry key +$productName2 = $registryKey.GetValue($valueName) # $ type="a value from the Windows registry" + + +# Get all value names in the registry key +$valueNames = $registryKey.GetValueNames() # $ type="a value from the Windows registry" + +# TODO: I think this should also have a positional element on the access path +$subKeyNames = $registryKey.GetSubKeyNames() # $ type="a value from the Windows registry" diff --git a/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ql b/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ql new file mode 100644 index 00000000000..5f832831f9b --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/microsoft_win32/test.ql @@ -0,0 +1 @@ +import TestUtilities.InlineFlowSourceTest \ No newline at end of file diff --git a/powershell/ql/test/library-tests/frameworks/system/test.expected b/powershell/ql/test/library-tests/frameworks/system/test.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/powershell/ql/test/library-tests/frameworks/system/test.ps1 b/powershell/ql/test/library-tests/frameworks/system/test.ps1 new file mode 100644 index 00000000000..369f800aa98 --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system/test.ps1 @@ -0,0 +1,11 @@ +$char = [System.Console]::Read() # $ type="read from stdin" +$keyInfo = [System.Console]::ReadKey($true) # $ type="read from stdin" +$userName = [System.Console]::ReadLine() # $ type="read from stdin" +# $input = [System.Console]::ReadToEnd() # TODO we need to model this one + +$path = "%USERPROFILE%\Documents" +$expandedPath = [System.Environment]::ExpandEnvironmentVariables($path) # $ type="environment variable" + +$args = [System.Environment]::GetCommandLineArgs() # $ type="command line argument" +$variableValue = [System.Environment]::GetEnvironmentVariable("PATH") # $ type="environment variable" +$envVariables = [System.Environment]::GetEnvironmentVariables() # $ type="environment variable" diff --git a/powershell/ql/test/library-tests/frameworks/system/test.ql b/powershell/ql/test/library-tests/frameworks/system/test.ql new file mode 100644 index 00000000000..5f832831f9b --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system/test.ql @@ -0,0 +1 @@ +import TestUtilities.InlineFlowSourceTest \ No newline at end of file diff --git a/powershell/ql/test/library-tests/frameworks/system_io/test.expected b/powershell/ql/test/library-tests/frameworks/system_io/test.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/powershell/ql/test/library-tests/frameworks/system_io/test.ps1 b/powershell/ql/test/library-tests/frameworks/system_io/test.ps1 new file mode 100644 index 00000000000..4ba44eed967 --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system_io/test.ps1 @@ -0,0 +1,21 @@ +$filePath = "C:\Temp\example.txt" +$fileStream = [System.IO.File]::Open($filePath, [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite) # $ type="file stream" +$fileStream2 = [System.IO.File]::OpenRead($filePath) # $ type="file stream" + +$reader = [System.IO.File]::OpenText($filePath) # $ type="file stream" +$bytes = [System.IO.File]::ReadAllBytes($filePath) # $ type="file stream" +$lines = [System.IO.File]::ReadAllLines($filePath) # $ type="file stream" +$bytesTask = [System.IO.File]::ReadAllBytesAsync($filePath) # $ type="file stream" +$linesTask = [System.IO.File]::ReadAllLinesAsync($filePath) # $ type="file stream" +$stream = [System.IO.File]::ReadAllText($filePath) # $ type="file stream" +$streamTask = [System.IO.File]::ReadAllTextAsync($filePath) # $ type="file stream" +$lines2 = [System.IO.File]::ReadLines($filePath) # $ type="file stream" +$lines3 = [System.IO.File]::ReadLinesAsync($filePath) # $ type="file stream" + + +$fileInfo = [System.IO.FileInfo]::new("C:\Temp\example.txt") + +# Open the file for reading and writing +$fileStream3 = $fileInfo.Open([System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite) # $ type="file stream" +$fileStream4 = $fileInfo.OpenRead() # $ type="file stream" +$reader2 = $fileInfo.OpenText() # $ type="file stream" diff --git a/powershell/ql/test/library-tests/frameworks/system_io/test.ql b/powershell/ql/test/library-tests/frameworks/system_io/test.ql new file mode 100644 index 00000000000..5f832831f9b --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system_io/test.ql @@ -0,0 +1 @@ +import TestUtilities.InlineFlowSourceTest \ No newline at end of file diff --git a/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.expected b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.expected new file mode 100644 index 00000000000..bfb872948cb --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.expected @@ -0,0 +1 @@ +| test.ps1:1:1:18:0 | [synth] pipeline | Unexpected result: type="command line argument" | diff --git a/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ps1 b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ps1 new file mode 100644 index 00000000000..1cb89070f03 --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ps1 @@ -0,0 +1,17 @@ +param($server) # $ type="command line argument" + +$tcpClient = [System.Net.Sockets.TcpClient]::new($server, 8080) +$networkStream = $tcpClient.GetStream() # $ type="remote flow source" + + +$localEndpoint = [System.Net.IPEndPoint]::new([System.Net.IPAddress]::Any, 8080) +$udpClient = [System.Net.Sockets.UdpClient]::new($localEndpoint) +$asyncResult = $udpClient.BeginReceive($null, $null) +$asyncResult.AsyncWaitHandle.WaitOne() +$remoteEndpoint = $null +$data = $udpClient.EndReceive($asyncResult, [ref]$remoteEndpoint) # $ type="remote flow source" + +$remoteEndpoint2 = $null +$data2 = $udpClient.Receive([ref]$remoteEndpoint2) # $ type="remote flow source" + +$receiveTask = $udpClient.ReceiveAsync() # $ type="remote flow source" diff --git a/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ql b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ql new file mode 100644 index 00000000000..5f832831f9b --- /dev/null +++ b/powershell/ql/test/library-tests/frameworks/system_net_sockets/test.ql @@ -0,0 +1 @@ +import TestUtilities.InlineFlowSourceTest \ No newline at end of file