diff --git a/test/data/logfile_errorframes.asc b/test/data/logfile_errorframes.asc new file mode 100644 index 000000000..bcb5584a7 --- /dev/null +++ b/test/data/logfile_errorframes.asc @@ -0,0 +1,21 @@ +date Sam Sep 30 15:06:13.191 2017 +base hex timestamps absolute +internal events logged +// version 9.0.0 +Begin Triggerblock Sam Sep 30 15:06:13.191 2017 + 0.000000 Start of measurement + 0.015991 CAN 1 Status:chip status error passive - TxErr: 132 RxErr: 0 + 0.015991 CAN 2 Status:chip status error active + 2.501000 1 ErrorFrame + 2.501010 1 ErrorFrame ECC: 10100010 + 2.501020 2 ErrorFrame Flags = 0xe CodeExt = 0x20a2 Code = 0x82 ID = 0 DLC = 0 Position = 5 Length = 11300 + 2.520002 3 200 Tx r Length = 1704000 BitCount = 145 ID = 88888888x + 2.584921 4 300 Tx r 8 Length = 1704000 BitCount = 145 ID = 88888888x + 3.098426 1 18EBFF00x Rx d 8 01 A0 0F A6 60 3B D1 40 Length = 273910 BitCount = 141 ID = 418119424x + 3.197693 1 18EBFF00x Rx d 8 03 E1 00 4B FF FF 3C 0F Length = 283910 BitCount = 146 ID = 418119424x + 17.876976 1 6F8 Rx d 8 FF 00 0C FE 00 00 00 00 Length = 239910 BitCount = 124 ID = 1784 + 20.105214 2 18EBFF00x Rx d 8 01 A0 0F A6 60 3B D1 40 Length = 273925 BitCount = 141 ID = 418119424x + 20.155119 2 18EBFF00x Rx d 8 02 1F DE 80 25 DF C0 2B Length = 272152 BitCount = 140 ID = 418119424x + 20.204671 2 18EBFF00x Rx d 8 03 E1 00 4B FF FF 3C 0F Length = 283910 BitCount = 146 ID = 418119424x + 20.248887 2 18EBFF00x Rx d 8 04 00 4B FF FF FF FF FF Length = 283925 BitCount = 146 ID = 418119424x +End TriggerBlock diff --git a/test/test_player.py b/test/test_player.py new file mode 100755 index 000000000..2f3307420 --- /dev/null +++ b/test/test_player.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python + +""" +This module tests the functions inside of player.py +""" + +import unittest +from unittest import mock +from unittest.mock import Mock +import os +import sys +import io +import can +import can.player + + +class TestPlayerScriptModule(unittest.TestCase): + def setUp(self) -> None: + # Patch VirtualBus object + patcher_virtual_bus = mock.patch("can.interfaces.virtual.VirtualBus", spec=True) + self.MockVirtualBus = patcher_virtual_bus.start() + self.addCleanup(patcher_virtual_bus.stop) + self.mock_virtual_bus = self.MockVirtualBus.return_value + self.mock_virtual_bus.__enter__ = Mock(return_value=self.mock_virtual_bus) + + # Patch time sleep object + patcher_sleep = mock.patch("can.io.player.sleep", spec=True) + self.MockSleep = patcher_sleep.start() + self.addCleanup(patcher_sleep.stop) + + self.baseargs = [sys.argv[0], "-i", "virtual"] + self.logfile = os.path.join( + os.path.dirname(__file__), "data", "test_CanMessage.asc" + ) + + def assertSuccessfulCleanup(self): + self.MockVirtualBus.assert_called_once() + self.mock_virtual_bus.__exit__.assert_called_once() + + def test_play_virtual(self): + sys.argv = self.baseargs + [self.logfile] + can.player.main() + msg1 = can.Message( + timestamp=2.501, + arbitration_id=0xC8, + is_extended_id=False, + is_fd=False, + is_rx=False, + channel=1, + dlc=8, + data=[0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2], + ) + msg2 = can.Message( + timestamp=17.876708, + arbitration_id=0x6F9, + is_extended_id=False, + is_fd=False, + is_rx=True, + channel=0, + dlc=8, + data=[0x5, 0xC, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0], + ) + self.assertEqual(self.MockSleep.call_count, 2) + if sys.version_info >= (3, 8): + # The args argument was introduced with python 3.8 + self.assertTrue( + msg1.equals(self.mock_virtual_bus.send.mock_calls[0].args[0]) + ) + self.assertTrue( + msg2.equals(self.mock_virtual_bus.send.mock_calls[1].args[0]) + ) + self.assertSuccessfulCleanup() + + def test_play_virtual_verbose(self): + sys.argv = self.baseargs + ["-v", self.logfile] + with unittest.mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: + can.player.main() + self.assertIn("09 08 07 06 05 04 03 02", mock_stdout.getvalue()) + self.assertIn("05 0c 00 00 00 00 00 00", mock_stdout.getvalue()) + self.assertEqual(self.mock_virtual_bus.send.call_count, 2) + self.assertEqual(self.MockSleep.call_count, 2) + self.assertSuccessfulCleanup() + + def test_play_virtual_exit(self): + self.MockSleep.side_effect = [None, KeyboardInterrupt] + + sys.argv = self.baseargs + [self.logfile] + can.player.main() + self.assertEqual(self.mock_virtual_bus.send.call_count, 1) + self.assertEqual(self.MockSleep.call_count, 2) + self.assertSuccessfulCleanup() + + def test_play_skip_error_frame(self): + logfile = os.path.join( + os.path.dirname(__file__), "data", "logfile_errorframes.asc" + ) + sys.argv = self.baseargs + ["-v", logfile] + can.player.main() + self.assertEqual(self.mock_virtual_bus.send.call_count, 9) + self.assertEqual(self.MockSleep.call_count, 12) + self.assertSuccessfulCleanup() + + def test_play_error_frame(self): + logfile = os.path.join( + os.path.dirname(__file__), "data", "logfile_errorframes.asc" + ) + sys.argv = self.baseargs + ["-v", "--error-frames", logfile] + can.player.main() + self.assertEqual(self.mock_virtual_bus.send.call_count, 12) + self.assertEqual(self.MockSleep.call_count, 12) + self.assertSuccessfulCleanup() + + +if __name__ == "__main__": + unittest.main()