Skip to content

Commit

Permalink
clean up safety tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbiasini committed Nov 15, 2019
1 parent e8f7a3b commit 8af1a01
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 95 deletions.
14 changes: 12 additions & 2 deletions tests/safety/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from panda.tests.safety import libpandasafety_py

def make_msg(bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4

return to_send

def test_relay_malfunction(test, addr):
# input is a test class and the address that, if seen on bus 0, triggers
# the relay_malfunction logic
test.assertFalse(test.safety.get_relay_malfunction())
test.safety.safety_rx_hook(test._send_msg(0, addr, 8))
test.safety.safety_rx_hook(make_msg(0, 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(test._send_msg(b, a, 8)))
test.assertFalse(test.safety.safety_tx_hook(make_msg(b, a, 8)))
13 changes: 4 additions & 9 deletions tests/safety/test_cadillac.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import make_msg


MAX_RATE_UP = 2
MAX_RATE_DOWN = 5
Expand Down Expand Up @@ -35,13 +37,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_CADILLAC, 0)
cls.safety.init_tests_cadillac()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _set_prev_torque(self, t):
self.safety.set_cadillac_desired_torque_last(t)
self.safety.set_cadillac_rt_torque_last(t)
Expand Down Expand Up @@ -190,7 +185,7 @@ def test_fwd_hook(self):
for b in buss:
for m in msgs:
# assume len 8
self.assertEqual(-1, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(-1, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_chrysler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 3
MAX_RATE_DOWN = 3
Expand Down Expand Up @@ -38,13 +38,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_CHRYSLER, 0)
cls.safety.init_tests_chrysler()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _button_msg(self, buttons):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = 1265 << 21
Expand Down Expand Up @@ -204,7 +197,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_gm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 7
MAX_RATE_DOWN = 17
Expand Down Expand Up @@ -37,13 +37,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_GM, 0)
cls.safety.init_tests_gm()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _speed_msg(self, speed):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = 842 << 21
Expand Down Expand Up @@ -293,7 +286,7 @@ def test_fwd_hook(self):
for b in buss:
for m in msgs:
# assume len 8
self.assertEqual(-1, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(-1, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
14 changes: 3 additions & 11 deletions tests/safety/test_honda.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_BRAKE = 255

Expand All @@ -16,14 +16,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_HONDA, 0)
cls.safety.init_tests_honda()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4

return to_send

def _speed_msg(self, speed):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = 0x158 << 21
Expand Down Expand Up @@ -291,7 +283,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))

self.safety.set_long_controls_allowed(True)
self.safety.set_honda_fwd_brake(False)
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_honda_bosch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
import unittest
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import make_msg

MAX_BRAKE = 255

Expand All @@ -12,14 +13,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_HONDA_BOSCH, 0)
cls.safety.init_tests_honda()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4

return to_send

def test_fwd_hook(self):
buss = range(0x0, 0x3)
msgs = range(0x1, 0x800)
Expand All @@ -46,7 +39,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_hyundai.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 3
MAX_RATE_DOWN = 7
Expand Down Expand Up @@ -34,13 +34,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI, 0)
cls.safety.init_tests_hyundai()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _button_msg(self, buttons):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = 1265 << 21
Expand Down Expand Up @@ -214,7 +207,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_subaru.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 50
MAX_RATE_DOWN = 70
Expand Down Expand Up @@ -34,13 +34,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_SUBARU, 0)
cls.safety.init_tests_subaru()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _set_prev_torque(self, t):
self.safety.set_subaru_desired_torque_last(t)
self.safety.set_subaru_rt_torque_last(t)
Expand Down Expand Up @@ -198,7 +191,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down
13 changes: 3 additions & 10 deletions tests/safety/test_toyota.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 10
MAX_RATE_DOWN = 25
Expand Down Expand Up @@ -38,13 +38,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_TOYOTA, 100)
cls.safety.init_tests_toyota()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _set_prev_torque(self, t):
self.safety.set_toyota_desired_torque_last(t)
self.safety.set_toyota_rt_torque_last(t)
Expand Down Expand Up @@ -311,7 +304,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))

self.safety.set_long_controls_allowed(True)

Expand Down
2 changes: 1 addition & 1 deletion tests/safety/test_toyota_ipas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety import libpandasafety_py

IPAS_OVERRIDE_THRESHOLD = 200

Expand Down
15 changes: 3 additions & 12 deletions tests/safety/test_volkswagen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
import unittest
import numpy as np
import libpandasafety_py # pylint: disable=import-error
from panda import Panda
from panda.tests.safety.common import test_relay_malfunction
from panda.tests.safety import libpandasafety_py
from panda.tests.safety.common import test_relay_malfunction, make_msg

MAX_RATE_UP = 4
MAX_RATE_DOWN = 10
Expand All @@ -28,13 +28,6 @@ def setUp(cls):
cls.safety.set_safety_hooks(Panda.SAFETY_VOLKSWAGEN, 0)
cls.safety.init_tests_volkswagen()

def _send_msg(self, bus, addr, length):
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
to_send[0].RIR = addr << 21
to_send[0].RDTR = length
to_send[0].RDTR = bus << 4
return to_send

def _set_prev_torque(self, t):
self.safety.set_volkswagen_desired_torque_last(t)
self.safety.set_volkswagen_rt_torque_last(t)
Expand Down Expand Up @@ -250,9 +243,7 @@ def test_fwd_hook(self):
fwd_bus = -1

# assume len 8
if fwd_bus != self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)):
print(b, hex(m), fwd_bus)
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, self._send_msg(b, m, 8)))
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(b, make_msg(b, m, 8)))


if __name__ == "__main__":
Expand Down

0 comments on commit 8af1a01

Please sign in to comment.