|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ============================================================================= |
| 3 | +# validate-artifact.sh |
| 4 | +# ============================================================================= |
| 5 | +# Same-shape artifact validator for local + CI. Given a path to one of our |
| 6 | +# published artifact files, does the minimum sanity-checks to catch |
| 7 | +# "built it but it's broken" before we publish: |
| 8 | +# |
| 9 | +# .zip → unzip list non-empty, contains expected files |
| 10 | +# .xcframework (as .zip) → Info.plist present + declares arch slices |
| 11 | +# .so → ELF, machine arch matches filename convention |
| 12 | +# .aar → unzip lists classes.jar + jni/{abi}/*.so |
| 13 | +# .wasm → starts with WebAssembly magic bytes (0x00 'asm') |
| 14 | +# .tgz (npm) → `tar -tzf` lists package/package.json |
| 15 | +# .jar → zip-listable, has META-INF/MANIFEST.MF |
| 16 | +# |
| 17 | +# Usage: |
| 18 | +# scripts/validate-artifact.sh PATH [PATH ...] |
| 19 | +# |
| 20 | +# Exit status: 0 if every path passed, 1 on first failure. |
| 21 | +# ============================================================================= |
| 22 | + |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +if [ $# -eq 0 ]; then |
| 26 | + echo "usage: $0 FILE [FILE ...]" >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +fail() { echo " ✗ $1" >&2; exit 1; } |
| 31 | +ok() { echo " ✓ $1"; } |
| 32 | + |
| 33 | +validate_one() { |
| 34 | + local path="$1" |
| 35 | + |
| 36 | + if [ ! -f "$path" ]; then |
| 37 | + fail "not a regular file: $path" |
| 38 | + fi |
| 39 | + |
| 40 | + local size |
| 41 | + size=$(stat -f%z "$path" 2>/dev/null || stat -c%s "$path") |
| 42 | + if [ "$size" -lt 64 ]; then |
| 43 | + fail "suspiciously small ($size bytes): $path" |
| 44 | + fi |
| 45 | + |
| 46 | + echo ">> $path ($size bytes)" |
| 47 | + |
| 48 | + case "$path" in |
| 49 | + *.sha256) |
| 50 | + # .sha256 is its own checksum file; sanity check it's single-line, 64 hex + space + filename |
| 51 | + local line |
| 52 | + line=$(head -1 "$path") |
| 53 | + if ! echo "$line" | grep -Eq '^[0-9a-f]{64} '; then |
| 54 | + fail "bad .sha256 format in $path: $line" |
| 55 | + fi |
| 56 | + ok ".sha256 looks well-formed" |
| 57 | + ;; |
| 58 | + *.wasm) |
| 59 | + # Magic: 0x00 'asm' (0x00 0x61 0x73 0x6d) |
| 60 | + local magic |
| 61 | + magic=$(head -c 4 "$path" | xxd -p | head -1) |
| 62 | + if [ "$magic" != "0061736d" ]; then |
| 63 | + fail "not a WebAssembly module (bad magic '$magic'): $path" |
| 64 | + fi |
| 65 | + ok "WebAssembly magic bytes OK" |
| 66 | + ;; |
| 67 | + *.so) |
| 68 | + if ! head -c 4 "$path" | grep -q $'\x7fELF' 2>/dev/null; then |
| 69 | + fail "not an ELF shared library: $path" |
| 70 | + fi |
| 71 | + ok "ELF shared library OK" |
| 72 | + if command -v readelf >/dev/null 2>&1; then |
| 73 | + local arch |
| 74 | + arch=$(readelf -h "$path" 2>/dev/null | awk -F'Machine:' 'NF>1 {print $2}' | xargs) |
| 75 | + [ -n "$arch" ] && echo " machine: $arch" |
| 76 | + fi |
| 77 | + ;; |
| 78 | + *.aar) |
| 79 | + if ! unzip -l "$path" >/dev/null 2>&1; then |
| 80 | + fail "cannot unzip $path" |
| 81 | + fi |
| 82 | + if ! unzip -l "$path" | grep -q '^.* classes.jar$'; then |
| 83 | + fail "AAR missing classes.jar: $path" |
| 84 | + fi |
| 85 | + ok "AAR contains classes.jar" |
| 86 | + if unzip -l "$path" | grep -q 'jni/[^/]*/.*\.so$'; then |
| 87 | + local count |
| 88 | + count=$(unzip -l "$path" | grep -c 'jni/[^/]*/.*\.so$' || true) |
| 89 | + ok "AAR contains $count jni/*.so entries" |
| 90 | + else |
| 91 | + echo " note: no JNI .so files bundled (AAR may link against external natives)" |
| 92 | + fi |
| 93 | + ;; |
| 94 | + *.jar) |
| 95 | + if ! unzip -l "$path" >/dev/null 2>&1; then |
| 96 | + fail "cannot unzip $path" |
| 97 | + fi |
| 98 | + if ! unzip -l "$path" | grep -q 'META-INF/MANIFEST.MF$'; then |
| 99 | + fail "JAR missing META-INF/MANIFEST.MF: $path" |
| 100 | + fi |
| 101 | + ok "JAR has valid manifest" |
| 102 | + ;; |
| 103 | + *.tgz|*.tar.gz) |
| 104 | + if ! tar -tzf "$path" >/dev/null 2>&1; then |
| 105 | + fail "cannot list tarball $path" |
| 106 | + fi |
| 107 | + if tar -tzf "$path" | grep -q '^package/package.json$'; then |
| 108 | + ok "npm tarball contains package/package.json" |
| 109 | + else |
| 110 | + ok "tarball listable ($(tar -tzf "$path" | wc -l | xargs) entries)" |
| 111 | + fi |
| 112 | + ;; |
| 113 | + *.zip) |
| 114 | + if ! unzip -l "$path" >/dev/null 2>&1; then |
| 115 | + fail "cannot unzip $path" |
| 116 | + fi |
| 117 | + # XCFramework ZIPs contain an Info.plist at top of the framework root |
| 118 | + if unzip -l "$path" | grep -q '\.xcframework/Info\.plist$'; then |
| 119 | + ok "XCFramework ZIP: Info.plist present" |
| 120 | + # Count arch slices (each subdir with an Info.plist sibling is a slice) |
| 121 | + local slices |
| 122 | + slices=$(unzip -l "$path" | grep -c '\.xcframework/[^/]*/Info\.plist$' || true) |
| 123 | + [ "$slices" -gt 0 ] && echo " arch slices declared: $slices" |
| 124 | + else |
| 125 | + ok "ZIP listable ($(unzip -l "$path" | tail -1 | awk '{print $2}') entries)" |
| 126 | + fi |
| 127 | + ;; |
| 128 | + *) |
| 129 | + ok "unknown extension — size check only" |
| 130 | + ;; |
| 131 | + esac |
| 132 | +} |
| 133 | + |
| 134 | +for path in "$@"; do |
| 135 | + validate_one "$path" |
| 136 | +done |
| 137 | + |
| 138 | +echo "" |
| 139 | +echo "All $# artifact(s) passed validation." |
0 commit comments