Add local Windows Server inventory collection and validation; include core package
This commit is contained in:
78
tests/powershell/Test-Collectors.ps1
Normal file
78
tests/powershell/Test-Collectors.ps1
Normal file
@@ -0,0 +1,78 @@
|
||||
#requires -Version 5.1
|
||||
# AST and synthetic collector contract tests only. Does not query any real server.
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
|
||||
$files = @(Get-ChildItem -LiteralPath (Join-Path $root 'powershell') -Filter '*.ps1')
|
||||
foreach ($file in $files) {
|
||||
$tokens = $null; $parseErrors = $null
|
||||
$null = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$parseErrors)
|
||||
if ($parseErrors.Count) { throw ('PowerShell syntax invalid: ' + $file.Name) }
|
||||
}
|
||||
foreach ($file in @($files | Where-Object { $_.Name -ne 'Get-ServerInventory.ps1' })) { . $file.FullName }
|
||||
$script:SamsaraIsElevated = $true
|
||||
$script:SyntheticBuild = '20348'
|
||||
function Get-CimInstance {
|
||||
[CmdletBinding()] param($ClassName, $Property, $Filter)
|
||||
switch ($ClassName) {
|
||||
'Win32_OperatingSystem' { [pscustomobject]@{ Caption='Synthetic Windows Server'; Version=('10.0.' + $script:SyntheticBuild); BuildNumber=$script:SyntheticBuild; ProductType=3; LastBootUpTime=[DateTime]::UtcNow.AddDays(-2) } }
|
||||
'Win32_ComputerSystem' { [pscustomobject]@{ Domain='example.test'; PartOfDomain=$true } }
|
||||
'Win32_NetworkAdapterConfiguration' { [pscustomobject]@{ Description='Synthetic NIC'; IPAddress=@('192.0.2.20') } }
|
||||
'Win32_Service' { [pscustomobject]@{ Name='W3SVC'; DisplayName='Synthetic Web'; State='Running'; StartMode='Auto'; StartName='EXAMPLE\svc_web'; ProcessId=123; PathName='SECRET command arguments' } }
|
||||
default { throw 'Unexpected synthetic CIM query' }
|
||||
}
|
||||
}
|
||||
function Get-WindowsFeature { [CmdletBinding()] param() [pscustomobject]@{ Name='Web-Server'; DisplayName='Synthetic Web Server'; Installed=$true; InstallState='Installed'; FeatureType='Role' } }
|
||||
function Get-Process { [CmdletBinding()] param() [pscustomobject]@{ Id=123; ProcessName='SyntheticWorker'; CommandLine='SECRET' } }
|
||||
function Get-NetTCPConnection { [CmdletBinding()] param() [pscustomobject]@{ State='Established'; LocalAddress='192.0.2.20'; LocalPort=443; RemoteAddress='192.0.2.21'; RemotePort=50000; OwningProcess=123 } }
|
||||
function Get-ScheduledTask {
|
||||
[CmdletBinding()] param()
|
||||
[pscustomobject]@{ TaskName='Synthetic report'; TaskPath='\Example\'; State='Ready'
|
||||
Settings=[pscustomobject]@{ Enabled=$true }; Principal=[pscustomobject]@{ UserId='EXAMPLE\svc_report' }
|
||||
Actions=@([pscustomobject]@{ Execute='C:\Tools\report.exe'; Arguments='--password=SECRET' })
|
||||
Triggers=@([pscustomobject]@{ CimClass=[pscustomobject]@{ CimClassName='MSFT_TaskDailyTrigger' }; StartBoundary='2026-10-01T02:00:00'; Enabled=$true; DaysInterval=1; Repetition=$null }) }
|
||||
}
|
||||
function Get-Volume { [CmdletBinding()] param() [pscustomobject]@{ FileSystemLabel='Synthetic OS'; DriveLetter='C'; Path='C:\'; FileSystem='NTFS'; Size=100GB; SizeRemaining=50GB } }
|
||||
function Get-SmbShare { [CmdletBinding()] param() [pscustomobject]@{ Name='SyntheticReports'; Path='C:\Reports'; Special=$false } }
|
||||
function Get-ChildItem {
|
||||
[CmdletBinding()] param($LiteralPath)
|
||||
if ($LiteralPath -eq 'Cert:\LocalMachine') { [pscustomobject]@{ PSPath='Cert:\LocalMachine\My'; PSChildName='My' } }
|
||||
else { [pscustomobject]@{ Subject='CN=synthetic.example.test'; Issuer='CN=Synthetic CA'; NotAfter=[DateTime]::UtcNow.AddDays(30); Thumbprint='0000000000000000000000000000000000000000'; PrivateKey='SECRET' } }
|
||||
}
|
||||
function Assert-True { param($Value, [string]$Message) if (-not $Value) { throw $Message } }
|
||||
$collectors = [ordered]@{
|
||||
system={param($r) Get-SamsaraSystem $r}; roles={param($r) Get-SamsaraRoles $r}
|
||||
services={param($r) Get-SamsaraServices $r}; network={param($r) Get-SamsaraNetwork $r}
|
||||
tasks={param($r) Get-SamsaraTasks $r}; storage={param($r) Get-SamsaraStorage $r}
|
||||
shares={param($r) Get-SamsaraShares $r}; certificates={param($r) Get-SamsaraCertificates $r}
|
||||
}
|
||||
foreach ($name in $collectors.Keys) {
|
||||
$section = Invoke-SamsaraSection $name $collectors[$name]
|
||||
Assert-True ($section.status -eq 'complete') ('Synthetic section failed: ' + $name)
|
||||
Assert-True ($section.started_at -and $section.completed_at) 'Missing timestamps'
|
||||
Assert-True (($section | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Secret field serialized'
|
||||
if ($name -eq 'network') { Assert-True ($section.data[0].process -eq 'SyntheticWorker') 'Process correlation failed' }
|
||||
if ($name -eq 'tasks') { Assert-True ($section.data[0].executables -eq 'report.exe') 'Executable basename missing' }
|
||||
}
|
||||
foreach ($build in @('14393','17763','20348','26100')) {
|
||||
$script:SyntheticBuild=$build
|
||||
$section=Invoke-SamsaraSection 'system' {param($r) Get-SamsaraSystem $r}
|
||||
Assert-True ($section.status -eq 'complete') 'Supported build mapping failed'
|
||||
}
|
||||
# Test registry projection with in-memory objects; never open a real registry hive.
|
||||
$fakeEntry = New-Object PSObject
|
||||
$fakeEntry | Add-Member ScriptMethod GetValue { param($key) switch ($key) { 'DisplayName' {'Synthetic application'}; 'DisplayVersion' {'1.0'}; 'Publisher' {'Example'}; 'InstallDate' {'20260101'}; default {throw 'Unexpected registry value requested'} } }
|
||||
$fakeEntry | Add-Member ScriptMethod Dispose { }
|
||||
$fakeUninstall = New-Object PSObject
|
||||
$fakeUninstall | Add-Member ScriptMethod GetSubKeyNames { @('Example') }
|
||||
$fakeUninstall | Add-Member ScriptMethod OpenSubKey { param($name,$writable) if ($writable) {throw 'Writable registry access'}; return $fakeEntry }
|
||||
$section=Invoke-SamsaraSection 'software' {param($r) Invoke-SamsaraRead $r 'Synthetic registry view' { Read-SamsaraSoftwareEntries $r $fakeUninstall 'Registry64' } }
|
||||
Assert-True ($section.status -eq 'complete' -and $section.data[0].name -eq 'Synthetic application') 'Registry metadata projection failed'
|
||||
function Get-SmbShare { [CmdletBinding()] param() throw [UnauthorizedAccessException]::new('SECRET protected details') }
|
||||
$section=Invoke-SamsaraSection 'shares' {param($r) Get-SamsaraShares $r}
|
||||
Assert-True ($section.status -eq 'failed') 'Denied collection was not failed'
|
||||
Assert-True ($section.errors[0].code -eq 'access_denied') 'Permission error not categorized'
|
||||
Assert-True (($section | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Raw error exposed'
|
||||
$script:SamsaraIsElevated=$false
|
||||
$section=Invoke-SamsaraSection 'services' {param($r) Get-SamsaraServices $r}
|
||||
Assert-True ($section.status -eq 'partial' -and $section.permission_limitations.Count -gt 0) 'Non-elevated scope not marked partial'
|
||||
Write-Output 'PowerShell AST and synthetic collector tests passed. No real Windows inventory was collected.'
|
||||
Reference in New Issue
Block a user