Prepare isolated Play bundle builds and distribution safeguards

This commit is contained in:
maxjivi05 committed 2026-11-02 22:28:18 -05:00
1 parent 773d37ff21
commit 2f6e43e3d1
102 files changed
+3070 -182

No files matched your search

+2 -2
View File
@@ -7,7 +7,7 @@ mkdir -p "$OUTDIR"
API=26
HERE=$(cd "$(dirname "$0")" && pwd)
REPO=$(cd "$HERE/../.." && pwd)
LIBS="$REPO/app/src/main/jniLibs/arm64-v8a"
LIBS=${DROIDDECK_PULSE_LIBS:-"$REPO/app/src/main/jniLibs/arm64-v8a"}
case "$(uname -s):$(uname -m)" in
Darwin:arm64) NDK_HOST=darwin-arm64 ;;
Darwin:x86_64) NDK_HOST=darwin-x86_64 ;;
@@ -38,7 +38,7 @@ build_one() {
local src=$1 OUT=$2 extra=$3 cc=${4:-$CC}
"$cc" -O2 -shared -fPIC -Wall -Wno-unused-parameter -Wno-error=int-conversion -Wno-visibility -DHAVE_CONFIG_H \
-I"$INC" -I"$PA_SRC/src" -I"$HERE" \
-o "$OUT" "$HERE/$src" \
-Wl,-z,max-page-size=16384 -o "$OUT" "$HERE/$src" \
-L"$LIBS" -l:libpulsecore-13.0.so -l:libpulsecommon-13.0.so -l:libpulse.so $extra
"$TOOLCHAIN/llvm-strip" --strip-unneeded "$OUT"
for sym in pa__init pa__done pa__get_author pa__get_description pa__get_usage pa__get_version pa__load_once; do
@@ -1,5 +1,8 @@
#!/bin/bash
set -u
if compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null; then
exit 2
fi
export FLATPAK_BWRAP=/usr/local/bin/droiddeck-bwrap
cores="libretro-core-info libretro-beetle-pce-fast libretro-beetle-psx libretro-beetle-vb libretro-beetle-wswan
libretro-bsnes-mercury-performance libretro-desmume libretro-gambatte libretro-genesisplusgx libretro-mgba
@@ -16,7 +16,11 @@ fex_install() {
local tag=$1 sha=$2 urls=$3 dst=/ tmp u f
tmp=$(mktemp -d)
for u in $urls; do
curl -fsSL --retry 2 --connect-timeout 20 --speed-limit 1024 --speed-time 60 "$u" -o "$tmp/fex.tzst" && echo "$sha $tmp/fex.tzst" | sha256sum -c --quiet >/dev/null 2>&1 && break
if compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null; then
python3 /usr/local/lib/droiddeck/component_delivery.py fetch "$u" "$tmp/fex.tzst" --sha256 "$sha" && break
else
curl -fsSL --retry 2 --connect-timeout 20 --speed-limit 1024 --speed-time 60 "$u" -o "$tmp/fex.tzst" && echo "$sha $tmp/fex.tzst" | sha256sum -c --quiet >/dev/null 2>&1 && break
fi
rm -f "$tmp/fex.tzst"
done
if [ ! -s "$tmp/fex.tzst" ]; then
@@ -1,5 +1,6 @@
#!/usr/bin/python3
import hashlib
import glob
import json
import os
import re
@@ -17,6 +18,11 @@ SOURCES = {
}
USER_AGENT = "droiddeck-proton-extra"
NEED_BYTES = 4 * 1024 * 1024 * 1024
PLAY_BUNDLES = bool(glob.glob('/opt/droiddeck-bundles/*/*.apk'))
if PLAY_BUNDLES:
SOURCES = {"droiddeck": (SOURCES["droiddeck"][0], re.compile(r"aarch64\.tar\.(gz|xz|zst)$"))}
sys.path.insert(0, '/usr/local/lib/droiddeck')
import component_delivery
def log(*args):
@@ -24,6 +30,9 @@ def log(*args):
def fetch_json(url):
if PLAY_BUNDLES:
with component_delivery.BundleReader() as reader:
return json.loads(reader.read(url))
req = urllib.request.Request(url, headers={
"User-Agent": USER_AGENT, "Accept": "application/vnd.github+json"})
with urllib.request.urlopen(req, timeout=60) as r:
@@ -58,6 +67,10 @@ def release_asset(source, tag):
def download(url, path):
log("downloading %s" % os.path.basename(path))
if PLAY_BUNDLES:
with component_delivery.BundleReader() as reader:
reader.fetch(url, path)
return
rc = subprocess.call([
"curl", "-fL", "--retry", "5", "--retry-delay", "3", "--retry-connrefused",
"-C", "-", "-A", USER_AGENT, "-o", path, url])
@@ -71,10 +84,16 @@ def verify(path, sums_url):
log("no checksum published; skipping verification")
return
try:
req = urllib.request.Request(sums_url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=60) as r:
want = r.read().decode("utf-8", "replace").split()[0].lower()
if PLAY_BUNDLES:
with component_delivery.BundleReader() as reader:
want = reader.read(sums_url).decode('utf-8').split()[0].lower()
else:
req = urllib.request.Request(sums_url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=60) as r:
want = r.read().decode("utf-8", "replace").split()[0].lower()
except (urllib.error.URLError, IndexError, OSError) as e:
if PLAY_BUNDLES:
raise
log("could not read the checksum (%s); skipping verification" % e)
return
h = hashlib.sha512()
@@ -138,7 +157,7 @@ def main():
if not url:
raise SystemExit("droiddeck-proton-extra: no ARM64 build published for %s" % what)
log("%s -> %s" % (what, tag))
elif what.startswith("http"):
elif what.startswith("http") or PLAY_BUNDLES and what.startswith("droiddeck-bundle://"):
url, sums = what, None
elif os.path.isfile(what):
local, url, sums = what, None, None
@@ -507,6 +507,10 @@ case $mode in
_bl_got_proton=0
while read -r _bl_extra || [ -n "$_bl_extra" ]; do
case ${_bl_extra# } in ""|"#"*) continue ;; esac
if compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null && [ "${_bl_extra%% *}" != droiddeck ]; then
echo "$_bl_extra" >> "$_bl_left"
continue
fi
step "installing Proton '$_bl_extra' (large download, several minutes)"
if droiddeck-proton-extra "$_bl_proton_root" $_bl_extra; then
_bl_got_proton=1
@@ -21,7 +21,13 @@ x64_default_dir() {
while IFS= read -r d; do
[ -f "$d/.droiddeck-arm64-proton" ] && { echo "$d"; return; }
done < <(ls -d "$STEAM_X64"/compatibilitytools.d/DroidDeck-Proton* 2>/dev/null | sort -V -r
ls -d "$STEAM_X64"/compatibilitytools.d/GE-Proton*-aarch64 2>/dev/null | sort -V -r)
if ! compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null; then
ls -d "$STEAM_X64"/compatibilitytools.d/GE-Proton*-aarch64 2>/dev/null | sort -V -r
fi)
if compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null; then
echo "$STEAM_X64/compatibilitytools.d/DroidDeck-Proton"
return
fi
echo "$STEAM_X64/compatibilitytools.d/droiddeck-proton-arm64"
}
x64_default_tool() {
@@ -321,6 +327,10 @@ PY
x64_fetch() {
local url=$1 out=$2 label=$3 total pid rc=0 last=-1 pct
if compgen -G '/opt/droiddeck-bundles/*/*.apk' >/dev/null; then
python3 /usr/local/lib/droiddeck/component_delivery.py fetch "$url" "$out" --sha256 "$ROOTFS_SHA256"
return $?
fi
total=$(curl -fsSIL "$url" 2>/dev/null | tr -d '\r' | awk 'tolower($1) == "content-length:" { v = $2 } END { print v + 0 }')
curl -fL -sS -C - "$url" -o "$out" &
pid=$!
@@ -102,7 +102,15 @@ install)
mkdir -p "$cache"
pkg=$cache/steamvr.pkg.tar.zst
echo "downloading SteamVR (571 MB) from Valve's package server"
curl -fsSL --retry 5 -C - -o "$pkg" "$PKG_URL" || { echo "download failed"; exit 1; }
play_bundle=
for apk in /opt/droiddeck-bundles/*/*.apk; do
if [ -f "$apk" ]; then play_bundle=1; break; fi
done
if [ -n "$play_bundle" ]; then
python3 /usr/local/lib/droiddeck/component_delivery.py fetch "$PKG_URL" "$pkg" --sha256 "$PKG_SHA256" || exit 1
else
curl -fsSL --retry 5 -C - -o "$pkg" "$PKG_URL" || { echo "download failed"; exit 1; }
fi
echo "$PKG_SHA256 $pkg" | sha256sum -c - >/dev/null 2>&1 || { echo "checksum mismatch: removing the download"; rm -f "$pkg"; exit 1; }
tmp=$cache/extract
rm -rf "$tmp" && mkdir -p "$tmp"
@@ -1,5 +1,6 @@
#!/usr/bin/python3
import json
import glob
import os
import re
import runpy
@@ -13,6 +14,7 @@ LABEL_FILE = "/etc/steam-compatibility-label"
PROTON_ARM64_APPID = "4427310"
LAUNCHER = "droiddeck-proton"
LEGACY_LAUNCHER = "bannerlator-proton"
PLAY_BUNDLES = bool(glob.glob('/opt/droiddeck-bundles/*/*.apk'))
LIBRARY = "/mnt/droiddeck"
SOURCES = ("Proton Experimental (ARM64)", "Proton 11.0 (ARM64)")
OWN_TOOLS = ((TOOL, SOURCES), (TOOL_11, ("Proton 11.0 (ARM64)",)))
@@ -1131,7 +1133,7 @@ def tool_catalog(steam_root, extras=None):
extras[first_key(os.path.join(tool_dir, "compatibilitytool.vdf"), ["compatibilitytools", "compat_tools"]) or entry] = entry
for name, entry in sorted(extras.items()):
catalog.append(tool_entry(name, display_name(entry), os.path.join(tools, entry), False))
return catalog
return own_builds(catalog) if PLAY_BUNDLES else catalog
def requested_tool(request, catalog):
@@ -1141,6 +1143,8 @@ def requested_tool(request, catalog):
for tool in catalog:
if tool["dir"] and tool["dir"] == request.get("dir") and tool["valve"] == bool(request.get("valve")):
return tool["name"]
if PLAY_BUNDLES:
return own_proton_moves({}, catalog)[0]
return None
@@ -1298,7 +1302,7 @@ def choose_default(steam_default, state, request, catalog, adopt_steam=True, app
elif adopt_steam and steam_default in names and steam_default != state.get("default"):
state.update(default=steam_default, seq=now or now_ms(), source="steam")
if state.get("default") not in names:
state["default"] = TOOL
state["default"] = (own_proton_moves({}, catalog)[0] or TOOL) if PLAY_BUNDLES else TOOL
return state["default"], state
@@ -0,0 +1,151 @@
import argparse
import hashlib
import io
import json
import os
from pathlib import Path
import re
import sys
import tempfile
import zipfile
DIRECTORY = Path('/opt/droiddeck-bundles')
INDEX = 'assets/play-components/index.json'
INDEX_LIMIT = 16 * 1024 * 1024
SHA256 = re.compile(r'[0-9a-f]{64}')
ASSET = re.compile(r'play-components/[0-9a-f]{64}/[0-9]{4,}')
def enabled(directory=DIRECTORY):
return any(directory.glob('*/*.apk'))
class BundleReader:
def __init__(self, directory=DIRECTORY):
self.archives = []
self.assets = {}
try:
paths = sorted(directory.glob('*/*.apk'))
if not paths or len(paths) > 128:
raise ValueError('Installed component bundles are unavailable')
for path in paths:
archive = zipfile.ZipFile(path)
self.archives.append(archive)
for entry in archive.infolist():
if not entry.filename.startswith('assets/play-components/'):
continue
if entry.filename in self.assets:
raise ValueError('Duplicate component asset: ' + entry.filename)
self.assets[entry.filename] = (archive, entry)
index = self.read_object(INDEX, 2 * 1024 * 1024)
if index.get('schema') != 1 or not isinstance(index.get('entries'), dict):
raise ValueError('Unsupported component index')
self.entries = index['entries']
except BaseException:
self.close()
raise
def close(self):
for archive in self.archives:
archive.close()
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
def read_object(self, path, limit=INDEX_LIMIT):
archive, entry = self.assets[path]
if entry.file_size > limit:
raise ValueError('Component metadata exceeds its size limit')
with archive.open(entry) as source:
return json.load(source)
def copy(self, url, output, limit, expected_sha256=None):
entry = self.entries.get(url)
if entry is None:
raise ValueError('Component is absent from this build: ' + url)
size, digest = entry['size'], entry['sha256']
if type(size) is not int or size <= 0 or size > limit or not SHA256.fullmatch(digest):
raise ValueError('Invalid component metadata')
if expected_sha256 is not None and digest != expected_sha256.lower():
raise ValueError('Component version does not match this build')
recipe = 'play-components/recipes/' + digest + '.json'
if entry.get('recipe') != recipe:
raise ValueError('Invalid component recipe path')
chunks = self.read_object('assets/' + recipe)['chunks']
if not chunks:
raise ValueError('Component contains no payload')
full_digest = hashlib.sha256()
total = 0
for chunk in chunks:
path, chunk_size, chunk_sha256 = chunk['asset'], chunk['size'], chunk['sha256']
if not ASSET.fullmatch(path) or type(chunk_size) is not int or not 0 < chunk_size <= size - total:
raise ValueError('Invalid component chunk metadata')
if not SHA256.fullmatch(chunk_sha256):
raise ValueError('Invalid component chunk digest')
archive, asset = self.assets['assets/' + path]
if asset.file_size != chunk_size:
raise ValueError('Component chunk size mismatch')
chunk_digest = hashlib.sha256()
copied = 0
with archive.open(asset) as source:
for block in iter(lambda: source.read(65536), b''):
copied += len(block)
total += len(block)
if copied > chunk_size:
raise ValueError('Component chunk exceeds pinned size')
chunk_digest.update(block)
full_digest.update(block)
output.write(block)
if copied != chunk_size or chunk_digest.hexdigest() != chunk_sha256:
raise ValueError('Component chunk failed verification')
if total != size or full_digest.hexdigest() != digest:
raise ValueError('Component failed verification')
return total
def read(self, url):
output = io.BytesIO()
self.copy(url, output, INDEX_LIMIT)
return output.getvalue()
def fetch(self, url, destination, expected_sha256=None):
destination = Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True)
descriptor, temporary = tempfile.mkstemp(prefix=destination.name + '.', suffix='.component-part', dir=destination.parent)
try:
with os.fdopen(descriptor, 'wb') as output:
self.copy(url, output, sys.maxsize, expected_sha256)
os.replace(temporary, destination)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['enabled', 'read', 'fetch'])
parser.add_argument('url', nargs='?')
parser.add_argument('destination', nargs='?', type=Path)
parser.add_argument('--sha256')
arguments = parser.parse_args()
if arguments.command == 'enabled':
return 0 if enabled() else 1
if not arguments.url or arguments.command == 'fetch' and arguments.destination is None:
parser.error('Component URL and fetch destination are required')
with BundleReader() as reader:
if arguments.command == 'read':
sys.stdout.buffer.write(reader.read(arguments.url))
else:
reader.fetch(arguments.url, arguments.destination, arguments.sha256)
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except (OSError, ValueError, KeyError, TypeError, zipfile.BadZipFile) as error:
print('Bundled component unavailable: ' + str(error), file=sys.stderr)
sys.exit(1)
+62
View File
@@ -0,0 +1,62 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.Certificate;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
import jdk.security.jarsigner.JarSigner;
class SignBundle {
static String fingerprint(Certificate certificate) throws Exception {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(certificate.getEncoded()));
}
public static void main(String[] arguments) {
char[] password = null;
try {
password = System.getenv("RELEASE_STORE_PASSWORD").toCharArray();
KeyStore keys = KeyStore.getInstance(Path.of(System.getenv("RELEASE_KEYSTORE_PATH")).toFile(), password);
var entry = (KeyStore.PrivateKeyEntry) keys.getEntry(System.getenv("RELEASE_KEY_ALIAS"), new KeyStore.PasswordProtection(password));
if (entry == null) throw new IllegalStateException();
if (!fingerprint(entry.getCertificate()).equals(arguments[2])) {
System.err.println("Signing certificate differs from the pinned release identity");
System.exit(2);
}
var signer = new JarSigner.Builder(entry).digestAlgorithm("SHA-256").signerName("RELEASE").build();
try (var source = new ZipFile(arguments[0]);
OutputStream output = Files.newOutputStream(Path.of(arguments[1]), StandardOpenOption.CREATE_NEW)) {
signer.sign(source, output);
}
try (var signed = new JarFile(arguments[1], true)) {
var entries = signed.entries();
while (entries.hasMoreElements()) {
var item = entries.nextElement();
if (item.isDirectory()) continue;
try (InputStream input = signed.getInputStream(item)) {
input.transferTo(OutputStream.nullOutputStream());
}
String name = item.getName().toUpperCase(java.util.Locale.ROOT);
boolean signature = name.equals("META-INF/MANIFEST.MF") || name.startsWith("META-INF/") && name.indexOf('/', 9) < 0 &&
(name.endsWith(".SF") || name.endsWith(".RSA") || name.endsWith(".DSA") || name.endsWith(".EC") || name.substring(9).startsWith("SIG-"));
if (signature) continue;
var identities = item.getCodeSigners();
if (identities == null || identities.length != 1 || !fingerprint(identities[0].getSignerCertPath().getCertificates().get(0)).equals(arguments[2])) {
throw new IllegalStateException();
}
}
}
System.out.println("Bundle signature and payload coverage verified");
} catch (Exception failure) {
System.err.println("Bundle signing or signature verification failed");
System.exit(1);
} finally {
if (password != null) Arrays.fill(password, '\0');
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"android-module": {
"sha256": "77637cae61e6200a8b3340b58080e9cf5b8740b924275009a86cb2ad3ba0b934",
"size": 3378196,
"url": "https://codeload.github.com/brunodev85/pulseaudio-android/tar.gz/77c8a7217c5746d44787e78f9b141e7503c981f4"
},
"libsndfile": {
"sha256": "82f6a864fb7fb5e9254f91d9191271a8d76ae01848f7284a21602329310088f9",
"size": 663130,
"url": "https://codeload.github.com/libsndfile/libsndfile/tar.gz/d60deb5d8691997b6bb28d88e3b43f322073d146"
},
"libtool": {
"sha256": "7c87a8c2c8c0fc9cd5019e402bed4292462d00a718a7cd5f11218153bf28b26f",
"size": 973080,
"url": "https://ftp.gnu.org/gnu/libtool/libtool-2.4.6.tar.xz"
},
"pulseaudio": {
"sha256": "8da3360e199609e64a413e0e181a121770b6aac16e6ec5327abb8f75928a0c7d",
"size": 2531732,
"url": "https://codeload.github.com/pulseaudio/pulseaudio/tar.gz/200618b32f0964a479d69c9b6e5073e6931c370a"
}
}
+205
View File
@@ -0,0 +1,205 @@
import argparse
import hashlib
import io
import json
import os
import re
from pathlib import Path
import shutil
import subprocess
import sys
import tarfile
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
from tools.playstore.payloads import fetch, sha256
from tools.release.check_android_libraries import page_alignment
LIBRARIES = ['libltdl.so', 'libpulseaudio.so', 'libpulse.so', 'libpulsecommon-13.0.so',
'libpulsecore-13.0.so', 'libsndfile.so']
SOURCES = Path(__file__).with_name('audio-sources.json')
def identity(ndk):
paths = [Path(__file__), SOURCES, *sorted((ROOT / 'tools/aaudio-sink').glob('*')),
*sorted((ROOT / 'tools/directaudio/sink').glob('*')),
*[ROOT / 'app/src/main/jniLibs/arm64-v8a' / name for name in LIBRARIES]]
entries = {str(path.relative_to(ROOT)): sha256(path) for path in paths if path.is_file()}
entries['ndk'] = sha256(ndk / 'source.properties')
return hashlib.sha256(json.dumps(entries, sort_keys=True).encode()).hexdigest()
def verify(output, ndk):
manifest = json.loads((output / 'complete.json').read_text())
if manifest.get('identity') != identity(ndk):
raise ValueError('Play audio sources or toolchain changed')
for name, digest in manifest['outputs'].items():
path = output / name
if path.is_symlink() or not path.is_file() or sha256(path) != digest:
raise ValueError('Play audio output failed verification: ' + name)
if set(LIBRARIES) != {Path(name).name for name in manifest['outputs'] if name.startswith('jni/arm64-v8a/')}:
raise ValueError('Play audio native inventory is incomplete')
if 'assets/pulseaudio.tzst' not in manifest['outputs']:
raise ValueError('Play audio modules are missing')
return manifest
def extract(source, destination):
destination.mkdir(parents=True)
with tarfile.open(source, 'r:*') as archive:
archive.extractall(destination, filter='data')
roots = list(destination.iterdir())
if len(roots) != 1 or not roots[0].is_dir():
raise ValueError('Unexpected audio source archive layout')
return roots[0]
def build(output, cache, ndk):
if output.exists():
return verify(output, ndk)
output.mkdir(parents=True)
work = output / 'work'
work.mkdir()
definitions = json.loads(SOURCES.read_text())
sources = {}
for name, definition in definitions.items():
archive = fetch(dict(definition, id=name, urls=[definition['url']]), cache)
sources[name] = extract(archive, work / name)
notices = output / 'assets/licenses/play-audio/sources'
notices.mkdir(parents=True, exist_ok=True)
shutil.copyfile(archive, notices / (name + ('.tar.xz' if name == 'libtool' else '.tar.gz')))
prefix = work / 'prefix'
prefix.mkdir()
toolchain = ndk / 'toolchains/llvm/prebuilt/linux-x86_64/bin'
environment = dict(os.environ)
environment.update({name: str(toolchain / value) for name, value in {
'CC': 'aarch64-linux-android26-clang', 'CXX': 'aarch64-linux-android26-clang++',
'AR': 'llvm-ar', 'NM': 'llvm-nm', 'RANLIB': 'llvm-ranlib', 'STRIP': 'llvm-strip',
}.items()})
environment.update(CFLAGS=f'-O2 -fPIC -Wno-error=int-conversion -I{prefix}/include',
LDFLAGS=f'-Wl,-z,max-page-size=16384 -L{prefix}/lib',
PKG_CONFIG_PATH=str(prefix / 'lib/pkgconfig'), PKG_CONFIG_LIBDIR=str(prefix / 'lib/pkgconfig'),
ac_cv_func_mkfifo='yes', ac_cv_func_getuid='no', ac_cv_func_seteuid='no',
ax_cv_PTHREAD_PRIO_INHERIT='no', ac_cv_header_glob_h='no', ac_cv_header_execinfo_h='no',
ac_cv_func_malloc_0_nonnull='yes', ac_cv_func_realloc_0_nonnull='yes',
ac_cv_lib_ltdl_lt_dladvise_init='yes')
environment['CXXFLAGS'] = environment['CFLAGS']
def run(arguments, directory=work):
subprocess.run([str(value) for value in arguments], cwd=directory, env=environment, check=True)
libtool = work / 'libtool-build'
libtool.mkdir()
run([sources['libtool'] / 'configure', '--host=aarch64-linux-android', '--prefix=' + str(prefix),
'HELP2MAN=/bin/true', 'MAKEINFO=/bin/true'], libtool)
run(['make', '-j8'], libtool)
run(['make', 'install'], libtool)
sndfile = work / 'sndfile-build'
run(['cmake', '-S', sources['libsndfile'], '-B', sndfile, '-G', 'Ninja',
'-DCMAKE_TOOLCHAIN_FILE=' + str(ndk / 'build/cmake/android.toolchain.cmake'),
'-DANDROID_ABI=arm64-v8a', '-DANDROID_PLATFORM=android-26', '-DCMAKE_BUILD_TYPE=Release',
'-DBUILD_SHARED_LIBS=ON', '-DBUILD_PROGRAMS=OFF', '-DBUILD_EXAMPLES=OFF', '-DBUILD_TESTING=OFF',
'-DENABLE_EXTERNAL_LIBS=OFF', '-DCMAKE_SHARED_LINKER_FLAGS=-Wl,-z,max-page-size=16384',
'-DCMAKE_INSTALL_PREFIX=' + str(prefix)])
run(['cmake', '--build', sndfile, '-j8'])
run(['cmake', '--install', sndfile])
pulse = sources['pulseaudio']
(pulse / '.tarball-version').write_text('13.0\n')
run(['autoreconf', '--install', '--force'], pulse)
pulse_build = work / 'pulse-build'
pulse_build.mkdir()
disabled = ['rpath', 'nls', 'x11', 'oss-wrapper', 'alsa', 'esound', 'waveout', 'glib2', 'gtk3', 'gconf',
'avahi', 'jack', 'asyncns', 'tcpwrap', 'lirc', 'dbus', 'bluez5', 'udev', 'openssl', 'manpages',
'samplerate', 'orc', 'systemd-daemon', 'systemd-login', 'systemd-journal', 'webrtc-aec',
'tests', 'neon-opt', 'gsettings']
run([pulse / 'configure', '--host=aarch64-linux-android', '--prefix=' + str(prefix), '--disable-static',
'--enable-shared', '--with-database=simple', '--without-speex', '--without-caps', '--without-fftw',
*['--disable-' + name for name in disabled]], pulse_build)
run(['make', '-j8'], pulse_build)
run(['make', 'install'], pulse_build)
jni = output / 'jni/arm64-v8a'
jni.mkdir(parents=True)
for name in LIBRARIES:
source = prefix / ('bin/pulseaudio' if name == 'libpulseaudio.so' else
'lib/pulseaudio/' + name if name.startswith(('libpulsecommon-', 'libpulsecore-')) else 'lib/' + name)
shutil.copyfile(source, jni / name)
modules = work / 'android-modules'
environment.update(NDK=str(ndk), DROIDDECK_PULSE_LIBS=str(jni))
run(['bash', ROOT / 'tools/aaudio-sink/build.sh', pulse, modules], ROOT)
run([environment['CC'], '-O2', '-shared', '-fPIC', '-Wno-error=int-conversion',
'-Wl,-z,max-page-size=16384', '-I' + str(pulse_build), '-I' + str(pulse / 'src'),
'-I' + str(prefix / 'include'), '-L' + str(jni), '-lpulsecore-13.0', '-lpulsecommon-13.0', '-lpulse',
'-laaudio', '-o', modules / 'module-aaudio-classic-sink.so',
sources['android-module'] / 'pulseaudio-module/module-aaudio-sink.c'])
replacements = {'modules/arm64/' + name: prefix / 'lib/pulse-13.0/modules' / name for name in
['module-pipe-source.so', 'module-pipe-sink.so', 'libprotocol-native.so', 'module-native-protocol-unix.so']}
replacements.update({'modules/arm64/' + name: modules / name for name in
['module-aaudio-sink.so', 'module-directaudio-native-sink.so', 'module-aaudio-classic-sink.so']})
replacements['pactl'] = prefix / 'bin/pactl'
for path in [*jni.glob('*.so'), *replacements.values()]:
errors = page_alignment(path, str(toolchain / 'llvm-readelf'), 16384)
if errors:
raise ValueError('\n'.join(errors))
def exports(path):
symbols = subprocess.check_output([str(toolchain / 'llvm-nm'), '-D', '--defined-only', str(path)], text=True)
return {line.split()[-1].split('@')[0] for line in symbols.splitlines() if line.split()}
for name in LIBRARIES:
old = ROOT / 'app/src/main/jniLibs/arm64-v8a' / name
if exports(old) - exports(jni / name):
raise ValueError('Play audio library dropped existing exports: ' + name)
original_dynamic = subprocess.check_output([str(toolchain / 'llvm-readelf'), '-d', str(old)], text=True)
rebuilt_dynamic = subprocess.check_output([str(toolchain / 'llvm-readelf'), '-d', str(jni / name)], text=True)
if re.findall(r'SONAME.*\[(.*?)\]', original_dynamic) != re.findall(r'SONAME.*\[(.*?)\]', rebuilt_dynamic):
raise ValueError('Play audio library changed its load identity: ' + name)
original = ROOT / 'app/src/main/assets/pulseaudio.tzst'
process = subprocess.Popen(['zstd', '-dc', str(original)], stdout=subprocess.PIPE)
seen = set()
plain = work / 'pulseaudio.tar'
with tarfile.open(fileobj=process.stdout, mode='r|') as source, tarfile.open(plain, 'w') as target:
for member in source:
name = member.name.removeprefix('./')
stream = source.extractfile(member) if member.isfile() else None
if name in replacements:
data = replacements[name].read_bytes()
member.size = len(data)
stream = io.BytesIO(data)
seen.add(name)
target.addfile(member, stream)
for name in sorted(set(replacements) - seen):
data = replacements[name].read_bytes()
member = tarfile.TarInfo(name)
member.size, member.mode = len(data), 0o755
target.addfile(member, io.BytesIO(data))
process.stdout.close()
if process.wait():
raise ValueError('Original audio archive failed to decode')
with (output / 'assets/pulseaudio.tzst').open('xb') as destination:
subprocess.run(['zstd', '-19', '-c', str(plain)], stdout=destination, check=True)
files = [path for tree in ['jni', 'assets'] for path in (output / tree).rglob('*') if path.is_file()]
manifest = {'identity': identity(ndk), 'sources': definitions,
'outputs': {path.relative_to(output).as_posix(): sha256(path) for path in files}}
(output / 'complete.json').write_text(json.dumps(manifest, indent=2, sort_keys=True) + '\n')
return verify(output, ndk)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['build', 'verify'])
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--ndk', type=Path, required=True)
parser.add_argument('--cache', type=Path)
arguments = parser.parse_args()
if arguments.command == 'build':
if arguments.cache is None:
parser.error('build requires --cache')
manifest = build(arguments.output.resolve(), arguments.cache.resolve(), arguments.ndk.resolve())
else:
manifest = verify(arguments.output.resolve(), arguments.ndk.resolve())
print(json.dumps({'verified_outputs': len(manifest['outputs']), 'identity': manifest['identity']}))
if __name__ == '__main__':
main()
+66
View File
@@ -0,0 +1,66 @@
import argparse
import gzip
import hashlib
from pathlib import Path
import subprocess
import sys
import tempfile
from zipfile import ZipFile
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
from tools.playstore.build_audio import LIBRARIES, verify
from tools.release.check_android_libraries import page_alignment
def check(bundle, output, ndk):
manifest = verify(output, ndk)
readelf = str(ndk / 'toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-readelf')
nm = str(ndk / 'toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm')
with ZipFile(bundle) as package, tempfile.TemporaryDirectory() as temporary:
names = package.namelist()
if len(names) != len(set(names)):
raise ValueError('Bundle contains duplicate entries')
for name, digest in manifest['outputs'].items():
if name.startswith('assets/'):
asset = 'base/' + name
if asset not in names and name.endswith('.tar.gz'):
asset = asset.removesuffix('.gz')
expected = hashlib.sha256()
with gzip.open(output / name, 'rb') as source:
for block in iter(lambda: source.read(1024 * 1024), b''):
expected.update(block)
digest = expected.hexdigest()
actual = hashlib.sha256()
with package.open(asset) as stream:
for block in iter(lambda: stream.read(1024 * 1024), b''):
actual.update(block)
if actual.hexdigest() != digest:
raise ValueError('Bundle audio asset differs from verified output: ' + asset)
for name in LIBRARIES:
path = Path(temporary) / name
path.write_bytes(package.read('base/lib/arm64-v8a/' + name))
errors = page_alignment(path, readelf, 16384)
if errors:
raise ValueError('\n'.join(errors))
def exports(library):
result = subprocess.check_output([nm, '-D', '--defined-only', str(library)], text=True)
return {line.split()[-1].split('@')[0] for line in result.splitlines() if line.split()}
rebuilt = output / 'jni/arm64-v8a' / name
if exports(rebuilt) - exports(path):
raise ValueError('Bundle audio library dropped rebuilt exports: ' + name)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--bundle', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--ndk', type=Path, required=True)
arguments = parser.parse_args()
check(arguments.bundle, arguments.output, arguments.ndk)
print('Bundle audio assets, corresponding sources and native exports verified')
if __name__ == '__main__':
main()
+366
View File
@@ -0,0 +1,366 @@
import argparse
import hashlib
import json
import re
import ssl
import sys
import urllib.parse
import urllib.request
from pathlib import Path
import zipfile
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
CHUNK_BYTES = 64 * 1024 * 1024
MODULE_BYTES = 448 * 1024 * 1024
TOTAL_BYTES = 4_000_000_000
RESERVED_BYTES = 250_000_000
def sha256(path):
result = hashlib.sha256()
with path.open('rb') as stream:
for block in iter(lambda: stream.read(1024 * 1024), b''):
result.update(block)
return result.hexdigest()
def https_url(value):
if not isinstance(value, str):
raise ValueError('URL must be a string')
parsed = urllib.parse.urlsplit(value)
if parsed.scheme != 'https' or not parsed.hostname or parsed.username or parsed.password or parsed.fragment:
raise ValueError('Expected a public HTTPS URL: ' + value)
return value
def catalog_bytes(content):
if not isinstance(content, (dict, list)):
raise ValueError('Frozen catalog must be a JSON object or array')
data = (json.dumps(content, sort_keys=True, separators=(',', ':'), ensure_ascii=False) + '\n').encode('utf-8')
if len(data) > 16 * 1024 * 1024:
raise ValueError('Frozen catalog exceeds its size limit')
return data
def load_manifest(path):
manifest = json.loads(path.read_text())
if manifest.get('schema') != 1:
raise ValueError('Unsupported payload manifest schema')
entries = manifest.get('entries')
if not isinstance(entries, list) or not entries:
raise ValueError('Payload manifest is empty')
urls = set()
identities = {}
ids = set()
for entry in entries:
identity = entry.get('id', '')
if not re.fullmatch(r'[a-z0-9][a-z0-9._-]{0,127}', identity) or identity in ids:
raise ValueError('Invalid or duplicate payload id: ' + identity)
ids.add(identity)
digest = entry.get('sha256', '')
if not re.fullmatch(r'[a-f0-9]{64}', digest):
raise ValueError('Payload requires a SHA-256: ' + identity)
size = entry.get('size')
if type(size) is not int or size < 1:
raise ValueError('Payload requires a positive byte size: ' + identity)
if digest in identities and identities[digest] != size:
raise ValueError('Inconsistent size for identical payload: ' + identity)
identities[digest] = size
aliases = entry.get('urls')
if not isinstance(aliases, list) or not aliases:
raise ValueError('Payload requires URL aliases: ' + identity)
for url in aliases:
if url.startswith('droiddeck-bundle://'):
if not re.fullmatch(r'droiddeck-bundle://[a-z0-9][a-z0-9._-]*/[A-Za-z0-9._+-]+', url) or not entry.get('prepare'):
raise ValueError('Invalid prepared component alias: ' + identity)
else:
https_url(url)
if url in urls:
raise ValueError('Duplicate URL alias: ' + url)
urls.add(url)
preparation = entry.get('prepare')
if 'content' in entry:
data = catalog_bytes(entry['content'])
if preparation is not None or 'upstream' in entry or len(data) != size or hashlib.sha256(data).hexdigest() != digest:
raise ValueError('Frozen catalog identity mismatch: ' + identity)
if preparation is None and 'upstream' in entry:
raise ValueError('Upstream archive requires explicit component preparation: ' + identity)
if preparation is not None:
if preparation.get('method') != 'gzip-to-zstd-19' or not re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', preparation.get('zstd_version', '')):
raise ValueError('Invalid component preparation method: ' + identity)
if not re.fullmatch(r'[a-f0-9]{64}', preparation.get('tar_sha256', '')) or type(preparation.get('tar_size')) is not int or not 0 < preparation['tar_size'] <= 16 * 1024 * 1024 * 1024:
raise ValueError('Prepared component requires a pinned tar identity: ' + identity)
upstream = entry.get('upstream', {})
if not re.fullmatch(r'[a-f0-9]{64}', upstream.get('sha256', '')) or type(upstream.get('size')) is not int or upstream['size'] < 1:
raise ValueError('Prepared component requires a pinned upstream archive: ' + identity)
if not isinstance(upstream.get('urls'), list) or not upstream['urls']:
raise ValueError('Prepared component requires upstream URLs: ' + identity)
for url in upstream['urls']:
https_url(url)
https_url(entry.get('source', ''))
if not re.fullmatch(r'[a-f0-9]{40}', entry.get('source_commit', '')):
raise ValueError('Payload requires a pinned source commit: ' + identity)
license_info = entry.get('license', {})
if not license_info.get('expression') or not license_info.get('redistribution_review'):
raise ValueError('Payload requires license and redistribution review: ' + identity)
https_url(license_info.get('url', ''))
required = manifest.get('required')
if not isinstance(required, list) or not required or len(set(required)) != len(required):
raise ValueError('Manifest requires a unique component coverage list')
provided = {feature for entry in entries for feature in entry.get('provides', [])}
missing = set(required) - provided
if missing:
raise ValueError('Missing component coverage: ' + ', '.join(sorted(missing)))
total = sum(identities.values())
if total + RESERVED_BYTES > TOTAL_BYTES:
raise ValueError(f'Payload size budget exceeded: {total} + {RESERVED_BYTES} reserved > {TOTAL_BYTES}')
return manifest
def validate_play_engines(manifest):
if manifest.get('release_ready') is False:
raise ValueError('Review-only component inventory cannot be published')
pending = [entry['id'] for entry in manifest['entries'] if entry.get('license', {}).get('approved') is not True]
if pending:
raise ValueError('Component redistribution review is incomplete: ' + ', '.join(pending))
if 'proton:droiddeck' not in manifest['required']:
raise ValueError('Play distribution requires DroidDeck Proton')
for entry in manifest['entries']:
if any(feature.startswith('emulator:') for feature in entry.get('provides', [])):
raise ValueError('Extra desktop emulators are excluded from the Play distribution')
engines = {name for name in entry.get('provides', []) if name.startswith('proton:')}
if engines - {'proton:droiddeck'}:
raise ValueError('Play distribution permits only DroidDeck Proton')
source_urls = entry.get('upstream', {}).get('urls', entry['urls'])
for url in [*entry['urls'], *source_urls]:
lower = url.lower()
parsed = urllib.parse.urlsplit(url)
if '/steamdeck-desktop-' in parsed.path and parsed.path.rsplit('/', 1)[-1] != 'desktop.tar.zst':
raise ValueError('Extra desktop emulator payload is forbidden in the Play distribution: ' + entry['id'])
if any(name in lower for name in ['/proton-ge-custom/', '/proton-cachyos/', '/steam-seed.json']):
raise ValueError('Alternate Proton input is forbidden in the Play distribution: ' + entry['id'])
if engines and not any('/Droid-Deck/DroidDeck-Proton/' in url for url in source_urls):
raise ValueError('DroidDeck Proton must come from its pinned release')
class HttpsRedirects(urllib.request.HTTPRedirectHandler):
def redirect_request(self, request, file, code, message, headers, url):
https_url(url)
return super().redirect_request(request, file, code, message, headers, url)
def fetch(entry, cache):
cache.mkdir(parents=True, exist_ok=True)
target = cache / entry['sha256']
if target.exists():
if target.is_symlink() or target.stat().st_size != entry['size'] or sha256(target) != entry['sha256']:
raise ValueError('Existing cache payload failed verification: ' + entry['id'])
return target
temporary = cache / (entry['sha256'] + '.part')
if temporary.exists():
raise ValueError('Incomplete payload exists; preserve it and select a fresh cache: ' + str(temporary))
if 'content' in entry:
data = catalog_bytes(entry['content'])
if len(data) != entry['size'] or hashlib.sha256(data).hexdigest() != entry['sha256']:
raise ValueError('Frozen catalog identity mismatch: ' + entry['id'])
with temporary.open('xb') as output:
output.write(data)
temporary.rename(target)
return target
if entry.get('prepare'):
from tools.playstore.prepare_archive import prepare
upstream = fetch(dict(entry['upstream'], id='upstream-' + entry['id']), cache)
prepare(upstream, temporary, entry['prepare'])
if temporary.stat().st_size != entry['size'] or sha256(temporary) != entry['sha256']:
raise ValueError('Prepared payload digest or size mismatch: ' + entry['id'])
temporary.rename(target)
return target
opener = urllib.request.build_opener(HttpsRedirects(), urllib.request.HTTPSHandler(context=ssl.create_default_context()))
request = urllib.request.Request(entry['urls'][0], headers={'Accept-Encoding': 'identity'})
digest = hashlib.sha256()
size = 0
with opener.open(request, timeout=60) as response, temporary.open('xb') as output:
https_url(response.url)
for block in iter(lambda: response.read(1024 * 1024), b''):
size += len(block)
if size > entry['size']:
raise ValueError('Payload exceeds pinned size: ' + entry['id'])
digest.update(block)
output.write(block)
if size != entry['size'] or digest.hexdigest() != entry['sha256']:
raise ValueError('Payload digest or size mismatch: ' + entry['id'])
temporary.rename(target)
return target
def stage(manifest, cache, output, chunk_bytes=CHUNK_BYTES, module_bytes=MODULE_BYTES):
if chunk_bytes < 1 or module_bytes < chunk_bytes or module_bytes > MODULE_BYTES:
raise ValueError('Invalid chunk or module size')
if output.exists():
raise ValueError('Output must be a new directory: ' + str(output))
payloads = {}
for entry in manifest['entries']:
if entry['sha256'] not in payloads:
payloads[entry['sha256']] = fetch(entry, cache)
output.mkdir(parents=True)
index = {'schema': 1, 'manifest_sha256': hashlib.sha256(
json.dumps(manifest, sort_keys=True, separators=(',', ':')).encode()).hexdigest(), 'entries': {}}
module = 1
used = 0
chunks = {}
for digest, path in sorted(payloads.items()):
pieces = []
with path.open('rb') as stream:
number = 0
for block in iter(lambda: stream.read(chunk_bytes), b''):
if used + len(block) > module_bytes:
module += 1
used = 0
name = f'playpayload{module:02d}'
asset = f'play-components/{digest}/{number:04d}'
target = output / name / 'src/main/assets' / asset
target.parent.mkdir(parents=True, exist_ok=True)
with target.open('xb') as destination:
destination.write(block)
pieces.append({'asset': asset, 'module': name, 'size': len(block),
'sha256': hashlib.sha256(block).hexdigest()})
used += len(block)
number += 1
chunks[digest] = pieces
base = output / 'base/src/main/assets/play-components'
recipes = base / 'recipes'
recipes.mkdir(parents=True)
for digest, pieces in chunks.items():
(recipes / (digest + '.json')).write_text(json.dumps({'chunks': pieces}, separators=(',', ':')) + '\n')
for entry in manifest['entries']:
for url in entry['urls']:
index['entries'][url] = {'id': entry['id'], 'sha256': entry['sha256'], 'size': entry['size'],
'recipe': 'play-components/recipes/' + entry['sha256'] + '.json'}
(output / 'index.json').write_text(json.dumps(index, indent=2, sort_keys=True) + '\n')
(output / 'provenance.json').write_text(json.dumps(manifest, indent=2, sort_keys=True) + '\n')
(base / 'index.json').write_text(json.dumps(index, sort_keys=True, separators=(',', ':')) + '\n')
(base / 'provenance.json').write_text(json.dumps(manifest, sort_keys=True, separators=(',', ':')) + '\n')
modules = [f'playpayload{number:02d}' for number in range(1, module + 1)]
for name in modules:
(output / name / 'build.gradle').write_text(
"plugins { id 'com.android.dynamic-feature' }\n"
"android {\n"
f" namespace 'com.droiddeck.launcher.{name}'\n"
" compileSdk 37\n"
" defaultConfig { minSdk 26 }\n"
"}\n"
"dependencies { implementation project(':app') }\n")
(output / name / 'src/main/AndroidManifest.xml').write_text(
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" '
'xmlns:dist="http://schemas.android.com/apk/distribution">\n'
' <dist:module dist:instant="false" dist:title="@string/app_name">\n'
' <dist:delivery><dist:install-time><dist:removable dist:value="true" />'
'</dist:install-time></dist:delivery>\n'
' <dist:fusing dist:include="true" />\n'
' </dist:module>\n'
' <application android:hasCode="false" />\n'
'</manifest>\n')
(output / 'modules.json').write_text(json.dumps(modules) + '\n')
return index
def verify_bundle(manifest, bundle):
expected = {url: entry for entry in manifest['entries'] for url in entry['urls']}
with zipfile.ZipFile(bundle) as package:
if len(package.namelist()) != len(set(package.namelist())):
raise ValueError('Bundle contains duplicate ZIP entries')
index = json.loads(package.read('base/assets/play-components/index.json'))
provenance = json.loads(package.read('base/assets/play-components/provenance.json'))
if provenance != manifest or index.get('schema') != 1 or set(index.get('entries', {})) != set(expected):
raise ValueError('Bundle component inventory differs from the pinned manifest')
canonical = hashlib.sha256(json.dumps(manifest, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
if index.get('manifest_sha256') != canonical:
raise ValueError('Bundle manifest identity mismatch')
referenced = set()
referenced_recipes = set()
verified = set()
for url, pinned in expected.items():
entry = index['entries'][url]
if any(entry.get(field) != pinned[field] for field in ['id', 'sha256', 'size']):
raise ValueError('Bundle component metadata mismatch: ' + pinned['id'])
recipe = 'play-components/recipes/' + pinned['sha256'] + '.json'
if entry.get('recipe') != recipe:
raise ValueError('Bundle component recipe path mismatch')
recipe_path = 'base/assets/' + recipe
referenced_recipes.add(recipe_path)
if package.getinfo(recipe_path).file_size > 16 * 1024 * 1024:
raise ValueError('Bundle component recipe exceeds its size limit')
pieces = json.loads(package.read(recipe_path)).get('chunks')
if not isinstance(pieces, list) or not pieces:
raise ValueError('Bundle component contains no chunks: ' + pinned['id'])
digest = hashlib.sha256()
size = 0
identity = json.dumps(entry, sort_keys=True)
for chunk in pieces:
if not re.fullmatch(r'playpayload[0-9]{2}', chunk['module']) or not re.fullmatch(
r'play-components/[0-9a-f]{64}/[0-9]{4,}', chunk['asset']):
raise ValueError('Invalid bundle chunk path')
path = chunk['module'] + '/assets/' + chunk['asset']
referenced.add(path)
if identity in verified:
continue
info = package.getinfo(path)
if info.file_size != chunk['size'] or not 0 < chunk['size'] <= CHUNK_BYTES:
raise ValueError('Bundle chunk size mismatch: ' + path)
chunk_digest = hashlib.sha256()
with package.open(info) as source:
for block in iter(lambda: source.read(1024 * 1024), b''):
digest.update(block)
chunk_digest.update(block)
size += len(block)
if chunk_digest.hexdigest() != chunk['sha256']:
raise ValueError('Bundle chunk digest mismatch: ' + path)
if identity not in verified and (digest.hexdigest() != pinned['sha256'] or size != pinned['size']):
raise ValueError('Bundle payload failed verification: ' + pinned['id'])
verified.add(identity)
delivered = {name for name in package.namelist() if '/assets/play-components/' in name and
not name.startswith('base/assets/play-components/')}
if delivered != referenced:
raise ValueError('Bundle contains unreferenced component chunks')
base_assets = {name for name in package.namelist() if name.startswith('base/assets/play-components/')}
if base_assets != referenced_recipes | {'base/assets/play-components/index.json', 'base/assets/play-components/provenance.json'}:
raise ValueError('Bundle contains unreferenced component recipes')
sizes = {}
for info in package.infolist():
module = info.filename.split('/')[0]
if module == 'base' or module.startswith('playpayload'):
sizes[module] = sizes.get(module, 0) + info.compress_size
if any(size > 500_000_000 for size in sizes.values()) or sum(sizes.values()) > TOTAL_BYTES:
raise ValueError('Bundle exceeds the conservative module download-size budget')
return sizes
def main():
parser = argparse.ArgumentParser()
parser.add_argument('command', choices=['validate', 'stage', 'verify-bundle'])
parser.add_argument('--manifest', type=Path, required=True)
parser.add_argument('--cache', type=Path)
parser.add_argument('--output', type=Path)
parser.add_argument('--bundle', type=Path)
parser.add_argument('--play-only', action='store_true')
arguments = parser.parse_args()
manifest = load_manifest(arguments.manifest)
if arguments.play_only:
validate_play_engines(manifest)
if arguments.command == 'stage':
if arguments.cache is None or arguments.output is None:
parser.error('stage requires --cache and --output')
stage(manifest, arguments.cache, arguments.output)
if arguments.command == 'verify-bundle':
if arguments.bundle is None:
parser.error('verify-bundle requires --bundle')
print(json.dumps({'compressed_module_bytes': verify_bundle(manifest, arguments.bundle)}, sort_keys=True))
print(json.dumps({'entries': len(manifest['entries']), 'required': len(manifest['required'])}))
if __name__ == '__main__':
main()
+34
View File
@@ -0,0 +1,34 @@
import gzip
import hashlib
from pathlib import Path
import subprocess
def prepare(source, destination, definition):
if definition['method'] != 'gzip-to-zstd-19':
raise ValueError('Unsupported component preparation method')
version = subprocess.check_output(['zstd', '--version'], text=True)
if 'v' + definition['zstd_version'] + ',' not in version:
raise ValueError('Component preparation requires the pinned zstd version')
digest = hashlib.sha256()
size = 0
with gzip.open(source, 'rb') as original, Path(destination).open('xb') as output:
process = subprocess.Popen(['zstd', '-19', '-T4', '-c'], stdin=subprocess.PIPE, stdout=output)
try:
for block in iter(lambda: original.read(1024 * 1024), b''):
size += len(block)
if size > definition['tar_size']:
raise ValueError('Prepared archive exceeds its pinned tar size')
digest.update(block)
process.stdin.write(block)
process.stdin.close()
if process.wait():
raise ValueError('Component archive compression failed')
except BaseException:
process.stdin.close()
if process.poll() is None:
process.terminate()
process.wait()
raise
if size != definition['tar_size'] or digest.hexdigest() != definition['tar_sha256']:
raise ValueError('Prepared archive changed its pinned tar content')
+59
View File
@@ -0,0 +1,59 @@
import argparse
import os
from pathlib import Path
import re
import subprocess
import tempfile
import zipfile
def sign(source, destination, keystore, alias, expected_signer):
if source.resolve() == destination.resolve() or destination.exists():
raise ValueError('Signing requires a new output file')
if not re.fullmatch(r'[0-9a-f]{64}', expected_signer):
raise ValueError('Invalid pinned signing certificate')
password = os.environ.get('RELEASE_STORE_PASSWORD')
if not password:
raise ValueError('Signing password is unavailable')
if not alias:
raise ValueError('Signing alias is unavailable')
with tempfile.TemporaryDirectory(prefix='play-signing-') as temporary:
unsigned = Path(temporary) / 'unsigned.aab'
sanitize(source, unsigned)
environment = dict(os.environ, RELEASE_KEY_ALIAS=alias, RELEASE_KEYSTORE_PATH=str(keystore.resolve()))
result = subprocess.run(['java', '--add-modules', 'jdk.jartool', str(Path(__file__).with_name('SignBundle.java')),
str(unsigned), str(destination), expected_signer], env=environment, capture_output=True, text=True)
if result.returncode == 2:
raise ValueError('Signing certificate differs from the pinned release identity')
if result.returncode:
raise ValueError('Bundle signing or signature verification failed')
print('Bundle signature and payload coverage verified')
def sanitize(source, destination):
with zipfile.ZipFile(source) as original, zipfile.ZipFile(destination, 'x') as output:
if len(original.namelist()) != len(set(original.namelist())):
raise ValueError('Bundle contains duplicate ZIP entries')
for entry in original.infolist():
name = entry.filename.upper()
signature = name == 'META-INF/MANIFEST.MF' or name.startswith('META-INF/') and '/' not in name[9:] and (
name.endswith(('.SF', '.RSA', '.DSA', '.EC')) or name[9:].startswith('SIG-'))
if signature:
continue
with original.open(entry) as source_stream, output.open(entry, 'w', force_zip64=True) as target:
while block := source_stream.read(1024 * 1024):
target.write(block)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('source', type=Path)
parser.add_argument('destination', type=Path)
parser.add_argument('--keystore', type=Path, required=True)
parser.add_argument('--signer', type=Path, default=Path('keystore/release-signer.sha256'))
arguments = parser.parse_args()
sign(arguments.source, arguments.destination, arguments.keystore, os.environ.get('RELEASE_KEY_ALIAS'), arguments.signer.read_text().strip())
if __name__ == '__main__':
main()
+16 -3
View File
@@ -18,16 +18,28 @@ EXPORTS = ['Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion',
'Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeInstall']
def check(apk, readelf, stores=True):
def page_alignment(path, readelf, page_size):
headers = subprocess.check_output([readelf, '--program-headers', '--wide', str(path)], text=True)
loads = [line.split() for line in headers.splitlines() if line.strip().startswith('LOAD ')]
if not loads:
return ['Library has no loadable segments: ' + path.name]
return [f'{path.name} has a LOAD segment incompatible with {page_size}-byte pages'
for load in loads if int(load[-1], 16) < page_size or
(int(load[1], 16) - int(load[2], 16)) % page_size != 0]
def check(apk, readelf, stores=True, page_size=None):
errors = []
with tempfile.TemporaryDirectory() as temp, ZipFile(apk) as package:
libraries = {}
for name in package.namelist():
if name.startswith('lib/arm64-v8a/') and name.endswith('.so'):
if (name.startswith('lib/arm64-v8a/') or name.startswith('base/lib/arm64-v8a/')) and name.endswith('.so'):
path = Path(temp) / Path(name).name
path.write_bytes(package.read(name))
libraries[path.name] = path
for name, path in libraries.items():
if page_size is not None:
errors.extend(page_alignment(path, readelf, page_size))
dynamic = subprocess.check_output([readelf, '-d', str(path)], text=True)
for dependency in re.findall(r'NEEDED.*\[(.*?)\]', dynamic):
if dependency not in libraries and dependency not in SYSTEM:
@@ -51,8 +63,9 @@ if __name__ == '__main__':
parser.add_argument('apk')
parser.add_argument('--readelf', default='readelf')
parser.add_argument('--without-stores', action='store_true')
parser.add_argument('--page-size', type=int, choices=[4096, 16384])
args = parser.parse_args()
errors = check(args.apk, args.readelf, not args.without_stores)
errors = check(args.apk, args.readelf, not args.without_stores, args.page_size)
if errors:
parser.exit(1, '\n'.join(errors) + '\n')
print('APK native exports and dependencies verified')
+1
View File
@@ -19,6 +19,7 @@ def source_assets(overlay=OVERLAY):
sources = list((overlay / 'usr/local/bin').glob('droiddeck-*'))
sources += [path for path in [overlay / 'usr/local/bin/steam-compatibility'] if path.is_file()]
sources += [path for path in (overlay / 'usr/bin').rglob('*') if path.is_file()]
sources += list((overlay / 'usr/local/lib/droiddeck').glob('*.py'))
result = {source: 'assets/linuxfs/' + source.relative_to(overlay).as_posix() for source in sources}
if overlay == OVERLAY:
desktop = overlay.parent / 'desktop'
@@ -0,0 +1,33 @@
import importlib.util
from pathlib import Path
import unittest
from unittest.mock import patch
SPEC = importlib.util.spec_from_file_location('android_libraries', Path(__file__).resolve().parents[1] / 'release/check_android_libraries.py')
libraries = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(libraries)
class AndroidPageAlignmentTest(unittest.TestCase):
def check(self, headers):
with patch.object(libraries.subprocess, 'check_output', return_value=headers):
return libraries.page_alignment(Path('libsample.so'), 'readelf', 16384)
def test_sixteen_kilobyte_segments_are_accepted(self):
self.assertEqual([], self.check(' LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x001000 0x001000 R E 0x4000\n'))
def test_old_four_kilobyte_segments_are_rejected(self):
self.assertEqual(1, len(self.check(' LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x001000 0x001000 R E 0x1000\n')))
def test_each_segment_must_have_congruent_offset_and_address(self):
headers = (' LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x001000 0x001000 R E 0x4000\n'
' LOAD 0x001000 0x0000000000008000 0x0000000000008000 0x001000 0x001000 RW 0x4000\n')
self.assertEqual(1, len(self.check(headers)))
def test_missing_load_segments_do_not_pass(self):
self.assertEqual(1, len(self.check('Program Headers:\n')))
if __name__ == '__main__':
unittest.main()
@@ -2,6 +2,7 @@ import json
import runpy
import tempfile
import unittest
from unittest.mock import patch
from pathlib import Path
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
@@ -19,6 +20,14 @@ def config_text(entries):
class DroidDeckProtonDefaultTest(unittest.TestCase):
def test_play_migrates_unavailable_alternate_requests_to_droiddeck_proton(self):
with patch.dict(COMPAT['requested_tool'].__globals__, PLAY_BUNDLES=True):
self.assertEqual(NEW['name'], COMPAT['requested_tool'](GE, [NEW]))
self.assertEqual(NEW['name'], COMPAT['requested_tool'](VALVE, [NEW]))
default, state = COMPAT['choose_default'](GE['name'], {'default': GE['name']}, {}, [NEW])
self.assertEqual(NEW['name'], default)
self.assertEqual(NEW['name'], state['default'])
def run_default(self, state, mapping, catalog, apps=(), request=None, settle=True):
with tempfile.TemporaryDirectory() as tmp:
directory = Path(tmp)
@@ -0,0 +1,51 @@
import gzip
import hashlib
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
from tools.playstore.prepare_archive import prepare
@unittest.skipUnless(shutil.which('zstd'), 'zstd is unavailable')
class PlayArchivePreparationTest(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory()
self.addCleanup(self.temporary.cleanup)
self.root = Path(self.temporary.name)
self.content = b'archive bytes including headers and file metadata\x00' * 1024
self.source = self.root / 'original.tar.gz'
self.source.write_bytes(gzip.compress(self.content))
version = re.search(r'v([0-9]+\.[0-9]+\.[0-9]+)', subprocess.check_output(['zstd', '--version'], text=True)).group(1)
self.definition = {'method': 'gzip-to-zstd-19', 'zstd_version': version,
'tar_sha256': hashlib.sha256(self.content).hexdigest(), 'tar_size': len(self.content)}
def test_recompression_preserves_every_original_archive_byte(self):
output = self.root / 'prepared.tar.zst'
prepare(self.source, output, self.definition)
self.assertEqual(self.content, subprocess.check_output(['zstd', '-dc', str(output)]))
self.assertEqual(self.content, gzip.decompress(self.source.read_bytes()))
def test_changed_archive_content_cannot_pass_the_pinned_identity(self):
self.definition['tar_sha256'] = '0' * 64
with self.assertRaisesRegex(ValueError, 'tar content'):
prepare(self.source, self.root / 'bad.tar.zst', self.definition)
def test_decompression_cannot_exceed_its_expected_size(self):
self.definition['tar_size'] -= 1
with self.assertRaisesRegex(ValueError, 'tar size'):
prepare(self.source, self.root / 'oversize.tar.zst', self.definition)
def test_different_compressor_version_is_rejected_before_writing(self):
self.definition['zstd_version'] = '0.0.0'
output = self.root / 'unexpected.tar.zst'
with self.assertRaisesRegex(ValueError, 'pinned zstd version'):
prepare(self.source, output, self.definition)
self.assertFalse(output.exists())
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,47 @@
import gzip
import hashlib
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from zipfile import ZipFile
from tools.playstore import check_audio_bundle
class PlayAudioBundleTest(unittest.TestCase):
def test_packaged_sources_allow_lossless_android_gzip_expansion(self):
self.check_source(b'corresponding source', b'corresponding source', expanded=True)
def test_packaged_sources_must_match_even_after_android_gzip_expansion(self):
with self.assertRaisesRegex(ValueError, 'differs'):
self.check_source(b'corresponding source', b'other source', expanded=True)
def test_packaged_audio_asset_cannot_be_replaced_with_stale_output(self):
with self.assertRaisesRegex(ValueError, 'differs'):
self.check_source(b'rebuilt audio', b'old audio', expanded=False)
def test_packaged_sources_cannot_be_omitted(self):
with self.assertRaises(KeyError):
self.check_source(b'corresponding source', None, expanded=True)
def check_source(self, expected, actual, expanded):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
name = 'assets/licenses/play-audio/sources/pulse.tar.gz' if expanded else 'assets/pulseaudio.tzst'
source = gzip.compress(expected) if expanded else expected
path = root / name
path.parent.mkdir(parents=True)
path.write_bytes(source)
bundle = root / 'test.aab'
with ZipFile(bundle, 'w') as package:
if actual is not None:
package.writestr('base/' + (name.removesuffix('.gz') if expanded else name), actual)
manifest = {'outputs': {name: hashlib.sha256(source).hexdigest()}}
with patch.object(check_audio_bundle, 'verify', return_value=manifest), \
patch.object(check_audio_bundle, 'LIBRARIES', []):
check_audio_bundle.check(bundle, root, root / 'ndk')
if __name__ == '__main__':
unittest.main()
+28
View File
@@ -0,0 +1,28 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-emulators'
class PlayEmulatorsTest(unittest.TestCase):
def test_guest_play_install_cannot_reach_external_package_managers(self):
with tempfile.TemporaryDirectory() as directory:
marker = Path(directory) / 'package-manager-called'
environment = dict(os.environ, SCRIPT=str(SCRIPT), MARKER=str(marker))
result = subprocess.run(['bash', '-c', '''
compgen() { return 0; }
droiddeck-root() { printf called > "$MARKER"; return 1; }
flatpak() { printf called > "$MARKER"; return 1; }
export -f compgen droiddeck-root flatpak
exec bash "$SCRIPT" install
'''], env=environment, capture_output=True, text=True)
self.assertEqual(2, result.returncode)
self.assertFalse(marker.exists())
if __name__ == '__main__':
unittest.main()
+313
View File
@@ -0,0 +1,313 @@
import hashlib
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import zipfile
SPEC = importlib.util.spec_from_file_location('playstore_payloads', Path(__file__).resolve().parents[1] / 'playstore/payloads.py')
payloads = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(payloads)
READER_SPEC = importlib.util.spec_from_file_location('component_delivery', Path(__file__).resolve().parents[1] /
'linuxfs/overlay/usr/local/lib/droiddeck/component_delivery.py')
delivery = importlib.util.module_from_spec(READER_SPEC)
READER_SPEC.loader.exec_module(delivery)
class PlaystorePayloadsTest(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
self.cache = self.root / 'cache'
self.cache.mkdir()
self.content = b'component-payload-for-offline-installation'
self.digest = hashlib.sha256(self.content).hexdigest()
(self.cache / self.digest).write_bytes(self.content)
self.manifest = {'schema': 1, 'required': ['runtime'], 'entries': [{
'id': 'runtime', 'size': len(self.content), 'sha256': self.digest,
'urls': ['https://example.test/runtime.tar.zst'], 'provides': ['runtime'],
'source': 'https://example.test/source', 'source_commit': 'a' * 40,
'license': {'expression': 'MIT', 'url': 'https://example.test/LICENSE', 'approved': True,
'redistribution_review': 'License permits redistribution with included notice'},
}]}
def validate(self):
path = self.root / 'manifest.json'
path.write_text(json.dumps(self.manifest))
return payloads.load_manifest(path)
def chunks(self, entry, staged=None):
staged = staged or self.root / "staged"
return json.loads((staged / "base/src/main/assets" / entry["recipe"]).read_text())["chunks"]
def test_chunks_reassemble_exactly_across_module_boundaries_without_network(self):
with patch.object(payloads.urllib.request, 'build_opener', side_effect=AssertionError('Unexpected network')):
index = payloads.stage(self.validate(), self.cache, self.root / 'staged', chunk_bytes=7, module_bytes=14)
entry = index['entries'][self.manifest['entries'][0]['urls'][0]]
restored = bytearray()
modules = {}
for chunk in self.chunks(entry):
content = (self.root / 'staged' / chunk['module'] / 'src/main/assets' / chunk['asset']).read_bytes()
self.assertEqual(hashlib.sha256(content).hexdigest(), chunk['sha256'])
self.assertEqual(len(content), chunk['size'])
modules[chunk['module']] = modules.get(chunk['module'], 0) + len(content)
restored.extend(content)
self.assertGreater(len(modules), 1)
self.assertTrue(all(size <= 14 for size in modules.values()))
self.assertEqual(restored, self.content)
self.assertEqual(hashlib.sha256(restored).hexdigest(), entry['sha256'])
def test_equal_payloads_share_chunks_without_duplicate_delivery(self):
duplicate = dict(self.manifest['entries'][0], id='driver', urls=['https://example.test/driver'], provides=['driver'])
self.manifest['entries'].append(duplicate)
self.manifest['required'].append('driver')
index = payloads.stage(self.validate(), self.cache, self.root / 'staged', chunk_bytes=7, module_bytes=14)
entries = list(index['entries'].values())
self.assertEqual(self.chunks(entries[0]), self.chunks(entries[1]))
delivered = [path for path in (self.root / 'staged').rglob('*') if path.is_file() and path.name.isdigit()]
self.assertEqual(sum(path.stat().st_size for path in delivered), len(self.content))
def test_corrupt_cache_does_not_fall_back_to_network_or_write_output(self):
(self.cache / self.digest).write_bytes(b'corrupt')
with patch.object(payloads.urllib.request, 'build_opener', side_effect=AssertionError('Unexpected network')):
with self.assertRaisesRegex(ValueError, 'verification'):
payloads.stage(self.validate(), self.cache, self.root / 'staged')
self.assertFalse((self.root / 'staged').exists())
self.assertEqual((self.cache / self.digest).read_bytes(), b'corrupt')
def test_existing_output_is_preserved(self):
output = self.root / 'staged'
output.mkdir()
sentinel = output / 'keep'
sentinel.write_bytes(b'existing')
with self.assertRaisesRegex(ValueError, 'new directory'):
payloads.stage(self.validate(), self.cache, output)
self.assertEqual(sentinel.read_bytes(), b'existing')
def test_required_feature_cannot_be_silently_omitted(self):
self.manifest['required'].append('proton')
with self.assertRaisesRegex(ValueError, 'Missing component coverage: proton'):
self.validate()
def test_unknown_schema_and_empty_manifest_are_rejected(self):
self.manifest['schema'] = 2
with self.assertRaisesRegex(ValueError, 'schema'):
self.validate()
self.manifest['schema'] = 1
self.manifest['entries'] = []
with self.assertRaisesRegex(ValueError, 'empty'):
self.validate()
def test_prepared_alias_cannot_bypass_upstream_pinning(self):
entry = self.manifest['entries'][0]
entry['urls'] = ['droiddeck-bundle://runtime/runtime.tar.zst']
with self.assertRaisesRegex(ValueError, 'prepared component alias'):
self.validate()
entry['prepare'] = {'method': 'gzip-to-zstd-19', 'zstd_version': '1.5.5',
'tar_sha256': 'a' * 64, 'tar_size': 1024}
with self.assertRaisesRegex(ValueError, 'upstream archive'):
self.validate()
entry['upstream'] = {'urls': ['https://example.test/runtime.tar.gz'], 'sha256': 'b' * 64, 'size': 100}
self.validate()
entry['upstream']['urls'] = ['http://example.test/runtime.tar.gz']
with self.assertRaisesRegex(ValueError, 'HTTPS'):
self.validate()
def test_review_only_inventory_cannot_enter_the_play_release_workflow(self):
self.manifest['release_ready'] = False
with self.assertRaisesRegex(ValueError, 'Review-only'):
payloads.validate_play_engines(self.manifest)
def test_unconfirmed_redistribution_cannot_be_published_by_toggling_the_release_flag(self):
self.manifest['release_ready'] = True
self.manifest['entries'][0]['license']['approved'] = False
with self.assertRaisesRegex(ValueError, 'redistribution review is incomplete'):
payloads.validate_play_engines(self.manifest)
def test_play_bundle_cannot_include_extra_desktop_emulators(self):
self.manifest['required'].append('proton:droiddeck')
entry = self.manifest['entries'][0]
entry['provides'].append('proton:droiddeck')
entry['urls'] = ['https://github.com/Droid-Deck/DroidDeck-Proton/releases/download/pinned/proton.tar.gz']
entry['provides'].append('emulator:ppsspp')
with self.assertRaisesRegex(ValueError, 'Extra desktop emulators'):
payloads.validate_play_engines(self.manifest)
def test_frozen_catalog_is_served_without_consulting_a_mutable_external_api(self):
entry = self.manifest['entries'][0]
entry['content'] = {'version': 'pinned', 'assets': []}
data = payloads.catalog_bytes(entry['content'])
entry['sha256'], entry['size'] = hashlib.sha256(data).hexdigest(), len(data)
with patch.object(payloads.urllib.request, 'build_opener', side_effect=AssertionError('Unexpected network')):
target = payloads.fetch(self.validate()['entries'][0], self.cache)
self.assertEqual(data, target.read_bytes())
def test_frozen_catalog_changes_cannot_keep_the_old_hash(self):
entry = self.manifest['entries'][0]
entry['content'] = {'version': 'changed'}
with self.assertRaisesRegex(ValueError, 'Frozen catalog identity'):
self.validate()
def test_unpinned_or_unreviewed_component_is_rejected(self):
for field, value, message in [('sha256', 'bad', 'SHA-256'), ('size', True, 'positive byte'),
('source_commit', 'main', 'pinned source'), ('license', {}, 'redistribution')]:
with self.subTest(field=field):
original = self.manifest['entries'][0][field]
self.manifest['entries'][0][field] = value
with self.assertRaisesRegex(ValueError, message):
self.validate()
self.manifest['entries'][0][field] = original
def test_duplicate_urls_and_unsafe_sources_are_rejected(self):
entry = self.manifest['entries'][0]
entry['urls'].append(entry['urls'][0])
with self.assertRaisesRegex(ValueError, 'Duplicate URL'):
self.validate()
for url in ['http://example.test/component', 'https://user:secret@example.test/component',
'file:///tmp/component', 'https://example.test/component#fragment']:
entry['urls'] = [url]
with self.subTest(url=url), self.assertRaisesRegex(ValueError, 'HTTPS'):
self.validate()
def test_total_delivery_budget_counts_unique_content_and_base_reserve(self):
with patch.object(payloads, 'TOTAL_BYTES', len(self.content) + payloads.RESERVED_BYTES - 1):
with self.assertRaisesRegex(ValueError, 'budget exceeded'):
self.validate()
def test_redirect_cannot_downgrade_to_plain_http(self):
with self.assertRaisesRegex(ValueError, 'HTTPS'):
payloads.HttpsRedirects().redirect_request(None, None, 302, '', {}, 'http://example.test/payload')
def test_partial_download_is_preserved_and_not_resumed_without_identity(self):
target = self.cache / self.digest
alternate = self.root / 'other-cache'
alternate.mkdir()
part = alternate / (self.digest + '.part')
part.write_bytes(target.read_bytes()[:3])
with self.assertRaisesRegex(ValueError, 'Incomplete payload'):
payloads.fetch(self.manifest['entries'][0], alternate)
self.assertEqual(part.read_bytes(), self.content[:3])
def apk_set(self, change=None):
staged = self.root / 'staged'
index = payloads.stage(self.validate(), self.cache, staged, chunk_bytes=7, module_bytes=14)
installed = self.root / 'installed/0'
installed.mkdir(parents=True)
if change is not None:
change(staged, index)
for module in staged.iterdir():
if not module.is_dir():
continue
assets = module / 'src/main/assets'
with zipfile.ZipFile(installed / (module.name + '.apk'), 'w', zipfile.ZIP_DEFLATED) as archive:
for path in sorted(assets.rglob('*')):
if path.is_file():
archive.write(path, 'assets/' + path.relative_to(assets).as_posix())
return installed.parent
def test_guest_reads_the_same_chunked_payload_directly_from_split_apks(self):
installed = self.apk_set()
self.assertTrue(delivery.enabled(installed))
with delivery.BundleReader(installed) as reader:
destination = self.root / 'restored'
reader.fetch(self.manifest['entries'][0]['urls'][0], destination, self.digest)
self.assertEqual(destination.read_bytes(), self.content)
def test_guest_corruption_preserves_existing_file_without_external_fallback(self):
def corrupt(staged, index):
chunk = self.chunks(next(iter(index['entries'].values())), staged)[0]
(staged / chunk['module'] / 'src/main/assets' / chunk['asset']).write_bytes(b'corrupt')
installed = self.apk_set(corrupt)
destination = self.root / 'restored'
destination.write_bytes(b'existing')
with delivery.BundleReader(installed) as reader:
with self.assertRaisesRegex(ValueError, 'verification'):
reader.fetch(self.manifest['entries'][0]['urls'][0], destination)
self.assertEqual(destination.read_bytes(), b'existing')
self.assertEqual(list(self.root.glob('*.component-part')), [])
def test_guest_missing_split_is_a_failure_without_any_download(self):
installed = self.apk_set()
split = installed / '0/playpayload02.apk'
split.rename(self.root / 'preserved-split.apk')
with delivery.BundleReader(installed) as reader:
with self.assertRaises(KeyError):
reader.read(self.manifest['entries'][0]['urls'][0])
def test_guest_rejects_missing_component_and_wrong_digest(self):
with delivery.BundleReader(self.apk_set()) as reader:
with self.assertRaisesRegex(ValueError, 'absent'):
reader.read('https://example.test/not-bundled')
with self.assertRaisesRegex(ValueError, 'version'):
reader.fetch(self.manifest['entries'][0]['urls'][0], self.root / 'restored', '0' * 64)
def test_guest_rejects_duplicate_assets_across_splits(self):
installed = self.apk_set()
original = installed / '0/base.apk'
(installed / '0/duplicate.apk').write_bytes(original.read_bytes())
with self.assertRaisesRegex(ValueError, 'Duplicate'):
delivery.BundleReader(installed)
def bundle(self, change=None):
staged = self.root / 'staged'
index = payloads.stage(self.validate(), self.cache, staged, chunk_bytes=7, module_bytes=14)
if change is not None:
change(staged, index)
target = self.root / 'fixture.aab'
with zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as archive:
for module in staged.iterdir():
if module.is_dir():
assets = module / 'src/main/assets'
for path in sorted(assets.rglob('*')):
if path.is_file():
archive.write(path, module.name + '/assets/' + path.relative_to(assets).as_posix())
return target
def test_final_bundle_inventory_and_each_delivered_byte_are_verified(self):
sizes = payloads.verify_bundle(self.validate(), self.bundle())
self.assertIn('base', sizes)
self.assertIn('playpayload02', sizes)
def test_final_bundle_cannot_contain_corrupt_chunk_bytes(self):
def corrupt(staged, index):
entry = next(iter(index['entries'].values()))
chunk = self.chunks(entry, staged)[0]
(staged / chunk['module'] / 'src/main/assets' / chunk['asset']).write_bytes(b'corrupt')
with self.assertRaisesRegex(ValueError, 'digest mismatch'):
payloads.verify_bundle(self.validate(), self.bundle(corrupt))
def test_final_bundle_cannot_silently_include_stale_unreferenced_components(self):
def add_stale(staged, index):
path = staged / 'playpayload01/src/main/assets/play-components/stale'
path.write_bytes(b'old component')
with self.assertRaisesRegex(ValueError, 'unreferenced'):
payloads.verify_bundle(self.validate(), self.bundle(add_stale))
def test_final_bundle_must_match_the_release_manifest(self):
bundle = self.bundle()
self.manifest['entries'][0]['id'] = 'changed'
with self.assertRaisesRegex(ValueError, 'inventory differs'):
payloads.verify_bundle(self.manifest, bundle)
def test_play_distribution_rejects_alternate_proton_engines_and_valve_seed(self):
with self.assertRaisesRegex(ValueError, 'requires DroidDeck'):
payloads.validate_play_engines(self.manifest)
self.manifest['required'].append('proton:droiddeck')
entry = self.manifest['entries'][0]
entry['provides'].append('proton:droiddeck')
entry['urls'] = ['https://github.com/Droid-Deck/DroidDeck-Proton/releases/download/pinned/proton.tar.gz']
payloads.validate_play_engines(self.manifest)
for url in ['https://github.com/GloriousEggroll/proton-ge-custom/releases/download/pinned/proton.tar.gz',
'https://github.com/CachyOS/proton-cachyos/releases/download/pinned/proton.tar.xz',
'https://raw.githubusercontent.com/Droid-Deck/DroidDeck-Components/main/steam-seed.json']:
entry['urls'] = [url]
with self.assertRaisesRegex(ValueError, 'forbidden'):
payloads.validate_play_engines(self.manifest)
if __name__ == '__main__':
unittest.main()
+82
View File
@@ -0,0 +1,82 @@
import importlib.util
import contextlib
import io
import os
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import zipfile
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location('playstore_signing', ROOT / 'tools/playstore/sign_bundle.py')
signing = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(signing)
class PlaystoreSigningTest(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.root = Path(temporary.name)
self.source = self.root / 'input.aab'
self.destination = self.root / 'signed.aab'
self.keystore = ROOT / 'keystore/testkey.p12'
self.signer = 'a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc'
with zipfile.ZipFile(self.source, 'w', zipfile.ZIP_DEFLATED) as package:
package.writestr('base/assets/runtime', b'component payload')
package.writestr('base/manifest/AndroidManifest.xml', b'fixture manifest')
package.writestr('META-INF/OLD.SF', b'old signature')
package.writestr('META-INF/OLD.RSA', b'old certificate')
package.writestr('META-INF/services/runtime', b'keep service metadata')
def sign(self, signer=None):
with patch.dict(os.environ, RELEASE_STORE_PASSWORD='android'):
signing.sign(self.source, self.destination, self.keystore, 'testkey', signer or self.signer)
def test_signs_with_pinned_certificate_preserving_every_payload_and_source(self):
original = self.source.read_bytes()
self.sign()
self.assertEqual(original, self.source.read_bytes())
with zipfile.ZipFile(self.source) as source, zipfile.ZipFile(self.destination) as output:
self.assertNotIn('META-INF/OLD.SF', output.namelist())
self.assertNotIn('META-INF/OLD.RSA', output.namelist())
for name in ['base/assets/runtime', 'base/manifest/AndroidManifest.xml', 'META-INF/services/runtime']:
self.assertEqual(source.read(name), output.read(name))
def test_unexpected_signing_identity_creates_no_output(self):
with self.assertRaisesRegex(ValueError, 'release identity'):
self.sign('0' * 64)
self.assertFalse(self.destination.exists())
def test_existing_destination_cannot_be_overwritten(self):
self.destination.write_bytes(b'keep existing')
with self.assertRaisesRegex(ValueError, 'new output'):
self.sign()
self.assertEqual(self.destination.read_bytes(), b'keep existing')
def test_missing_password_cannot_sign_a_bundle(self):
with patch.dict(os.environ, RELEASE_STORE_PASSWORD=''):
with self.assertRaisesRegex(ValueError, 'password'):
signing.sign(self.source, self.destination, self.keystore, 'testkey', self.signer)
self.assertFalse(self.destination.exists())
def test_alias_is_not_used_as_signature_metadata_or_printed(self):
output = io.StringIO()
with contextlib.redirect_stdout(output):
self.sign()
self.assertNotIn('testkey', output.getvalue())
with zipfile.ZipFile(self.destination) as package:
self.assertIn('META-INF/RELEASE.SF', package.namelist())
self.assertFalse(any('TESTKEY' in name.upper() for name in package.namelist()))
def test_wrong_alias_fails_without_printing_it_or_creating_an_output(self):
with patch.dict(os.environ, RELEASE_STORE_PASSWORD='android'):
with self.assertRaisesRegex(ValueError, '^Bundle signing or signature verification failed$'):
signing.sign(self.source, self.destination, self.keystore, 'private-alias-value', self.signer)
self.assertFalse(self.destination.exists())
if __name__ == '__main__':
unittest.main()
+106
View File
@@ -0,0 +1,106 @@
from pathlib import Path
import re
import unittest
import yaml
ROOT = Path(__file__).resolve().parents[2]
class PlaystoreWorkflowTest(unittest.TestCase):
def setUp(self):
self.play = yaml.load((ROOT / '.github/workflows/playstore.yml').read_text(), Loader=yaml.BaseLoader)
self.shared = yaml.load((ROOT / '.github/workflows/build.yml').read_text(), Loader=yaml.BaseLoader)
self.release = yaml.load((ROOT / '.github/workflows/release.yml').read_text(), Loader=yaml.BaseLoader)
def test_play_source_defaults_to_playstore_and_is_pinned_for_build_and_signing(self):
self.assertEqual(['playstore'], self.play['on']['push']['branches'])
inputs = self.play['on']['workflow_dispatch']['inputs']
self.assertEqual('playstore', inputs['source_branch']['default'])
self.assertEqual(['playstore'], inputs['source_branch']['options'])
self.assertEqual('Version for the file names, e.g. 0.1.7', inputs['version']['description'])
self.assertEqual('true', inputs['version']['required'])
call = self.play['jobs']['bundle']
self.assertEqual('./.github/workflows/build.yml', call['uses'])
self.assertEqual('true', call['with']['play_bundle'])
self.assertEqual('${{ needs.source.outputs.sha }}', call['with']['source_sha'])
for name in ['sign', 'publish']:
self.assertEqual('${{ needs.source.outputs.sha }}', self.play['jobs'][name]['steps'][0]['with']['ref'])
self.assertEqual('signing-playstore', self.play['jobs']['sign']['environment'])
signing = next(step for step in self.play['jobs']['sign']['steps'] if step.get('name') == 'Sign the verified Play bundle')
self.assertEqual('${{ secrets.RELEASE_KEY_ALIAS }}', signing['env']['RELEASE_KEY_ALIAS'])
self.assertNotIn('--alias', signing['run'])
self.assertEqual('${{ needs.source.outputs.version_code }}', call['with']['play_version_code'])
def test_shared_release_and_catalog_jobs_cannot_run_for_a_play_bundle(self):
for name in ['sign', 'packs', 'announce', 'catalog']:
with self.subTest(job=name):
condition = self.shared['jobs'][name]['if']
self.assertIn('!inputs.play_bundle', condition)
self.assertIn("github.ref == 'refs/heads/main'", condition)
def test_only_manual_play_releases_can_publish_and_never_replace_latest(self):
self.assertEqual('read', self.play['permissions']['contents'])
self.assertEqual('Build PlayStore Bundle', self.play['name'])
publish = self.play['jobs']['publish']
self.assertEqual("github.event_name == 'workflow_dispatch'", publish['if'])
self.assertEqual('write', publish['permissions']['contents'])
command = publish['steps'][-1]['run']
self.assertIn('--latest=false', command)
self.assertIn('tag="playstore-$VERSION"', command)
self.assertNotRegex(command, r'--latest(?:\s|$)')
allowed = {'actions/checkout@v4', 'actions/setup-java@v4', 'actions/download-artifact@v4',
'actions/upload-artifact@v4', 'actions/setup-python@v5'}
artifacts = []
for name, job in self.play['jobs'].items():
if name != 'publish':
self.assertNotEqual('write', job.get('permissions', {}).get('contents'))
for step in job.get('steps', []):
if 'uses' in step:
self.assertIn(step['uses'], allowed)
command = step.get('run', '')
if name != 'publish':
self.assertIsNone(re.search(r'gh\s+(?:release|api\b[^\n]*releases)|publish-ci|announce-preview|update-catalog', command))
if step.get('uses') == 'actions/upload-artifact@v4':
artifacts.append(step['with']['name'])
self.assertIn('droiddeck-play-bundle-signed', artifacts)
def test_play_source_hash_reaches_every_shared_compile_and_test_checkout(self):
for name in ['source', 'xbox', 'components', 'build', 'store-tests', 'python-tests']:
checkout = self.shared['jobs'][name]['steps'][0]
self.assertIn('inputs.source_sha', checkout['with']['ref'])
def test_existing_manual_release_entry_point_can_build_play_without_changing_main(self):
entry = self.release['jobs']['playstore']
self.assertEqual("github.ref == 'refs/heads/playstore'", entry['if'])
self.assertEqual('./.github/workflows/playstore.yml', entry['uses'])
self.assertEqual('playstore', entry['with']['source_branch'])
self.assertEqual('${{ inputs.version }}', entry['with']['version'])
self.assertEqual('true', self.play['on']['workflow_call']['inputs']['version']['required'])
for name in ['find', 'sign', 'packs', 'publish']:
self.assertEqual("github.ref == 'refs/heads/main'", self.release['jobs'][name]['if'])
self.assertIn("'release-playstore' || 'release'", self.release['concurrency']['group'])
def test_play_version_codes_are_independent_of_the_entry_workflow_counter(self):
self.assertEqual('playstore-bundle', self.play['concurrency']['group'])
self.assertEqual('false', self.play['concurrency']['cancel-in-progress'])
source = self.play['jobs']['source']['steps'][-1]['run']
self.assertIn('date -u +%s', source)
self.assertNotIn('GITHUB_RUN_NUMBER', source)
self.assertIn('version_code=$((base + elapsed))', source)
def test_distribution_checks_run_without_the_production_manifest_or_signing_secrets(self):
checks = self.play['jobs']['checks']
self.assertNotIn('needs', checks)
self.assertNotIn('environment', checks)
self.assertEqual('read', checks['permissions']['contents'])
self.assertIn('checks', self.play['jobs']['sign']['needs'])
for step in checks['steps']:
self.assertNotIn('secrets.', str(step))
command = checks['steps'][-1]['run']
self.assertIn('tools.tests.test_playstore_signing', command)
self.assertIn('tools.tests.test_playstore_payloads', command)
if __name__ == '__main__':
unittest.main()