tft: firmware files from a local firmware/ folder, checked by SHA-256
Nextion's seven firmware files cannot be in the repo, so the repo now ships their SHA-256 hashes (src/nxhmi/firmware.sha256, Editor 1.68.2.3035) and every build checks the files against them. The files live in the git-ignored firmware/ folder; `nxhmi firmware --from <Editor folder>` copies them, `nxhmi firmware` reports ok / missing / wrong hash. build-tft and the MCP tool take --firmware-dir / firmware_dir (also NXHMI_FIRMWARE_DIR) instead of the Editor folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0287cf21e4
commit
85422a1ef4
14 files changed
+259
-44
No files matched your search
+53
-7
@@ -1,32 +1,75 @@
|
||||
"""The .tft builder. Real Editor output cannot be in the repo (it embeds Nextion firmware), so
|
||||
these tests use a fake "Editor folder" with generated stand-in files and check the structure.
|
||||
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 <name>.HMI + <name>.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, project, tft, tft_build
|
||||
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()
|
||||
for i, n in enumerate(tft_build.FIRMWARE_FILES):
|
||||
with open(os.path.join(self.tmp, n), "wb") as fh:
|
||||
fh.write(bytes([i + 1]) * (100 + 7 * i))
|
||||
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"))
|
||||
@@ -110,7 +153,7 @@ class Build(unittest.TestCase):
|
||||
import contextlib
|
||||
import io
|
||||
with contextlib.redirect_stdout(io.StringIO()) as text:
|
||||
self.assertEqual(cli.main(["build-tft", hmi, out, "--editor-dir", self.tmp]), 0)
|
||||
self.assertEqual(cli.main(["build-tft", hmi, out, "--firmware-dir", self.tmp]), 0)
|
||||
self.assertEqual(cli.main(["tft-info", out]), 0)
|
||||
self.assertIn('"trailer_ok": true', text.getvalue())
|
||||
|
||||
@@ -134,7 +177,10 @@ class Build(unittest.TestCase):
|
||||
self.assertEqual(struct.unpack_from("<II", out, pics + 28), (1, 48 + len(h.read("0.i")) - 24))
|
||||
self.assertEqual(out[lay[12]:lay[12] + 44][:8], h.read("0.zi")[:8])
|
||||
|
||||
@unittest.skipUnless(SAMPLES and os.path.isdir(tft_build.DEFAULT_EDITOR_DIR), "needs Editor-made samples")
|
||||
|
||||
class EditorSamples(unittest.TestCase):
|
||||
@unittest.skipUnless(SAMPLES and all(v == "ok" for v in firmware.status().values()),
|
||||
"needs Editor-made samples and the real firmware files in firmware/")
|
||||
def test_against_editor_samples(self):
|
||||
from nxhmi.container import HMIFile
|
||||
done = 0
|
||||
|
||||
Reference in new issue
Block a user