5761710fa1
Добавлены Get-RdpRepairCertificateArray/FirstCertificate, самотесты на вложенные массивы; Fix-RdpTls больше не использует Select-Object -First 1 на cert. Co-authored-by: Cursor <cursoragent@cursor.com>
172 lines
5.9 KiB
PowerShell
172 lines
5.9 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Самотесты модуля RdpRepair (синтаксис, функции, dry-run логики).
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[switch]$IncludeAdminTests
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$script:SelfTestFailed = 0
|
|
$script:SelfTestPassed = 0
|
|
|
|
function Assert-Test {
|
|
param(
|
|
[string]$Name,
|
|
[scriptblock]$Test
|
|
)
|
|
try {
|
|
& $Test
|
|
$script:SelfTestPassed++
|
|
Write-Host "[PASS] $Name" -ForegroundColor Green
|
|
} catch {
|
|
$script:SelfTestFailed++
|
|
Write-Host "[FAIL] $Name - $($_.Exception.Message)" -ForegroundColor Red
|
|
}
|
|
}
|
|
|
|
$libPath = Join-Path $PSScriptRoot 'lib\RdpRepair.Common.ps1'
|
|
$mainPath = Join-Path $PSScriptRoot 'Fix-RdpTls.ps1'
|
|
. $libPath
|
|
|
|
Write-Host "`n=== RdpRepair SelfTest ===" -ForegroundColor Cyan
|
|
|
|
Assert-Test 'RdpRepair.Common.ps1 exists' {
|
|
if (-not (Test-Path -LiteralPath $libPath)) { throw 'file missing' }
|
|
}
|
|
|
|
Assert-Test 'Fix-RdpTls.ps1 exists' {
|
|
if (-not (Test-Path -LiteralPath $mainPath)) { throw 'file missing' }
|
|
}
|
|
|
|
Assert-Test 'PowerShell parse RdpRepair.Common.ps1' {
|
|
$parseErrors = $null
|
|
$null = [System.Management.Automation.Language.Parser]::ParseFile($libPath, [ref]$null, [ref]$parseErrors)
|
|
if ($parseErrors.Count -gt 0) { throw (($parseErrors | ForEach-Object { $_.ToString() }) -join '; ') }
|
|
}
|
|
|
|
Assert-Test 'PowerShell parse Fix-RdpTls.ps1' {
|
|
$parseErrors = $null
|
|
$null = [System.Management.Automation.Language.Parser]::ParseFile($mainPath, [ref]$null, [ref]$parseErrors)
|
|
if ($parseErrors.Count -gt 0) { throw (($parseErrors | ForEach-Object { $_.ToString() }) -join '; ') }
|
|
}
|
|
|
|
Assert-Test 'Dot-source Common without throw' {
|
|
if (-not (Get-Command Initialize-RdpRepairDataRoot -ErrorAction SilentlyContinue)) {
|
|
throw 'Common module not loaded'
|
|
}
|
|
}
|
|
|
|
Assert-Test 'Initialize-RdpRepairDataRoot returns path' {
|
|
$p = Initialize-RdpRepairDataRoot
|
|
if (-not (Test-Path -LiteralPath $p)) { throw 'dir not created' }
|
|
}
|
|
|
|
Assert-Test 'Get-RdpRepairComputerDnsNames non-empty' {
|
|
$n = @(Get-RdpRepairComputerDnsNames)
|
|
if ($n.Count -lt 1) { throw 'no dns names' }
|
|
}
|
|
|
|
Assert-Test 'Get-RdpRepairRemoteDesktopCertificates exposes Count' {
|
|
$certs = @(Get-RdpRepairRemoteDesktopCertificates)
|
|
$null = $certs.Count
|
|
}
|
|
|
|
Assert-Test 'single cert in PSCustomObject keeps Thumbprint' {
|
|
$mock = [PSCustomObject]@{
|
|
Thumbprint = 'DEADBEEF'
|
|
Subject = 'CN=test'
|
|
NotAfter = (Get-Date).AddDays(30)
|
|
HasKey = $true
|
|
}
|
|
$box = [PSCustomObject]@{ Certificates = @($mock) }
|
|
$first = Get-RdpRepairFirstCertificate -Certificates $box.Certificates
|
|
if ($first.Thumbprint -ne 'DEADBEEF') { throw 'Thumbprint lost via property unwrap' }
|
|
}
|
|
|
|
Assert-Test 'Get-RdpRepairCertificateArray flattens nested Object[]' {
|
|
$mock = [PSCustomObject]@{
|
|
Thumbprint = 'NESTED01'
|
|
Subject = 'CN=nested'
|
|
NotAfter = (Get-Date).AddDays(1)
|
|
HasKey = $true
|
|
}
|
|
$box = [PSCustomObject]@{ Certificates = ,[object[]]@(,[object[]]@($mock)) }
|
|
$first = Get-RdpRepairFirstCertificate -Certificates $box.Certificates
|
|
if ($first.Thumbprint -ne 'NESTED01') { throw "expected NESTED01 got $($first.Thumbprint)" }
|
|
}
|
|
|
|
Assert-Test 'Get-RdpRepairDiagnosticSnapshot object shape' {
|
|
if (-not (Test-RdpRepairIsAdministrator)) {
|
|
Write-Host ' (skip snapshot fields - not admin)' -ForegroundColor DarkYellow
|
|
return
|
|
}
|
|
$s = Get-RdpRepairDiagnosticSnapshot
|
|
foreach ($prop in @('ComputerName', 'SecurityLayer', 'CertCount', 'TermService')) {
|
|
if (-not $s.PSObject.Properties[$prop]) { throw "missing property $prop" }
|
|
}
|
|
if ($s.CertCount -gt 0) {
|
|
$first = Get-RdpRepairFirstCertificate -Certificates $s.Certificates
|
|
if (-not $first.Thumbprint) { throw 'snapshot cert missing Thumbprint' }
|
|
}
|
|
}
|
|
|
|
Assert-Test 'Registry path constants well-formed' {
|
|
if ($script:RdpRepairRdpTcpKey -notmatch 'RDP-Tcp$') { throw 'bad rdp tcp key' }
|
|
}
|
|
|
|
Assert-Test 'Export-RdpRepairDiagnosticReport writes file' {
|
|
if (-not (Test-RdpRepairIsAdministrator)) {
|
|
Write-Host ' (skip report write - not admin)' -ForegroundColor DarkYellow
|
|
return
|
|
}
|
|
$log = Join-Path $env:TEMP 'rdp-selftest.log'
|
|
$rp = Export-RdpRepairDiagnosticReport -LogPath $log
|
|
if (-not (Test-Path -LiteralPath $rp)) { throw 'report not written' }
|
|
if ((Get-Item $rp).Length -lt 50) { throw 'report too small' }
|
|
}
|
|
|
|
Assert-Test 'Confirm-RdpRepairContinue noninteractive' {
|
|
$env:RDP_REPAIR_NONINTERACTIVE = '1'
|
|
if (-not (Confirm-RdpRepairContinue 'test')) { throw 'expected true' }
|
|
Remove-Item Env:\RDP_REPAIR_NONINTERACTIVE -ErrorAction SilentlyContinue
|
|
}
|
|
|
|
Assert-Test 'SecurityLayer values documented (0,1,2)' {
|
|
foreach ($v in 0, 1, 2) {
|
|
if ($v -lt 0 -or $v -gt 2) { throw 'invalid' }
|
|
}
|
|
}
|
|
|
|
Assert-Test 'docs REPAIR-INSTALL.md exists' {
|
|
$d = Join-Path (Split-Path $PSScriptRoot -Parent) 'docs\REPAIR-INSTALL.md'
|
|
if (-not (Test-Path -LiteralPath $d)) { throw 'missing' }
|
|
}
|
|
|
|
Assert-Test 'docs RETURN-TO-TLS.md exists' {
|
|
$d = Join-Path (Split-Path $PSScriptRoot -Parent) 'docs\RETURN-TO-TLS.md'
|
|
if (-not (Test-Path -LiteralPath $d)) { throw 'missing' }
|
|
}
|
|
|
|
if ($IncludeAdminTests -and (Test-RdpRepairIsAdministrator)) {
|
|
Write-Host "`n--- Admin-only tests ---" -ForegroundColor Cyan
|
|
Assert-Test 'TermService service exists' {
|
|
if (-not (Get-Service TermService -ErrorAction SilentlyContinue)) { throw 'TermService missing' }
|
|
}
|
|
Assert-Test 'Get-RdpRepairRemoteDesktopCertificates no throw' {
|
|
$null = Get-RdpRepairRemoteDesktopCertificates
|
|
}
|
|
} elseif ($IncludeAdminTests) {
|
|
Write-Host "`n(Warning: -IncludeAdminTests skipped - not running as Administrator)" -ForegroundColor Yellow
|
|
}
|
|
|
|
Write-Host "`n=== Results: PASS=$script:SelfTestPassed FAIL=$script:SelfTestFailed ===" -ForegroundColor Cyan
|
|
if ($script:SelfTestFailed -gt 0) {
|
|
exit 1
|
|
}
|
|
exit 0
|