Files
StarPilot/panda/tests/test_build.py
T
2026-11-28 12:30:35 -06:00

74 lines
2.8 KiB
Python

#!/usr/bin/env python3
import os
import shutil
import struct
import sys
import subprocess
import tempfile
import unittest
ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
# RM0468 4.5.1: FLASH_CR and FLASH_OPTCR only unlock with these keys
FLASH_KEYS = (0x45670123, 0xCDEF89AB)
OPT_KEYS = (0x08192A3B, 0x4C5D6E7F)
def word(w):
return struct.pack("<I", w)
class TestBuild(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Build each profile in its own source copy.
files = [f for f in subprocess.check_output(["git", "ls-files"], cwd=ROOT, encoding="utf8").split() if os.path.isfile(os.path.join(ROOT, f))]
env = {k: v for k, v in os.environ.items() if k not in ("RELEASE", "CERT", "PANDA_FAULT_INJECTION")}
cls.tmp = {}
for profile in ("debug", "faulttest", "release"):
tmp = tempfile.TemporaryDirectory()
cls.addClassCleanup(tmp.cleanup)
d = cls.tmp[profile] = tmp.name
for f in files:
os.makedirs(os.path.dirname(f"{d}/{f}"), exist_ok=True)
shutil.copy2(f"{ROOT}/{f}", f"{d}/{f}")
build_env = {}
if profile == "release":
build_env = {"RELEASE": "1", "CERT": f"{d}/board/crypto/certs/debug"}
elif profile == "faulttest":
build_env = {"PANDA_FAULT_INJECTION": "1"}
subprocess.check_call(
[sys.executable, "-B", "-m", "SCons", "-u", "board/obj/panda_h7.bin.signed", "board/obj/bootstub.panda_h7.bin"],
cwd=d, env=env | build_env, stdout=subprocess.DEVNULL,
)
def _read(self, profile, fn):
with open(f"{self.tmp[profile]}/board/obj/{fn}", "rb") as f:
return f.read()
def _has_symbol(self, profile, elf, name):
# Symbols in the ELF string table are null terminated.
return f"\0{name}\0".encode() in self._read(profile, f"panda_h7/{elf}")
def test_release_has_no_debug_features(self):
for profile in self.tmp:
with self.subTest(profile=profile):
assert self._has_symbol(profile, "main.elf", "stop_mode_requested") == (profile != "release")
for symbol in ("self_test_inject", "relay_readback_inject"):
assert self._has_symbol(profile, "main.elf", symbol) == (profile == "faulttest"), symbol
def test_only_bootstub_programs_flash(self):
for profile in self.tmp:
for func in ("flash_unlock", "flash_erase_sector", "flash_write_word"):
assert self._has_symbol(profile, "bootstub.elf", func), func
assert not self._has_symbol(profile, "main.elf", func), func
assert not any(word(k) in self._read(profile, "panda_h7/main.bin") for k in FLASH_KEYS)
def test_option_bytes_never_unlocked(self):
for profile in self.tmp:
for fn in ("panda_h7/main.bin", "bootstub.panda_h7.bin"):
assert not any(word(k) in self._read(profile, fn) for k in OPT_KEYS), (profile, fn)
if __name__ == "__main__":
unittest.main()