From 4cd3a9cdf65a8f0b96de21420b98b8e02181e4f4 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 30 Mar 2026 11:49:10 +0200 Subject: move testing-only things into test/ --- test/scripts/verify_arch.ps1 | 82 +++++++++++++++++++++++++++++++++++++++++ test/scripts/verify_arch.sh | 56 ++++++++++++++++++++++++++++ test/scripts/verify_symbols.ps1 | 1 + test/scripts/verify_symbols.sh | 43 +++++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 test/scripts/verify_arch.ps1 create mode 100755 test/scripts/verify_arch.sh create mode 100644 test/scripts/verify_symbols.ps1 create mode 100755 test/scripts/verify_symbols.sh (limited to 'test/scripts') diff --git a/test/scripts/verify_arch.ps1 b/test/scripts/verify_arch.ps1 new file mode 100644 index 0000000..183a8c5 --- /dev/null +++ b/test/scripts/verify_arch.ps1 @@ -0,0 +1,82 @@ +param( + [Parameter(Mandatory=$true)] + [string] $Path, + [Parameter(Mandatory=$true)] + [ValidateSet('x64', 'x86')] + [string] $Arch +) + +$ErrorActionPreference = "Stop"; +Set-PSDebug -Strict + +function Get-MachineType { + param( + [Parameter(Mandatory=$true)] + [string] $Arch + ) + + $machine_type = switch ($Arch) { + 'x64' { 0x8664 } + 'x86' { 0x14c } + default { throw "Unsupported architecture: $Arch" } + } + return $machine_type +} + +function Parse-MachineType { + param( + [Parameter(Mandatory=$true)] + [string] $Path + ) + + $header_offset_offset = 0x3c + $machine_type_offset = 4 # Two words + + $bytes = [System.IO.File]::ReadAllBytes($Path) + $header_offset = [System.BitConverter]::ToUInt32($bytes, $header_offset_offset) + $machine_type = [System.BitConverter]::ToUInt16($bytes, $header_offset + $machine_type_offset) + + return $machine_type +} + +function Verify-Architecture { + param( + [Parameter(Mandatory=$true)] + [string] $Path, + [Parameter(Mandatory=$true)] + [string] $Arch + ) + + $actual = Parse-MachineType -Path $Path + $actual_hex = "0x{0:x}" -f $actual + $expected = Get-MachineType -Arch $Arch + $expected_hex = "0x{0:x}" -f $expected + + if ($actual -eq $expected) { + Write-Host "File '$Path' matches architecture '$Arch'." + } else { + Write-Host "File '$Path' DOES NOT match architecture '$Arch'." + Write-Error "Expected machine type '$expected_hex', actual machine type is '$actual_hex'." + } +} + +function Test-AppVeyor { + return Test-Path env:APPVEYOR +} + +function Verify-ArchitectureAppVeyor { + if (Test-AppVeyor) { + $appveyor_cwd = pwd + } + + try { + Verify-Architecture -Path $script:Path -Arch $script:Arch + } finally { + if (Test-AppVeyor) { + cd $appveyor_cwd + Set-PSDebug -Off + } + } +} + +Verify-ArchitectureAppVeyor diff --git a/test/scripts/verify_arch.sh b/test/scripts/verify_arch.sh new file mode 100755 index 0000000..aafc5e8 --- /dev/null +++ b/test/scripts/verify_arch.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +script_name="$( basename -- "${BASH_SOURCE[0]}" )" +readonly script_name + +elf_file_class() { + local path + for path; do + od --address-radix=n --skip-bytes=4 --read-bytes=1 --format=x1 -- "$path" + done +} + +arch_to_file_class() { + local arch + for arch; do + case "$arch" in + x86) + echo '01' + ;; + x64) + echo '02' + ;; + *) + echo "$script_name: unsupported architecture: $arch" >&2 + return 1 + ;; + esac + done +} + +main() { + if [ "$#" -ne 2 ]; then + echo "usage: $script_name BIN_PATH ARCH" >&2 + return 1 + fi + + local path="$1" + local arch="$2" + + local expected + expected=" $( arch_to_file_class "$arch" )" + local actual + actual="$( elf_file_class "$path" )" + + if [ "$expected" = "$actual" ]; then + echo "$script_name: file '$path' matches architecture '$arch'" + else + echo "$script_name: file '$path' DOES NOT match architecture '$arch'" + echo "$script_name: expected ELF file class '$expected', actual file class is '$actual'" >&2 + return 1 + fi +} + +main "$@" diff --git a/test/scripts/verify_symbols.ps1 b/test/scripts/verify_symbols.ps1 new file mode 100644 index 0000000..ebe6d99 --- /dev/null +++ b/test/scripts/verify_symbols.ps1 @@ -0,0 +1 @@ +Write-Host "Not going to check symbols for a Windows executable." diff --git a/test/scripts/verify_symbols.sh b/test/scripts/verify_symbols.sh new file mode 100755 index 0000000..73cc6ba --- /dev/null +++ b/test/scripts/verify_symbols.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +script_name="$( basename -- "${BASH_SOURCE[0]}" )" +readonly script_name + +main() { + if [ "$#" -lt 1 ]; then + echo "usage: $script_name BIN_PATH [SYMBOL...]" >&2 + return 1 + fi + + local path="$1" + shift + + local nm + nm="$( nm --demangle -- "$path" 2>&1 )" + + if [ "$#" -eq 0 ]; then + if [ "$nm" == "nm: $path: no symbols" ]; then + echo "$script_name: file '$path' has no symbols, as expected" + return 0 + else + echo "$script_name: file '$path' should not have symbols, but it does" >&2 + return 1 + fi + fi + + local symbol + for symbol; do + if echo "$nm" | grep -F -e " $symbol"; then + echo "$script_name: file '$path' has symbol '$symbol'" + else + echo "$script_name: symbol '$symbol' wasn't found in file '$path'" + echo "$script_name: here's the complete symbol list:" + echo "$nm" + return 1 + fi + done +} + +main "$@" -- cgit v1.3