From ceff91d3c5a9c0911230ffd11a4d5e1c1deee99e Mon Sep 17 00:00:00 2001 From: rbiasini Date: Sat, 7 Mar 2020 23:22:47 -0800 Subject: [PATCH] Standardize brake safety tests (#465) * WIP: improve safety test format * Fix tests --- tests/safety/common.py | 78 ++++++++++++++++++++--------- tests/safety/test_cadillac.py | 6 +-- tests/safety/test_chrysler.py | 34 +++---------- tests/safety/test_gm.py | 34 +++---------- tests/safety/test_honda.py | 30 +++-------- tests/safety/test_hyundai.py | 35 +++---------- tests/safety/test_nissan.py | 32 +++--------- tests/safety/test_subaru.py | 36 +++---------- tests/safety/test_toyota.py | 30 +++-------- tests/safety/test_volkswagen_mqb.py | 60 ++++++++-------------- 10 files changed, 126 insertions(+), 249 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 572b27176eb1ff..e0296d3ac11d54 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -13,26 +13,58 @@ def make_msg(bus, addr, length=8): return to_send -def test_relay_malfunction(test, addr, bus=0): - # input is a test class and the address that, if seen on specified bus, triggers - # the relay_malfunction protection logic: both tx_hook and fwd_hook are - # expected to return failure - test.assertFalse(test.safety.get_relay_malfunction()) - test.safety.safety_rx_hook(make_msg(bus, addr, 8)) - test.assertTrue(test.safety.get_relay_malfunction()) - for a in range(1, 0x800): - for b in range(0, 3): - test.assertFalse(test.safety.safety_tx_hook(make_msg(b, a, 8))) - test.assertEqual(-1, test.safety.safety_fwd_hook(b, make_msg(b, a, 8))) - -def test_manually_enable_controls_allowed(test): - test.safety.set_controls_allowed(1) - test.assertTrue(test.safety.get_controls_allowed()) - test.safety.set_controls_allowed(0) - test.assertFalse(test.safety.get_controls_allowed()) - -def test_spam_can_buses(test, TX_MSGS): - for addr in range(1, 0x800): - for bus in range(0, 4): - if all(addr != m[0] or bus != m[1] for m in TX_MSGS): - test.assertFalse(test.safety.safety_tx_hook(make_msg(bus, addr, 8))) +class StdTest: + @staticmethod + def test_relay_malfunction(test, addr, bus=0): + # input is a test class and the address that, if seen on specified bus, triggers + # the relay_malfunction protection logic: both tx_hook and fwd_hook are + # expected to return failure + test.assertFalse(test.safety.get_relay_malfunction()) + test.safety.safety_rx_hook(make_msg(bus, addr, 8)) + test.assertTrue(test.safety.get_relay_malfunction()) + for a in range(1, 0x800): + for b in range(0, 3): + test.assertFalse(test.safety.safety_tx_hook(make_msg(b, a, 8))) + test.assertEqual(-1, test.safety.safety_fwd_hook(b, make_msg(b, a, 8))) + + @staticmethod + def test_manually_enable_controls_allowed(test): + test.safety.set_controls_allowed(1) + test.assertTrue(test.safety.get_controls_allowed()) + test.safety.set_controls_allowed(0) + test.assertFalse(test.safety.get_controls_allowed()) + + @staticmethod + def test_spam_can_buses(test, TX_MSGS): + for addr in range(1, 0x800): + for bus in range(0, 4): + if all(addr != m[0] or bus != m[1] for m in TX_MSGS): + test.assertFalse(test.safety.safety_tx_hook(make_msg(bus, addr, 8))) + + @staticmethod + def test_allow_brake_at_zero_speed(test): + # Brake was already pressed + test.safety.safety_rx_hook(test._speed_msg(0)) + test.safety.safety_rx_hook(test._brake_msg(1)) + test.safety.set_controls_allowed(1) + test.safety.safety_rx_hook(test._brake_msg(1)) + test.assertTrue(test.safety.get_controls_allowed()) + test.safety.safety_rx_hook(test._brake_msg(0)) + test.assertTrue(test.safety.get_controls_allowed()) + # rising edge of brake should disengage + test.safety.safety_rx_hook(test._brake_msg(1)) + test.assertFalse(test.safety.get_controls_allowed()) + test.safety.safety_rx_hook(test._brake_msg(0)) # reset no brakes + + @staticmethod + def test_not_allow_brake_when_moving(test, standstill_threshold): + # Brake was already pressed + test.safety.safety_rx_hook(test._brake_msg(1)) + test.safety.set_controls_allowed(1) + test.safety.safety_rx_hook(test._speed_msg(standstill_threshold)) + test.safety.safety_rx_hook(test._brake_msg(1)) + test.assertTrue(test.safety.get_controls_allowed()) + test.safety.safety_rx_hook(test._speed_msg(standstill_threshold + 1)) + test.safety.safety_rx_hook(test._brake_msg(1)) + test.assertFalse(test.safety.get_controls_allowed()) + test.safety.safety_rx_hook(test._speed_msg(0)) diff --git a/tests/safety/test_cadillac.py b/tests/safety/test_cadillac.py index 7806fb7b285cc1..e4e75f15bdf4a9 100644 --- a/tests/safety/test_cadillac.py +++ b/tests/safety/test_cadillac.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import make_msg, StdTest MAX_RATE_UP = 2 @@ -56,13 +56,13 @@ def _torque_msg(self, torque): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): to_push = make_msg(0, 0x370) diff --git a/tests/safety/test_chrysler.py b/tests/safety/test_chrysler.py index ec431e9cafa8ed..c66b90b6ba3eb0 100755 --- a/tests/safety/test_chrysler.py +++ b/tests/safety/test_chrysler.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import StdTest, make_msg MAX_RATE_UP = 3 MAX_RATE_DOWN = 3 @@ -107,10 +107,10 @@ def _torque_msg(self, torque): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, 0x292) + StdTest.test_relay_malfunction(self, 0x292) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) @@ -126,7 +126,7 @@ def test_steer_safety_check(self): self.assertTrue(self.safety.safety_tx_hook(self._torque_msg(t))) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): to_push = self._cruise_msg(True) @@ -149,29 +149,9 @@ def test_gas_disable(self): self.safety.safety_rx_hook(self._gas_msg(1)) self.assertFalse(self.safety.get_controls_allowed()) - def test_brake_disable(self): - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.safety_rx_hook(self._speed_msg(0)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertTrue(self.safety.get_controls_allowed()) - - self.safety.safety_rx_hook(self._brake_msg(False)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.safety_rx_hook(self._speed_msg(1)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - self.safety.safety_rx_hook(self._speed_msg(1)) - self.safety.safety_rx_hook(self._brake_msg(False)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, 0) def test_non_realtime_limit_up(self): self.safety.set_controls_allowed(True) diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index 06872dede5b1e4..34d92357c79d9f 100644 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import StdTest, make_msg MAX_RATE_UP = 7 MAX_RATE_DOWN = 17 @@ -90,10 +90,10 @@ def _torque_msg(self, torque): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, 384) + StdTest.test_relay_malfunction(self, 384) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) @@ -116,29 +116,9 @@ def test_cancel_button(self): self.safety.safety_rx_hook(self._button_msg(CANCEL_BTN)) self.assertFalse(self.safety.get_controls_allowed()) - def test_disengage_on_brake(self): - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(False)) - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.safety_rx_hook(self._speed_msg(100)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(False)) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, 0) def test_disengage_on_gas(self): self.safety.set_controls_allowed(1) @@ -182,7 +162,7 @@ def test_steer_safety_check(self): self.assertTrue(self.safety.safety_tx_hook(self._torque_msg(t))) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_non_realtime_limit_up(self): self.safety.set_gm_torque_driver(0, 0) diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index 52aaa84dfa1057..b4f8f9d5d7333b 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -3,9 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, \ - test_manually_enable_controls_allowed, \ - test_spam_can_buses, MAX_WRONG_COUNTERS +from panda.tests.safety.common import StdTest, make_msg, MAX_WRONG_COUNTERS MAX_BRAKE = 255 @@ -110,18 +108,18 @@ def test_spam_can_buses(self): tx_msgs = BH_TX_MSGS elif hw_type == HONDA_BG_HW: tx_msgs = BG_TX_MSGS - test_spam_can_buses(self, tx_msgs) + StdTest.test_spam_can_buses(self, tx_msgs) def test_relay_malfunction(self): hw = self.safety.get_honda_hw() bus = 2 if hw == HONDA_BG_HW else 0 - test_relay_malfunction(self, 0xE4, bus=bus) + StdTest.test_relay_malfunction(self, 0xE4, bus=bus) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_resume_button(self): RESUME_BTN = 4 @@ -167,23 +165,9 @@ def test_alt_disengage_on_brake(self): self.safety.safety_rx_hook(self._alt_brake_msg(1)) self.assertTrue(self.safety.get_controls_allowed()) - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(False)) # reset no brakes - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.safety_rx_hook(self._speed_msg(100)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, 0) def test_prev_gas(self): self.safety.safety_rx_hook(self._gas_msg(False)) diff --git a/tests/safety/test_hyundai.py b/tests/safety/test_hyundai.py index 8dd9f9e6b74765..2d022759e102f2 100644 --- a/tests/safety/test_hyundai.py +++ b/tests/safety/test_hyundai.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import StdTest, make_msg MAX_RATE_UP = 3 MAX_RATE_DOWN = 7 @@ -74,10 +74,10 @@ def _torque_msg(self, torque): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, 832) + StdTest.test_relay_malfunction(self, 832) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) @@ -93,7 +93,7 @@ def test_steer_safety_check(self): self.assertTrue(self.safety.safety_tx_hook(self._torque_msg(t))) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): to_push = make_msg(0, 1057) @@ -114,30 +114,9 @@ def test_disengage_on_gas(self): self.safety.safety_rx_hook(self._gas_msg(1)) self.assertFalse(self.safety.get_controls_allowed()) - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(1)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(0)) - self.assertTrue(self.safety.get_controls_allowed()) - # rising edge of brake should disengage - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertFalse(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(0)) # reset no brakes - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(1)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._speed_msg(SPEED_THRESHOLD)) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._speed_msg(SPEED_THRESHOLD + 1)) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertFalse(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._speed_msg(0)) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, SPEED_THRESHOLD) def test_non_realtime_limit_up(self): self.safety.set_hyundai_torque_driver(0, 0) diff --git a/tests/safety/test_nissan.py b/tests/safety/test_nissan.py index 15682083b7363e..d38db55951a8f1 100644 --- a/tests/safety/test_nissan.py +++ b/tests/safety/test_nissan.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import make_msg, test_relay_malfunction +from panda.tests.safety.common import StdTest, make_msg ANGLE_MAX_BP = [1.3, 10., 30.] ANGLE_MAX_V = [540., 120., 23.] @@ -72,7 +72,7 @@ def _speed_msg(self, speed): return to_send - def _send_brake_cmd(self, brake): + def _brake_msg(self, brake): to_send = make_msg(1, 0x454) to_send[0].RDLR = ((brake & 0x1) << 23) @@ -144,29 +144,9 @@ def test_angle_cmd_when_disabled(self): self.assertFalse(self.safety.safety_tx_hook(self._lkas_control_msg(0, 1))) self.assertFalse(self.safety.get_controls_allowed()) - def test_brake_rising_edge(self): - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._speed_msg(0)) - self._set_brake_prev(True) - self.safety.safety_rx_hook(self._send_brake_cmd(True)) - self.assertTrue(self.safety.get_controls_allowed()) - - self.safety.set_controls_allowed(1) - self._set_brake_prev(False) - self.safety.safety_rx_hook(self._send_brake_cmd(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._speed_msg(1)) - self._set_brake_prev(True) - self.safety.safety_rx_hook(self._send_brake_cmd(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._speed_msg(1)) - self._set_brake_prev(False) - self.safety.safety_rx_hook(self._send_brake_cmd(True)) - self.assertFalse(self.safety.get_controls_allowed()) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, 0) def test_gas_rising_edge(self): self.safety.set_controls_allowed(1) @@ -193,7 +173,7 @@ def test_acc_buttons(self): self.assertFalse(self.safety.get_controls_allowed()) def test_relay_malfunction(self): - test_relay_malfunction(self, 0x169) + StdTest.test_relay_malfunction(self, 0x169) if __name__ == "__main__": unittest.main() diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 4ae4bd2ba2a73e..d68090d19eb4d9 100644 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import StdTest, make_msg MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 @@ -131,10 +131,10 @@ def _set_torque_driver(self, min_t, max_t): self.safety.safety_rx_hook(self._torque_driver_msg(max_t)) def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS if self.safety.get_subaru_global() else TX_L_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS if self.safety.get_subaru_global() else TX_L_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, 0x122 if self.safety.get_subaru_global() else 0x164) + StdTest.test_relay_malfunction(self, 0x122 if self.safety.get_subaru_global() else 0x164) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) @@ -155,32 +155,10 @@ def test_disengage_on_gas(self): self.safety.safety_rx_hook(self._gas_msg(1)) self.assertFalse(self.safety.get_controls_allowed()) - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed + def test_brake_disengage(self): if (self.safety.get_subaru_global()): - self.safety.safety_rx_hook(self._brake_msg(1)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(0)) - self.assertTrue(self.safety.get_controls_allowed()) - # rising edge of brake should disengage - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertFalse(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(0)) # reset no brakes - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - if (self.safety.get_subaru_global()): - self.safety.safety_rx_hook(self._brake_msg(1)) - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._speed_msg(SPEED_THRESHOLD)) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._speed_msg(SPEED_THRESHOLD + 1)) - self.safety.safety_rx_hook(self._brake_msg(1)) - self.assertFalse(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._speed_msg(0)) + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, SPEED_THRESHOLD) def test_steer_safety_check(self): for enabled in [0, 1]: @@ -193,7 +171,7 @@ def test_steer_safety_check(self): self.assertTrue(self.safety.safety_tx_hook(self._torque_msg(t))) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_non_realtime_limit_up(self): self._set_torque_driver(0, 0) diff --git a/tests/safety/test_toyota.py b/tests/safety/test_toyota.py index a7506c0029be63..5426e5b97d7cd0 100644 --- a/tests/safety/test_toyota.py +++ b/tests/safety/test_toyota.py @@ -3,7 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, test_manually_enable_controls_allowed, test_spam_can_buses +from panda.tests.safety.common import StdTest, make_msg MAX_RATE_UP = 10 MAX_RATE_DOWN = 25 @@ -112,16 +112,16 @@ def _pcm_cruise_msg(self, cruise_on): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, 0x2E4) + StdTest.test_relay_malfunction(self, 0x2E4) def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): self.safety.safety_rx_hook(self._pcm_cruise_msg(False)) @@ -171,24 +171,9 @@ def test_disengage_on_gas_interceptor(self): self.safety.safety_rx_hook(self._send_interceptor_msg(0, 0x201)) self.safety.set_gas_interceptor_detected(False) - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._speed_msg(0)) - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._brake_msg(False)) # reset no brakes - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._brake_msg(True)) - self.safety.safety_rx_hook(self._speed_msg(STANDSTILL_THRESHOLD + 1)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._brake_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, STANDSTILL_THRESHOLD) def test_allow_engage_with_gas_interceptor_pressed(self): self.safety.safety_rx_hook(self._send_interceptor_msg(0x1000, 0x201)) @@ -339,6 +324,5 @@ def test_fwd_hook(self): self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8))) - if __name__ == "__main__": unittest.main() diff --git a/tests/safety/test_volkswagen_mqb.py b/tests/safety/test_volkswagen_mqb.py index 026116c74feafe..99280ba1a0fad5 100644 --- a/tests/safety/test_volkswagen_mqb.py +++ b/tests/safety/test_volkswagen_mqb.py @@ -4,8 +4,7 @@ import crcmod from panda import Panda from panda.tests.safety import libpandasafety_py -from panda.tests.safety.common import test_relay_malfunction, make_msg, \ - test_manually_enable_controls_allowed, test_spam_can_buses, MAX_WRONG_COUNTERS +from panda.tests.safety.common import StdTest, make_msg, MAX_WRONG_COUNTERS MAX_RATE_UP = 4 MAX_RATE_DOWN = 10 @@ -78,16 +77,16 @@ def _set_prev_torque(self, t): self.safety.set_volkswagen_desired_torque_last(t) self.safety.set_volkswagen_rt_torque_last(t) - # Wheel speeds - def _esp_19_msg(self, speed): + # Wheel speeds _esp_19_msg + def _speed_msg(self, speed): wheel_speed_scaled = int(speed / 0.0075) to_send = make_msg(0, MSG_ESP_19) to_send[0].RDLR = wheel_speed_scaled | (wheel_speed_scaled << 16) to_send[0].RDHR = wheel_speed_scaled | (wheel_speed_scaled << 16) return to_send - # Brake light switch - def _esp_05_msg(self, brake): + # Brake light switch _esp_05_msg + def _brake_msg(self, brake): to_send = make_msg(0, MSG_ESP_05) to_send[0].RDLR = (0x1 << 26) if brake else 0 to_send[0].RDLR |= (self.cnt_esp_05 % 16) << 8 @@ -147,10 +146,10 @@ def _gra_acc_01_msg(self, bit): return to_send def test_spam_can_buses(self): - test_spam_can_buses(self, TX_MSGS) + StdTest.test_spam_can_buses(self, TX_MSGS) def test_relay_malfunction(self): - test_relay_malfunction(self, MSG_HCA_01) + StdTest.test_relay_malfunction(self, MSG_HCA_01) def test_prev_gas(self): for g in range(0, 256): @@ -172,45 +171,26 @@ def test_disable_control_allowed_from_cruise(self): def test_sample_speed(self): # Stationary - self.safety.safety_rx_hook(self._esp_19_msg(0)) + self.safety.safety_rx_hook(self._speed_msg(0)) self.assertEqual(0, self.safety.get_volkswagen_moving()) # 1 km/h, just under 0.3 m/s safety grace threshold - self.safety.safety_rx_hook(self._esp_19_msg(1)) + self.safety.safety_rx_hook(self._speed_msg(1)) self.assertEqual(0, self.safety.get_volkswagen_moving()) # 2 km/h, just over 0.3 m/s safety grace threshold - self.safety.safety_rx_hook(self._esp_19_msg(2)) + self.safety.safety_rx_hook(self._speed_msg(2)) self.assertEqual(1, self.safety.get_volkswagen_moving()) # 144 km/h, openpilot V_CRUISE_MAX - self.safety.safety_rx_hook(self._esp_19_msg(144)) + self.safety.safety_rx_hook(self._speed_msg(144)) self.assertEqual(1, self.safety.get_volkswagen_moving()) def test_prev_brake(self): self.assertFalse(self.safety.get_volkswagen_brake_pressed_prev()) - self.safety.safety_rx_hook(self._esp_05_msg(True)) + self.safety.safety_rx_hook(self._brake_msg(True)) self.assertTrue(self.safety.get_volkswagen_brake_pressed_prev()) - def test_disengage_on_brake(self): - self.safety.set_controls_allowed(1) - self.safety.safety_rx_hook(self._esp_05_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) - - def test_allow_brake_at_zero_speed(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._esp_05_msg(True)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._esp_05_msg(True)) - self.assertTrue(self.safety.get_controls_allowed()) - self.safety.safety_rx_hook(self._esp_05_msg(False)) # reset no brakes - - def test_not_allow_brake_when_moving(self): - # Brake was already pressed - self.safety.safety_rx_hook(self._esp_05_msg(True)) - self.safety.safety_rx_hook(self._esp_19_msg(100)) - self.safety.set_controls_allowed(1) - - self.safety.safety_rx_hook(self._esp_05_msg(True)) - self.assertFalse(self.safety.get_controls_allowed()) + def test_brake_disengage(self): + StdTest.test_allow_brake_at_zero_speed(self) + StdTest.test_not_allow_brake_when_moving(self, 1) def test_disengage_on_gas(self): self.safety.safety_rx_hook(self._motor_20_msg(0)) @@ -237,7 +217,7 @@ def test_steer_safety_check(self): self.assertTrue(self.safety.safety_tx_hook(self._hca_01_msg(t))) def test_manually_enable_controls_allowed(self): - test_manually_enable_controls_allowed(self) + StdTest.test_manually_enable_controls_allowed(self) def test_spam_cancel_safety_check(self): BIT_CANCEL = 13 @@ -356,7 +336,7 @@ def test_rx_hook(self): if msg == MSG_EPS_01: to_push = self._eps_01_msg(0) if msg == MSG_ESP_05: - to_push = self._esp_05_msg(False) + to_push = self._brake_msg(False) if msg == MSG_TSK_06: to_push = self._tsk_06_msg(3) if msg == MSG_MOTOR_20: @@ -376,12 +356,12 @@ def test_rx_hook(self): if i < MAX_WRONG_COUNTERS: self.safety.set_controls_allowed(1) self.safety.safety_rx_hook(self._eps_01_msg(0)) - self.safety.safety_rx_hook(self._esp_05_msg(False)) + self.safety.safety_rx_hook(self._brake_msg(False)) self.safety.safety_rx_hook(self._tsk_06_msg(3)) self.safety.safety_rx_hook(self._motor_20_msg(0)) else: self.assertFalse(self.safety.safety_rx_hook(self._eps_01_msg(0))) - self.assertFalse(self.safety.safety_rx_hook(self._esp_05_msg(False))) + self.assertFalse(self.safety.safety_rx_hook(self._brake_msg(False))) self.assertFalse(self.safety.safety_rx_hook(self._tsk_06_msg(3))) self.assertFalse(self.safety.safety_rx_hook(self._motor_20_msg(0))) self.assertFalse(self.safety.get_controls_allowed()) @@ -390,7 +370,7 @@ def test_rx_hook(self): for i in range(2): self.safety.set_controls_allowed(1) self.safety.safety_rx_hook(self._eps_01_msg(0)) - self.safety.safety_rx_hook(self._esp_05_msg(False)) + self.safety.safety_rx_hook(self._brake_msg(False)) self.safety.safety_rx_hook(self._tsk_06_msg(3)) self.safety.safety_rx_hook(self._motor_20_msg(0)) self.assertTrue(self.safety.get_controls_allowed())