Files
2026-11-06 15:16:47 -05:00

101 lines
4.7 KiB
PowerShell

<#
.SYNOPSIS
Checks that Windows executables carry the product icon and version info
(README, Branding).
.DESCRIPTION
For each .exe: its first icon group (RT_GROUP_ICON, what Explorer, Task
Manager and Services show) must hold exactly the frames of
assets/icons/app.ico, byte for byte, and its version resource must name the
file, the product, the company and the copyright. Exits 1 on any failure.
.EXAMPLE
./scripts/tests/Test-Branding.ps1 artifacts/agent/1.4.0.7/win-x64/*.exe
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0, ValueFromRemainingArguments)]
[string[]]$Path,
[string]$Icon
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if (-not $Icon) { $Icon = Join-Path $repoRoot 'assets/icons/app.ico' }
Add-Type -Namespace ExecuteR -Name ExeResources -MemberDefinition @'
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr LoadLibraryExW(string path, IntPtr file, uint flags);
[DllImport("kernel32.dll")] static extern bool FreeLibrary(IntPtr module);
[DllImport("kernel32.dll")] static extern IntPtr FindResourceW(IntPtr module, IntPtr name, IntPtr type);
[DllImport("kernel32.dll")] static extern IntPtr LoadResource(IntPtr module, IntPtr info);
[DllImport("kernel32.dll")] static extern IntPtr LockResource(IntPtr data);
[DllImport("kernel32.dll")] static extern uint SizeofResource(IntPtr module, IntPtr info);
delegate bool EnumNameProc(IntPtr module, IntPtr type, IntPtr name, IntPtr param);
[DllImport("kernel32.dll")] static extern bool EnumResourceNamesW(IntPtr module, IntPtr type, EnumNameProc proc, IntPtr param);
static byte[] Read(IntPtr module, IntPtr name, IntPtr type) {
IntPtr info = FindResourceW(module, name, type);
if (info == IntPtr.Zero) return null;
byte[] bytes = new byte[SizeofResource(module, info)];
System.Runtime.InteropServices.Marshal.Copy(LockResource(LoadResource(module, info)), bytes, 0, bytes.Length);
return bytes;
}
// The first RT_GROUP_ICON's frames (RT_ICON payloads), in group order.
public static System.Collections.Generic.List<byte[]> IconFrames(string path) {
const uint LOAD_LIBRARY_AS_DATAFILE = 0x2, LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x20;
IntPtr module = LoadLibraryExW(path, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (module == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
try {
IntPtr group = IntPtr.Zero;
EnumResourceNamesW(module, (IntPtr)14, (m, t, n, p) => { group = n; return false; }, IntPtr.Zero);
var frames = new System.Collections.Generic.List<byte[]>();
if (group == IntPtr.Zero) return frames;
byte[] dir = Read(module, group, (IntPtr)14);
int count = BitConverter.ToUInt16(dir, 4);
for (int i = 0; i < count; i++) {
int id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12);
frames.Add(Read(module, (IntPtr)id, (IntPtr)3));
}
return frames;
} finally { FreeLibrary(module); }
}
'@
# An .ico's frames, in file order.
function Get-IcoFrames([string]$file) {
$ico = [System.IO.File]::ReadAllBytes((Resolve-Path $file).Path)
for ($i = 0; $i -lt [BitConverter]::ToUInt16($ico, 4); $i++) {
$length = [BitConverter]::ToInt32($ico, 6 + 16 * $i + 8)
$offset = [BitConverter]::ToInt32($ico, 6 + 16 * $i + 12)
[Convert]::ToBase64String($ico, $offset, $length)
}
}
# Executables with an icon of their own (the Being Viewed banner); everything else carries $Icon.
$ownIcons = @{ 'ExecuteR-Banner.exe' = (Join-Path $repoRoot 'assets/icons/banner.ico') }
$failed = 0
foreach ($file in Get-Item $Path) {
$problems = @()
$iconFile = if ($ownIcons.ContainsKey($file.Name) -and -not $PSBoundParameters.ContainsKey('Icon')) { $ownIcons[$file.Name] } else { $Icon }
$expected = @(Get-IcoFrames $iconFile)
$actual = @([ExecuteR.ExeResources]::IconFrames($file.FullName) | ForEach-Object { [Convert]::ToBase64String($_) })
if ($actual.Count -eq 0) { $problems += 'no icon' }
elseif (Compare-Object $expected $actual -SyncWindow 0) { $problems += "icon is not $iconFile ($($actual.Count) frames)" }
$info = $file.VersionInfo
foreach ($field in 'FileDescription', 'ProductName', 'CompanyName', 'LegalCopyright', 'FileVersion') {
if ([string]::IsNullOrWhiteSpace($info.$field)) { $problems += "no $field" }
}
if ($problems) {
$failed++
Write-Host "FAIL $($file.Name): $($problems -join '; ')"
}
else {
Write-Host ("ok {0}: {1} icon frames; {2} | {3} | {4} | {5} | {6}" -f $file.Name, $actual.Count,
$info.FileDescription, $info.ProductName, $info.FileVersion, $info.CompanyName, $info.LegalCopyright)
}
}
if ($failed) { exit 1 }