"""The .tft builder. Real Editor output cannot be in the repo (it embeds Nextion firmware), so these tests use a fake firmware folder with generated stand-in files (and their hashes) and check the structure. Byte-identity with the Editor is checked by test_against_editor_samples when NXHMI_TFT_SAMPLES points to a folder of .HMI + .tft pairs made with Nextion Editor 1.68.2.""" import glob import hashlib import os import shutil import struct import tempfile import unittest from unittest import mock from helpers import HAVE_PIL from nxhmi import cli, components as C, firmware, project, tft, tft_build from nxhmi.page import Page SAMPLES = os.environ.get("NXHMI_TFT_SAMPLES") def fake_firmware(folder, size=lambda i: 100 + 7 * i): """Write stand-ins for Nextion's firmware files and return a patch that makes their SHA-256 the expected one (the real files cannot be in the repo).""" hashes = {} for i, n in enumerate(firmware.FILES): data = bytes([i + 1]) * size(i) with open(os.path.join(folder, n), "wb") as fh: fh.write(data) hashes[n] = hashlib.sha256(data).hexdigest() return mock.patch.object(firmware, "expected_hashes", return_value=hashes) class Build(unittest.TestCase): def setUp(self): self.tmp = tempfile.mkdtemp() patch = fake_firmware(self.tmp) patch.start() self.addCleanup(patch.stop) def tearDown(self): shutil.rmtree(self.tmp) def test_firmware_hash_check(self): with open(os.path.join(self.tmp, firmware.FILES[2]), "ab") as fh: fh.write(b"x") # one byte more: wrong file with self.assertRaisesRegex(tft_build.FirmwareError, "SHA-256"): tft_build.build_tft(self.project(), self.tmp) os.remove(os.path.join(self.tmp, firmware.FILES[2])) with self.assertRaisesRegex(tft_build.FirmwareError, "not found"): tft_build.build_tft(self.project(), self.tmp) def test_firmware_command(self): import contextlib import io dst = os.path.join(self.tmp, "fw") with contextlib.redirect_stdout(io.StringIO()) as text: self.assertEqual(cli.main(["firmware", "--from", self.tmp, "--firmware-dir", dst]), 0) self.assertEqual(cli.main(["firmware", "--firmware-dir", self.tmp + "-none"]), 2) self.assertEqual(sorted(os.listdir(dst)), sorted(firmware.FILES)) self.assertIn("missing", text.getvalue()) def test_manifest(self): """firmware.sha256: the seven files in .tft order, each with a SHA-256 (sha256sum format).""" with open(firmware.MANIFEST, encoding="ascii") as fh: rows = [line.split() for line in fh if line.strip()] self.assertEqual([r[1] for r in rows], list(firmware.FILES)) self.assertEqual(firmware.FILES[:3], ("input.bin", "asp0.dll", "cd0.dll")) for digest, _ in rows: self.assertRegex(digest, "^[0-9a-f]{64}$") def project(self): h = project.new_project("NX4832T035_011") p = Page.parse(h.read("0.pa")) C.add(p, "hotspot", x=10, y=20, w=30, h=40) C.add(p, "variable") h.write("0.pa", p.pack()) project.add_page(h, "second") return h def test_structure(self): out = tft_build.build_tft(self.project(), self.tmp) self.assertEqual(len(out) % 4, 0) self.assertTrue(tft.verify_header(out)) self.assertTrue(tft.verify_trailer(out)) self.assertEqual(struct.unpack_from("