#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")/../.."
unset BASH_ENV
umask 077
root=$PWD
PARCEL_DEV_BINARY=$(Scripts/parcel-dev.sh --path)
export PARCEL_DEV_BINARY
work=$(mktemp -d "${TMPDIR:-/tmp}/parcel-updates-tests.XXXXXX")
keychain="$work/mock.keychain-db"
trap 'rm -rf "$work"' EXIT
mkdir "$work/bin"
for tool in security xcrun; do
printf '#!/bin/bash\nprintf "Forbidden keychain or notarization call\\n" >&2\nexit 88\n' > "$work/bin/$tool"
chmod +x "$work/bin/$tool"
done
export PATH="$work/bin:$PATH"
count=0
check() { if ! "$@"; then printf 'Update script test failed: %s\n' "$*" >&2; exit 1; fi; count=$((count + 1)); }
fails() { if "$@" > "$work/failure.log" 2>&1; then printf 'Expected failure: %s\n' "$*" >&2; /bin/cat "$work/failure.log" >&2; return 1; fi; }
xml_value() { /usr/bin/xmllint --xpath "$2" "$1"; }
plist_value() { /usr/libexec/PlistBuddy -c "Print ':$2'" "$1"; }
crypto=(Scripts/parcel-dev.sh update-crypto)
export TEST_CRYPTO_HELPER="$root/Scripts/parcel-dev.sh"
signer="$work/bin/sign_update"
cat > "$signer" <<'SIGNER'
#!/bin/bash
set -euo pipefail
[[ $# == 4 && $1 == --ed-key-file && $3 == -p ]]
if [[ $2 == - ]]; then
exec "$TEST_CRYPTO_HELPER" update-crypto sign "$4"
else
exec "$TEST_CRYPTO_HELPER" update-crypto sign "$4" < "$2"
fi
SIGNER
chmod +x "$signer"
check test -x "$signer"
public=$("${crypto[@]}" temporary-key "$work/seed")
check test "$(stat -f %Lp "$work/seed")" = 600
check fails "${crypto[@]}" temporary-key "$work/seed"
check fails "${crypto[@]}" invalid
printf 'Archive fixture\n' > "$work/update.dmg"
signature=$("$signer" --ed-key-file "$work/seed" -p "$work/update.dmg")
check "${crypto[@]}" verify "$work/update.dmg" "$signature" "$public"
stdin_signature=$("${crypto[@]}" sign "$work/update.dmg" < "$work/seed")
check test "$(printf '%s' "$stdin_signature" | base64 -D | wc -c | tr -d ' ')" = 64
check test "$(printf '%s' "$stdin_signature" | base64 -D | base64 -b 0)" = "$stdin_signature"
check "${crypto[@]}" verify "$work/update.dmg" "$stdin_signature" "$public"
check fails "${crypto[@]}" sign < "$work/seed"
check fails "${crypto[@]}" sign "$work/update.dmg" extra < "$work/seed"
check fails "${crypto[@]}" sign "$work/missing" < "$work/seed"
check Scripts/parcel-dev.sh release-data-not-contained "$work/failure.log" "$work/seed"
Scripts/parcel-dev.sh update-seed-fixtures "$work"
for input in "$work/"*.seed-input; do
check fails "${crypto[@]}" sign "$work/update.dmg" < "$input"
check grep -Fq 'canonical base64 Ed25519 seed' "$work/failure.log"
check Scripts/parcel-dev.sh release-data-not-contained "$work/failure.log" "$work/seed" "$input"
done
check fails "${crypto[@]}" verify "$work/update.dmg" bad "$public"
check fails "${crypto[@]}" verify "$work/update.dmg" "$signature" bad
printf 'Changed\n' >> "$work/update.dmg"
check fails "${crypto[@]}" verify "$work/update.dmg" "$signature" "$public"
check fails "${crypto[@]}" verify "$work/missing" "$signature" "$public"
source Scripts/script-root-test.sh
check script_root_test UpdateFeedIntegrationTests
check Scripts/parcel-dev.sh generate-update-key --dry-run --keychain "$keychain" > "$work/key-plan.log"
check test ! -e "$keychain"
check fails Scripts/parcel-dev.sh generate-update-key --keychain "$keychain"
check fails Scripts/parcel-dev.sh generate-update-key --account '' --dry-run
check script_root_test UpdateKeyTests
stored_public=$("${crypto[@]}" temporary-key "$work/stored-seed")
printf '%s' "$stored_public" > "$work/stored-public"
key_signature=$("${crypto[@]}" sign "$work/update.dmg" < "$work/stored-seed")
check "${crypto[@]}" verify "$work/update.dmg" "$key_signature" "$(<"$work/stored-public")"
mkdir -p "$work/project/Scripts" "$work/project/Packaging" "$work/bin"
cp Scripts/product.env Scripts/version.sh Scripts/packaging-paths.sh Scripts/release.sh Scripts/parcel-dev.sh "$work/project/Scripts/"
cp Packaging/parcel.rb "$work/project/Packaging/"
export TEST_CHAIN_LOG="$work/chain.log" TEST_RELEASE_FAIL=
export PARCEL_SPARKLE_BIN="$work/bin"
cat > "$work/project/Scripts/bundle.sh" <<'MOCK'
#!/bin/bash
set -euo pipefail
printf 'bundle %s identity=%s\n' "$*" "$PARCEL_SIGN_IDENTITY" >> "$TEST_CHAIN_LOG"
[[ ${TEST_RELEASE_FAIL:-} != bundle ]] || exit 70
[[ $1 == release && $PARCEL_SIGN_IDENTITY == '-' ]] || exit 71
mkdir -p build/Parcel.app/Contents
printf '\n' > build/Parcel.app/Contents/Info.plist
Scripts/parcel-dev.sh update-metadata configure build/Parcel.app/Contents/Info.plist release
MOCK
cat > "$work/project/Scripts/check-bundle.sh" <<'MOCK'
#!/bin/bash
set -euo pipefail
printf 'check %s\n' "$*" >> "$TEST_CHAIN_LOG"
[[ ${TEST_RELEASE_FAIL:-} != check ]] || exit 70
Scripts/parcel-dev.sh update-metadata check build/Parcel.app/Contents/Info.plist
MOCK
cat > "$work/project/Scripts/dmg.sh" <<'MOCK'
#!/bin/bash
set -euo pipefail
printf 'dmg %s\n' "$*" >> "$TEST_CHAIN_LOG"
[[ ${TEST_RELEASE_FAIL:-} != dmg ]] || exit 70
printf 'Signed disk image fixture\n' > "$2"
MOCK
chmod +x "$work/project/Scripts/"*.sh
cd "$work/project"
unset PARCEL_SIGN_IDENTITY PARCEL_NOTARY_PROFILE PARCEL_APPCAST_URL PARCEL_UPDATE_PUBLIC_KEY PARCEL_DOWNLOAD_URL PARCEL_DOWNLOAD_URL_PREFIX PARCEL_RELEASE_DIR PARCEL_PREVIOUS_APP
export PARCEL_VERSION=1.2.3 PARCEL_BUILD_NUMBER=123
mkdir "$work/failing-bin"
cat > "$work/failing-bin/parcel-dev" <<'MOCK'
#!/bin/bash
set -euo pipefail
if [[ $1 == update-crypto && $2 == temporary-key ]]; then
printf 'Mock temporary-key generation failed\n' >&2
exit 70
fi
exec "$REAL_DEV_BINARY" "$@"
MOCK
chmod +x "$work/failing-bin/parcel-dev"
check fails env REAL_DEV_BINARY="$PARCEL_DEV_BINARY" PARCEL_DEV_BINARY="$work/failing-bin/parcel-dev" Scripts/release.sh --dry-run
check grep -Fq 'Mock temporary-key generation failed' "$work/failure.log"
check test ! -e "$TEST_CHAIN_LOG"
check test ! -d build/dry-run/1.2.3
check test -z "$(find build -name seed -o -name '.release.*')"
check fails Scripts/release.sh --bad
check fails Scripts/release.sh
check fails env PARCEL_SIGN_IDENTITY=Fixture PARCEL_APPCAST_URL=https://example.invalid/appcast.xml PARCEL_UPDATE_PUBLIC_KEY="$public" PARCEL_DOWNLOAD_URL=https://example.invalid/artifact PARCEL_DOWNLOAD_URL_PREFIX=https://example.invalid/files Scripts/release.sh
check grep -Fq 'Set only one download URL or download URL prefix' "$work/failure.log"
check test ! -e "$TEST_CHAIN_LOG"
check fails env PARCEL_VERSION=../bad Scripts/release.sh --dry-run
check fails env TEST_RELEASE_FAIL=bundle Scripts/release.sh --dry-run
check test ! -d build/dry-run/1.2.3
check fails env TEST_RELEASE_FAIL=check Scripts/release.sh --dry-run
check fails env TEST_RELEASE_FAIL=dmg Scripts/release.sh --dry-run
check fails env PARCEL_SPARKLE_BIN=/missing Scripts/release.sh --dry-run
check test ! -d build/dry-run/1.2.3
check env PARCEL_PREVIOUS_APP=/previous-release.app PARCEL_DOWNLOAD_URL=https://example.invalid/ignored Scripts/release.sh --dry-run > "$work/dry-run.log"
check test -s build/dry-run/1.2.3/DRY-RUN.txt
check grep -q 'bundle release identity=-' "$TEST_CHAIN_LOG"
check grep -q 'check --no-launch build/Parcel.app' "$TEST_CHAIN_LOG"
check fails Scripts/release.sh --dry-run
check grep -q 'Refusing to replace' "$work/failure.log"
check test -z "$(find build -name seed -o -name '.release.*')"
check "$root/Scripts/parcel-dev.sh" check-ruby-syntax build/dry-run/1.2.3/parcel.rb
out=build/dry-run/1.2.3
dmg="$out/Parcel-1.2.3.dmg"
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="version"])')" = 123
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="shortVersionString"])')" = 1.2.3
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="minimumSystemVersion"])')" = 15.0
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/enclosure/@length)')" = "$(stat -f %z "$dmg")"
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/enclosure/@url)')" = https://example.invalid/parcel/Parcel-1.2.3.dmg
sha=$(shasum -a 256 "$dmg"); sha=${sha%% *}
check grep -Fq "$sha" "$out/parcel.rb"
check grep -Fq "version '1.2.3'" "$out/parcel.rb"
check fails grep -Fq '@VERSION@' "$out/parcel.rb"
check Scripts/parcel-dev.sh release-checksum "$dmg" "$out/SHA256SUMS"
xml_value "$out/appcast.xml" 'string(/rss/channel/item/enclosure/@*[local-name()="edSignature"])' > signature
plist_value build/Parcel.app/Contents/Info.plist SUPublicEDKey | tr -d '\n' > public
check Scripts/parcel-dev.sh update-crypto verify "$dmg" "$( "$work/version-startup.sh"
check env BASH_ENV="$work/version-startup.sh" PARCEL_DOWNLOAD_URL_PREFIX=https://example.org/files "${metadata[@]}"
check test ! -e "$work/unexpected-version-startup"
for version in '' '1.2.3 bad' $'1.2.3\n1.2.3' $'1.2.3\t' letters; do
check fails env PARCEL_VERSION="$version" "${metadata[@]}"
check grep -Fq 'Invalid version' "$work/failure.log"
done
for build in '' 1.2 -1 letters $'123\n' $'123\t' '123 '; do
check fails env PARCEL_BUILD_NUMBER="$build" "${metadata[@]}"
check grep -Fq 'Invalid build number' "$work/failure.log"
done
mkdir -p "$work/renamed/Scripts" "$work/renamed/output"
cp Scripts/parcel-dev.sh Scripts/version.sh "$work/renamed/Scripts/"
cat > "$work/renamed/Scripts/product.env" <<'PRODUCT'
# Quoted literals and a distinct prefix exercise the product settings reader.
export PRODUCT_NAME='Crate Kit'
export PRODUCT_ENV_PREFIX=CRATE
export PRODUCT_CASK=crate-kit
PRODUCT
cat > "$work/renamed/template.rb" <<'CASK'
cask 'crate-kit' do
version '@VERSION@'
sha256 '@SHA256@'
url '@DOWNLOAD_URL@'
name 'Crate Kit'
app 'Crate Kit.app'
end
CASK
printf '\n' > "$work/renamed/Info.plist"
printf 'Renamed disk image fixture\n' > "$work/renamed/Crate Kit-2.3.4.dmg"
renamed_metadata() {
env -u PRODUCT_NAME -u PRODUCT_ENV_PREFIX -u PRODUCT_CASK \
CRATE_VERSION="${CRATE_VERSION-2.3.4}" CRATE_BUILD_NUMBER="${CRATE_BUILD_NUMBER-234}" CRATE_UPDATE_PUBLIC_KEY="$public" \
CRATE_APPCAST_URL=https://example.invalid/crate/appcast.xml CRATE_DOWNLOAD_URL_PREFIX=https://example.invalid/crate/releases \
PARCEL_VERSION=9.9.9 PARCEL_BUILD_NUMBER=999 PARCEL_UPDATE_PUBLIC_KEY=bad PARCEL_APPCAST_URL=http://example.invalid/ignored \
"$work/renamed/Scripts/parcel-dev.sh" update-metadata "$@"
}
check renamed_metadata configure "$work/renamed/Info.plist" release
check renamed_metadata check "$work/renamed/Info.plist"
check test "$(plist_value "$work/renamed/Info.plist" 'Crate KitUpdatesEnabled')" = true
check fails plist_value "$work/renamed/Info.plist" ParcelUpdatesEnabled
check test "$(plist_value "$work/renamed/Info.plist" SUPublicEDKey)" = "$public"
check test "$(plist_value "$work/renamed/Info.plist" SUFeedURL)" = https://example.invalid/crate/appcast.xml
check renamed_metadata release "$work/renamed/Crate Kit-2.3.4.dmg" "$signature" "$work/renamed/output" "$work/renamed/template.rb"
check "$root/Scripts/parcel-dev.sh" check-ruby-syntax "$work/renamed/output/crate-kit.rb"
out="$work/renamed/output"
dmg="$work/renamed/Crate Kit-2.3.4.dmg"
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/title)')" = 'Crate Kit Updates'
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/title)')" = 'Crate Kit 2.3.4'
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="version"])')" = 234
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="shortVersionString"])')" = 2.3.4
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/enclosure/@url)')" = 'https://example.invalid/crate/releases/Crate Kit-2.3.4.dmg'
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/enclosure/@length)')" = "$(stat -f %z "$dmg")"
check Scripts/parcel-dev.sh release-checksum "$dmg" "$out/SHA256SUMS"
sha=$(shasum -a 256 "$dmg"); sha=${sha%% *}
check grep -Fq "$sha" "$out/crate-kit.rb"
check grep -Fq "version '2.3.4'" "$out/crate-kit.rb"
check grep -Fq 'Crate Kit.app' "$out/crate-kit.rb"
for token in '@VERSION@' '@SHA256@' '@DOWNLOAD_URL@'; do check fails grep -Fq "$token" "$out/crate-kit.rb"; done
check test ! -e "$out/parcel.rb"
renamed_release=(release "$dmg" "$signature" "$out" "$work/renamed/template.rb")
CRATE_VERSION=2.3.4-beta.1+build.42 CRATE_BUILD_NUMBER=00235 check renamed_metadata "${renamed_release[@]}"
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="shortVersionString"])')" = 2.3.4-beta.1+build.42
check test "$(xml_value "$out/appcast.xml" 'string(/rss/channel/item/*[local-name()="version"])')" = 00235
for version in '' letters '2.3.4 bad' $'2.3.4\n2.3.4'; do
CRATE_VERSION="$version" check fails renamed_metadata "${renamed_release[@]}"
check grep -Fq 'Invalid version' "$work/failure.log"
done
for build in '' 2.3 -1 $'234\n'; do
CRATE_BUILD_NUMBER="$build" check fails renamed_metadata "${renamed_release[@]}"
check grep -Fq 'Invalid build number' "$work/failure.log"
done
check renamed_metadata configure "$work/renamed/Info.plist" debug
check renamed_metadata check "$work/renamed/Info.plist"
check test "$(plist_value "$work/renamed/Info.plist" 'Crate KitUpdatesEnabled')" = false
check test "$(plist_value "$work/renamed/Info.plist" SUEnableAutomaticChecks)" = false
check fails plist_value "$work/renamed/Info.plist" SUPublicEDKey
check fails plist_value "$work/renamed/Info.plist" SUFeedURL
# shellcheck disable=SC2016
printf 'export PRODUCT_NAME=$(touch %s)\n' "$work/should-not-exist" > "$work/renamed/Scripts/product.env"
check fails renamed_metadata check "$work/renamed/Info.plist"
check test ! -e "$work/should-not-exist"
check grep -q 'literal exports' "$work/failure.log"
printf 'Update script checks passed: %s\n' "$count"