openhmi-nx: decode, encode, render Nextion HMI projects; .tft research
Black-box reverse-engineered .HMI format (container, three checksums, pages, all 31 component types, pictures, fonts), readable JSON, page renderer matching Nextion Editor, demo project built from code and a test suite (66 tests). See docs/COVERAGE.md for status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69e22cfb06
70 files changed
+25058
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import struct
|
||||
import unittest
|
||||
|
||||
from helpers import FIXTURE
|
||||
|
||||
from nxhmi.container import (BACKUP_TABLE_OFFSET, DATA_OFFSET, MARKER_OFFSET, VERSION_MAGIC, VERSION_OFFSET,
|
||||
HMIError, HMIFile, build)
|
||||
|
||||
|
||||
class Container(unittest.TestCase):
|
||||
def test_build_layout(self):
|
||||
f = build([("a.bin", b"hello"), ("b.bin", b"world!")])
|
||||
raw = f.to_bytes()
|
||||
self.assertEqual(raw[VERSION_OFFSET:VERSION_OFFSET + 8], VERSION_MAGIC)
|
||||
self.assertEqual(struct.unpack_from("<I", raw, MARKER_OFFSET)[0], 0xFFFFFFFF)
|
||||
self.assertEqual(raw[DATA_OFFSET:DATA_OFFSET + 5], b"hello")
|
||||
g = HMIFile.from_bytes(raw)
|
||||
self.assertEqual(g.names(), ["a.bin", "b.bin"])
|
||||
self.assertEqual(g.read("b.bin"), b"world!")
|
||||
self.assertEqual(raw[0:200], raw[BACKUP_TABLE_OFFSET:BACKUP_TABLE_OFFSET + 200])
|
||||
|
||||
def test_replace_like_the_editor(self):
|
||||
g = HMIFile.from_bytes(build([("a.bin", b"one")]).to_bytes())
|
||||
g.write("a.bin", b"two, longer")
|
||||
raw = g.to_bytes()
|
||||
h = HMIFile.from_bytes(raw)
|
||||
self.assertEqual(h.read("a.bin"), b"two, longer")
|
||||
old = [e for e in h.entries if e.deleted]
|
||||
self.assertEqual(len(old), 1)
|
||||
self.assertEqual(old[0].raw_name[:1], b"\0") # deleted entries lose name[0]
|
||||
self.assertEqual(h.table_ok(), (True, True))
|
||||
|
||||
def test_delete(self):
|
||||
g = HMIFile.from_bytes(build([("a", b"1"), ("b", b"2")]).to_bytes())
|
||||
g.delete("a")
|
||||
self.assertEqual(HMIFile.from_bytes(g.to_bytes()).names(), ["b"])
|
||||
with self.assertRaises(KeyError):
|
||||
g.read("a")
|
||||
|
||||
def test_backup_table_fallback_and_both_corrupt(self):
|
||||
raw = bytearray(build([("x", b"data")]).to_bytes())
|
||||
raw[5] ^= 0xFF
|
||||
self.assertEqual(HMIFile.from_bytes(bytes(raw)).names(), ["x"])
|
||||
raw[BACKUP_TABLE_OFFSET + 5] ^= 0xFF
|
||||
with self.assertRaises(HMIError):
|
||||
HMIFile.from_bytes(bytes(raw))
|
||||
|
||||
def test_compacted(self):
|
||||
g = HMIFile.from_bytes(build([("a", b"1" * 10)]).to_bytes())
|
||||
g.write("a", b"2" * 10)
|
||||
c = g.compacted()
|
||||
self.assertEqual(len(c.entries), 1)
|
||||
self.assertEqual(c.read("a"), b"2" * 10)
|
||||
|
||||
def test_editor_file_roundtrip_byte_identical(self):
|
||||
data = open(FIXTURE, "rb").read()
|
||||
self.assertEqual(HMIFile.from_bytes(data).to_bytes(), data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user