Files
openhmi-nx/tests/test_checksum.py
T
mangavalkandClaude Opus 5.5 69e22cfb06 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>
2026-11-15 15:11:02 +01:00

53 lines
1.9 KiB
Python

import struct
import unittest
from helpers import FIXTURE, bitwise_dword_crc, bitwise_word_crc
from nxhmi import checksum
from nxhmi.container import HMIFile
class ChecksumAlgorithms(unittest.TestCase):
def test_word_per_byte_matches_bitwise_reference(self):
for data in (b"", b"\x00", bytes(range(256)), b"nextion" * 13):
self.assertEqual(checksum.word_per_byte_crc(data), bitwise_word_crc(data))
def test_dword_matches_bitwise_reference(self):
for data in (b"", bytes(range(64)), b"abcd" * 50):
self.assertEqual(checksum.dword_crc(data), bitwise_dword_crc(data))
def test_dword_needs_multiple_of_4(self):
with self.assertRaises(ValueError):
checksum.dword_crc(b"abc")
def test_table_crc_appends_magic(self):
tbl = struct.pack("<I", 0)
expect = bitwise_dword_crc(tbl + struct.pack("<I", checksum.TABLE_MAGIC))
self.assertEqual(checksum.table_crc(tbl), expect)
def test_page_crc_definition(self):
page = bytearray(80)
struct.pack_into("<IIII", page, 4, 80, 56, 2, 0)
page[20], page[21] = 0x00, 0x4F
tail = struct.pack("<II", 80, 2) + bytes(page[20:22])
self.assertEqual(checksum.page_crc(bytes(page)), bitwise_word_crc(bytes(page[4:]) + tail))
class ChecksumsOfEditorOutput(unittest.TestCase):
"""Every checksum in a file written by Nextion Editor 1.68 must verify."""
def test_fixture(self):
h = HMIFile.load(FIXTURE)
self.assertEqual(h.table_ok(), (True, True))
for e in h.live():
blob = h.read(e.name)
stored = struct.unpack_from("<I", blob, 0)[0]
if e.name.endswith(".pa"):
self.assertEqual(checksum.page_crc(blob), stored, e.name)
if e.name == "main.HMI":
self.assertEqual(checksum.main_crc(blob), stored)
if __name__ == "__main__":
unittest.main()