mirror of
https://git.k-space.ee/arti/vt1200.git
synced 2026-10-30 12:03:54 +02:00
63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
"""Tests for tools/ptr_constants.py.
|
|
|
|
The VT1200 firmware stores TMS34010 *bit* addresses: a 32-bit value P refers to
|
|
physical byte P >> 3. ptr_constants.scan() finds those constants in an
|
|
address-ordered image and resolves where they point.
|
|
"""
|
|
import os
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(HERE, "tools"))
|
|
|
|
import ptr_constants
|
|
|
|
ROM_BASE = 0x07E40000
|
|
|
|
|
|
class ScanSyntheticTest(unittest.TestCase):
|
|
def test_finds_string_pointer(self):
|
|
img = bytearray(0x1000)
|
|
img[0x100:0x100 + 11] = b"HELLO WORLD"
|
|
ptr = (ROM_BASE + 0x100) << 3 # bit address of the string
|
|
struct.pack_into("<I", img, 0x10, ptr)
|
|
hits = list(ptr_constants.scan(bytes(img)))
|
|
self.assertIn((0x10, ptr, 0x100, "string"), hits)
|
|
|
|
def test_ignores_values_that_do_not_resolve(self):
|
|
self.assertEqual(list(ptr_constants.scan(b"\xff" * 0x100)), [])
|
|
|
|
|
|
class ClassifyTest(unittest.TestCase):
|
|
def test_printable_run_is_a_string(self):
|
|
img = b"\x00" * 8 + b"ABCDEFGH"
|
|
self.assertEqual(ptr_constants.classify(img, 8), "string")
|
|
|
|
def test_non_printable_is_other(self):
|
|
img = b"\x00" * 16
|
|
self.assertEqual(ptr_constants.classify(img, 8), "other")
|
|
|
|
def test_out_of_range_returns_none(self):
|
|
self.assertIsNone(ptr_constants.classify(b"\x00\x01", 99))
|
|
|
|
|
|
class RealImageTest(unittest.TestCase):
|
|
def test_known_pointer_resolves_to_its_string(self):
|
|
path = os.path.join(HERE, "vt1200_v21.bin")
|
|
if not os.path.exists(path):
|
|
self.skipTest("image not built")
|
|
with open(path, "rb") as fh:
|
|
img = fh.read()
|
|
by_value = {v: (off, tgt, kind) for off, v, tgt, kind in ptr_constants.scan(img)}
|
|
self.assertIn(0x3F816ED8, by_value)
|
|
off, tgt, kind = by_value[0x3F816ED8]
|
|
self.assertEqual(tgt, 0xC2DDB)
|
|
self.assertEqual(kind, "string")
|
|
self.assertEqual(img[tgt:tgt + 15], b"Messages re-xmt")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|