diff --git a/opendbc_repo/opendbc/safety/tests/native_callback.py b/opendbc_repo/opendbc/safety/tests/native_callback.py new file mode 100644 index 0000000000..e355c93b73 --- /dev/null +++ b/opendbc_repo/opendbc/safety/tests/native_callback.py @@ -0,0 +1,52 @@ +import signal +import subprocess +import sys + +from opendbc.safety.tests.libsafety import libsafety_py + + +def assert_native_callback_finishes(test, model, param, address, bus, data): + try: + mutant = libsafety_py.libsafety.mutation_get_active_mutant() + except AttributeError: + mutant = None + script = """import sys +from opendbc.safety.tests.libsafety import libsafety_py +libsafety_py.load(sys.argv[1]) +safety = libsafety_py.libsafety +if sys.argv[2] != 'None': + safety.mutation_set_active_mutant(int(sys.argv[2])) +packet = libsafety_py.make_CANPacket(int(sys.argv[5]), int(sys.argv[6]), bytes.fromhex(sys.argv[7])) +print('NATIVE_SETUP_BEGIN', flush=True) +status = safety.set_safety_hooks(int(sys.argv[3]), int(sys.argv[4])) +print('NATIVE_SETUP_RETURN', flush=True) +if status != 0: + print('NATIVE_SETUP_ASSERTION', flush=True) + raise SystemExit(1) +safety.init_tests() +print('NATIVE_CALLBACK_BEGIN', flush=True) +accepted = safety.safety_rx_hook(packet) +print('NATIVE_CALLBACK_RETURN', flush=True) +raise SystemExit(0 if accepted else 1) +""" + try: + result = subprocess.run([sys.executable, '-c', script, libsafety_py.libpath, str(mutant), str(model), + str(param), str(address), str(bus), bytes(data).hex()], + capture_output=True, text=True, timeout=10) + except subprocess.TimeoutExpired as exc: + output = exc.stdout or b'' + if isinstance(output, bytes): + output = output.decode(errors='replace') + setup_entered = 'NATIVE_SETUP_BEGIN' in output and 'NATIVE_SETUP_RETURN' not in output + callback_entered = 'NATIVE_CALLBACK_BEGIN' in output and 'NATIVE_CALLBACK_RETURN' not in output + if not (setup_entered or callback_entered): + raise RuntimeError('Native probe timed out outside the checked native calls') from exc + test.fail('Native initialization or valid RX callback did not finish within ten seconds') + setup_entered = 'NATIVE_SETUP_BEGIN' in result.stdout and 'NATIVE_SETUP_RETURN' not in result.stdout + callback_entered = 'NATIVE_CALLBACK_BEGIN' in result.stdout and 'NATIVE_CALLBACK_RETURN' not in result.stdout + if result.returncode == -signal.SIGSEGV and (setup_entered or callback_entered): + test.fail('Native initialization or valid RX callback faulted: ' + result.stderr) + asserted = 'NATIVE_SETUP_ASSERTION' in result.stdout or 'NATIVE_CALLBACK_RETURN' in result.stdout + if result.returncode not in (0, 1) or not asserted: + raise RuntimeError(f'Native probe infrastructure failure ({result.returncode}): {result.stdout}{result.stderr}') + test.assertEqual(result.returncode, 0, result.stdout + result.stderr) diff --git a/opendbc_repo/opendbc/safety/tests/test_tesla_hw1.py b/opendbc_repo/opendbc/safety/tests/test_tesla_hw1.py index 1ad7d741f2..89c615c5b1 100644 --- a/opendbc_repo/opendbc/safety/tests/test_tesla_hw1.py +++ b/opendbc_repo/opendbc/safety/tests/test_tesla_hw1.py @@ -1,4 +1,5 @@ import unittest +from opendbc.safety.tests.native_callback import assert_native_callback_finishes import pytest from opendbc.can import CANPacker from opendbc.car.structs import CarParams @@ -31,6 +32,10 @@ def reset(param=16): class TestModelSHW1Safety(unittest.TestCase): + def test_native_initialization_and_checksum_callback_finish(self): + address, data, _ = module.ModelSHW1CAN(packer).create_steering_control(0, 0, False) + assert_native_callback_finishes(self, 10, 16, address, 2, data) + def test_all_16bit_profile_words_are_isolated(self): debug = lib.set_safety_hooks(CarParams.SafetyModel.allOutput, 0) == 0 supported = {0, 1, 16, 17, 256, 257, 512, 768, 1536, 1792, 2048, 2304, 3072, 3328} | ({513, 769, 1537, 1793, 2049, 2305, 3073, 3329} if debug else set()) diff --git a/opendbc_repo/opendbc/safety/tests/test_volvo_c1.py b/opendbc_repo/opendbc/safety/tests/test_volvo_c1.py index af02ce4b68..40619206f3 100644 --- a/opendbc_repo/opendbc/safety/tests/test_volvo_c1.py +++ b/opendbc_repo/opendbc/safety/tests/test_volvo_c1.py @@ -1,4 +1,5 @@ import unittest +from opendbc.safety.tests.native_callback import assert_native_callback_finishes import pytest from opendbc.can import CANPacker from opendbc.safety import LEN_TO_DLC @@ -30,6 +31,10 @@ def rx(name, bus, values): class TestVolvoC1Safety(unittest.TestCase): + def test_native_initialization_and_speed_callback_finish(self): + address, data, bus = packer.make_can_msg('VehicleSpeed1', 0, {'VehicleSpeed': 72}) + assert_native_callback_finishes(self, 36, 2, address, bus, data) + def test_exhaustive_only_assigned_profiles_are_admitted(self): for param in range(65536): status = lib.set_safety_hooks(36, param) diff --git a/opendbc_repo/opendbc/safety/tests/test_volvo_cma.py b/opendbc_repo/opendbc/safety/tests/test_volvo_cma.py index c9212d7dca..8bfaf32b9b 100644 --- a/opendbc_repo/opendbc/safety/tests/test_volvo_cma.py +++ b/opendbc_repo/opendbc/safety/tests/test_volvo_cma.py @@ -4,6 +4,7 @@ The fixtures use the same DBCs and checksum encoders as the existing Volvo car integration tests. Permissions come from physical RX, never a forced active flag. """ import unittest +from opendbc.safety.tests.native_callback import assert_native_callback_finishes from opendbc.can import CANPacker from opendbc.car.volvo import cma_can @@ -40,6 +41,10 @@ class TestVolvoCMASafety(unittest.TestCase): self.pt = CANPacker('volvo_front_1_spa' if self.PARAM else 'volvo_front_1_cma') self.reset() + def test_native_initialization_and_angle_callback_finish(self): + address, data, bus = self.mid.make_can_msg('PSCM', 2, {'PSCM_ANGLE_SENSOR': .05596}) + assert_native_callback_finishes(self, 36, self.PARAM, address, bus, data) + def reset(self): self.assertEqual(self.safety.set_safety_hooks(36, self.PARAM), 0) self.safety.init_tests() diff --git a/prebuilt.json b/prebuilt.json index 9fe1c9e36f..7130027a0b 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "0ad9bbe1597ac3c3347f75ac9e97b0cf8eb327a07c24b0b6dab3d9fd5e8f29ce", "schema_version": 1} +{"protected_inventory_sha256": "065b26d396886f44252dab96e873651e4cb0609373cb9e7c1d584dabda96adac", "schema_version": 1}