174 lines
10 KiB
PowerShell
174 lines
10 KiB
PowerShell
[CmdletBinding()]
|
|
param([string]$LauncherPath, [string]$RepositoryRoot)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$repoRoot = if ($RepositoryRoot) { $RepositoryRoot } else { Split-Path $PSScriptRoot -Parent }
|
|
$sourcePath = Join-Path $repoRoot 'Plain Craft Launcher 2\Modules\Base\ModLauncherUpdate.vb'
|
|
$testRoot = Join-Path $repoRoot ('artifacts\update-tests\' + [Guid]::NewGuid().ToString('N'))
|
|
[IO.Directory]::CreateDirectory($testRoot) | Out-Null
|
|
|
|
$framework = Join-Path $env:WINDIR 'Microsoft.NET\Framework\v4.0.30319'
|
|
$testLibrary = Join-Path $testRoot ('LauncherUpdateTests_' + (Split-Path $testRoot -Leaf) + '.dll')
|
|
& (Join-Path $framework 'vbc.exe') /nologo /target:library "/out:$testLibrary" $sourcePath
|
|
if ($LASTEXITCODE -ne 0) { throw 'Update module compilation failed' }
|
|
$testAssembly = [Reflection.Assembly]::LoadFrom($testLibrary)
|
|
$updateType = $testAssembly.GetType('ModLauncherUpdate')
|
|
$parse = $updateType.GetMethod('ParseLauncherVersion')
|
|
$replace = $updateType.GetMethod('ReplaceLauncherFile')
|
|
$testState = @{ Passed = 0 }
|
|
function Assert-True([bool]$Condition, [string]$Description) {
|
|
if (-not $Condition) { throw "FAIL: $Description" }
|
|
$testState.Passed++
|
|
Write-Host "PASS: $Description"
|
|
}
|
|
function Replace-File([int]$PreviousId, [string]$Target, [string]$Source, [int]$Timeout = 30000, $Operations = $null) {
|
|
$replace.Invoke($null, @($PreviousId, $Target, $Source, $Timeout, $Operations))
|
|
}
|
|
|
|
foreach ($inputVersion in @('0.1.1', 'v0.1.1', '0.1.1-dev', '0.1.1+build')) {
|
|
Assert-True ($parse.Invoke($null, @($inputVersion)) -eq [Version]'0.1.1.0') "parse $inputVersion"
|
|
}
|
|
foreach ($inputVersion in @('', '1', '1.2', '1.2.3.4', '-1.2.3', '1.two.3', '2147483648.0.0')) {
|
|
Assert-True ($null -eq $parse.Invoke($null, @($inputVersion))) "reject invalid version [$inputVersion]"
|
|
}
|
|
Assert-True ($parse.Invoke($null, @('0.1.1')) -gt $parse.Invoke($null, @('0.1.0'))) '0.1.1 updates 0.1.0'
|
|
|
|
$target = Join-Path $testRoot 'PCL-ISE.exe'
|
|
$download = Join-Path $testRoot 'download.exe'
|
|
[IO.File]::WriteAllText($target, 'old version')
|
|
[IO.File]::WriteAllText($download, 'new version')
|
|
Replace-File 0 $target $download
|
|
Assert-True ([IO.File]::ReadAllText($target) -eq 'new version') 'replace target'
|
|
Assert-True ([IO.File]::ReadAllText($target + '.bak') -eq 'old version') 'retain backup'
|
|
Assert-True ([IO.File]::ReadAllText($download) -eq 'new version') 'retain helper/download'
|
|
|
|
$missingRejected = $false
|
|
try { Replace-File 0 $target (Join-Path $testRoot 'missing.exe') } catch { $missingRejected = $true }
|
|
Assert-True ($missingRejected -and [IO.File]::ReadAllText($target) -eq 'new version') 'missing download leaves target intact'
|
|
|
|
# An isolated test process holds the old executable open during a normal update.
|
|
$oldProgram = Join-Path $testRoot 'OldLauncher.cs'
|
|
[IO.File]::WriteAllText($oldProgram, 'using System.Threading; class OldLauncher { static void Main() { Thread.Sleep(1500); } }')
|
|
$oldExe = Join-Path $testRoot 'OldLauncher.exe'
|
|
$csc = Join-Path $framework 'csc.exe'
|
|
& $csc /nologo /target:winexe "/out:$oldExe" $oldProgram
|
|
if ($LASTEXITCODE -ne 0) { throw 'Test launcher compilation failed' }
|
|
$oldProcess = Start-Process -FilePath $oldExe -WindowStyle Hidden -PassThru
|
|
$timeoutRejected = $false
|
|
try { Replace-File $oldProcess.Id $target $download 20 } catch { $timeoutRejected = $_.Exception.InnerException -is [TimeoutException] }
|
|
Assert-True ($timeoutRejected -and [IO.File]::ReadAllText($target) -eq 'new version') 'wait timeout leaves target intact'
|
|
$oldProcess.WaitForExit()
|
|
|
|
$newTarget = Join-Path $testRoot 'new-target.exe'
|
|
Replace-File 0 $newTarget $download
|
|
Assert-True ([IO.File]::ReadAllText($newTarget) -eq 'new version') 'install into absent target'
|
|
Assert-True (@(Get-ChildItem $testRoot -Filter '*.new').Count -eq 0) 'no staged files left behind'
|
|
|
|
$faultLibrary = Join-Path $testRoot 'UpdateFileOperationsTests.dll'
|
|
& $csc /nologo /target:library "/reference:$testLibrary" "/out:$faultLibrary" (Join-Path $repoRoot 'tests\LauncherUpdateFileOperations.cs')
|
|
if ($LASTEXITCODE -ne 0) { throw 'Fault operation compilation failed' }
|
|
$faultType = [Reflection.Assembly]::LoadFrom($faultLibrary).GetType('TestLauncherUpdateFileOperations')
|
|
function New-FaultOperations([string]$TargetPath) {
|
|
$operations = [Activator]::CreateInstance($faultType)
|
|
$operations.TargetPath = $TargetPath
|
|
$operations.RetryTimeoutMilliseconds = 0
|
|
return $operations
|
|
}
|
|
function Get-ReplaceFailure([string]$TargetPath, $Operations) {
|
|
try { Replace-File 0 $TargetPath $download 30000 $Operations } catch { return $_.Exception.InnerException }
|
|
throw 'Expected a file operation failure'
|
|
}
|
|
|
|
$backupFailureTarget = Join-Path $testRoot 'backup-failure.exe'
|
|
[IO.File]::WriteAllText($backupFailureTarget, 'old version')
|
|
$fault = New-FaultOperations $backupFailureTarget
|
|
$fault.FailBackup = $true
|
|
$failure = Get-ReplaceFailure $backupFailureTarget $fault
|
|
Assert-True ([IO.File]::ReadAllText($backupFailureTarget) -eq 'old version') 'backup failure never deletes old program'
|
|
|
|
$copyFailureTarget = Join-Path $testRoot 'copy-failure.exe'
|
|
[IO.File]::WriteAllText($copyFailureTarget, 'old version')
|
|
$fault = New-FaultOperations $copyFailureTarget
|
|
$fault.CopyFailuresRemaining = 1
|
|
$fault.LeavePartialFile = $true
|
|
$failure = Get-ReplaceFailure $copyFailureTarget $fault
|
|
Assert-True ([IO.File]::ReadAllText($copyFailureTarget) -eq 'old version') 'interrupted copy restores old program over a partial target'
|
|
Assert-True ([IO.File]::ReadAllText($copyFailureTarget + '.bak') -eq 'old version') 'rollback retains backup'
|
|
|
|
$hashFailureTarget = Join-Path $testRoot 'hash-failure.exe'
|
|
[IO.File]::WriteAllText($hashFailureTarget, 'old version')
|
|
$fault = New-FaultOperations $hashFailureTarget
|
|
$fault.CorruptInstalledFile = $true
|
|
$failure = Get-ReplaceFailure $hashFailureTarget $fault
|
|
Assert-True ([IO.File]::ReadAllText($hashFailureTarget) -eq 'old version') 'installed file hash failure restores old program'
|
|
|
|
$restoreFailureTarget = Join-Path $testRoot 'restore-failure.exe'
|
|
[IO.File]::WriteAllText($restoreFailureTarget, 'old version')
|
|
$fault = New-FaultOperations $restoreFailureTarget
|
|
$fault.CopyFailuresRemaining = 1
|
|
$fault.LeavePartialFile = $true
|
|
$fault.FailRestore = $true
|
|
$failure = Get-ReplaceFailure $restoreFailureTarget $fault
|
|
Assert-True ($failure -is [AggregateException]) 'failed restore reports installation and restoration failures'
|
|
Assert-True ([IO.File]::ReadAllText($restoreFailureTarget + '.bak') -eq 'old version') 'failed restore retains backup for manual recovery'
|
|
Assert-True ([IO.File]::ReadAllText($download) -eq 'new version') 'failed restore retains source download'
|
|
|
|
$retryTarget = Join-Path $testRoot 'retry.exe'
|
|
[IO.File]::WriteAllText($retryTarget, 'old version')
|
|
$fault = New-FaultOperations $retryTarget
|
|
$fault.CopyFailuresRemaining = 1
|
|
$fault.LeavePartialFile = $true
|
|
$fault.RetryTimeoutMilliseconds = 1000
|
|
Replace-File 0 $retryTarget $download 30000 $fault
|
|
Assert-True ([IO.File]::ReadAllText($retryTarget) -eq 'new version') 'temporary copy failure retries successfully'
|
|
|
|
$newFailedTarget = Join-Path $testRoot 'failed-first-install.exe'
|
|
$fault = New-FaultOperations $newFailedTarget
|
|
$fault.CopyFailuresRemaining = 1
|
|
$fault.LeavePartialFile = $true
|
|
$failure = Get-ReplaceFailure $newFailedTarget $fault
|
|
Assert-True (-not [IO.File]::Exists($newFailedTarget)) 'failed first install removes partial target'
|
|
|
|
$cleanupTarget = Join-Path $testRoot 'cleanup.exe'
|
|
[IO.File]::WriteAllText($cleanupTarget, 'old version')
|
|
$fault = New-FaultOperations $cleanupTarget
|
|
$fault.FailCleanup = $true
|
|
Replace-File 0 $cleanupTarget $download 30000 $fault
|
|
Assert-True ([IO.File]::ReadAllText($cleanupTarget) -eq 'new version') 'cleanup failure does not report a successful installation as failed'
|
|
|
|
if ($LauncherPath) {
|
|
$LauncherPath = (Resolve-Path $LauncherPath).Path
|
|
$liveTarget = Join-Path $testRoot 'RunningLauncher.exe'
|
|
Copy-Item -LiteralPath $oldExe -Destination $liveTarget
|
|
$previous = Start-Process -FilePath $liveTarget -WindowStyle Hidden -PassThru
|
|
$arguments = '--update {0} "{1}" "{2}" false' -f $previous.Id, $liveTarget, $LauncherPath
|
|
$helper = Start-Process -FilePath $LauncherPath -ArgumentList $arguments -WorkingDirectory $testRoot -WindowStyle Hidden -PassThru
|
|
if (-not $helper.WaitForExit(20000)) { throw 'Update helper did not finish within 20 seconds' }
|
|
$helper.Refresh()
|
|
# The launcher uses ProcessReturnValues.TaskDone (5) for command-line tasks.
|
|
Assert-True ($helper.ExitCode -eq 5) 'real launcher update helper exits successfully'
|
|
Assert-True ((Get-FileHash $liveTarget).Hash -eq (Get-FileHash $LauncherPath).Hash) 'real helper replaces running old launcher after it exits'
|
|
Assert-True ((Get-FileHash ($liveTarget + '.bak')).Hash -eq (Get-FileHash $oldExe).Hash) 'real helper preserves previous launcher'
|
|
$resultPath = $updateType.GetMethod('GetLauncherUpdateResultPath').Invoke($null, @([string]$liveTarget))
|
|
$receipt = Get-Content -LiteralPath $resultPath -Raw | ConvertFrom-Json
|
|
Assert-True ($receipt.Succeeded -and $receipt.HelperProcessId -eq $helper.Id -and $receipt.TargetPath -eq $liveTarget) 'real helper saves a successful update receipt for the next startup'
|
|
|
|
$lockedTarget = Join-Path $testRoot 'locked-target.exe'
|
|
Copy-Item -LiteralPath $oldExe -Destination $lockedTarget
|
|
$originalHash = (Get-FileHash $lockedTarget).Hash
|
|
$lockStream = [IO.File]::Open($lockedTarget, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::None)
|
|
try {
|
|
$arguments = '--update 0 "{0}" "{1}" false' -f $lockedTarget, $LauncherPath
|
|
$helper = Start-Process -FilePath $LauncherPath -ArgumentList $arguments -WorkingDirectory $testRoot -WindowStyle Hidden -PassThru
|
|
if (-not $helper.WaitForExit(20000)) { throw 'Failed update helper did not finish within 20 seconds' }
|
|
$helper.Refresh()
|
|
Assert-True ($helper.ExitCode -ne 5) 'real helper reports file replacement failure without a modal dialog'
|
|
} finally { $lockStream.Dispose() }
|
|
Assert-True ((Get-FileHash $lockedTarget).Hash -eq $originalHash) 'real helper failure leaves old executable intact'
|
|
$resultPath = $updateType.GetMethod('GetLauncherUpdateResultPath').Invoke($null, @([string]$lockedTarget))
|
|
$receipt = Get-Content -LiteralPath $resultPath -Raw | ConvertFrom-Json
|
|
Assert-True (-not $receipt.Succeeded -and -not [string]::IsNullOrWhiteSpace($receipt.ErrorMessage)) 'real helper saves an error receipt for the next startup'
|
|
}
|
|
|
|
Write-Host "$($testState.Passed) checks passed. Artifacts: $testRoot"
|