133 lines
7.0 KiB
Python
133 lines
7.0 KiB
Python
#!/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<start+newsize:
|
|
length=int.from_bytes(prepared[off+2:off+4],'big');m=prepared[off:off+length]
|
|
parity=0
|
|
for c in m[:9]:parity^=c
|
|
self.assertEqual(parity,255)
|
|
crc=0xffffff
|
|
for c in m:
|
|
crc^=c<<16
|
|
for _ in range(8):crc=((crc<<1) ^ (0x800063 if crc&0x800000 else 0))&0xffffff
|
|
self.assertEqual(crc,0x800fe3)
|
|
no=int.from_bytes(m[4:6],'big');name=''
|
|
for c in m[no:]:
|
|
name+=chr(c&127)
|
|
if c&128:break
|
|
modules[name.lower()]=m;off+=length
|
|
self.assertEqual(off,start+newsize);self.assertEqual(modules['h1'][0x13],1)
|
|
self.assertEqual(modules['h0'][0x13],0);self.assertEqual(len(modules['emudsk']),240)
|
|
# Check the new module pack via directory navigation, independently of
|
|
# how the preparer chose an extent. Only these OS-9 sectors may change.
|
|
def u(data, off, size):
|
|
return int.from_bytes(data[off:off + size], 'big')
|
|
|
|
def record(data, fd):
|
|
pos = fd * 256
|
|
start = u(data, pos + 16, 3) * 256
|
|
size = u(data, pos + 9, 4)
|
|
return start, data[start:start + size]
|
|
|
|
def directory(data, fd):
|
|
_, entries = record(data, fd)
|
|
return {bytes(c & 127 for c in entries[i:i + 29]).split(b'\0')[0].lower():
|
|
u(entries, i + 29, 3)
|
|
for i in range(0, len(entries), 32) if entries[i]}
|
|
|
|
root = directory(original, u(original, 8, 3))
|
|
folder = root[b'boot_with_beckerdw']
|
|
old = directory(original, folder)
|
|
new = directory(prepared, folder)
|
|
pack_fd = new.pop(b'dwnet')
|
|
self.assertEqual(old, new)
|
|
payload = b''.join(record(original, old[n])[1] for n in (b'dwio', b'scdwv', b'n', b'n1'))
|
|
start, contents = record(prepared, pack_fd)
|
|
self.assertEqual(payload, contents)
|
|
self.assertEqual(prepared[pack_fd * 256] & 0x05, 0x05)
|
|
data_sectors = u(prepared, pack_fd * 256 + 19, 2)
|
|
self.assertEqual(data_sectors, (len(payload) + 255) // 256)
|
|
self.assertEqual(start, (pack_fd + 1) * 256)
|
|
self.assertEqual(u(prepared, pack_fd * 256 + 21, 3), 0)
|
|
allowed.update(range(pack_fd * 256, start + data_sectors * 256))
|
|
|
|
expected_map = bytearray(original[256:256 + u(original, 4, 2)])
|
|
for sector in range(pack_fd, pack_fd + 1 + data_sectors):
|
|
mask = 128 >> (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()
|