From 04e17b4d0de9a49fb05f387871a5b5ae4b929ed8 Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Mon, 8 Jun 2026 16:58:11 +0530 Subject: [PATCH] examples/elf: extend nxpkg validation coverage Extend the ELF package fixtures with both success and failure repository data so nxpkg validation paths are exercised from the same example flow. Format the helper generator with black so it matches the current apps lint checks. Signed-off-by: aviralgarg05 --- examples/elf/main/Makefile | 18 +++- examples/elf/main/mk_pkg_fixture.py | 130 ++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 examples/elf/main/mk_pkg_fixture.py diff --git a/examples/elf/main/Makefile b/examples/elf/main/Makefile index 76093ea5f88..5ad3d9f623c 100644 --- a/examples/elf/main/Makefile +++ b/examples/elf/main/Makefile @@ -69,8 +69,24 @@ ifeq ($(CONFIG_EXAMPLES_ELF_ROMFS),y) ROMFSIMG = elf_romfs.img ROMFSSRC = elf_romfs.c ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT)) +ifdef CONFIG_SYSTEM_NXPKG +PKGINDEX = $(BINDIR)/index.json +PKGBADINDEX = $(BINDIR)/bad-index.json +PKGTEST = $(BINDIR)/pkgtest.nsh +PKGFAIL = $(BINDIR)/pkgfail.nsh +PKG_FIXTURE_GEN = $(APPDIR)/examples/elf/main/mk_pkg_fixture.py +PKG_FIXTURE_OUTPUTS = $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) +PKG_FIXTURE_INPUTS = $(BINDIR)/hello +PKG_FIXTURE_ARGS = \ + $(BINDIR)/hello $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) \ + $(CONFIG_ARCH) $(CONFIG_ARCH_BOARD) + +$(PKG_FIXTURE_OUTPUTS) &: $(PKG_FIXTURE_INPUTS) $(PKG_FIXTURE_GEN) + $(Q) python3 $(PKG_FIXTURE_GEN) \ + $(PKG_FIXTURE_ARGS) +endif -$(ROMFSIMG): +$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS) $(Q) genromfs -d $(BINDIR) -f $@ $(ROMFSSRC): $(ROMFSIMG) diff --git a/examples/elf/main/mk_pkg_fixture.py b/examples/elf/main/mk_pkg_fixture.py new file mode 100644 index 00000000000..da8b395c077 --- /dev/null +++ b/examples/elf/main/mk_pkg_fixture.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: Apache-2.0 + +import hashlib +import json +import pathlib +import sys + + +def sha256_file(path: pathlib.Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as infile: + while True: + chunk = infile.read(4096) + if not chunk: + break + digest.update(chunk) + + return digest.hexdigest() + + +def write_index( + path: pathlib.Path, + arch: str, + compat: str, + artifact: str, + digest: str, +) -> None: + payload = { + "packages": [ + { + "name": "hello", + "version": "1.0.0", + "arch": arch, + "compat": compat, + "artifact": artifact, + "sha256": digest, + "type": "elf", + }, + { + "name": "hello", + "version": "9.9.9", + "arch": "arm", + "compat": "stm32f4discovery", + "artifact": artifact, + "sha256": digest, + "type": "elf", + }, + ] + } + + path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8") + + +def write_bad_index(path: pathlib.Path, arch: str, compat: str) -> None: + payload = { + "packages": [ + { + "name": "hello-missing", + "version": "1.0.0", + "arch": arch, + "compat": compat, + "artifact": "/mnt/elf/romfs/hello-missing", + "sha256": "0" * 64, + "type": "elf", + } + ] + } + + path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8") + + +def write_script(path: pathlib.Path) -> None: + script = "\n".join( + [ + "mount -t tmpfs /etc", + "mount -t tmpfs /var", + "mkdir /etc/nxpkg", + "cp /mnt/elf/romfs/index.json /etc/nxpkg/index.json", + "nxpkg install hello", + "nxpkg list", + "", + ] + ) + path.write_text(script, encoding="utf-8") + + +def write_fail_script(path: pathlib.Path) -> None: + script = "\n".join( + [ + "mount -t tmpfs /etc", + "mount -t tmpfs /var", + "mkdir /etc/nxpkg", + "cp /mnt/elf/romfs/bad-index.json /etc/nxpkg/index.json", + "nxpkg install hello-missing", + "", + ] + ) + path.write_text(script, encoding="utf-8") + + +def main() -> int: + if len(sys.argv) != 8: + print( + "usage: mk_pkg_fixture.py " + " ", + file=sys.stderr, + ) + return 1 + + hello = pathlib.Path(sys.argv[1]) + index = pathlib.Path(sys.argv[2]) + bad_index = pathlib.Path(sys.argv[3]) + script = pathlib.Path(sys.argv[4]) + fail_script = pathlib.Path(sys.argv[5]) + arch = sys.argv[6] + compat = sys.argv[7] + artifact = "/mnt/elf/romfs/hello" + + digest = sha256_file(hello) + write_index(index, arch, compat, artifact, digest) + write_bad_index(bad_index, arch, compat) + write_script(script) + write_fail_script(fail_script) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())