Skip to content

Commit c180ecf

Browse files
committed
examples/elf: extend nxpkg validation coverage
Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 97802bd commit c180ecf

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

examples/elf/main/Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,24 @@ ifeq ($(CONFIG_EXAMPLES_ELF_ROMFS),y)
6969
ROMFSIMG = elf_romfs.img
7070
ROMFSSRC = elf_romfs.c
7171
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
72+
ifdef CONFIG_SYSTEM_NXPKG
73+
PKGINDEX = $(BINDIR)/index.json
74+
PKGBADINDEX = $(BINDIR)/bad-index.json
75+
PKGTEST = $(BINDIR)/pkgtest.nsh
76+
PKGFAIL = $(BINDIR)/pkgfail.nsh
77+
PKG_FIXTURE_GEN = $(APPDIR)/examples/elf/main/mk_pkg_fixture.py
78+
PKG_FIXTURE_OUTPUTS = $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL)
79+
PKG_FIXTURE_INPUTS = $(BINDIR)/hello
80+
PKG_FIXTURE_ARGS = \
81+
$(BINDIR)/hello $(PKGINDEX) $(PKGBADINDEX) $(PKGTEST) $(PKGFAIL) \
82+
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)
83+
84+
$(PKG_FIXTURE_OUTPUTS) &: $(PKG_FIXTURE_INPUTS) $(PKG_FIXTURE_GEN)
85+
$(Q) python3 $(PKG_FIXTURE_GEN) \
86+
$(PKG_FIXTURE_ARGS)
87+
endif
7288

73-
$(ROMFSIMG):
89+
$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS)
7490
$(Q) genromfs -d $(BINDIR) -f $@
7591

7692
$(ROMFSSRC): $(ROMFSIMG)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python3
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import hashlib
6+
import json
7+
import pathlib
8+
import sys
9+
10+
11+
def sha256_file(path: pathlib.Path) -> str:
12+
digest = hashlib.sha256()
13+
with path.open("rb") as infile:
14+
while True:
15+
chunk = infile.read(4096)
16+
if not chunk:
17+
break
18+
digest.update(chunk)
19+
20+
return digest.hexdigest()
21+
22+
23+
def write_index(
24+
path: pathlib.Path,
25+
arch: str,
26+
compat: str,
27+
artifact: str,
28+
digest: str,
29+
) -> None:
30+
payload = {
31+
"packages": [
32+
{
33+
"name": "hello",
34+
"version": "1.0.0",
35+
"arch": arch,
36+
"compat": compat,
37+
"artifact": artifact,
38+
"sha256": digest,
39+
"type": "elf",
40+
},
41+
{
42+
"name": "hello",
43+
"version": "9.9.9",
44+
"arch": "arm",
45+
"compat": "stm32f4discovery",
46+
"artifact": artifact,
47+
"sha256": digest,
48+
"type": "elf",
49+
},
50+
]
51+
}
52+
53+
path.write_text(
54+
json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8"
55+
)
56+
57+
58+
def write_bad_index(path: pathlib.Path, arch: str, compat: str) -> None:
59+
payload = {
60+
"packages": [
61+
{
62+
"name": "hello-missing",
63+
"version": "1.0.0",
64+
"arch": arch,
65+
"compat": compat,
66+
"artifact": "/mnt/elf/romfs/hello-missing",
67+
"sha256": "0" * 64,
68+
"type": "elf",
69+
}
70+
]
71+
}
72+
73+
path.write_text(
74+
json.dumps(payload, separators=(",", ":")) + "\n", encoding="utf-8"
75+
)
76+
77+
78+
def write_script(path: pathlib.Path) -> None:
79+
script = "\n".join(
80+
[
81+
"mount -t tmpfs /etc",
82+
"mount -t tmpfs /var",
83+
"mkdir /etc/nxpkg",
84+
"cp /mnt/elf/romfs/index.json /etc/nxpkg/index.json",
85+
"nxpkg install hello",
86+
"nxpkg list",
87+
"",
88+
]
89+
)
90+
path.write_text(script, encoding="utf-8")
91+
92+
93+
def write_fail_script(path: pathlib.Path) -> None:
94+
script = "\n".join(
95+
[
96+
"mount -t tmpfs /etc",
97+
"mount -t tmpfs /var",
98+
"mkdir /etc/nxpkg",
99+
"cp /mnt/elf/romfs/bad-index.json /etc/nxpkg/index.json",
100+
"nxpkg install hello-missing",
101+
"",
102+
]
103+
)
104+
path.write_text(script, encoding="utf-8")
105+
106+
107+
def main() -> int:
108+
if len(sys.argv) != 8:
109+
print(
110+
"usage: mk_pkg_fixture.py <hello-bin> <index-json> <bad-index-json> "
111+
"<pkgtest-nsh> <pkgfail-nsh> <arch> <compat>",
112+
file=sys.stderr,
113+
)
114+
return 1
115+
116+
hello = pathlib.Path(sys.argv[1])
117+
index = pathlib.Path(sys.argv[2])
118+
bad_index = pathlib.Path(sys.argv[3])
119+
script = pathlib.Path(sys.argv[4])
120+
fail_script = pathlib.Path(sys.argv[5])
121+
arch = sys.argv[6]
122+
compat = sys.argv[7]
123+
artifact = "/mnt/elf/romfs/hello"
124+
125+
digest = sha256_file(hello)
126+
write_index(index, arch, compat, artifact, digest)
127+
write_bad_index(bad_index, arch, compat)
128+
write_script(script)
129+
write_fail_script(fail_script)
130+
return 0
131+
132+
133+
if __name__ == "__main__":
134+
raise SystemExit(main())

0 commit comments

Comments
 (0)