| 1 | param( |
| 2 | [Parameter(Mandatory = $true)][string]$Installer, |
| 3 | [ValidateSet("Snapshot", "Compare", "Rollback", "Upgrade")][string]$Mode, |
| 4 | [Parameter(Mandatory = $true)][string]$Evidence |
| 5 | ) |
| 6 | $ErrorActionPreference = "Stop" |
| 7 | $root = Join-Path $env:LOCALAPPDATA "Programs\Zipelo" |
| 8 | $names = @("Zipelo.exe", "zipelo-archive-worker.exe", "LICENSE", "THIRD-PARTY-LICENSES.txt", "BUILD-INFO.json", "Uninstall.exe", "install.ini") |
| 9 | |
| 10 | function Snapshot { |
| 11 | $files = [ordered]@{} |
| 12 | foreach ($name in $names) { |
| 13 | $files[$name] = (Get-FileHash (Join-Path $root $name) -Algorithm SHA256).Hash |
| 14 | } |
| 15 | $keys = @("Software\Zipelo", "Software\Classes\Zipelo.Archive", "Software\Microsoft\Windows\CurrentVersion\Uninstall\Zipelo") |
| 16 | foreach ($extension in @(".zip", ".7z", ".tar", ".gz", ".tgz")) { |
| 17 | $keys += "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\$extension\UserChoice" |
| 18 | } |
| 19 | $registry = [ordered]@{} |
| 20 | foreach ($key in $keys) { |
| 21 | $item = Get-Item "HKCU:\$key" -ErrorAction SilentlyContinue |
| 22 | if ($null -eq $item) { continue } |
| 23 | $items = @($item) + @(Get-ChildItem "HKCU:\$key" -Recurse) |
| 24 | foreach ($child in $items) { |
| 25 | foreach ($name in ($child.GetValueNames() | Sort-Object)) { |
| 26 | $registry["$($child.Name)\$name"] = $child.GetValue($name) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | $registry["RegisteredApplications\Zipelo"] = (Get-Item "HKCU:\Software\RegisteredApplications").GetValue("Zipelo") |
| 31 | return [ordered]@{ |
| 32 | files = $files |
| 33 | shortcut = (Get-FileHash (Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\Zipelo.lnk")).Hash |
| 34 | registry = $registry |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function RunInstaller { |
| 39 | $process = Start-Process $Installer -ArgumentList "/S" -PassThru |
| 40 | if (-not $process.WaitForExit(60000)) { |
| 41 | throw "설치가 제한 시간 안에 종료되지 않음. VM에서 상태 확인이 필요함." |
| 42 | } |
| 43 | return $process.ExitCode |
| 44 | } |
| 45 | |
| 46 | function AssertEqual($expected, $actual, $message) { |
| 47 | if (($expected | ConvertTo-Json -Depth 8 -Compress) -cne ($actual | ConvertTo-Json -Depth 8 -Compress)) { |
| 48 | throw $message |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | $before = Snapshot |
| 53 | switch ($Mode) { |
| 54 | "Snapshot" { |
| 55 | $before | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $Evidence |
| 56 | "snapshot_saved" |
| 57 | } |
| 58 | "Compare" { |
| 59 | $saved = Get-Content $Evidence -Raw | ConvertFrom-Json |
| 60 | AssertEqual $saved $before "기존 설치 또는 연결이 바뀜" |
| 61 | "all_seven_files_shortcut_and_associations_unchanged" |
| 62 | } |
| 63 | "Rollback" { |
| 64 | # 첫 파일 이동 뒤 작업자 파일 이동 실패도 복원되는지 확인함. |
| 65 | foreach ($name in @("Zipelo.exe", "zipelo-archive-worker.exe", "install.ini")) { |
| 66 | $stream = [IO.File]::Open((Join-Path $root $name), [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read) |
| 67 | try { $code = RunInstaller } finally { $stream.Dispose() } |
| 68 | if ($code -ne 1) { throw "잠긴 $name 업데이트의 종료 코드가 1이 아님: $code" } |
| 69 | AssertEqual $before (Snapshot) "잠긴 $name 실패 뒤 기존 설치가 바뀜" |
| 70 | "rollback_passed: $name" |
| 71 | } |
| 72 | $before | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $Evidence |
| 73 | } |
| 74 | "Upgrade" { |
| 75 | $code = RunInstaller |
| 76 | if ($code -ne 0) { throw "업데이트 실패: $code" } |
| 77 | $after = Snapshot |
| 78 | AssertEqual $before.registry $after.registry "업데이트가 기존 연결 또는 등록을 바꿈" |
| 79 | AssertEqual $before.shortcut $after.shortcut "업데이트가 기존 바로가기를 바꿈" |
| 80 | if ($before.files['Uninstall.exe'] -eq $after.files['Uninstall.exe']) { |
| 81 | throw "새 제거 프로그램으로 교체되지 않음" |
| 82 | } |
| 83 | $after | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $Evidence |
| 84 | "upgrade_passed: new_uninstaller_and_preserved_associations" |
| 85 | } |
| 86 | } |