From: Jonas Rebmann <jre@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
BAREBOX <barebox@lists.infradead.org>
Cc: Tobias Waldekranz <tobias@waldekranz.com>,
Jonas Rebmann <jre@pengutronix.de>
Subject: [PATCH v2 9/9] test: py: add TLV integration tests
Date: Mon, 29 Sep 2025 10:04:00 +0200 [thread overview]
Message-ID: <20250929-tlv-integration-v2-9-55f75da7e670@pengutronix.de> (raw)
In-Reply-To: <20250929-tlv-integration-v2-0-55f75da7e670@pengutronix.de>
With TLV signature coming up, we want to test all things TLV more
thoroughly, including "roundtrip" integration tests that include the
bareboxtlv-generator python-script and the tlv barebox-command.
- Encode the example TLV data
- Add a "corrupted" variant of that tlv binary with a bit error
- Test decoding those binaries using bareboxtlv-generator
- Test decoding those binaries using the tlv-command
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
test/py/test_tlv.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/test/py/test_tlv.py b/test/py/test_tlv.py
new file mode 100644
index 0000000000..963f3749b3
--- /dev/null
+++ b/test/py/test_tlv.py
@@ -0,0 +1,78 @@
+import os
+import re
+import subprocess
+from pathlib import Path
+from crcmod.predefined import mkPredefinedCrcFun
+from .helper import skip_disabled
+
+_crc32_mpeg = mkPredefinedCrcFun("crc-32-mpeg")
+
+import pytest
+
+
+class _TLV_Testdata:
+ def generator(self, args, check=True):
+ cmd = [os.sys.executable, str(self.generator_py)] + args
+ res = subprocess.run(cmd, text=True)
+ if check and res.returncode != 0:
+ raise RuntimeError(f"generator failed ({res.returncode}): {res.stdout}\n{res.stderr}")
+ return res
+
+ def __init__(self, testfs):
+ self.dir = Path(testfs)
+ self.scripts_dir = Path("scripts/bareboxtlv-generator")
+ self.data = self.scripts_dir / "data-example.yaml"
+ self.schema = self.scripts_dir / "schema-example.yaml"
+ self.generator_py = self.scripts_dir / "bareboxtlv-generator.py"
+ self.unsigned_bin = self.dir / 'unsigned.tlv'
+ self.corrupted_bin = self.dir / 'unsigned_corrupted.tlv'
+
+@pytest.fixture(scope="module")
+def tlv_testdata(testfs):
+ t = _TLV_Testdata(testfs)
+ t.generator(["--input-data", str(t.data), str(t.schema), str(t.unsigned_bin)])
+ assert t.unsigned_bin.exists(), "unsigned TLV not created"
+
+ with open(t.unsigned_bin, 'r+b') as f:
+ data = bytearray(f.read())
+ data[0x20] ^= 1
+ with open(t.corrupted_bin, "wb") as f:
+ f.write(data)
+
+ return t
+
+def test_tlv_generator(tlv_testdata):
+ t = tlv_testdata
+ out_yaml = t.dir / 'out.yaml'
+
+
+ good = t.generator(["--output-data", str(out_yaml), str(t.schema), str(t.unsigned_bin)], check=False)
+ assert good.returncode == 0, f"valid unsigned TLV failed to decode: {good.stderr}\n{good.stdout}"
+
+ bad = t.generator(["--output-data", str(t.dir / 'bad.yaml'), str(t.schema), str(t.corrupted_bin)], check=False)
+ assert bad.returncode != 0, "unsigned TLV with invalid CRC unexpectedly decoded successfully"
+
+def test_tlv_command(barebox, barebox_config, tlv_testdata):
+ skip_disabled(barebox_config, "CONFIG_CMD_TLV")
+ t = tlv_testdata
+ with open(t.data, 'r', encoding='utf-8') as f:
+ yaml_lines = [l.strip() for l in f if l.strip() and not l.strip().startswith('#')]
+
+ stdout = barebox.run_check(f"tlv /mnt/9p/testfs/{t.unsigned_bin.name}")
+
+ # work around 9pfs printing here after a failed network test
+ tlv_offset = next((i for i, line in enumerate(stdout) if line.startswith("tlv")), None)
+ tlv_lines = stdout[tlv_offset + 1:-1]
+
+ assert len(yaml_lines) == len(tlv_lines), \
+ f"YAML and TLV output line count mismatch for {t.unsigned_bin.name}"
+
+ for yline, tline in zip(yaml_lines, tlv_lines):
+ m = re.match(r'^\s*([^=]+) = "(.*)";$', tline)
+ assert m, f"malformed tlv line: {tline}"
+ tkey, tval = m.group(1), m.group(2)
+ m = re.match(r'^([^:]+):\s*(?:"([^"]*)"\s*|(.*))$', yline)
+ assert m, f"malformed yaml line: {yline}"
+ ykey, yval = m.group(1), m.group(2) or m.group(3)
+ assert ykey == tkey, f"key mismatch: {ykey} != {tkey}"
+ assert str(yval) == str(tval), f"value mismatch for {ykey}: {yval} != {tval}"
--
2.51.0.297.gca2559c1d6
prev parent reply other threads:[~2025-09-29 8:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-29 8:03 [PATCH v2 0/9] TLV integration tests and test/py cleanup Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 1/9] test: when testfs feature is available, always enable it Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 2/9] test: provide testfs via fixture Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 3/9] test: move dm-verity testdata generation to fixture Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 4/9] test: move fitimage testdata generation to fixture and drop script Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 5/9] test: py: test_bootchooser: remove dead code Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 6/9] commands: tlv: clarify error opening tlv Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 7/9] ci: container: install crcmod and cryptography Jonas Rebmann
2025-09-29 8:03 ` [PATCH v2 8/9] configs: enable tlv command for multi_v7 and multi_v8 Jonas Rebmann
2025-09-29 8:04 ` Jonas Rebmann [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250929-tlv-integration-v2-9-55f75da7e670@pengutronix.de \
--to=jre@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=s.hauer@pengutronix.de \
--cc=tobias@waldekranz.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox