#!/bin/bash set -euo pipefail cd "$(dirname "$0")/../.." unset BASH_ENV if [[ -z ${PARCEL_DEV_BINARY:-} ]]; then PARCEL_DEV_BINARY=$(Scripts/parcel-dev.sh --path) fi PARCEL_DEV_BINARY=$(realpath "$PARCEL_DEV_BINARY") export PARCEL_DEV_BINARY work=$(mktemp -d "${TMPDIR:-/tmp}/parcel smoke tests.XXXXXX") checker= cleanup() { if [[ -n $checker ]]; then kill -TERM "$checker" 2>/dev/null || true wait "$checker" 2>/dev/null || true fi if [[ -f $work/launch.log ]]; then while IFS='|' read -r pid _; do if kill -0 "$pid" 2>/dev/null; then kill -KILL "$pid"; fi done < "$work/launch.log" fi rm -rf "$work" } trap cleanup EXIT mkdir -p "$work/project/Scripts" "$work/bin" "$work/tmp" cp Scripts/product.env Scripts/version.sh Scripts/packaging-paths.sh Scripts/bundle-preflight.sh \ Scripts/parcel-dev.sh Scripts/check-bundle.sh Scripts/check-app-icons.sh "$work/project/Scripts/" mkdir -p "$work/project/Packaging/AlternateIcons" cp Packaging/AlternateIcons/IconTool.swift "$work/project/Packaging/AlternateIcons/" app="$work/project/build/Parcel.app" framework="$app/Contents/Frameworks/Sparkle.framework/Versions/B" mkdir -p "$app/Contents/MacOS" "$app/Contents/Resources/bin" "$framework/XPCServices" for component in Sparkle Autoupdate Updater.app XPCServices/Installer.xpc XPCServices/Downloader.xpc; do touch "$framework/$component" done ditto Sources/ParcelUI/Resources/AppIcons "$app/Contents/Resources/Parcel_ParcelUI.bundle/Contents/Resources" cp Resources/Info.plist "$app/Contents/Info.plist" Scripts/parcel-dev.sh update-metadata configure "$app/Contents/Info.plist" debug cat > "$work/fixture.c" <<'FIXTURE' #include #include #include #include #include #ifdef LINKED_DEPENDENCY extern int smoke_dependency(void); #endif int main(int argc, char **argv) { FILE *log = fopen(getenv("TEST_LAUNCH_LOG"), "a"); if (!log) { return 90; } const char *binding = getenv("DYLD_BIND_AT_LAUNCH"); fprintf(log, "%d|%s|%s|%s\n", getpid(), argv[0], argc == 2 ? argv[1] : "invalid", binding ? binding : "unset"); if (fclose(log)) { return 91; } if (argc != 2) { return 92; } const char *mode; if (!strcmp(argv[1], "--version")) { mode = getenv("TEST_CLI_MODE"); if (!mode || !strcmp(mode, "success")) { puts("0.1.0"); return 0; } if (!strcmp(mode, "invalid")) { puts("not a version"); return 0; } } else if (!strcmp(argv[1], "--smoke")) { mode = getenv("TEST_APP_MODE"); if (!mode || !strcmp(mode, "success")) { return 0; } } else { #ifdef LINKED_DEPENDENCY return smoke_dependency(); #else return 93; #endif } if (!strcmp(mode, "failure")) { fputs("Fixture launch failure\n", stderr); return 7; } if (!strcmp(mode, "dyld")) { fputs("dyld: Library not loaded: Fixture\n", stderr); return 127; } if (!strcmp(mode, "crash")) { raise(SIGKILL); return 94; } if (!strcmp(mode, "timeout")) { signal(SIGTERM, SIG_IGN); for (;;) { pause(); } } return 95; } FIXTURE /usr/bin/clang -Wall -Wextra -Werror -mmacosx-version-min=15.0 -Wl,-rpath,@executable_path/../Frameworks "$work/fixture.c" -o "$work/fixture" cp "$work/fixture" "$app/Contents/MacOS/Parcel" cp "$work/fixture" "$app/Contents/Resources/bin/prcl" cat > "$work/bin/codesign" <<'MOCK' #!/bin/bash set -euo pipefail printf '%s\n' "$*" >> "$TEST_CODESIGN_LOG" case "${1:-}" in --verify) [[ ${TEST_FAIL_VERIFY:-0} != 1 ]] || exit 1 if [[ ${TEST_FAIL_RELOCATED_VERIFY:-0} == 1 && ${!#} == *parcel-smoke.* ]]; then exit 1; fi ;; -dv) [[ ${TEST_FAIL_SIGNATURE:-0} != 1 ]] || exit 1 if [[ ${TEST_TEAM:-not set} != missing ]]; then printf 'TeamIdentifier=%s\n' "${TEST_TEAM:-not set}" >&2; fi ;; -d) [[ $2 == --entitlements && $3 == - && $4 == --xml && ${TEST_FAIL_ENTITLEMENTS:-0} != 1 ]] || exit 1 case "${TEST_ENTITLEMENT:-true}" in absent) exit 0 ;; true) value='' ;; false) value='' ;; string) value='true' ;; integer) value='1' ;; malformed) printf 'not a plist\n'; exit 0 ;; *) exit 2 ;; esac printf '%s\n' '' \ "com.apple.security.cs.disable-library-validation$value" '' ;; *) exit 2 ;; esac MOCK cat > "$work/bin/lipo" <<'MOCK' #!/bin/bash set -euo pipefail [[ $# == 4 && $2 == -verify_arch && $3 == arm64 && $4 == x86_64 ]] MOCK chmod +x "$work/bin/"* export PATH="$work/bin:$PATH" TMPDIR="$work/tmp" export TEST_LAUNCH_LOG="$work/launch.log" TEST_CODESIGN_LOG="$work/codesign.log" unset TEST_TEAM TEST_ENTITLEMENT TEST_APP_MODE TEST_CLI_MODE count=0 check() { if ! "$@"; then printf 'Smoke test failed: %s\n' "$*" >&2 exit 1 fi count=$((count + 1)) } fails() { if "$@" > "$work/failure.log" 2>&1; then printf 'Expected failure at line %s: %s\n' "${BASH_LINENO[1]}" "$*" >&2 /bin/cat "$work/failure.log" >&2 return 1 fi } no_leftovers() { if [[ -f $TEST_LAUNCH_LOG ]]; then while IFS='|' read -r pid _; do if kill -0 "$pid" 2>/dev/null; then printf 'Smoke left process %s running.\n' "$pid" >&2 return 1 fi done < "$TEST_LAUNCH_LOG" fi [[ -z $(find "$TMPDIR" -mindepth 1 -print -quit) ]] } check_bundle="$work/project/Scripts/check-bundle.sh" : > "$TEST_CODESIGN_LOG" printf 'Outside fixture, never mutate\n' > "$work/external-target" for attack in absolute relative fifo; do case $attack in absolute) ln -s "$work/external-target" "$app/Contents/unsafe" ;; relative) ln -s '../../../../external-target' "$app/Contents/unsafe" ;; fifo) mkfifo "$app/Contents/unsafe" ;; esac check fails "$check_bundle" --no-launch check grep -q 'Bundle preflight refused' "$work/failure.log" check test ! -s "$TEST_CODESIGN_LOG" check test ! -e "$TEST_LAUNCH_LOG" check no_leftovers rm "$app/Contents/unsafe" done mv "$work/project/build" "$work/project/saved-build" ln -s saved-build "$work/project/build" check fails "$check_bundle" --no-launch check grep -q 'symlinked build directory' "$work/failure.log" check test ! -s "$TEST_CODESIGN_LOG" check test ! -e "$TEST_LAUNCH_LOG" check no_leftovers rm "$work/project/build" mv "$work/project/saved-build" "$work/project/build" check "$check_bundle" --no-launch check test ! -e "$TEST_LAUNCH_LOG" for entitlement in absent false string integer malformed; do check fails env TEST_ENTITLEMENT="$entitlement" "$check_bundle" --no-launch check test ! -e "$TEST_LAUNCH_LOG" done check env TEST_TEAM=ABCDE12345 TEST_ENTITLEMENT=absent "$check_bundle" --no-launch for entitlement in true false string integer; do check fails env TEST_TEAM=ABCDE12345 TEST_ENTITLEMENT="$entitlement" "$check_bundle" --no-launch check grep -q 'Team-signed apps must not carry' "$work/failure.log" done check fails env TEST_TEAM=missing "$check_bundle" --no-launch check grep -q 'no team identifier metadata' "$work/failure.log" for failure in TEST_FAIL_VERIFY TEST_FAIL_SIGNATURE TEST_FAIL_ENTITLEMENTS TEST_FAIL_RELOCATED_VERIFY; do check fails env "$failure=1" "$check_bundle" check test ! -e "$TEST_LAUNCH_LOG" check no_leftovers done check "$check_bundle" check test "$(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ')" = 2 check grep -Eq '\|.*/parcel-smoke\.[^/]+/Parcel.app/Contents/MacOS/Parcel\|--smoke\|1$' "$TEST_LAUNCH_LOG" check grep -Eq '\|.*/parcel-smoke\.[^/]+/Parcel.app/Contents/Resources/bin/prcl\|--version\|1$' "$TEST_LAUNCH_LOG" check grep -q -- '--verify --strict --deep .*parcel-smoke\.' "$TEST_CODESIGN_LOG" check no_leftovers for mode in failure dyld crash timeout; do : > "$TEST_LAUNCH_LOG" check fails env TEST_APP_MODE="$mode" "$check_bundle" case "$mode" in failure) check grep -q 'status 7' "$work/failure.log"; check grep -q 'Fixture launch failure' "$work/failure.log" ;; dyld) check grep -q 'dyld: Library not loaded' "$work/failure.log" ;; crash) check grep -q 'status 137' "$work/failure.log" ;; timeout) check grep -q 'app timed out' "$work/failure.log" ;; esac check no_leftovers done for mode in failure invalid timeout; do : > "$TEST_LAUNCH_LOG" check fails env TEST_CLI_MODE="$mode" "$check_bundle" check test "$(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ')" = 1 if [[ $mode == timeout ]]; then check grep -q 'CLI timed out' "$work/failure.log"; fi check no_leftovers done # Like the app's chained fixups, this unused-on-smoke import must bind before main. printf 'int smoke_dependency(void) { return 0; }\n' > "$work/dependency.c" /usr/bin/clang -dynamiclib "$work/dependency.c" -Wl,-install_name,@rpath/SmokeDependency.dylib -o "$work/SmokeDependency.dylib" /usr/bin/clang -Wall -Wextra -Werror -mmacosx-version-min=15.0 -DLINKED_DEPENDENCY \ -Wl,-rpath,@executable_path/../Frameworks "$work/fixture.c" "$work/SmokeDependency.dylib" -o "$app/Contents/MacOS/Parcel" : > "$TEST_LAUNCH_LOG" check fails "$check_bundle" check grep -q 'Library not loaded: @rpath/SmokeDependency.dylib' "$work/failure.log" check test "$(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ')" = 1 check no_leftovers printf 'int different_symbol(void) { return 0; }\n' > "$work/dependency.c" /usr/bin/clang -dynamiclib "$work/dependency.c" -Wl,-install_name,@rpath/SmokeDependency.dylib \ -o "$app/Contents/Frameworks/SmokeDependency.dylib" : > "$TEST_LAUNCH_LOG" check fails "$check_bundle" check grep -qi 'symbol.*not found.*smoke_dependency' "$work/failure.log" check test "$(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ')" = 1 check no_leftovers cp "$work/fixture" "$app/Contents/MacOS/Parcel" rm "$app/Contents/Frameworks/SmokeDependency.dylib" : > "$TEST_LAUNCH_LOG" TEST_APP_MODE=timeout "$check_bundle" > "$work/interrupted.log" 2>&1 & checker=$! for (( tick=0; tick<100; tick++ )); do if [[ $(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ') == 2 ]]; then break; fi sleep 0.05 done check test "$(wc -l < "$TEST_LAUNCH_LOG" | tr -d ' ')" = 2 kill -TERM "$checker" status=0 wait "$checker" || status=$? checker= check test "$status" = 143 check no_leftovers printf 'Smoke checks passed: %s\n' "$count"