2026-11-15 15:11:02 +01:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
2026-11-15 15:20:27 +01:00
|
|
|
class EditorVersion(unittest.TestCase):
|
|
|
|
|
def test_fixture_written_by_tested_editor(self):
|
|
|
|
|
"""Canary: the fixture's main.HMI carries the marker Editor 1.68.2 writes (offset 9 = 0x44).
|
|
|
|
|
If a re-saved fixture from another Editor build changes it, update docs + TESTED_EDITOR_VERSION."""
|
|
|
|
|
import nxhmi
|
|
|
|
|
self.assertEqual(nxhmi.TESTED_EDITOR_VERSION, "1.68.2.3035")
|
|
|
|
|
self.assertEqual(HMIFile.load(FIXTURE).read("main.HMI")[9], 0x44)
|
|
|
|
|
|
|
|
|
|
|
2026-11-15 15:11:02 +01:00
|
|
|
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()
|