#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Public authored-header checks; private image checks opt in through an env var.""" import importlib.util import os from pathlib import Path import subprocess import tempfile import unittest ROOT=Path(__file__).resolve().parents[2] def module(name,file): spec=importlib.util.spec_from_file_location(name,ROOT/file) result=importlib.util.module_from_spec(spec);spec.loader.exec_module(result);return result PREP=module('prepare','platforms/macos/Tools/prepare-nitros9.py') FMT=module('header','platforms/macos/Tools/make-os9-data-header.py') class Nitros9MediaTests(unittest.TestCase): def test_authored_filesystem_allocation_and_basic_boundary(self): b=FMT.header();u=lambda o,n:int.from_bytes(b[o:o+n],'big') self.assertEqual(b,(ROOT/'platforms/macos/Resources/StarterMedia/Blank OS9 Header.bin').read_bytes()) self.assertEqual(u(0,3),0x5a000);self.assertEqual(u(4,2)*8*u(6,2),u(0,3)) allocated=[bit for bit in range(u(4,2)*8) if b[256+bit//8] & (128>>(bit%8))] self.assertEqual(allocated,[0,1,2,3]) fd=u(8,3)*256;self.assertEqual(u(fd+9,4),64) first=u(fd+16,3);count=u(fd+19,2) self.assertEqual((first,count),(25,7));self.assertEqual((first+count)*256,len(b)) self.assertEqual(u(first*256+29,3),24);self.assertEqual(u(first*256+61,3),24) def test_unknown_archive_rejected_without_overwriting_output(self): with tempfile.TemporaryDirectory() as tmp: src=Path(tmp)/'unknown.vhd';out=Path(tmp)/'keep.vhd' src.write_bytes(bytes(256));out.write_bytes(b'keep') result=subprocess.run(['python3',str(ROOT/'platforms/macos/Tools/prepare-nitros9.py'),str(src),str(out)],capture_output=True) self.assertNotEqual(result.returncode,0);self.assertEqual(out.read_bytes(),b'keep') @unittest.skipUnless(os.environ.get('VCC_NITROS9_IMAGE'),'explicit private archive input required') def test_private_boot_modules_and_unrelated_bytes_preserved(self): original=Path(os.environ['VCC_NITROS9_IMAGE']).read_bytes();prepared=PREP.prepare(original) self.assertEqual(len(prepared),len(original)) allowed=set() for base in [0,(0x5a000+255*630)*256]: u=lambda o,n=3:int.from_bytes(original[base+o:base+o+n],'big') root=u(8)*256;entry=u(root+16)*256;size=u(root+9,4) fd=None for off in range(entry,entry+size,32): name=bytes(c&127 for c in original[base+off:base+off+29]).split(b'\0')[0].lower() if name==b'os9boot':fd=u(off+29)*256;break self.assertIsNotNone(fd) start=base+u(fd+16)*256;oldsize=u(fd+9,4) newsize=int.from_bytes(prepared[base+fd+9:base+fd+13],'big') self.assertEqual(newsize-oldsize,99) allowed.update(range(start,start+newsize));allowed.update(range(base+fd+9,base+fd+13));allowed.update(range(base+24,base+26)) modules={};off=start while off> (sector % 8) self.assertFalse(expected_map[sector // 8] & mask) expected_map[sector // 8] |= mask allowed.add(256 + sector // 8) self.assertEqual(prepared[256:256 + len(expected_map)], expected_map) directory_start, original_entries = record(original, folder) _, updated_entries = record(prepared, folder) self.assertEqual(updated_entries[:-32], original_entries) allowed.update(range(folder * 256 + 9, folder * 256 + 13)) entry = directory_start + len(original_entries) allowed.update(range(entry, entry + 32)) startup = root[b'startup'] start, old_script = record(original, startup) _, script = record(prepared, startup) addition = b'load /dd/boot_with_beckerdw/dwnet\r' self.assertEqual(script.count(addition), 1) self.assertEqual(script.replace(addition, b''), old_script) self.assertLess(script.index(addition), script.index(b'shell i=')) allowed.update(range(start, start + len(script))) allowed.update(range(startup * 256 + 9, startup * 256 + 13)) self.assertEqual(PREP.prepare(prepared), prepared) # Compare spans rather than iterating all 135 MB byte by byte. last=0 for offset in sorted(allowed): if offset>last:self.assertEqual(prepared[last:offset],original[last:offset]) last=offset+1 self.assertEqual(prepared[last:],original[last:]) if __name__=='__main__':unittest.main()