mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
PlayStore: deliver runtime modules and resources on demand
Preserve component file inventories, reuse setup controls, and keep Play-managed resources behind a read-only guest stream. Build version 0.6.9.2 with version code 19 and support large signed release assets.
This commit is contained in:
1 parent
d25f6940ee
commit
5d10c8e96d
25 files changed
+1329
-83
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -18,6 +19,77 @@ READER_SPEC.loader.exec_module(delivery)
|
||||
|
||||
|
||||
class PlaystorePayloadsTest(unittest.TestCase):
|
||||
def test_split_data_is_play_hosted_and_guest_reassembles_the_exact_archive(self):
|
||||
import io
|
||||
import subprocess
|
||||
import tarfile
|
||||
from tools.playstore.split_archive import prepare, combine, identity
|
||||
content = io.BytesIO()
|
||||
with tarfile.open(fileobj=content, mode='w') as archive:
|
||||
for name, body in [('program', b'\x7fELFprogram'), ('font.ttf', b'\x00\x01\x00\x00font')]:
|
||||
member = tarfile.TarInfo(name)
|
||||
member.size = len(body)
|
||||
archive.addfile(member, io.BytesIO(body))
|
||||
original = self.root / 'original'
|
||||
original.write_bytes(subprocess.check_output(['zstd', '-c'], input=content.getvalue()))
|
||||
code, data, combined = [self.root / name for name in ['code', 'data', 'combined']]
|
||||
version = re.search(r'v([0-9.]+),', subprocess.check_output(['zstd', '--version'], text=True)).group(1)
|
||||
layout = prepare(original, code, data, version)
|
||||
layout['zstd_version'] = '1.5.5'
|
||||
combine([code, data], combined)
|
||||
entry = self.manifest['entries'][0]
|
||||
source = dict(entry, **identity(original))
|
||||
(self.cache / source['sha256']).write_bytes(original.read_bytes())
|
||||
entry.update(identity(combined))
|
||||
entry['layout'] = dict(layout, source=source)
|
||||
(self.cache / entry['sha256']).write_bytes(combined.read_bytes())
|
||||
staged = self.root / 'staged'
|
||||
index = payloads.stage(self.validate(), self.cache, staged, chunk_bytes=64, module_bytes=256)
|
||||
packs = json.loads((staged / 'assetpacks.json').read_text())
|
||||
self.assertEqual(['playdata01'], packs)
|
||||
for module in json.loads((staged / 'modules.json').read_text()):
|
||||
self.assertIn('<dist:on-demand />', (staged / module / 'src/main/AndroidManifest.xml').read_text())
|
||||
bundled = self.root / 'installed'
|
||||
(bundled / '0').mkdir(parents=True)
|
||||
for module in ['base'] + json.loads((staged / 'modules.json').read_text()):
|
||||
with zipfile.ZipFile(bundled / '0' / (module + '.apk'), 'w') as archive:
|
||||
root = staged / module / 'src/main'
|
||||
for file in root.rglob('*'):
|
||||
if file.is_file():
|
||||
archive.write(file, str(file.relative_to(root)))
|
||||
import socket
|
||||
import struct
|
||||
import threading
|
||||
endpoint = str(self.root / 'resource-socket')
|
||||
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.addCleanup(listener.close)
|
||||
listener.bind(endpoint)
|
||||
listener.listen()
|
||||
listener.settimeout(5)
|
||||
count = sum(chunk['module'] == packs[0] for chunk in self.chunks(index['entries'][entry['urls'][0]]))
|
||||
errors = []
|
||||
def serve():
|
||||
try:
|
||||
for _ in range(count):
|
||||
client, _ = listener.accept()
|
||||
with client, client.makefile('rb') as source:
|
||||
length = struct.unpack('>I', source.read(4))[0]
|
||||
path = source.read(length).decode('utf-8')
|
||||
client.sendall(b'\x01' + (staged / packs[0] / 'src/main/assets' / path).read_bytes())
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
server = threading.Thread(target=serve, daemon=True)
|
||||
server.start()
|
||||
with patch.object(delivery, 'SOCKET', endpoint), delivery.BundleReader(bundled) as reader:
|
||||
restored = io.BytesIO()
|
||||
reader.copy(entry['urls'][0], restored, entry['size'])
|
||||
server.join(timeout=5)
|
||||
self.assertFalse(server.is_alive())
|
||||
self.assertFalse(errors)
|
||||
self.assertEqual(combined.read_bytes(), restored.getvalue())
|
||||
chunks = self.chunks(index['entries'][entry['urls'][0]])
|
||||
self.assertTrue(any(chunk['module'] == packs[0] for chunk in chunks))
|
||||
|
||||
def setUp(self):
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
@@ -318,6 +390,16 @@ class PlaystorePayloadsTest(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ValueError, 'digest mismatch'):
|
||||
payloads.verify_bundle(self.validate(), self.bundle(corrupt))
|
||||
|
||||
def test_native_payload_cannot_be_relabelled_as_a_data_asset_pack(self):
|
||||
def relabel(staged, index):
|
||||
entry = next(iter(index['entries'].values()))
|
||||
recipe = staged / 'base/src/main/assets' / entry['recipe']
|
||||
definition = json.loads(recipe.read_text())
|
||||
definition['chunks'][0]['module'] = 'playdata01'
|
||||
recipe.write_text(json.dumps(definition))
|
||||
with self.assertRaisesRegex(ValueError, 'delivery are mixed'):
|
||||
payloads.verify_bundle(self.validate(), self.bundle(relabel))
|
||||
|
||||
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'
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.playstore.release_assets import prepare
|
||||
|
||||
|
||||
class PlayReleaseAssetsTest(unittest.TestCase):
|
||||
def test_large_signed_bundle_is_reassembled_without_changing_a_byte(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
bundle = root / 'DroidDeck.aab'
|
||||
content = bytes(range(256)) * 4
|
||||
bundle.write_bytes(content)
|
||||
checksum = root / 'SHA256SUMS'
|
||||
checksum.write_text(hashlib.sha256(content).hexdigest() + ' ' + bundle.name + '\n')
|
||||
output = root / 'release'
|
||||
parts = prepare(bundle, checksum, output, limit=300)
|
||||
self.assertEqual(content, b''.join((output / name).read_bytes() for name in parts))
|
||||
self.assertTrue(all((output / name).stat().st_size <= 300 for name in parts))
|
||||
self.assertEqual(checksum.read_text(), (output / 'SHA256SUMS').read_text())
|
||||
self.assertEqual(content, bundle.read_bytes())
|
||||
self.assertEqual(4, len((output / 'PART-SHA256SUMS').read_text().splitlines()))
|
||||
|
||||
def test_changed_signed_bundle_cannot_be_published(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
bundle = root / 'bundle.aab'
|
||||
bundle.write_bytes(b'changed')
|
||||
checksum = root / 'SHA256SUMS'
|
||||
checksum.write_text('0' * 64 + ' bundle.aab\n')
|
||||
with self.assertRaisesRegex(ValueError, 'checksum verification'):
|
||||
prepare(bundle, checksum, root / 'release', limit=4)
|
||||
@@ -0,0 +1,63 @@
|
||||
import io
|
||||
import re
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from tools.playstore.split_archive import combine, data_file, inventory, prepare, validate_data
|
||||
|
||||
|
||||
class PlaySplitArchiveTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.directory.cleanup)
|
||||
self.root = Path(self.directory.name)
|
||||
self.zstd_version = re.search(r'v([0-9.]+),', subprocess.check_output(['zstd', '--version'], text=True)).group(1)
|
||||
|
||||
def archive(self, entries, name='source'):
|
||||
content = io.BytesIO()
|
||||
with tarfile.open(fileobj=content, mode='w', format=tarfile.GNU_FORMAT) as archive:
|
||||
for path, body, mode in entries:
|
||||
member = tarfile.TarInfo(path)
|
||||
member.size, member.mode = len(body), mode
|
||||
member.uid, member.gid, member.mtime = 1000, 1001, 1234567
|
||||
archive.addfile(member, io.BytesIO(body))
|
||||
target = self.root / name
|
||||
target.write_bytes(subprocess.check_output(['zstd', '-19', '-T4', '-c'], input=content.getvalue()))
|
||||
return target
|
||||
|
||||
def test_split_preserves_all_file_content_and_metadata_across_tar_encodings(self):
|
||||
font = 'usr/share/fonts/' + 'long-directory/' * 8 + 'example.ttf'
|
||||
source = self.archive([('usr/bin/program', b'\x7fELF' + b'code' * 100, 0o755),
|
||||
(font, b'\x00\x01\x00\x00' + b'font' * 100, 0o755),
|
||||
('usr/share/config.json', b'{"enabled":true}', 0o644)])
|
||||
code, data, combined = [self.root / name for name in ['code', 'data', 'combined']]
|
||||
result = prepare(source, code, data, self.zstd_version)
|
||||
combine([code, data], combined)
|
||||
self.assertEqual(result['inventory_sha256'], inventory(combined, 'zstd')[0])
|
||||
self.assertEqual(inventory(source, 'zstd')[0], inventory(combined, 'zstd')[0])
|
||||
validate_data(data)
|
||||
raw = subprocess.check_output(['zstd', '-dc', str(combined)])
|
||||
with tarfile.open(fileobj=io.BytesIO(raw)) as archive:
|
||||
self.assertEqual(archive.getnames(), ['usr/bin/program', 'usr/share/config.json', font])
|
||||
self.assertEqual(archive.getmember(font).mode, 0o755)
|
||||
|
||||
def test_data_validation_rejects_executables_and_scripts_with_resource_names(self):
|
||||
for content in [b'\x7fELF' + b'library', b'MZ' + b'program', b'#!/bin/sh\necho bad']:
|
||||
member = tarfile.TarInfo('image.png')
|
||||
self.assertFalse(data_file(member, content))
|
||||
source = self.archive([('image.png', content, 0o644)], name=str(len(content)))
|
||||
with self.assertRaisesRegex(ValueError, 'unclassified'):
|
||||
validate_data(source)
|
||||
|
||||
def test_missing_assets_do_not_silently_produce_an_empty_pack(self):
|
||||
source = self.archive([('program', b'\x7fELFcode', 0o755)])
|
||||
with self.assertRaisesRegex(ValueError, 'no classified'):
|
||||
prepare(source, self.root / 'code', self.root / 'data', self.zstd_version)
|
||||
|
||||
def test_archive_paths_cannot_escape_the_runtime(self):
|
||||
source = self.archive([('../font.ttf', b'\x00\x01\x00\x00font', 0o644)])
|
||||
with self.assertRaisesRegex(ValueError, 'unsafe'):
|
||||
inventory(source, 'zstd')
|
||||
Reference in new issue
Block a user