Reject incomplete install packages and exclude inherited guest disk

This commit is contained in:
bholman committed 2026-11-18 16:33:51 -07:00
1 parent 8088c1fbf6
commit 9b8e25d977
9 files changed
+277 -25

No files matched your search

+130 -7
View File
@@ -3,6 +3,7 @@
Copyright (c) 2026 Brian K. Holman. BSD-3-Clause; see LICENSES/.
The target is a disposable local filesystem, never an SSH/device connection.
"""
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
@@ -103,14 +104,17 @@ class InstallerTransactionTests(unittest.TestCase):
self.boot0 = 'games/coco3plus/boot0.rom'
self.boot1 = 'games/coco3plus/boot1.rom'
self.config = 'config/coco3plus_v51.CFG'
self.rbf = '_Computer/.coco3plus/fixture/core.rbf'
rbf_data = b'new bitstream fixture\n'
digest = hashlib.sha256(rbf_data).hexdigest()
self.rbf = '_Computer/.coco3plus/' + digest[:12] + '/coco3plus.rbf'
contents = {
# Deliberately put the launcher first: manifest ordering cannot activate it early.
self.launcher: b'new launcher fixture\n',
self.launcher: ('<mistergamedescription><rbf>' + self.rbf[:-4] +
'</rbf><setname>coco3plus</setname></mistergamedescription>').encode(),
self.boot0: b'new boot0 fixture\n',
self.config: b'new default configuration\n',
self.config: installer.default_config(False),
self.boot1: b'new boot1 fixture\n',
self.rbf: b'new bitstream fixture\n',
self.rbf: rbf_data,
}
entries = {}
for name, data in contents.items():
@@ -120,7 +124,15 @@ class InstallerTransactionTests(unittest.TestCase):
entries[name] = dict(
bytes=len(data), sha256=installer.sha(path),
policy='create-if-missing' if name == self.config else 'backup-and-replace')
self.manifest = dict(files=entries, rbf_sha256=entries[self.rbf]['sha256'])
# Synthetic bytes are accepted ONLY through per-test patched identities.
# These fixtures are not original ROMs and never leave disposable storage.
boot = {Path(name).name: (len(contents[name]), entries[name]['sha256'])
for name in (self.boot0, self.boot1)}
boot_patch = patch.object(installer, 'BOOT', boot)
boot_patch.start()
self.addCleanup(boot_patch.stop)
self.manifest = dict(format=1, public_package=False, roms_required_separately=False,
ssc_included=False, files=entries, rbf_sha256=digest)
(self.package / 'manifest.json').write_text(json.dumps(self.manifest))
for name, data in ((self.launcher, b'old launcher remains selectable\n'),
(self.boot0, b'old boot0 recoverable\n'),
@@ -133,6 +145,103 @@ class InstallerTransactionTests(unittest.TestCase):
return installer.install_release(
self.package, 'disposable.invalid', load=load, target=target, stamp=stamp)
def write_manifest(self):
(self.package / 'manifest.json').write_text(json.dumps(self.manifest))
def replace_member(self, name, content):
path = self.package / 'fat' / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(content)
self.manifest['files'][name] = dict(bytes=len(content), sha256=installer.sha(path),
policy='create-if-missing' if name == self.config else 'backup-and-replace')
def add_synthetic_ssc(self):
identities = {}
for name in installer.SSC:
content = ('synthetic SSC fixture ' + name).encode()
path = 'games/coco3plus/' + name
self.replace_member(path, content)
identities[name] = (len(content), self.manifest['files'][path]['sha256'])
ssc_patch = patch.object(installer, 'SSC', identities)
ssc_patch.start()
self.addCleanup(ssc_patch.stop)
self.manifest['ssc_included'] = True
self.replace_member(self.config, installer.default_config(True))
for index, name in ((4, 'pic-7040-510.bin'), (5, 'sp0256-al2.bin')):
self.replace_member('config/coco3plus.f' + str(index),
('/media/fat/games/coco3plus/' + name).encode().ljust(1024, b'\0'))
def assert_rejected_before_target(self):
self.write_manifest()
target = DisposableTarget(self.device, self.package)
with self.assertRaises(RuntimeError):
self.install(target)
self.assertEqual(target.events, [])
self.assertFalse((self.package / 'install-receipt.json').exists())
def test_missing_rbf_boot_or_config_rejected_before_target_access(self):
for name in (self.rbf, self.boot0, self.boot1, self.config):
with self.subTest(name=name):
entry = self.manifest['files'].pop(name)
self.assert_rejected_before_target()
self.manifest['files'][name] = entry
def test_missing_ssc_rom_or_path_record_rejected_before_target_access(self):
self.add_synthetic_ssc()
for name in ('games/coco3plus/pic-7040-510.bin',
'games/coco3plus/sp0256-al2.bin',
'config/coco3plus.f4', 'config/coco3plus.f5'):
with self.subTest(name=name):
entry = self.manifest['files'].pop(name)
self.assert_rejected_before_target()
self.manifest['files'][name] = entry
def test_complete_synthetic_ssc_package_validates(self):
self.add_synthetic_ssc()
self.write_manifest()
checked = installer.validated_manifest(self.package)
self.assertTrue(checked['ssc_included'])
def test_contradictory_launcher_or_rbf_digest_rejected_before_target(self):
original = (self.package / 'fat' / self.launcher).read_bytes()
for content in (original.replace(b'coco3plus</setname>', b'other</setname>'),
original.replace(self.rbf[:-4].encode(), b'_Computer/other'),
original.replace(b'</rbf>', b'</rbf><rbf>other</rbf>')):
with self.subTest(content=content):
self.replace_member(self.launcher, content)
self.assert_rejected_before_target()
self.replace_member(self.launcher, original)
# Keep the addressed prefix so this specifically checks full digest agreement.
digest = self.manifest['rbf_sha256']
self.manifest['rbf_sha256'] = digest[:12] + ('0' if digest[12] != '0' else '1') + digest[13:]
self.assert_rejected_before_target()
def test_bad_flags_digests_and_rom_identity_rejected_before_target(self):
for field, value in (('format', 2), ('format', True), ('public_package', True),
('public_package', 0), ('roms_required_separately', True),
('ssc_included', None), ('rbf_sha256', 'not-a-digest')):
with self.subTest(field=field, value=value):
original = self.manifest[field]
self.manifest[field] = value
self.assert_rejected_before_target()
self.manifest[field] = original
original_digest = self.manifest['files'][self.boot0]['sha256']
self.manifest['files'][self.boot0]['sha256'] = 'bad-file-digest'
self.assert_rejected_before_target()
self.manifest['files'][self.boot0]['sha256'] = original_digest
self.replace_member(self.boot0, b'wrong synthetic boot identity')
self.assert_rejected_before_target()
def test_ssc_wrong_path_or_rom_identity_rejected_before_target(self):
self.add_synthetic_ssc()
self.replace_member('config/coco3plus.f4',
b'/media/fat/games/wrong/pic.bin'.ljust(1024, b'\0'))
self.assert_rejected_before_target()
self.replace_member('config/coco3plus.f4',
b'/media/fat/games/coco3plus/pic-7040-510.bin'.ljust(1024, b'\0'))
self.replace_member('games/coco3plus/sp0256-al2.bin', b'wrong synthetic speech identity')
self.assert_rejected_before_target()
def test_support_interruption_keeps_old_launcher_and_recovery_evidence(self):
target = DisposableTarget(self.device, self.package, interrupt_on=self.boot1)
original_launcher = (self.device / self.launcher).read_bytes()
@@ -148,7 +257,7 @@ class InstallerTransactionTests(unittest.TestCase):
self.assertIn(self.launcher, receipt['planned_files'])
self.assertFalse(receipt['activation_attempted'])
self.assertIs(receipt['launcher_activated'], False)
self.assertFalse(receipt['loaded'])
self.assertFalse(receipt['load_command_sent'])
self.assertEqual(receipt['error']['type'], 'KeyboardInterrupt')
self.assertEqual((self.device / self.launcher).read_bytes(), original_launcher)
self.assertNotIn('activate', target.events)
@@ -171,7 +280,9 @@ class InstallerTransactionTests(unittest.TestCase):
self.assertEqual(receipt['status'], 'complete')
self.assertTrue(receipt['launcher_activated'])
self.assertTrue(receipt['loaded'])
self.assertTrue(receipt['load_command_sent'])
self.assertIs(receipt['startup_observed'], False)
self.assertNotIn('loaded', receipt)
self.assertEqual((self.device / self.config).read_bytes(), original_config)
self.assertEqual((self.device / self.launcher).read_bytes(),
(self.package / 'fat' / self.launcher).read_bytes())
@@ -186,6 +297,18 @@ class InstallerTransactionTests(unittest.TestCase):
self.assertFalse(target.local(receipt['launcher_temporary']).exists())
self.assertTrue(target.local(receipt['stage'] + '/fat/' + self.launcher).exists())
def test_load_command_failure_is_uncertain_and_does_not_claim_startup(self):
target = DisposableTarget(self.device, self.package)
with patch.object(target, 'load', side_effect=OSError('Unconfirmed command write')):
with self.assertRaisesRegex(OSError, 'Unconfirmed command write'):
self.install(target, load=True)
receipt = json.loads((self.package / 'install-receipt.json').read_text())
self.assertEqual(receipt['status'], 'failed')
self.assertTrue(receipt['launcher_activated'])
self.assertIsNone(receipt['load_command_sent'])
self.assertIs(receipt['startup_observed'], False)
self.assertNotIn('loaded', receipt)
def test_preflight_failure_keeps_plan_and_does_not_mutate_target(self):
target = DisposableTarget(self.device, self.package, fail_preflight=True)
before = {p.relative_to(self.device): p.read_bytes()
+44
View File
@@ -6,7 +6,9 @@ Original test contributions: BSD-3-Clause; see LICENSES/.
import hashlib
import io
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tarfile
@@ -25,6 +27,48 @@ def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
class ActualRepositoryExportPolicy(unittest.TestCase):
def test_actual_inherited_guest_disk_is_not_source(self):
revision = subprocess.check_output(
['git', 'rev-parse', 'HEAD'], cwd=REPOSITORY).decode().strip()
tree = release.committed_tree(revision)
# Inspect the real inherited Git object, not a fabricated .vhd filename.
# changes.txt identifies boot.vhd as mounted/autorun guest software.
self.assertIn('docs/Boot.vhd', tree)
self.assertEqual(len(release.committed_blob(tree['docs/Boot.vhd'])), 224016)
exported = {name for name in tree if release.source_member(name)}
self.assertNotIn('docs/Boot.vhd', exported)
self.assertEqual(release.SOURCE_SUFFIXES['docs'], {'.md'})
self.assertIn('rtl/CPU09/cpu09_119.vhd', exported)
def test_powershell_wrapper_passes_selected_and_default_build(self):
shell = shutil.which('pwsh') or shutil.which('powershell')
if not shell:
self.skipTest('PowerShell unavailable for native wrapper contract')
with tempfile.TemporaryDirectory(dir=REPOSITORY / 'build') as folder:
base = Path(folder)
capture = base / 'capture.json'
fake = base / 'capture-python.ps1'
fake.write_text('$args | ConvertTo-Json -Compress | Set-Content -LiteralPath $env:COCO3PLUS_WRAPPER_CAPTURE\n$global:LASTEXITCODE=0\n')
environment = os.environ.copy()
environment['COCO3PLUS_WRAPPER_CAPTURE'] = str(capture)
for selection in ([], ['-Build', 'build/fpga-qualified']):
with self.subTest(selection=selection):
subprocess.run(
[shell, '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File',
str(REPOSITORY / 'windows/Package-Release.ps1'),
'-Python', str(fake), '-Output', 'dist/qualified',
'-PublicPackage', *selection], cwd=REPOSITORY,
env=environment, check=True, capture_output=True)
arguments = json.loads(capture.read_text(encoding='utf-8-sig'))
index = arguments.index('--build')
self.assertEqual(arguments[index + 1],
selection[-1] if selection else 'build/fpga')
self.assertIn('--public', arguments)
self.assertEqual(arguments[arguments.index('--output') + 1],
'dist/qualified')
class SimulatorInstallGuards(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(dir=REPOSITORY / 'build')