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

@@ -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()