adds OfficeStaging and SystemTemp

This commit is contained in:
2026-10-23 12:25:00 -04:00
parent f9daec0542
commit e2163a6158
4 changed files with 229 additions and 9 deletions
+97 -4
View File
@@ -34,6 +34,8 @@ try {
foreach ($definition in $core.FindAll({param($n) $n -is [Management.Automation.Language.FunctionDefinitionAst]}, $false)) {
. ([scriptblock]::Create($definition.Extent.Text))
}
# Never contend with a real deployment on the test host.
function New-SystemCleanupOfficeMutex { return [Threading.Mutex]::new($false) }
$messageWriter = ${function:Write-SystemCleanupMessage}
$script:messages = @(); $script:details = @()
function Write-SystemCleanupMessage {
@@ -209,7 +211,7 @@ try {
$o.ReportOnly=$true
foreach ($name in $blocked) { if ($name -ne 'DiskCleanup') { $o[$name]=$true } }
$result = @(Invoke-SystemCleanup $o)
Assert-Cleanup ($result.Count -eq 22 -and @($result | Where-Object Status -eq 'Blocked on server').Count -eq $blocked.Count) 'Server report must list every blocked category.'
Assert-Cleanup ($result.Count -eq 23 -and @($result | Where-Object Status -eq 'Blocked on server').Count -eq $blocked.Count) 'Server report must list every blocked category.'
Assert-Cleanup (@($script:assessed | Where-Object { $_ -in $blocked }).Count -eq 0) 'Server report must not assess blocked categories.'
}
@@ -329,6 +331,96 @@ try {
Assert-Cleanup ($d.Count -eq 0 -and $script:fileReads -eq 0 -and $script:storageReads -eq 0 -and $script:diagnosticProcesses -eq 0) 'Apply mode must not run report diagnostics.'
}
# Abandoned Office payloads: report/apply, disabled options, server support,
# identity, retention, process/lock/link protection, and durable helper survival.
& {
$officeAst=Read-CleanupAst 'Install-MicrosoftOffice365.ps1'
foreach ($definition in $officeAst.FindAll({param($n) $n -is [Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -in @('Get-OfficeDeploymentDefinitions','Get-OfficeStagingNames')},$false)) {
. ([scriptblock]::Create($definition.Extent.Text))
}
$names=@(Get-OfficeStagingNames)
$script:officeProcesses=@(); $script:officeInventoryFails=$false
function Get-CimInstance { if ($script:officeInventoryFails) { throw 'Synthetic process inventory failure' }; $script:officeProcesses }
function New-StageFixture {
param($Parent,$Id)
$relative=$Parent+'\'+($Id*32)
$null=New-CleanupFixture ($relative+'\options.json') '{"ProductId":"O365BusinessRetail","OfficeArchitecture":"64","UpdateChannel":"Current"}'
foreach ($name in $names) {
$leaf=if ($name -in @('ODT','GetHelp')) { $name+'\payload.dat' } else { $name }
$null=New-CleanupFixture ($relative+'\'+$leaf)
}
foreach ($name in @('Office-UserCleanup.ps1','user-cleanup.lock','install.xml','office-install.stdout.log','unknown.exe')) { $null=New-CleanupFixture ($relative+'\'+$name) }
$folder=Join-Path $testRoot $relative
foreach ($dir in @(Get-ChildItem -LiteralPath $folder -Recurse -Directory)+@(Get-Item -LiteralPath $folder)) { [IO.Directory]::SetLastWriteTimeUtc($dir.FullName,[datetime]::UtcNow.AddDays(-60)) }
return $folder
}
function Test-StageCleanup {
param([bool]$ReportOnly=$true,[bool]$Enabled=$true,[bool]$Server=$false)
$c=New-CleanupContext -ReportOnly $ReportOnly -Server $Server; $c.Options.OfficeStaging=$Enabled
Invoke-SystemCleanupCategory $c 'OfficeStaging' { Clear-SystemCleanupOfficeStaging $c $stageRoots ([datetime]::UtcNow.AddDays(-7)) }
return $c
}
$folder=New-StageFixture 'office-fixture\Windows\SystemTemp\EmberkomOfficeDeployment' 'a'
$legacy=New-StageFixture 'office-fixture\ProgramData\EmberkomOfficeDeployment' 'b'
$stageRoots=@((Split-Path -Parent $folder),(Split-Path -Parent $legacy))
Assert-Cleanup ((Get-SystemCleanupOfficeStagingRoots @()) -contains (Join-Path $env:SystemRoot 'SystemTemp\EmberkomOfficeDeployment')) 'SystemTemp must be a supported Office staging root.'
$c=Test-StageCleanup -Enabled $false
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq (2*$names.Count) -and !$c.Stats.OfficeStaging.Enabled -and (Test-Path -LiteralPath (Join-Path $folder 'ODT\payload.dat'))) 'Disabled report must measure every installer artifact without deleting it.'
$c=Test-StageCleanup -Server $true
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq (2*$names.Count) -and $c.Errors -eq 0) 'Owned staging cleanup must support server OSes.'
$c=New-CleanupContext
Invoke-SystemCleanupCategory $c 'SystemTemp' { Clear-SystemCleanupTree $c 'SystemTemp' (Split-Path -Parent $stageRoots[0]) ([datetime]::UtcNow.AddDays(-7)) -Recurse }
Assert-Cleanup ($c.Stats.SystemTemp.CandidateFiles -eq 0) 'Generic temp traversal must exclude Office staging entirely.'
$c=Test-StageCleanup -ReportOnly $false -Enabled $false
Assert-Cleanup ((Test-Path -LiteralPath (Join-Path $folder 'ODT\payload.dat')) -and !$c.Stats.Count) 'Disabled apply must preserve staged media.'
$script:officeProcesses=@([pscustomobject]@{Name='setup.exe'})
$c=Test-StageCleanup
Assert-Cleanup ($c.Stats.OfficeStaging.Status -eq 'Skipped' -and $c.Stats.OfficeStaging.EstimateStatus -eq 'Not assessed') 'An installer without a workspace path must block staging cleanup.'
$script:officeProcesses=@([pscustomobject]@{Name='worker.exe'; ExecutablePath=(Join-Path $folder 'ODT\worker.exe')})
$c=Test-StageCleanup
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq $names.Count -and $c.Stats.OfficeStaging.Notes -match 'retained') 'A running process must retain its workspace while allowing other abandoned media.'
$script:officeProcesses=@(); $script:officeInventoryFails=$true
$c=Test-StageCleanup -ReportOnly $false
Assert-Cleanup ($c.Errors -eq 1 -and (Test-Path -LiteralPath (Join-Path $folder 'ODT\payload.dat'))) 'Failed process inventory must prevent deletion.'
$script:officeInventoryFails=$false
& {
function New-SystemCleanupOfficeMutex {
$m=[pscustomobject]@{}
$m | Add-Member ScriptMethod WaitOne { param($Timeout) return $false }
$m | Add-Member ScriptMethod Dispose { }
return $m
}
$c=Test-StageCleanup -ReportOnly $false
Assert-Cleanup ($c.Stats.OfficeStaging.Status -eq 'Skipped' -and (Test-Path -LiteralPath (Join-Path $folder 'odt.exe'))) 'Active deployment mutex must block cleanup.'
}
$payload=Join-Path $folder 'ODT\payload.dat'
$handle=[IO.File]::Open($payload,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::None)
try { $c=Test-StageCleanup; Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq $names.Count) 'Locked media must not count as reclaimable.' } finally { $handle.Dispose() }
[IO.File]::SetLastWriteTimeUtc($payload,[datetime]::UtcNow)
$c=Test-StageCleanup
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq $names.Count) 'A fresh payload must retain the whole workspace media set.'
[IO.File]::SetLastWriteTimeUtc($payload,[datetime]::UtcNow.AddDays(-60))
$outside=New-CleanupFixture 'office-outside\keep.dat'
$junction=Join-Path $folder 'ODT\linked'
$null=New-Item -ItemType Junction -Path $junction -Target (Split-Path -Parent $outside)
try {
[IO.Directory]::SetLastWriteTimeUtc((Split-Path -Parent $junction),[datetime]::UtcNow.AddDays(-60))
$c=Test-StageCleanup
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq $names.Count -and (Test-Path -LiteralPath $outside)) 'Linked artifacts must retain the whole media set without traversing the link.'
} finally { [IO.Directory]::Delete($junction,$false); [IO.Directory]::SetLastWriteTimeUtc((Split-Path -Parent $junction),[datetime]::UtcNow.AddDays(-60)) }
$marker=Join-Path $folder 'options.json'; [IO.File]::WriteAllText($marker,'{}'); [IO.File]::SetLastWriteTimeUtc($marker,[datetime]::UtcNow.AddDays(-60))
$c=Test-StageCleanup
Assert-Cleanup ($c.Stats.OfficeStaging.CandidateFiles -eq $names.Count) 'Unrecognized folders must not be deleted based only on their names.'
[IO.File]::WriteAllText($marker,'{"ProductId":"O365BusinessRetail","OfficeArchitecture":"64","UpdateChannel":"Current"}'); [IO.File]::SetLastWriteTimeUtc($marker,[datetime]::UtcNow.AddDays(-60))
$c=Test-StageCleanup -ReportOnly $false
Assert-Cleanup ($c.Errors -eq 0 -and $c.Stats.OfficeStaging.RemovedFiles -eq (2*$names.Count) -and !(Test-Path -LiteralPath (Join-Path $folder 'ODT')) -and !(Test-Path -LiteralPath (Join-Path $legacy 'ODT'))) 'Apply must remove known aged media from SystemTemp and legacy ProgramData.'
foreach ($workspace in @($folder,$legacy)) {
foreach ($name in @('options.json','Office-UserCleanup.ps1','user-cleanup.lock','install.xml','office-install.stdout.log','unknown.exe')) {
Assert-Cleanup (Test-Path -LiteralPath (Join-Path $workspace $name)) "Office staging cleanup removed protected data: $name"
}
}
}
# File ages, literal brackets, nested fresh files, no changes in preview.
$root=Join-Path $testRoot 'temps'
$old=New-CleanupFixture 'temps\old[1].tmp'
@@ -558,6 +650,7 @@ try {
}
}
function Test-SystemCleanupAppRunning { return $false }
function Get-SystemCleanupOfficeStagingRoots { return (Join-Path $env:SystemRoot 'SystemTemp\EmberkomOfficeDeployment') }
function Get-Service { [pscustomobject]@{Status='Stopped'} }
function Remove-Item { throw 'PREVIEW MUTATION: Remove-Item' }
function New-ItemProperty { throw 'PREVIEW MUTATION: registry write' }
@@ -569,13 +662,13 @@ try {
$o=Get-SystemCleanupOptions
$script:messages=@(); $script:details=@()
$result=@(Invoke-SystemCleanup $o)
Assert-Cleanup ($result.Count -eq 22 -and @($result | Where-Object Status -ne 'Preview').Count -eq 0) "Full preview did not cover every category (count=$($result.Count))."
Assert-Cleanup ($result.Count -eq 23 -and @($result | Where-Object Status -ne 'Preview').Count -eq 0) "Full preview did not cover every category (count=$($result.Count))."
Assert-Cleanup (@($result | Where-Object { $_.Category -eq 'DownloadedExecutables' -and -not $_.Enabled -and $_.CandidateFiles -eq 1 }).Count -eq 1) 'Default-disabled downloads were not assessed by the entry point.'
Assert-Cleanup (@($result | Where-Object { $_.Category -eq 'ComponentStore' -and -not $_.Enabled -and $null -eq $_.CandidateBytes -and $_.EstimateStatus -eq 'Not estimated' }).Count -eq 1) 'Unknown native savings must not be reported as zero.'
Assert-Cleanup (Test-Path -LiteralPath (Join-Path $env:SystemRoot 'Temp\old.tmp')) 'Full preview removed a fixture.'
$compactOutput = ($script:messages | ForEach-Object { '[SystemCleanup] ' + $_ }) -join "`r`n"
$script:compactReportBytes = [Text.Encoding]::UTF8.GetByteCount($compactOutput)
Assert-Cleanup ($script:compactReportBytes -lt 4096) "22-category preview exceeded the 4 KiB regression budget ($script:compactReportBytes bytes)."
Assert-Cleanup ($script:compactReportBytes -lt 4096) "23-category preview exceeded the 4 KiB regression budget ($script:compactReportBytes bytes)."
foreach ($row in $result) {
Assert-Cleanup ([regex]::Matches($compactOutput, ('(?m)^\[SystemCleanup\] ' + [regex]::Escape($row.Category) + '\s')).Count -eq 1) "Compact report lost or repeated category $($row.Category)."
}
@@ -604,7 +697,7 @@ try {
Assert-Cleanup ($c.Errors -eq 1 -and $report -match 'CrashDumps.*Failed; incomplete.*Synthetic failure.*see verbose' -and $report.Length -lt 1600) 'A long multiline error must retain its failure status without flooding the summary.'
Assert-Cleanup (($script:details -join "`n").Contains(('long diagnostic ' * 300))) 'Compacted errors must remain complete in verbose diagnostics.'
}
Write-Host "Compact 22-category preview: $script:compactReportBytes UTF-8 bytes."
Write-Host "Compact 23-category preview: $script:compactReportBytes UTF-8 bytes."
Write-Host "PASS: $script:assertions offline cleanup assertions ($($PSVersionTable.PSVersion))."
} finally {
# Only this test's generated directory; links are removed non-recursively above.