Skip to content

Commit

Permalink
heartbeat engaged mads mismatch mutation test
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnyhaibin committed Dec 12, 2024
1 parent 2dcf380 commit cf115df
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/libpanda/safety_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ void set_heartbeat_engaged_mads(bool c){
heartbeat_engaged_mads = c;
}

int get_heartbeat_engaged_mads_mismatches(void){
return heartbeat_engaged_mads_mismatches;
}

int get_mads_transition(void){
return m_mads_state.acc_main.transition;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/libpanda/safety_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def setup_safety_helpers(ffi):
uint32_t get_acc_main_on_mismatches(void);
void set_mads_params(bool enable_mads, bool disengage_lat_on_brake);
void set_heartbeat_engaged_mads(bool c);
int get_heartbeat_engaged_mads_mismatches(void);
int get_mads_transition(void);
""")

Expand Down Expand Up @@ -145,5 +146,6 @@ def get_acc_main_on_mismatches(self) -> int: ...

def set_mads_params(self, enable_mads: bool, disengage_lat_on_brake: bool) -> None: ...
def set_heartbeat_engaged_mads(self, c: bool) -> None: ...
def get_heartbeat_engaged_mads_mismatches(self) -> int: ...
def get_mads_transition(self) -> int: ...
# def get_temp_debug(self) -> int: ...
55 changes: 55 additions & 0 deletions tests/safety/mads_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,58 @@ def test_enable_control_allowed_with_mads_button_and_disable_with_main_cruise(se
self.assertFalse(self.safety.get_controls_allowed_lat())
finally:
self._mads_states_cleanup()

def test_heartbeat_engaged_mads_mismatches(self):
"""Test reset logic using controls_allowed_lat and heartbeat_engaged_mads"""
self._mads_states_cleanup()
self.safety.set_mads_params(True, False)

# Test exact mismatch boundary conditions
self.safety.set_controls_allowed_lat(True)
self.safety.set_heartbeat_engaged_mads(False)

# Should stay engaged through first two rx messages
self._rx(self._speed_msg(0))
self.assertTrue(self.safety.get_controls_allowed_lat())
self.assertEqual(1, self.safety.get_heartbeat_engaged_mads_mismatches())

self._rx(self._speed_msg(0))
self.assertTrue(self.safety.get_controls_allowed_lat())
self.assertEqual(2, self.safety.get_heartbeat_engaged_mads_mismatches())

# Third rx should trigger disengagement
self._rx(self._speed_msg(0))
self.assertFalse(self.safety.get_controls_allowed_lat())
self.assertEqual(3, self.safety.get_heartbeat_engaged_mads_mismatches())
self.assertEqual(6, self.safety.mads_get_current_disengage_reason())

# Test reset condition
self.safety.set_heartbeat_engaged_mads(True)
self._rx(self._speed_msg(0))
self.assertEqual(0, self.safety.get_heartbeat_engaged_mads_mismatches())

# Test all combinations
for controls_allowed_lat in (True, False):
with self.subTest("controls_allowed_lat", controls_allowed_lat=controls_allowed_lat):
for heartbeat_engaged_mads in (True, False):
with self.subTest("heartbeat_engaged_mads", heartbeat_engaged_mads=heartbeat_engaged_mads):
self._mads_states_cleanup()
self.safety.set_mads_params(True, False)
self.safety.set_controls_allowed_lat(controls_allowed_lat)
self.safety.set_heartbeat_engaged_mads(heartbeat_engaged_mads)

# Send three rx messages and check state after each
for i in range(3):
self._rx(self._speed_msg(0))
if controls_allowed_lat and not heartbeat_engaged_mads:
expected_control = i < 2 # Should stay engaged for first two rx
expected_mismatches = i + 1
self.assertEqual(expected_control, self.safety.get_controls_allowed_lat(),
f"Incorrect control state after rx {i + 1}")
self.assertEqual(expected_mismatches, self.safety.get_heartbeat_engaged_mads_mismatches(),
f"Incorrect mismatch count after rx {i + 1}")
else:
self.assertEqual(controls_allowed_lat, self.safety.get_controls_allowed_lat())
self.assertEqual(0, self.safety.get_heartbeat_engaged_mads_mismatches())

self._mads_states_cleanup()

0 comments on commit cf115df

Please sign in to comment.