# SPDX-License-Identifier: GPL-3.0-or-later """Authored integration fixtures for post-native Sound Lab report qualification.""" import contextlib import importlib.util import io import json from pathlib import Path import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] def load(name, path): spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module fixture = load('soundlab_report_fixtures', ROOT / 'tests' / 'test_reports.py') verify = load('soundlab_verify_reports', ROOT / 'verify_reports.py') def run_text(number=1, human='UNSURE'): text = fixture.make_report(human='NOT_OBSERVED', ending=False).decode('ascii') header, body = text.split('CASE=', 1) header = header.replace('RUN=0001', f'RUN={number:04d}') case0 = 'CASE=' + body.replace('ENDCASE=', 'REPEAT=0\r\nNOTE_LENGTH=0\r\nNOTE=\r\nENDCASE=') case1 = case0.replace('REPEAT=0', 'REPEAT=1').replace('HUMAN=NOT_OBSERVED', 'HUMAN=' + human) text = header + case0 + case1 raw = text.encode('ascii') footer = f'ENDRUN={number:04d} COMPLETE=YES RECORD_COUNT={len(text.splitlines())} CHECKSUM_CRC16={verify.reports.crc16(raw):04X}\r\n' return raw + footer.encode('ascii') def setup_build(root, *, bad_human=False, changed_binary=False, retained_changed=False): (root / 'qualification').mkdir() binary = b'Authored loader fixture; not executable.' basic_source = b'10 PRINT "FIXTURE"\n' basic_disk = b'\r' + basic_source.replace(b'\n', b'\r') pristine = [('SOUNDLAB.BIN', binary, [0]), ('SOUNDLAB.BAS', basic_disk, [1])] (root / 'SOUNDLAB.BIN').write_bytes(binary) (root / 'SOUNDLAB.BAS').write_bytes(basic_source) (root / 'SOUNDLAB.DSK').write_bytes(fixture.make_disk(pristine)) manifest = {'build_id': 'TEST-1', 'vector_set_crc16': '1234', 'vectors': [{'case_id': 'TON01-A', 'crc16': '7ABC'}], 'artifacts': []} for name in ('SOUNDLAB.BIN', 'SOUNDLAB.BAS', 'SOUNDLAB.DSK'): data = (root / name).read_bytes() manifest['artifacts'].append({'filename': name, 'bytes': len(data), 'sha256': verify.sha256(data)}) (root / 'SOUNDLAB-manifest.json').write_text(json.dumps(manifest)) for label in ('6809', '6309', 'engineering'): files = list(pristine) if changed_binary and label == '6309': files[0] = ('SOUNDLAB.BIN', b'Changed fixture', [0]) text = run_text(human='HEARD' if bad_human and label == '6309' else 'UNSURE') if retained_changed and label == 'engineering': text = text.replace(b'HUMAN=UNSURE', b'HUMAN=NO_SOUND') # Restore valid footer so preservation is independently tested. text = text.split(b'ENDRUN=')[0] text += f'ENDRUN=0001 COMPLETE=YES RECORD_COUNT={len(text.splitlines())} CHECKSUM_CRC16={verify.reports.crc16(text):04X}\r\n'.encode() files.append(('SND0001.TXT', text, [2])) if label == 'engineering': files.append(('SND0002.TXT', run_text(2), [3])) (root / 'qualification' / f'{label}-reports.dsk').write_bytes(fixture.make_disk(files)) class GuestContractTests(unittest.TestCase): def test_complete_native_contract_preserves_sources(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) setup_build(root) inputs = {path: path.read_bytes() for path in root.rglob('*') if path.is_file()} result = verify.qualify(root) self.assertTrue(result['passed']) self.assertTrue(result['engineering_original_reports_byte_identical']) self.assertEqual(result['engineering_new_reports'], ['SND0002.TXT']) self.assertTrue(all(run['repeat_observed'] and run['unsure_recorded'] for run in result['runs'].values())) self.assertEqual(inputs, {path: path.read_bytes() for path in inputs}) def test_native_contract_rejects_fabricated_heard(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) setup_build(root, bad_human=True) with self.assertRaisesRegex(verify.reports.SoundLabError, 'fabricate HUMAN=HEARD'): verify.qualify(root) def test_native_contract_rejects_changed_binary(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) setup_build(root, changed_binary=True) with self.assertRaisesRegex(verify.reports.SoundLabError, 'SOUNDLAB.BIN missing or changed'): verify.qualify(root) def test_native_contract_rejects_changed_retained_report(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) setup_build(root, retained_changed=True) with self.assertRaisesRegex(verify.reports.SoundLabError, 'modified or removed original 6809 report'): verify.qualify(root) def test_optional_receipt_never_implicitly_replaces_existing_output(self): with tempfile.TemporaryDirectory() as directory, contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): root = Path(directory) setup_build(root) receipt = root / 'receipt.json' args = ['--build-dir', str(root), '--receipt', str(receipt)] self.assertEqual(verify.main(args), 0) self.assertEqual(verify.main(args), 1) self.assertEqual(verify.main(args + ['--overwrite']), 0) self.assertEqual(verify.main(['--build-dir', str(root), '--receipt', str(root / 'SOUNDLAB.DSK'), '--overwrite']), 1) self.assertTrue(verify.qualify(root)['passed']) if __name__ == '__main__': unittest.main()