Files
openhmi-nx/tests/test_tft_cli.py
T
mangavalkandClaude Fable 5.2 7337e693a5 tft: all four checksums solved (three header CRCs, trailer for any length)
0x44 = CRC of the resource area, 0xC4 = CRC of header [0,0xC4), 0x18C = CRC of
[0xC8,0x18C); trailer = CRC of the whole file ^ (len & 0xFF) ^ 0x56. tft.seal()
recomputes all of them. Verified against 24 Editor-made files of 8 lengths.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 16:54:59 +01:00

138 lines
5.8 KiB
Python

import contextlib
import io
import os
import shutil
import struct
import tempfile
import unittest
from helpers import FIXTURE, HAVE_PIL, bitwise_word_crc
from nxhmi import cli, tft
def fake_tft(payload: bytes) -> bytes:
"""A synthetic .tft-shaped blob: layout table + trailer CRC computed the way the trailer behaves
(word-per-byte CRC whose stream ends at the trailer). Real .tft files cannot be used in tests:
they contain Nextion's firmware."""
head = bytearray(0x200)
struct.pack_into("<HH", head, 0x0C, 480, 320)
for i, o in enumerate(range(tft.LAYOUT_START, tft.LAYOUT_END, 4)):
struct.pack_into("<I", head, o, (i * 1000) ^ tft.LAYOUT_KEY)
body = bytes(head) + payload
return body + struct.pack("<I", bitwise_word_crc(body, 0x12345678))
def sealed_tft(resources: bytes, pages: bytes) -> bytes:
"""A synthetic image with all four checksums computed by the independent bitwise CRC."""
head = bytearray(b"\xff" * tft.RES_START)
head[:0x200] = bytes(0x200)
struct.pack_into("<I", head, tft.OFF_RES_SIZE, len(resources))
for i, o in enumerate(range(tft.LAYOUT_START, tft.LAYOUT_END, 4)):
struct.pack_into("<I", head, o, (i * 7) ^ tft.LAYOUT_KEY)
struct.pack_into("<I", head, tft.OFF_RES_CRC, bitwise_word_crc(resources, 0xFFFFFFFF))
struct.pack_into("<I", head, tft.OFF_HEAD1_CRC, bitwise_word_crc(bytes(head[:0xC4]), 0xFFFFFFFF))
struct.pack_into("<I", head, tft.OFF_HEAD2_CRC, bitwise_word_crc(bytes(head[0xC8:0x18C]), 0xFFFFFFFF))
body = bytes(head) + resources + pages
n = len(body) + 4
return body + struct.pack("<I", bitwise_word_crc(body, 0xFFFFFFFF) ^ (n & 0xFF) ^ 0x56)
class Tft(unittest.TestCase):
def test_layout_table(self):
t = fake_tft(b"x" * 10)
self.assertEqual(tft.layout_table(t)[:3], [0, 1000, 2000])
self.assertEqual(tft.header_info(t)["width"], 480)
def test_patch_trailer_same_length(self):
old = fake_tft(b"page data: colour ff ff")
new_body = old[:-4].replace(b"ff ff", b"00 f8")
expect = fake_tft(b"page data: colour 00 f8")
self.assertEqual(tft.patch_trailer(old, new_body + b"\0\0\0\0"), expect)
def test_checksums_and_seal(self):
# rules measured on Editor output: 0x44 = CRC of the resource area, 0xC4 = CRC of
# [0, 0xC4), 0x18C = CRC of [0xC8, 0x18C), trailer = CRC of the file ^ (len & 0xFF) ^ 0x56
good = sealed_tft(b"firmware and pictures", b"page data, any length!")
self.assertTrue(tft.verify_header(good))
self.assertTrue(tft.verify_trailer(good))
broken = bytearray(good)
for o in (tft.OFF_RES_CRC, tft.OFF_HEAD1_CRC, tft.OFF_HEAD2_CRC, len(good) - 4):
broken[o] ^= 0x5A
self.assertFalse(tft.verify_header(bytes(broken)))
self.assertFalse(tft.verify_trailer(bytes(broken)))
self.assertEqual(tft.seal(bytes(broken)), good)
# a different length changes the trailer's XOR byte
longer = sealed_tft(b"firmware and pictures", b"page data, any length!!!")
self.assertEqual(tft.seal(longer[:-4] + b"\0\0\0\0"), longer)
def test_patch_trailer_rejects_length_change(self):
with self.assertRaises(ValueError):
tft.patch_trailer(b"\0" * 20, b"\0" * 24)
class Cli(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.mkdtemp()
self.hmi = os.path.join(self.tmp, "p.HMI")
shutil.copy(FIXTURE, self.hmi)
def tearDown(self):
shutil.rmtree(self.tmp)
def run_cli(self, *args):
out = io.StringIO()
with contextlib.redirect_stdout(out):
rc = cli.main(list(args))
return rc, out.getvalue()
def test_info_verify_dump(self):
self.assertIn("home", self.run_cli("info", self.hmi)[1])
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
self.assertIn('"title"', self.run_cli("dump", self.hmi, "--page", "home")[1])
def test_json_info_and_schema(self):
import json
info = json.loads(self.run_cli("info", self.hmi, "--json")[1])
self.assertEqual(info["size"], [480, 320])
sch = json.loads(self.run_cli("schema", "slider")[1])
self.assertIn("vertical", next(x for x in sch["attributes"] if x["name"] == "mode")["values"])
def test_unknown_kind_is_a_clean_error(self):
err = io.StringIO()
with contextlib.redirect_stderr(err):
rc, _ = self.run_cli("schema", "qr_code")
self.assertEqual(rc, 2)
self.assertIn("known kinds", err.getvalue())
self.assertIn("qrcode", err.getvalue())
def test_decode_encode(self):
d = os.path.join(self.tmp, "dec")
self.run_cli("decode", self.hmi, d)
self.run_cli("encode", d, os.path.join(self.tmp, "re.HMI"))
self.assertEqual(self.run_cli("verify", os.path.join(self.tmp, "re.HMI"))[0], 0)
def test_set_add_newpage_kinds(self):
self.run_cli("set", self.hmi, "home", "title", "txt", "hello")
self.run_cli("add", self.hmi, "home", "button", "x=1", "y=2", "w=30", "h=20", "txt=ok", "--name", "bNew")
self.run_cli("new-page", self.hmi, "extra")
out = self.run_cli("info", self.hmi)[1]
self.assertIn("extra", out)
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
self.assertIn("button", self.run_cli("kinds")[1])
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_add_picture_font_render(self):
from PIL import Image
png = os.path.join(self.tmp, "i.png")
Image.new("RGB", (8, 8), (1, 2, 3)).save(png)
self.run_cli("add-picture", self.hmi, png)
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
out = os.path.join(self.tmp, "png")
self.run_cli("render", self.hmi, out)
self.assertTrue(os.path.exists(os.path.join(out, "home.png")))
if __name__ == "__main__":
unittest.main()