1param([string] $ExecutableDirectory = (Join-Path (Split-Path $PSScriptRoot) 'x64\Release'))
2
3$ErrorActionPreference = 'Stop'
4$root = Split-Path $PSScriptRoot
5$tokens = $null
6$parseErrors = $null
7$ast = [System.Management.Automation.Language.Parser]::ParseFile(
8 (Join-Path $root 'Send-ReleaseToBill.ps1'), [ref] $tokens, [ref] $parseErrors)
9if ($parseErrors.Count) { throw ($parseErrors | Out-String) }
10# Load only the pure packaging function: never read credentials or contact Discord.
11$function = $ast.Find({ param($node)
12 $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'New-ReleaseUpload'
13}, $true)
14Invoke-Expression $function.Extent.Text
15
16function Check($Condition, [string] $Message) {
17 if (-not $Condition) { throw $Message }
18 $script:checks++
19}
20function Hash([byte[]] $Bytes) {
21 $sha = [System.Security.Cryptography.SHA256]::Create()
22 try { return [Convert]::ToBase64String($sha.ComputeHash($Bytes)) }
23 finally { $sha.Dispose() }
24}
25$checks = 0
26$small = @('Rise.exe', 'CrashHandler.exe') | ForEach-Object {
27 [PSCustomObject]@{ Name = $_; Bytes = [byte[]](1, 2, 3); Length = 3; Type = 'application/octet-stream' }
28}
29$raw = New-ReleaseUpload -Files $small
30Check (-not $raw.Zipped -and $raw.Files.Count -eq 2) 'Small builds must retain two executable attachments.'
31Check ((Hash $raw.Files[0].Bytes) -eq (Hash $small[0].Bytes)) 'Raw attachment changed bytes.'
32$rejected = $false
33try { $null = New-ReleaseUpload -Files $small -MaximumFileBytes 1 }
34catch { $rejected = $_.Exception.Message -like 'Compressed alpha is still too large*' }
35Check $rejected 'Oversized compressed output must fail before a network request.'
36
37$compressible = @('Rise.exe', 'CrashHandler.exe') | ForEach-Object {
38 [PSCustomObject]@{ Name = $_; Bytes = (New-Object byte[] 4096); Length = 4096; Type = 'application/octet-stream' }
39}
40$singleZip = New-ReleaseUpload -Files $compressible -MaximumFileBytes 1024
41Check ($singleZip.Zipped -and -not $singleZip.Split -and $singleZip.Files.Count -eq 1) 'Small compressed archives should remain one ZIP.'
42
43$rejected = $false
44try { $null = New-ReleaseUpload -Files $small -MaximumFileBytes 1 -AllowSplitArchive }
45catch { $rejected = $_.Exception.Message -like '*ten attachments*' }
46Check $rejected 'Excessive split counts must fail before a network request.'
47
48$noise = New-Object byte[] (26MB)
49(New-Object System.Random(2138)).NextBytes($noise)
50$rejected = $false
51try { $null = New-ReleaseUpload -Files @([PSCustomObject]@{Name='Rise.exe';Bytes=$noise;Length=$noise.Length;Type='application/octet-stream'}) -AllowSplitArchive }
52catch { $rejected = $_.Exception.Message -like '*exceeds one Discord message*' }
53Check $rejected 'Splitting must not bypass the total message size limit.'
54$noise = $null
55
56$files = @('Rise.exe', 'CrashHandler.exe') | ForEach-Object {
57 $bytes = [System.IO.File]::ReadAllBytes((Join-Path $ExecutableDirectory $_))
58 [PSCustomObject]@{ Name = $_; Bytes = $bytes; Length = $bytes.Length; Type = 'application/octet-stream' }
59}
60$upload = New-ReleaseUpload -Files $files -AllowSplitArchive
61Check ($upload.Zipped -and $upload.Split -and $upload.Files.Count -eq 2) 'Current x64 alpha must become two ZIP parts.'
62Check ((@($upload.Files | Where-Object { $_.Length -gt 20MB })).Count -eq 0) 'Part exceeds the attachment limit.'
63Check (($upload.Files | Measure-Object -Property Length -Sum).Sum -le (25MB - 64KB)) 'Parts exceed message size budget.'
64$stream = New-Object System.IO.MemoryStream
65$partIndex = 0
66foreach ($part in $upload.Files) {
67 $partIndex++
68 Check ($part.Name -eq ('Rise-alpha.zip.{0:D3}' -f $partIndex)) 'Split parts must be consecutively named in reassembly order.'
69 $stream.Write($part.Bytes, 0, $part.Bytes.Length)
70}
71$sha = [System.Security.Cryptography.SHA256]::Create()
72try { $archiveHash = [BitConverter]::ToString($sha.ComputeHash($stream.ToArray())).Replace('-', '').ToLowerInvariant() }
73finally { $sha.Dispose() }
74Check ($archiveHash -eq $upload.ArchiveSHA256) 'Reassembled archive hash differs from the delivery instructions.'
75$archiveBytes = $stream.Length
76$stream.Position = 0
77$archive = New-Object System.IO.Compression.ZipArchive($stream, [System.IO.Compression.ZipArchiveMode]::Read)
78try {
79 Check ($archive.Entries.Count -eq 2) 'ZIP must contain exactly the two authorized binaries.'
80 foreach ($file in $files) {
81 $entry = $archive.GetEntry($file.Name)
82 Check ($null -ne $entry) "ZIP is missing $($file.Name)."
83 $extracted = New-Object System.IO.MemoryStream
84 $entryStream = $entry.Open()
85 try {
86 $entryStream.CopyTo($extracted)
87 Check ((Hash $extracted.ToArray()) -eq (Hash $file.Bytes)) "ZIP changed $($file.Name)."
88 }
89 finally { $entryStream.Dispose(); $extracted.Dispose() }
90 }
91}
92finally { $archive.Dispose(); $stream.Dispose() }
93Write-Host "PASS $checks release upload checks; $($upload.Files.Count) parts, ZIP $archiveBytes bytes; both extracted binary hashes match."