Publish VCC Plus OSS source distribution for 3.6.2 build 60

This commit is contained in:
Brian Holman committed 2026-11-13 00:14:03 -07:00
commit c652a5276d
1192 files changed
+272368

No files matched your search

+249
View File
@@ -0,0 +1,249 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Reconstruct pinned upstream firmware from verified assembly-only inputs.
Preparation is explicit and offline. Fetch cocoroms/LWTools separately, or supply
an existing LWASM. Builds use fresh copies and verify all generated ROM hashes.
Only the two boot ROMs, provenance manifest and notice enter an optional bundle.
"""
import argparse
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import time
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location('vcc_dependency_fetch', ROOT / 'tools/dependencies/fetch.py')
DEPENDENCIES = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(DEPENDENCIES)
ROM_NAMES = ('bas13.rom', 'extbas11.rom', 'disk11.rom', 'coco3.rom')
BUNDLE_NAMES = ('coco3.rom', 'disk11.rom')
SOURCE_NAMES = {'Makefile', 'equates.asm', 'bas.asm', 'extbas.asm', 'disk.asm', 'coco3.asm', 'md5sums.txt', 'mugateerspic.asm'}
NOTICE = '''Reconstructed upstream Color Computer firmware
Source: ToolShed cocoroms, https://github.com/nitros9project/toolshed
Pinned revision: 3667a41c9605b7fcb150eace29cc7f42d460be1a
The original assembly headers identify material copied from Color BASIC
Unravelled, Extended Color BASIC Unravelled, Disk Color BASIC Unravelled and
Super Color BASIC Unravelled, adjusted for assembly. Their original source
comments are retained in the explicitly prepared source dependency.
Original firmware notices retained in the generated ROM data include:
Color BASIC: (C) 1982 TANDY; MICROSOFT
Extended Color BASIC: COPYRIGHT (C) 1982 BY TANDY;
UNDER LICENSE FROM MICROSOFT
Disk Extended Color BASIC: COPYRIGHT (C) 1982 BY TANDY;
UNDER LICENSE FROM MICROSOFT
Super Color BASIC: COPR. 1982, 1986 BY TANDY;
UNDER LICENSE FROM MICROSOFT; AND MICROWARE SYSTEMS CORP.
These are reconstructed upstream firmware, not newly authored or clean-room
replacement ROMs. The emulator's GPL notice does not relabel this firmware.
No preassembled ROM or binary input is used by this reconstruction process.
'''
def encoded(value):
return (json.dumps(value, sort_keys=True, indent=2) + '\n').encode('utf-8')
def sha(path):
return DEPENDENCIES.sha(path)
def source_record(records):
record = records['cocoroms']
if set(record['vendoredFileSHA256']) != SOURCE_NAMES or set(record['upstreamFileSHA256']) != SOURCE_NAMES:
raise ValueError('Firmware input inventory must contain exactly the eight assembly/build reference files')
if (record.get('sourceRevision') != '3667a41c9605b7fcb150eace29cc7f42d460be1a'
or record.get('archiveRootPrefix') != 'cocoroms/' or record.get('optional') is not True):
raise ValueError('Unsupported firmware source identity')
outputs = record.get('romOutputs')
if not isinstance(outputs, dict) or set(outputs) != set(ROM_NAMES):
raise ValueError('Firmware output inventory must contain the four qualified ROMs')
sizes = {'coco3.rom': 32768, 'disk11.rom': 8192, 'bas13.rom': 8192, 'extbas11.rom': 8192}
for name, spec in outputs.items():
if (not isinstance(spec, dict) or spec.get('bytes') != sizes[name]
or not isinstance(spec.get('sha256'), str) or not DEPENDENCIES.HASH.fullmatch(spec['sha256'])):
raise ValueError('Invalid generated firmware fingerprint: ' + name)
return record
def copy_verified(source, destination, fingerprints):
DEPENDENCIES.verify_tree(source, fingerprints)
destination.mkdir(parents=True, exist_ok=False)
for name in sorted(fingerprints):
original = source / name
target = destination / name
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(original, target)
target.chmod(0o755 if original.stat().st_mode & 0o111 else 0o644)
DEPENDENCIES.verify_tree(destination, fingerprints)
def validate_outputs(directory, expected):
if set(expected) != set(ROM_NAMES):
raise ValueError('Expected all four reconstructed ROM outputs')
results = {}
for name in ROM_NAMES:
path = directory / name
if path.is_symlink() or not path.is_file() or path.stat().st_nlink != 1:
raise ValueError('Generated ROM must be an ordinary unlinked file: ' + name)
result = {'bytes': path.stat().st_size, 'sha256': sha(path)}
if result != expected[name]:
raise ValueError('Generated ROM does not match the qualified fingerprint: ' + name)
results[name] = result
return results
def bundle_manifest(record, metadata, assembler, outputs):
return {
'schemaVersion': 1,
'generator': 'vccplus-rom-build',
'source': {
'project': 'ToolShed cocoroms', 'url': record['sourceURL'],
'revision': record['sourceRevision'], 'manifestSHA256': sha(metadata),
'filesSHA256': record['vendoredFileSHA256'], 'preassembledROMInputs': False,
},
'assembler': assembler,
'roms': {name: outputs[name] for name in BUNDLE_NAMES},
}
def write_bundle(destination, source, manifest):
"""Atomically create a fresh optional payload; never replace a prior bundle."""
destination = Path(destination).absolute()
DEPENDENCIES.require_unlinked_ancestors(destination, 'ROM bundle destination')
if destination.exists() or destination.is_symlink():
raise ValueError('Choose a fresh ROM bundle destination; existing payloads are never overwritten')
for name in BUNDLE_NAMES:
path = source / name
spec = manifest['roms'][name]
if path.is_symlink() or not path.is_file() or path.stat().st_nlink != 1 or path.stat().st_size != spec['bytes'] or sha(path) != spec['sha256']:
raise ValueError('ROM output changed before optional bundle staging')
destination.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='.rom-bundle-', dir=destination.parent) as temporary:
staged = Path(temporary) / 'bundle'
staged.mkdir()
for name in BUNDLE_NAMES:
shutil.copyfile(source / name, staged / name)
if (staged / name).stat().st_size != manifest['roms'][name]['bytes'] or sha(staged / name) != manifest['roms'][name]['sha256']:
raise ValueError('ROM output changed while copying the optional bundle')
(staged / 'ROMBuild.json').write_bytes(encoded(manifest))
(staged / 'ROM_SOURCE_NOTICE.txt').write_text(NOTICE, encoding='utf-8')
if destination.exists() or destination.is_symlink():
raise ValueError('ROM bundle destination changed during preparation')
staged.rename(destination)
def build(output, dependency_root, lwasm=None, bundle_output=None, jobs=4):
output, dependency_root = Path(output).absolute(), Path(dependency_root).absolute()
DEPENDENCIES.require_unlinked_ancestors(output, 'Firmware build output')
DEPENDENCIES.require_unlinked_ancestors(dependency_root, 'Firmware dependency root')
if output.exists() or output.is_symlink():
raise ValueError('Choose a fresh build output; previous evidence is never overwritten')
if output == ROOT or output in ROOT.parents or output == dependency_root or dependency_root in output.parents:
raise ValueError('Choose build output separate from the source/dependency tree')
records, locations = DEPENDENCIES.load_records(ROOT)
record = source_record(records)
source = dependency_root / DEPENDENCIES.validate_record(record)
DEPENDENCIES.verify_tree(source, record['vendoredFileSHA256'])
if lwasm is None:
lwrecord = records['lwtools']
lwsource = dependency_root / DEPENDENCIES.validate_record(lwrecord)
DEPENDENCIES.verify_tree(lwsource, lwrecord['vendoredFileSHA256'])
else:
lwasm = Path(lwasm).resolve()
if not lwasm.is_file() or not os.access(lwasm, os.X_OK):
raise ValueError('The explicitly selected LWASM is unavailable')
if bundle_output is not None:
bundle = Path(bundle_output).absolute()
DEPENDENCIES.require_unlinked_ancestors(bundle, 'ROM bundle destination')
# An optional package must not alter verified dependency inputs. Reject
# a parent of the build too: creating the build would occupy that bundle
# destination before its final atomic installation.
canonical_bundle, canonical_dependencies, canonical_output = bundle.resolve(), dependency_root.resolve(), output.resolve()
if (canonical_bundle == canonical_dependencies or canonical_dependencies in canonical_bundle.parents
or canonical_bundle == canonical_output or canonical_bundle in canonical_output.parents):
raise ValueError('ROM bundle must be separate from dependency inputs and the build ancestor path')
if bundle.exists() or bundle.is_symlink():
raise ValueError('Choose a fresh ROM bundle destination')
output.mkdir(parents=True)
report = {'schemaVersion': 1, 'status': 'running', 'networkUsed': False,
'sourceRevision': record['sourceRevision'], 'preassembledROMInputs': False, 'commands': []}
started = time.monotonic()
try:
with (output / 'build.log').open('wb') as log:
def run(command, cwd, stdout=None):
report['commands'].append({'argv': [str(p) for p in command], 'cwd': str(cwd)})
log.write(('$ ' + ' '.join(str(p) for p in command) + '\n').encode())
log.flush()
subprocess.run(command, cwd=cwd, check=True, stdout=log if stdout is None else stdout, stderr=log)
if lwasm is None:
lwcopy = output / 'work/lwtools'
copy_verified(lwsource, lwcopy, lwrecord['vendoredFileSHA256'])
run(['/usr/bin/make', '-j' + str(jobs), 'lwasm'], lwcopy)
lwasm = lwcopy / 'lwasm/lwasm'
assembler = {'version': subprocess.check_output([str(lwasm), '--version'], stderr=subprocess.STDOUT).decode().strip(),
'sha256': sha(lwasm)}
report['assembler'] = assembler
work = output / 'work/cocoroms'
copy_verified(source, work, record['vendoredFileSHA256'])
if any(p.suffix.lower() in ('.rom', '.bin') for p in work.rglob('*')):
raise ValueError('Preassembled firmware input is forbidden')
# Execute the pinned Makefile's assembly sequence directly. A fresh
# directory forces every output and --lwasm selects the exact tool,
# independent of executable names or the user's PATH.
run([str(lwasm), '--raw', '-o', 'bas13.rom', 'equates.asm', 'bas.asm'], work)
with (work / 'cb_equates.asm').open('wb') as symbols:
run([str(lwasm), '--symbol-dump', '--no-output', 'equates.asm', 'bas.asm'], work, symbols)
run([str(lwasm), '--raw', '-o', 'extbas11.rom', 'cb_equates.asm', 'extbas.asm'], work)
with (work / 'ecb_equates.asm').open('wb') as symbols:
run([str(lwasm), '--symbol-dump', '--no-output', 'equates.asm', 'bas.asm', 'extbas.asm'], work, symbols)
run([str(lwasm), '--raw', '--pragma=noindex0tonone', '-o', 'disk11.rom', 'ecb_equates.asm', 'disk.asm'], work)
run([str(lwasm), '--raw', '-o', 'coco3.rom', 'equates.asm', 'coco3.asm'], work)
outputs = validate_outputs(work, record['romOutputs'])
for name in ROM_NAMES:
shutil.copyfile(work / name, output / name)
manifest = bundle_manifest(record, locations['cocoroms'] / 'cocoroms.json', assembler, outputs)
(output / 'ROMBuild.json').write_bytes(encoded(manifest))
(output / 'ROM_SOURCE_NOTICE.txt').write_text(NOTICE, encoding='utf-8')
if bundle_output is not None:
write_bundle(Path(bundle_output), output, manifest)
report.update(status='passed', roms=outputs)
except Exception as error:
report.update(status='failed', failure=str(error))
raise
finally:
report['elapsedSeconds'] = round(time.monotonic() - started, 3)
(output / 'BuildReport.json').write_bytes(encoded(report))
return report
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', type=Path, default=ROOT / '.build/roms')
parser.add_argument('--dependency-root', type=Path, default=ROOT / 'third_party')
parser.add_argument('--lwasm', type=Path, help='Explicit existing assembler; otherwise build verified LWTools lwasm only')
parser.add_argument('--bundle-output', type=Path, help='Fresh optional SourceROMBundle destination')
parser.add_argument('--jobs', type=int, default=4)
args = parser.parse_args()
if not 1 <= args.jobs <= 32:
parser.error('--jobs must be between 1 and 32')
result = build(args.output, args.dependency_root, args.lwasm, args.bundle_output, args.jobs)
print(json.dumps({'status': result['status'], 'roms': result['roms'], 'networkUsed': False}, indent=2))
if __name__ == '__main__':
try:
main()
except (ValueError, OSError, subprocess.CalledProcessError) as error:
raise SystemExit(str(error))
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Validate an explicitly selected source-built core ROM bundle.
Use the same bounded directory, manifest and byte checks as source-profile
staging. Validation establishes byte identity, not a redistribution license.
"""
import argparse
from pathlib import Path
import runpy
_STAGE = runpy.run_path(str(Path(__file__).with_name('stage.py')))
def validate_bundle(path):
source = _STAGE['absolute'](path)
_STAGE['directory'](source, 'Source ROM bundle')
return _STAGE['read_bundle'](source)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('action', choices=['validate'])
parser.add_argument('bundle', type=Path)
args = parser.parse_args()
try:
validate_bundle(args.bundle)
except (OSError, ValueError, UnicodeError) as error:
parser.error(str(error))
print('PASS: source ROM bundle validated')
if __name__ == '__main__':
main()
+319
View File
@@ -0,0 +1,319 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Stage an optional explicitly built ROM bundle for source-profile Mac builds.
Validation establishes byte identity and a safe copy boundary, not redistribution
permission or hardware compatibility. No maintainer ROM or media path is probed.
Isolated UI-test products omit all bundles without inspecting their input paths.
"""
import argparse
import ctypes
import errno
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import stat
import sys
import tempfile
ROM_SIZES = {'coco3.rom': 32768, 'disk11.rom': 8192}
BUNDLE_FILES = set(ROM_SIZES) | {'ROMBuild.json', 'ROM_SOURCE_NOTICE.txt'}
STARTER_FILES = ('Hello World.dsk', 'Hello World.cas', 'Blank Disk.dsk',
'Hello Hard Disk.dsk', 'Blank OS9 Header.bin')
HASH = re.compile(r'^[0-9a-f]{64}$')
MANIFEST_LIMIT = 256 * 1024
NOTICE_LIMIT = 64 * 1024
STARTER_LIMIT = 2 * 1024 * 1024
def absolute(path):
value = Path(path)
if '..' in value.parts:
raise ValueError('Staging paths must not contain parent traversal')
return value if value.is_absolute() else Path.cwd() / value
def check_ancestors(path, description):
"""Reject redirected inputs/outputs, except the two public macOS aliases."""
aliases = {Path('/tmp'): Path('/private/tmp'), Path('/var'): Path('/private/var')}
for parent in path.parents:
try:
info = parent.lstat()
except FileNotFoundError:
continue
if stat.S_ISLNK(info.st_mode):
if parent in aliases and parent.resolve() == aliases[parent]:
continue
raise ValueError(description + ' must not have a linked ancestor')
if not stat.S_ISDIR(info.st_mode):
raise ValueError(description + ' must have directory ancestors')
def directory(path, description, optional=False):
check_ancestors(path, description)
try:
info = path.lstat()
except FileNotFoundError:
if optional:
return False
raise ValueError(description + ' is missing')
if not stat.S_ISDIR(info.st_mode):
raise ValueError(description + ' must be an unlinked directory')
return True
def signature(info):
return (info.st_dev, info.st_ino, info.st_mode, info.st_nlink,
info.st_size, info.st_mtime_ns, info.st_ctime_ns)
def read_regular(path, maximum, description, expected_size=None):
check_ancestors(path, description)
try:
before = path.lstat()
except FileNotFoundError:
raise ValueError(description + ' is missing')
if not stat.S_ISREG(before.st_mode) or before.st_nlink != 1:
raise ValueError(description + ' must be a regular file without links')
if not 0 < before.st_size <= maximum or (expected_size is not None and before.st_size != expected_size):
raise ValueError(description + ' has an invalid size')
flags = os.O_RDONLY | getattr(os, 'O_NOFOLLOW', 0) | getattr(os, 'O_CLOEXEC', 0)
with os.fdopen(os.open(path, flags), 'rb') as stream:
if signature(os.fstat(stream.fileno())) != signature(before):
raise ValueError(description + ' changed before it could be read')
data = stream.read(maximum + 1)
after = os.fstat(stream.fileno())
if signature(before) != signature(after) or len(data) != before.st_size:
raise ValueError(description + ' changed while it was read')
# A concurrently replaced pathname must not be accepted as the same input.
if signature(path.lstat()) != signature(before):
raise ValueError(description + ' was replaced while it was read')
return data
def unique_object(pairs):
value = {}
for key, item in pairs:
if key in value:
raise ValueError('ROM build manifest contains a duplicate field')
value[key] = item
return value
def read_bundle(source):
if set(item.name for item in source.iterdir()) != BUNDLE_FILES:
raise ValueError('Source ROM bundle must contain exactly the manifest, notice, and two required ROMs')
manifest_bytes = read_regular(source / 'ROMBuild.json', MANIFEST_LIMIT, 'ROM build manifest')
try:
record = json.loads(manifest_bytes.decode('utf-8'), object_pairs_hook=unique_object)
except RecursionError:
raise ValueError('ROM build manifest nesting is excessive')
if not isinstance(record, dict) or type(record.get('schemaVersion')) is not int or record['schemaVersion'] != 1:
raise ValueError('Unsupported ROM build manifest schema')
if record.get('generator') != 'vccplus-rom-build':
raise ValueError('Unsupported ROM bundle generator')
if not isinstance(record.get('source'), dict) or not record['source']:
raise ValueError('ROM build manifest requires source metadata')
roms = record.get('roms')
if not isinstance(roms, dict) or set(roms) != set(ROM_SIZES):
raise ValueError('ROM build manifest must identify both required ROMs only')
payload = {'ROMBuild.json': manifest_bytes}
notice = read_regular(source / 'ROM_SOURCE_NOTICE.txt', NOTICE_LIMIT, 'ROM source notice')
if not notice.decode('utf-8').strip() or b'\0' in notice:
raise ValueError('ROM source notice must be nonempty UTF-8 text')
payload['ROM_SOURCE_NOTICE.txt'] = notice
for name, size in ROM_SIZES.items():
entry = roms[name]
if (not isinstance(entry, dict) or type(entry.get('bytes')) is not int or entry['bytes'] != size
or not isinstance(entry.get('sha256'), str) or not HASH.fullmatch(entry['sha256'])):
raise ValueError('Invalid ROM manifest entry: ' + name)
data = read_regular(source / name, size, name, expected_size=size)
if hashlib.sha256(data).hexdigest() != entry['sha256']:
raise ValueError('ROM bytes do not match their build manifest: ' + name)
payload[name] = data
return payload
def check_resources(resources):
if (resources.name != 'Resources' or resources.parent.name != 'Contents'
or resources.parent.parent.suffix != '.app'):
raise ValueError('Destination must be APP.app/Contents/Resources')
directory(resources, 'App Resources destination', optional=True)
def check_kit(kit):
if not directory(kit, 'Build-owned StarterKit', optional=True):
return False
# Cleanup is confined to this build-owned directory. Reject redirected files
# before either swapping an existing kit or removing an obsolete payload.
for folder, names, files in os.walk(kit, followlinks=False):
for name in names:
directory(Path(folder) / name, 'StarterKit member directory')
for name in files:
info = (Path(folder) / name).lstat()
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
raise ValueError('StarterKit members must be regular files without links')
return True
def overlaps(first, second):
# Ancestors were checked before canonicalizing the accepted /tmp,/var aliases.
first, second = first.resolve(), second.resolve()
return first == second or first in second.parents or second in first.parents
def atomic_file(destination, data):
check_ancestors(destination, 'Build resource')
if destination.exists() or destination.is_symlink():
info = destination.lstat()
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
raise ValueError('Build resource must be a regular file without links')
if info.st_size == len(data) and read_regular(destination, STARTER_LIMIT, 'Existing build resource') == data:
return
temporary = None
try:
with tempfile.NamedTemporaryFile(prefix='.source-rom-resource-', dir=destination.parent, delete=False) as stream:
temporary = Path(stream.name)
stream.write(data)
os.chmod(temporary, 0o644)
# Recheck the owned output boundary immediately before replacement.
check_ancestors(destination, 'Build resource')
if destination.exists() or destination.is_symlink():
info = destination.lstat()
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
raise ValueError('Build resource changed to a linked or nonregular file')
os.replace(temporary, destination)
finally:
if temporary is not None:
temporary.unlink(missing_ok=True)
def exchange_directories(first, second):
"""Atomically exchange complete directories; never expose a partial kit."""
library = ctypes.CDLL(None, use_errno=True)
if sys.platform == 'darwin':
operation = getattr(library, 'renamex_np', None)
if operation is not None:
operation.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_uint)
operation.restype = ctypes.c_int
result = operation(os.fsencode(first), os.fsencode(second), 2) # RENAME_SWAP
else:
raise OSError(errno.ENOTSUP, 'Atomic directory exchange is unavailable')
elif sys.platform.startswith('linux'):
operation = getattr(library, 'renameat2', None)
if operation is not None:
operation.argtypes = (ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint)
operation.restype = ctypes.c_int
result = operation(-100, os.fsencode(first), -100, os.fsencode(second), 2) # RENAME_EXCHANGE
else:
raise OSError(errno.ENOTSUP, 'Atomic directory exchange is unavailable')
else:
raise OSError(errno.ENOTSUP, 'Atomic directory exchange is unavailable')
if result:
error = ctypes.get_errno()
raise OSError(error, os.strerror(error))
def selected_source(srcroot, explicit=None, environ=None):
environment = os.environ if environ is None else environ
return absolute(explicit if explicit is not None else
environment.get('VCC_SOURCE_ROM_DIR') or srcroot / 'LocalResources/SourceROMBundle')
def isolated_ui_testing(environment):
if environment.get('CONFIGURATION') == 'UITesting':
return True
swift_conditions = environment.get('SWIFT_ACTIVE_COMPILATION_CONDITIONS', '').split()
if 'VCC_UI_TESTING' in swift_conditions:
return True
definitions = environment.get('GCC_PREPROCESSOR_DEFINITIONS', '').split()
return any(value in ('VCC_UI_TESTING', 'VCC_UI_TESTING=1') for value in definitions)
def remove_interrupted_staging(resources):
# After an exchange, the temporary pathname contains the former StarterKit.
# A killed build must not leave that old profile's payload in the next app.
# Match only this helper's generated eight-character temporary names.
for path in resources.iterdir():
if re.fullmatch(r'\.source-rom-starter-[a-z0-9_]{8}', path.name):
check_kit(path)
shutil.rmtree(path)
elif re.fullmatch(r'\.source-rom-resource-[a-z0-9_]{8}', path.name):
info = path.lstat()
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
raise ValueError('Interrupted build resource must be a regular file without links')
path.unlink()
def stage_source_roms(srcroot, resources, source=None, environ=None):
srcroot, resources = absolute(srcroot), absolute(resources)
directory(srcroot, 'Mac source directory')
check_resources(resources)
environment = os.environ if environ is None else environ
ui_testing = isolated_ui_testing(environment)
# Do not even resolve or inspect an explicit/generated ROM location for the
# isolated UI app. Only its build-owned output and authored header are used.
bundle = None if ui_testing else selected_source(srcroot, source, environment)
present = directory(bundle, 'Source ROM bundle', optional=True) if bundle is not None else False
starter = srcroot / 'Resources/StarterMedia'
directory(starter, 'Authored starter media')
if (bundle is not None and overlaps(bundle, resources)) or overlaps(starter, resources):
raise ValueError('Build resources must not overlap source inputs')
kit = resources / 'StarterKit'
check_kit(kit)
# Validate all selected inputs before creating or changing a build resource.
payload = read_bundle(bundle) if present else None
header = read_regular(starter / 'Blank OS9 Header.bin', STARTER_LIMIT, 'Authored OS-9 header', expected_size=8192)
if payload is not None:
for name in STARTER_FILES:
payload[name] = read_regular(starter / name, STARTER_LIMIT, 'Authored starter media: ' + name)
resources.mkdir(parents=True, exist_ok=True)
check_resources(resources)
remove_interrupted_staging(resources)
atomic_file(resources / 'Blank OS9 Header.bin', header)
if payload is None:
if check_kit(kit):
shutil.rmtree(kit)
return {'staged': False, 'roms': {}, 'ui_testing': ui_testing}
staging = Path(tempfile.mkdtemp(prefix='.source-rom-starter-', dir=resources))
try:
for name, data in payload.items():
(staging / name).write_bytes(data)
os.chmod(staging / name, 0o644)
os.chmod(staging, 0o755)
check_resources(resources)
if check_kit(kit):
exchange_directories(staging, kit)
# The staging pathname now contains only the previous owned kit.
else:
os.rename(staging, kit)
return {'staged': True, 'roms': {name: {'bytes': len(payload[name]), 'sha256': hashlib.sha256(payload[name]).hexdigest()}
for name in ROM_SIZES}}
finally:
if staging.exists():
shutil.rmtree(staging)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--srcroot', type=Path, required=True, help='Mac project directory (platforms/macos)')
parser.add_argument('--resources', type=Path, required=True, help='Build-owned APP.app/Contents/Resources')
parser.add_argument('--source', type=Path, help='Explicit source ROM bundle; overrides VCC_SOURCE_ROM_DIR')
args = parser.parse_args()
try:
result = stage_source_roms(args.srcroot, args.resources, args.source)
except (OSError, ValueError, UnicodeError) as error:
parser.error(str(error))
if result['staged']:
print('Included explicitly built source ROMs, original source notice, and authored starter media.')
elif result.get('ui_testing'):
print('Isolated UI-test build omits ROM bundles and retains only the authored standalone header.')
else:
print('Optional source ROM bundle is absent; building without bundled ROMs.')
if __name__ == '__main__':
main()
+182
View File
@@ -0,0 +1,182 @@
# SPDX-License-Identifier: GPL-3.0-or-later
"""Authored regressions for explicit firmware preparation and bundle boundaries."""
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
MODULE = Path(__file__).resolve().parents[1] / 'build.py'
SPEC = importlib.util.spec_from_file_location('rom_build', MODULE)
BUILD = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(BUILD)
def fingerprint(data):
return hashlib.sha256(data).hexdigest()
class ROMBuildTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix='vcc-rom-builder-test-')
self.addCleanup(self.temporary.cleanup)
self.root = Path(self.temporary.name)
self.records, self.locations = BUILD.DEPENDENCIES.load_records()
self.record = json.loads(json.dumps(self.records['cocoroms']))
def fixture_outputs(self):
directory = self.root / 'roms'
directory.mkdir()
expected = {}
for name in BUILD.ROM_NAMES:
data = bytes(32768 if name == 'coco3.rom' else 8192)
(directory / name).write_bytes(data)
expected[name] = {'bytes': len(data), 'sha256': fingerprint(data)}
return directory, expected
def fixture_inputs(self):
dependencies = self.root / 'dependencies'
source = dependencies / 'cocoroms'
source.mkdir(parents=True)
hashes = {}
for name in BUILD.SOURCE_NAMES:
data = ('Authored source fixture: ' + name + '\n').encode()
(source / name).write_bytes(data)
hashes[name] = fingerprint(data)
self.record['upstreamFileSHA256'] = self.record['vendoredFileSHA256'] = hashes
_, self.record['romOutputs'] = self.fixture_outputs()
return dependencies
def fixture_assembler(self, fail=False):
tool = self.root / 'explicit-assembler-with-another-name'
tool.write_text('''#!/usr/bin/env python3
from pathlib import Path
import sys
if '--version' in sys.argv: print('Authored assembler fixture'); raise SystemExit(0)
''' + ("raise SystemExit(7)\n" if fail else '''if '--raw' in sys.argv:
name = sys.argv[sys.argv.index('-o') + 1]
Path(name).write_bytes(bytes(32768 if name == 'coco3.rom' else 8192))
else: print('fixture EQU 0')
'''))
tool.chmod(0o755)
return tool
def test_firmware_fetch_is_explicit_and_manifest_contains_only_source(self):
record = BUILD.source_record(self.records)
self.assertEqual(set(record['upstreamFileSHA256']), BUILD.SOURCE_NAMES)
self.assertNotIn('cocoroms', BUILD.DEPENDENCIES.default_selection(self.records))
self.assertEqual(len(BUILD.DEPENDENCIES.default_selection(self.records)), 17)
self.assertNotIn('.rom', ''.join(record['upstreamFileSHA256']))
self.assertEqual(record['archiveRootPrefix'], 'cocoroms/')
def test_unreviewed_firmware_input_is_refused(self):
self.record['vendoredFileSHA256']['supplied.rom'] = '0' * 64
with self.assertRaisesRegex(ValueError, 'exactly the eight'):
BUILD.source_record({'cocoroms': self.record})
def test_optional_flag_requires_a_boolean(self):
self.record['optional'] = 'true'
with self.assertRaisesRegex(ValueError, 'optional flag'):
BUILD.DEPENDENCIES.validate_record(self.record)
def test_malformed_output_metadata_is_rejected_explicitly(self):
for spec in [None, {'bytes': 32768, 'sha256': None}, {'bytes': 32768, 'sha256': 'not-a-hash'}]:
record = json.loads(json.dumps(self.records['cocoroms']))
record['romOutputs']['coco3.rom'] = spec
with self.assertRaisesRegex(ValueError, 'firmware fingerprint'):
BUILD.source_record({'cocoroms': record})
record = json.loads(json.dumps(self.records['cocoroms']))
record['romOutputs'] = None
with self.assertRaisesRegex(ValueError, 'output inventory'):
BUILD.source_record({'cocoroms': record})
def test_generated_hash_size_links_and_hardlinks_are_checked(self):
source, expected = self.fixture_outputs()
self.assertEqual(BUILD.validate_outputs(source, expected), expected)
target = source / 'disk11.rom'
target.write_bytes(b'changed')
with self.assertRaisesRegex(ValueError, 'qualified fingerprint'):
BUILD.validate_outputs(source, expected)
target.unlink()
target.symlink_to(source / 'bas13.rom')
with self.assertRaisesRegex(ValueError, 'unlinked'):
BUILD.validate_outputs(source, expected)
target.unlink()
os.link(source / 'bas13.rom', target)
with self.assertRaisesRegex(ValueError, 'unlinked'):
BUILD.validate_outputs(source, expected)
def test_bundle_has_exactly_two_roms_manifest_notice_and_no_private_paths(self):
source, expected = self.fixture_outputs()
manifest = BUILD.bundle_manifest(self.record, self.locations['cocoroms'] / 'cocoroms.json',
{'version': 'fixture', 'sha256': 'a' * 64}, expected)
destination = self.root / 'bundle'
BUILD.write_bundle(destination, source, manifest)
self.assertEqual({p.name for p in destination.iterdir()},
{'ROMBuild.json', 'ROM_SOURCE_NOTICE.txt', 'coco3.rom', 'disk11.rom'})
published = json.loads((destination / 'ROMBuild.json').read_text())
self.assertEqual(published['schemaVersion'], 1)
self.assertEqual(published['generator'], 'vccplus-rom-build')
self.assertEqual(set(published['roms']), set(BUILD.BUNDLE_NAMES))
self.assertNotIn(str(self.root), (destination / 'ROMBuild.json').read_text())
self.assertIn('MICROWARE', (destination / 'ROM_SOURCE_NOTICE.txt').read_text())
with self.assertRaisesRegex(ValueError, 'fresh ROM bundle'):
BUILD.write_bundle(destination, source, manifest)
def test_cold_build_uses_exact_explicit_assembler_and_no_existing_roms(self):
dependencies = self.fixture_inputs()
records = dict(self.records, cocoroms=self.record)
tool = self.fixture_assembler()
output, bundle = self.root / 'output', self.root / 'bundle'
with patch.object(BUILD.DEPENDENCIES, 'load_records', return_value=(records, self.locations)):
report = BUILD.build(output, dependencies, tool, bundle)
self.assertEqual(report['status'], 'passed')
self.assertEqual(len(report['commands']), 6)
self.assertTrue(all(command['argv'][0] == str(tool.resolve()) for command in report['commands']))
self.assertFalse(report['preassembledROMInputs'])
self.assertEqual(report['roms'], self.record['romOutputs'])
self.assertTrue((bundle / 'coco3.rom').is_file())
def test_preassembled_dependency_payload_is_refused_before_execution(self):
dependencies = self.fixture_inputs()
(dependencies / 'cocoroms/preassembled.rom').write_bytes(b'fixture')
records = dict(self.records, cocoroms=self.record)
with patch.object(BUILD.DEPENDENCIES, 'load_records', return_value=(records, self.locations)), \
patch.object(BUILD.subprocess, 'run') as execute:
with self.assertRaisesRegex(ValueError, 'differs from reviewed import'):
BUILD.build(self.root / 'output', dependencies, self.fixture_assembler())
execute.assert_not_called()
self.assertFalse((self.root / 'output').exists())
def test_failed_assembly_retains_evidence_without_creating_bundle(self):
dependencies = self.fixture_inputs()
records = dict(self.records, cocoroms=self.record)
output, bundle = self.root / 'output', self.root / 'bundle'
with patch.object(BUILD.DEPENDENCIES, 'load_records', return_value=(records, self.locations)):
with self.assertRaises(BUILD.subprocess.CalledProcessError):
BUILD.build(output, dependencies, self.fixture_assembler(fail=True), bundle)
self.assertEqual(json.loads((output / 'BuildReport.json').read_text())['status'], 'failed')
self.assertTrue((output / 'build.log').is_file())
self.assertFalse(bundle.exists())
def test_bundle_cannot_contaminate_dependencies_or_occupy_build_ancestor(self):
dependencies = self.fixture_inputs()
records = dict(self.records, cocoroms=self.record)
tool = self.fixture_assembler()
for bundle in [dependencies / 'cocoroms/package', dependencies.resolve() / 'cocoroms/canonical-package', self.root / 'new-build-parent']:
output = self.root / 'new-build-parent/output'
with patch.object(BUILD.DEPENDENCIES, 'load_records', return_value=(records, self.locations)), \
patch.object(BUILD.subprocess, 'run') as execute:
with self.assertRaisesRegex(ValueError, 'separate from dependency'):
BUILD.build(output, dependencies, tool, bundle)
execute.assert_not_called()
self.assertFalse(output.exists())
self.assertFalse(bundle.exists())
if __name__ == '__main__':
unittest.main()
+468
View File
@@ -0,0 +1,468 @@
# SPDX-License-Identifier: GPL-3.0-or-later
"""Source-ROM staging checks using authored synthetic bytes, never real ROMs."""
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[3]
SCRIPT = ROOT / 'tools/roms/stage.py'
SPEC = importlib.util.spec_from_file_location('source_rom_stage', SCRIPT)
stage = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(stage)
class SourceROMStageTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory(prefix='vcc-source-rom-stage-')
self.addCleanup(temporary.cleanup)
self.root = Path(temporary.name)
self.srcroot = self.root / 'Source With Spaces/platforms/macos'
self.starter = self.srcroot / 'Resources/StarterMedia'
self.starter.mkdir(parents=True)
self.media = {
'Hello World.dsk': b'AUTHORED HELLO DISK',
'Hello World.cas': b'AUTHORED HELLO CASSETTE',
'Blank Disk.dsk': b'AUTHORED BLANK DISK',
'Hello Hard Disk.dsk': b'AUTHORED HARD DISK',
'Blank OS9 Header.bin': b'\x00' * 8192,
}
for name, data in self.media.items():
(self.starter / name).write_bytes(data)
self.resources = self.root / 'Build With Spaces/VCC Plus.app/Contents/Resources'
self.bundle = self.root / 'Source ROM Bundle'
self.create_bundle(self.bundle)
def create_bundle(self, path, seed=0x12):
path.mkdir(parents=True, exist_ok=True)
roms = {}
for index, (name, size) in enumerate(stage.ROM_SIZES.items()):
data = bytes([(seed + index) & 255]) * size
(path / name).write_bytes(data)
roms[name] = {'bytes': size, 'sha256': hashlib.sha256(data).hexdigest()}
record = {'schemaVersion': 1, 'generator': 'vccplus-rom-build',
'source': {'project': 'authored unit fixture', 'revision': '0' * 40},
'assembler': {'version': 'authored fixture'}, 'roms': roms}
(path / 'ROMBuild.json').write_text(json.dumps(record, indent=2) + '\n')
(path / 'ROM_SOURCE_NOTICE.txt').write_text('Authored synthetic test bytes. No hardware ROM payload.\n')
return record
def apply(self, source=None, environ=None):
return stage.stage_source_roms(self.srcroot, self.resources, source,
environ={} if environ is None else environ)
def tree(self, root):
if not root.exists():
return None
return {str(path.relative_to(root)): (path.read_bytes(), path.stat().st_mode & 0o777,
path.stat().st_mtime_ns)
for path in root.rglob('*') if path.is_file()}
def alter_manifest(self, operation):
path = self.bundle / 'ROMBuild.json'
record = json.loads(path.read_text())
operation(record)
path.write_text(json.dumps(record))
def seed_old_kit(self):
kit = self.resources / 'StarterKit'
kit.mkdir(parents=True)
(kit / 'stale-private-marker').write_bytes(b'OLDER BUILD ONLY')
(self.resources / 'Blank OS9 Header.bin').write_bytes(self.media['Blank OS9 Header.bin'])
return kit
def test_valid_bundle_copies_owned_bytes_and_preserves_all_inputs(self):
bundle_before, starter_before = self.tree(self.bundle), self.tree(self.starter)
result = self.apply(self.bundle)
self.assertTrue(result['staged'])
kit = self.resources / 'StarterKit'
self.assertEqual(set(path.name for path in kit.iterdir()), stage.BUNDLE_FILES | set(stage.STARTER_FILES))
for name in stage.BUNDLE_FILES:
self.assertEqual((kit / name).read_bytes(), (self.bundle / name).read_bytes())
self.assertNotEqual((kit / name).stat().st_ino, (self.bundle / name).stat().st_ino)
for name, data in self.media.items():
self.assertEqual((kit / name).read_bytes(), data)
self.assertEqual((self.resources / 'Blank OS9 Header.bin').read_bytes(), self.media['Blank OS9 Header.bin'])
self.assertEqual(self.tree(self.bundle), bundle_before)
self.assertEqual(self.tree(self.starter), starter_before)
self.assertEqual(result['roms']['coco3.rom']['bytes'], 32768)
self.assertEqual(result['roms']['disk11.rom']['bytes'], 8192)
def test_missing_default_removes_only_owned_stale_kit(self):
kit = self.seed_old_kit()
untouched = self.resources / 'unrelated-resource.txt'
untouched.write_bytes(b'PRESERVE')
self.assertFalse(self.apply()['staged'])
self.assertFalse(kit.exists())
self.assertEqual(untouched.read_bytes(), b'PRESERVE')
self.assertTrue((self.resources / 'Blank OS9 Header.bin').is_file())
def test_explicit_and_environment_selection_never_fall_back(self):
default = self.srcroot / 'LocalResources/SourceROMBundle'
self.create_bundle(default, seed=0x44)
chosen = stage.selected_source(self.srcroot, self.bundle, {'VCC_SOURCE_ROM_DIR': str(default)})
self.assertEqual(chosen, self.bundle)
self.assertEqual(stage.selected_source(self.srcroot, environ={'VCC_SOURCE_ROM_DIR': str(self.bundle)}), self.bundle)
self.assertEqual(stage.selected_source(self.srcroot, environ={'VCC_SOURCE_ROM_DIR': ''}), default)
self.assertTrue(self.apply(environ={'VCC_SOURCE_ROM_DIR': str(self.bundle)})['staged'])
self.assertEqual((self.resources / 'StarterKit/coco3.rom').read_bytes(), (self.bundle / 'coco3.rom').read_bytes())
self.assertFalse(self.apply(self.root / 'Absent Explicit Bundle')['staged'])
self.assertFalse((self.resources / 'StarterKit').exists())
def test_default_bundle_is_used_only_from_source_specific_directory(self):
destination = self.srcroot / 'LocalResources/SourceROMBundle'
shutil.copytree(self.bundle, destination)
self.assertTrue(self.apply()['staged'])
shutil.rmtree(destination)
private = self.srcroot / 'LocalResources/ROMs'
private.mkdir()
(private / 'coco3.rom').write_bytes(b'PRIVATE PATH MUST NOT BE READ')
media = self.srcroot / 'LocalResources/Media'
media.mkdir()
(media / 'NitrOS-9 L2 V3.3.0.vhd').write_bytes(b'NO PRIVATE MEDIA DISCOVERY')
self.assertFalse(self.apply()['staged'])
self.assertFalse((self.resources / 'StarterKit').exists())
self.assertEqual((private / 'coco3.rom').read_bytes(), b'PRIVATE PATH MUST NOT BE READ')
def test_isolated_ui_product_never_selects_or_reads_prepared_rom_bundles(self):
default = self.srcroot / 'LocalResources/SourceROMBundle'
shutil.copytree(self.bundle, default)
original, default_original = self.tree(self.bundle), self.tree(default)
configurations = (
{'CONFIGURATION': 'UITesting'},
{'CONFIGURATION': 'Debug', 'SWIFT_ACTIVE_COMPILATION_CONDITIONS': 'DEBUG VCC_UI_TESTING'},
{'CONFIGURATION': 'Release', 'GCC_PREPROCESSOR_DEFINITIONS': 'VCC_UI_TESTING=1'},
)
for environment in configurations:
with self.subTest(environment=environment):
self.seed_old_kit()
environment['VCC_SOURCE_ROM_DIR'] = str(self.bundle)
with mock.patch.object(stage, 'selected_source', side_effect=AssertionError('UI build selected a ROM input')):
with mock.patch.object(stage, 'read_bundle', side_effect=AssertionError('UI build read a ROM input')):
result = self.apply(self.bundle, environment)
self.assertFalse(result['staged'])
self.assertTrue(result['ui_testing'])
self.assertFalse((self.resources / 'StarterKit').exists())
self.assertEqual((self.resources / 'Blank OS9 Header.bin').read_bytes(), self.media['Blank OS9 Header.bin'])
self.assertEqual(self.tree(self.bundle), original)
self.assertEqual(self.tree(default), default_original)
def test_isolated_ui_product_does_not_probe_unsafe_explicit_or_environment_paths(self):
alias = self.root / 'Unsafe ROM Alias'
alias.symlink_to(self.bundle, target_is_directory=True)
original = self.tree(self.bundle)
self.seed_old_kit()
# Parent traversal and linked input locations would both be refused for
# an ordinary source app, but neither location is probed by a UI build.
result = self.apply(alias / '..' / 'missing', {'CONFIGURATION': 'UITesting',
'VCC_SOURCE_ROM_DIR': str(alias)})
self.assertFalse(result['staged'])
self.assertFalse((self.resources / 'StarterKit').exists())
self.assertEqual(self.tree(self.bundle), original)
self.assertEqual((self.resources / 'Blank OS9 Header.bin').read_bytes(), self.media['Blank OS9 Header.bin'])
def test_atomic_replacement_removes_all_previous_profile_payload(self):
old = self.seed_old_kit()
nested = old / 'old-nested'
nested.mkdir()
(nested / 'payload').write_bytes(b'OLD')
self.assertTrue(self.apply(self.bundle)['staged'])
self.assertFalse((old / 'stale-private-marker').exists())
self.assertFalse(nested.exists())
self.assertEqual(set(path.name for path in old.iterdir()), stage.BUNDLE_FILES | set(stage.STARTER_FILES))
self.assertFalse(list(self.resources.glob('.source-rom-starter-*')))
def test_failed_atomic_exchange_preserves_previous_kit(self):
old = self.seed_old_kit()
before = self.tree(old)
with mock.patch.object(stage, 'exchange_directories', side_effect=OSError('authored exchange refusal')):
with self.assertRaises(OSError):
self.apply(self.bundle)
self.assertEqual(self.tree(old), before)
self.assertFalse(list(self.resources.glob('.source-rom-starter-*')))
def test_next_build_removes_only_interrupted_owned_staging(self):
self.resources.mkdir(parents=True)
stale = self.resources / '.source-rom-starter-abcdefgh'
stale.mkdir()
(stale / 'prior-private-rom-marker').write_bytes(b'PRIOR PROFILE')
(self.resources / '.source-rom-resource-12345678').write_bytes(b'PARTIAL HEADER')
unrelated = self.resources / '.source-rom-starter-not-a-generated-name'
unrelated.mkdir()
(unrelated / 'preserve').write_bytes(b'KEEP')
self.assertFalse(self.apply()['staged'])
self.assertFalse(stale.exists())
self.assertFalse((self.resources / '.source-rom-resource-12345678').exists())
self.assertEqual((unrelated / 'preserve').read_bytes(), b'KEEP')
# Even a helper-shaped name cannot redirect cleanup through a link.
stale.symlink_to(unrelated, target_is_directory=True)
with self.assertRaises(ValueError):
self.apply()
self.assertEqual((unrelated / 'preserve').read_bytes(), b'KEEP')
def test_invalid_manifest_fields_refuse_before_changing_build_output(self):
cases = (
lambda r: r.update(schemaVersion=2),
lambda r: r.update(schemaVersion=True),
lambda r: r.update(generator='different-generator'),
lambda r: r.update(source={}),
lambda r: r.update(roms=[]),
lambda r: r['roms'].pop('disk11.rom'),
lambda r: r['roms'].update({'extra.rom': {'bytes': 1, 'sha256': '0' * 64}}),
lambda r: r['roms']['coco3.rom'].update(bytes=32767),
lambda r: r['roms']['coco3.rom'].update(bytes=True),
lambda r: r['roms']['disk11.rom'].update(sha256='A' * 64),
lambda r: r['roms']['disk11.rom'].update(sha256='0' * 64),
)
old = self.seed_old_kit()
before = self.tree(self.resources)
for operation in cases:
with self.subTest(operation=operation):
self.create_bundle(self.bundle)
self.alter_manifest(operation)
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.assertEqual(self.tree(self.resources), before)
self.assertTrue((old / 'stale-private-marker').is_file())
def test_invalid_json_duplicate_fields_and_nonobject_manifest_are_refused(self):
for data in (b'{', b'[]', b'{"schemaVersion":1,"schemaVersion":1}', b'\xff', b' ' * (stage.MANIFEST_LIMIT + 1)):
with self.subTest(data=data[:25]):
(self.bundle / 'ROMBuild.json').write_bytes(data)
with self.assertRaises((ValueError, UnicodeError)):
self.apply(self.bundle)
self.assertFalse(self.resources.exists())
def test_missing_extra_or_nonregular_bundle_members_are_refused(self):
for name in stage.BUNDLE_FILES:
with self.subTest(missing=name):
self.create_bundle(self.bundle)
(self.bundle / name).unlink()
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.create_bundle(self.bundle)
(self.bundle / '.unexpected').write_bytes(b'NO EXTRA FILES')
with self.assertRaises(ValueError):
self.apply(self.bundle)
(self.bundle / '.unexpected').unlink()
(self.bundle / 'disk11.rom').unlink()
(self.bundle / 'disk11.rom').mkdir()
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.assertFalse(self.resources.exists())
def test_rom_payload_sizes_and_manifest_identity_are_enforced(self):
for name, size in stage.ROM_SIZES.items():
for length in (0, size - 1, size + 1):
with self.subTest(name=name, length=length):
self.create_bundle(self.bundle)
(self.bundle / name).write_bytes(bytes(length))
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.create_bundle(self.bundle)
(self.bundle / name).write_bytes(bytes([0x77]) * size)
with self.assertRaisesRegex(ValueError, 'do not match'):
self.apply(self.bundle)
self.assertFalse(self.resources.exists())
def test_notice_is_required_bounded_nonempty_utf8(self):
for notice in (b'', b' \n\t', b'\xff', b'notice\0', b'x' * (stage.NOTICE_LIMIT + 1)):
with self.subTest(notice=notice[:25]):
(self.bundle / 'ROM_SOURCE_NOTICE.txt').write_bytes(notice)
with self.assertRaises((ValueError, UnicodeError)):
self.apply(self.bundle)
self.assertFalse(self.resources.exists())
def test_source_bundle_leaf_and_ancestor_symlinks_are_refused(self):
alias = self.root / 'Linked Bundle'
alias.symlink_to(self.bundle, target_is_directory=True)
with self.assertRaises(ValueError):
self.apply(alias)
parent = self.root / 'Linked Parent'
parent.symlink_to(self.root, target_is_directory=True)
with self.assertRaisesRegex(ValueError, 'linked ancestor'):
self.apply(parent / self.bundle.name)
dangling = self.root / 'Dangling Bundle'
dangling.symlink_to(self.root / 'missing')
with self.assertRaises(ValueError):
self.apply(dangling)
self.assertFalse(self.resources.exists())
def test_source_members_must_not_be_symbolic_or_hard_links(self):
for name in stage.BUNDLE_FILES:
for kind in ('symbolic', 'hard'):
with self.subTest(name=name, kind=kind):
self.create_bundle(self.bundle)
original = self.bundle / name
victim = self.root / 'Input Victim'
victim.write_bytes(original.read_bytes())
before = victim.read_bytes()
original.unlink()
if kind == 'symbolic':
original.symlink_to(victim)
else:
os.link(victim, original)
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.assertEqual(victim.read_bytes(), before)
original.unlink()
victim.unlink()
self.assertFalse(self.resources.exists())
def test_output_shape_and_linked_ancestors_cannot_redirect_writes(self):
with self.assertRaisesRegex(ValueError, 'APP.app'):
stage.stage_source_roms(self.srcroot, self.root / 'Not An App/Resources', self.bundle, {})
victim = self.root / 'Untouched Output Victim'
victim.mkdir()
build = self.root / 'Linked Build'
build.symlink_to(victim, target_is_directory=True)
with self.assertRaisesRegex(ValueError, 'linked ancestor'):
stage.stage_source_roms(self.srcroot, build / 'VCC.app/Contents/Resources', self.bundle, {})
self.assertEqual(list(victim.iterdir()), [])
def test_existing_kit_links_refuse_cleanup_or_replacement(self):
kit = self.seed_old_kit()
victim = self.root / 'Kit Victim'
victim.write_bytes(b'UNCHANGED')
for kind in ('symbolic', 'hard', 'directory'):
with self.subTest(kind=kind):
link = kit / 'unsafe-member'
if kind == 'symbolic':
link.symlink_to(victim)
elif kind == 'hard':
os.link(victim, link)
else:
link.symlink_to(self.root, target_is_directory=True)
for source in (None, self.bundle):
with self.assertRaises(ValueError):
self.apply(source)
self.assertEqual(victim.read_bytes(), b'UNCHANGED')
self.assertTrue((kit / 'stale-private-marker').is_file())
link.unlink()
shutil.rmtree(kit)
kit.symlink_to(self.bundle, target_is_directory=True)
with self.assertRaises(ValueError):
self.apply()
self.assertEqual(set(path.name for path in self.bundle.iterdir()), stage.BUNDLE_FILES)
def test_linked_standalone_header_is_never_replaced(self):
self.resources.mkdir(parents=True)
victim = self.root / 'Header Victim'
victim.write_bytes(b'UNCHANGED')
for kind in ('symbolic', 'hard'):
with self.subTest(kind=kind):
destination = self.resources / 'Blank OS9 Header.bin'
if kind == 'symbolic':
destination.symlink_to(victim)
else:
os.link(victim, destination)
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.assertEqual(victim.read_bytes(), b'UNCHANGED')
self.assertFalse((self.resources / 'StarterKit').exists())
destination.unlink()
def test_authored_input_links_and_bad_header_refuse_before_mutation(self):
header = self.starter / 'Blank OS9 Header.bin'
header.write_bytes(b'SHORT')
with self.assertRaises(ValueError):
self.apply(self.bundle)
header.write_bytes(self.media[header.name])
target = self.starter / 'Hello World.cas'
victim = self.root / 'Authored Input Victim'
victim.write_bytes(target.read_bytes())
target.unlink()
target.symlink_to(victim)
with self.assertRaises(ValueError):
self.apply(self.bundle)
self.assertFalse(self.resources.exists())
def test_inputs_and_build_output_must_not_overlap(self):
self.resources.mkdir(parents=True)
embedded = self.resources / 'StarterKit'
shutil.copytree(self.bundle, embedded)
before = self.tree(embedded)
with self.assertRaisesRegex(ValueError, 'overlap'):
self.apply(embedded)
self.assertEqual(self.tree(embedded), before)
with self.assertRaisesRegex(ValueError, 'parent traversal'):
self.apply(self.bundle / '..' / self.bundle.name)
def run_build_phase(self, *, profile, configuration='Release', source=None):
helper = self.srcroot.parents[1] / 'tools/roms/stage.py'
helper.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(SCRIPT, helper)
environment = os.environ.copy()
environment.update(SRCROOT=str(self.srcroot), TARGET_BUILD_DIR=str(self.resources.parents[2]),
UNLOCALIZED_RESOURCES_FOLDER_PATH='VCC Plus.app/Contents/Resources',
VCC_BUILD_CONTENT_PROFILE=profile, CONFIGURATION=configuration)
environment.pop('VCC_SOURCE_ROM_DIR', None)
if source is not None:
environment['VCC_SOURCE_ROM_DIR'] = str(source)
return subprocess.run(['/bin/sh', str(ROOT / 'platforms/macos/Packaging/copy-development-starter.sh')],
env=environment, capture_output=True, text=True)
def test_source_build_phase_accepts_bundle_in_debug_and_release_without_private_discovery(self):
private = self.srcroot / 'LocalResources/ROMs'
private.mkdir(parents=True)
(private / 'coco3.rom').write_bytes(b'INVALID NORMAL PROFILE INPUT')
for configuration in ('Debug', 'Release'):
with self.subTest(configuration=configuration):
result = self.run_build_phase(profile='source', configuration=configuration, source=self.bundle)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual((self.resources / 'StarterKit/coco3.rom').read_bytes(), (self.bundle / 'coco3.rom').read_bytes())
self.assertEqual((private / 'coco3.rom').read_bytes(), b'INVALID NORMAL PROFILE INPUT')
def test_source_ui_testing_build_phase_omits_prepared_bundle_and_stale_kit(self):
default = self.srcroot / 'LocalResources/SourceROMBundle'
shutil.copytree(self.bundle, default)
original, default_original = self.tree(self.bundle), self.tree(default)
self.seed_old_kit()
result = self.run_build_phase(profile='source', configuration='UITesting', source=self.bundle)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn('Isolated UI-test build', result.stdout)
self.assertFalse((self.resources / 'StarterKit').exists())
self.assertEqual((self.resources / 'Blank OS9 Header.bin').read_bytes(), self.media['Blank OS9 Header.bin'])
self.assertEqual(self.tree(self.bundle), original)
self.assertEqual(self.tree(default), default_original)
def test_source_build_phase_rejects_malformed_present_bundle(self):
(self.bundle / 'coco3.rom').write_bytes(b'INVALID')
result = self.run_build_phase(profile='source', source=self.bundle)
self.assertNotEqual(result.returncode, 0)
self.assertIn('invalid size', result.stderr)
self.assertFalse((self.resources / 'StarterKit').exists())
def test_normal_debug_phase_preserves_existing_private_workflow(self):
private = self.srcroot / 'LocalResources/ROMs'
private.mkdir(parents=True)
expected = {'coco3.rom': bytes([0x33]) * 32768, 'disk11.rom': bytes([0x44]) * 8192,
'orch90.rom': bytes([0x55]) * 8192}
for name, data in expected.items():
(private / name).write_bytes(data)
result = self.run_build_phase(profile='normal', configuration='Debug', source=self.bundle)
self.assertEqual(result.returncode, 0, result.stderr)
kit = self.resources / 'StarterKit'
for name, data in expected.items():
self.assertEqual((kit / name).read_bytes(), data)
self.assertFalse((kit / 'ROMBuild.json').exists())
for name, data in self.media.items():
self.assertEqual((kit / name).read_bytes(), data)
def test_normal_release_phase_still_excludes_optional_development_roms(self):
self.seed_old_kit()
result = self.run_build_phase(profile='normal', configuration='Release', source=self.bundle)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertFalse((self.resources / 'StarterKit').exists())
self.assertEqual((self.resources / 'Blank OS9 Header.bin').read_bytes(), self.media['Blank OS9 Header.bin'])
if __name__ == '__main__':
unittest.main()