File: test_socketcan_helpers.py

package info (click to toggle)
python-can 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 25,840; makefile: 38; sh: 20
file content (66 lines) | stat: -rw-r--r-- 2,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python

"""
Tests helpers in `can.interfaces.socketcan.socketcan_common`.
"""

import unittest
from pathlib import Path
from unittest import mock

from can.interfaces.socketcan.utils import error_code_to_str, find_available_interfaces

from .config import IS_LINUX, TEST_INTERFACE_SOCKETCAN


class TestSocketCanHelpers(unittest.TestCase):
    @unittest.skipUnless(IS_LINUX, "socketcan is only available on Linux")
    def test_error_code_to_str(self):
        """
        Check that the function does not crash & always
        returns at least one character.
        """

        # all possible & also some invalid error codes
        test_data = list(range(0, 256)) + [-1, 256, 5235, 346264, None]

        for error_code in test_data:
            string = error_code_to_str(error_code)
            self.assertTrue(string)  # not None or empty

    @unittest.skipUnless(
        TEST_INTERFACE_SOCKETCAN, "socketcan is only available on Linux"
    )
    def test_find_available_interfaces(self):
        result = find_available_interfaces()

        self.assertGreaterEqual(len(result), 3)
        self.assertIn("vcan0", result)
        self.assertIn("vxcan0", result)
        self.assertIn("slcan0", result)

    def test_find_available_interfaces_w_patch(self):
        # Contains lo, eth0, wlan0, vcan0, mycustomCan123
        ip_output = (Path(__file__).parent / "data" / "ip_link_list.json").read_text()

        with mock.patch("subprocess.check_output") as check_output:
            check_output.return_value = ip_output

            with mock.patch("sys.platform", "linux"):
                ifs = find_available_interfaces()

        self.assertEqual(["vcan0", "mycustomCan123"], ifs)

    def test_find_available_interfaces_exception(self):
        with mock.patch("subprocess.check_output") as check_output:
            check_output.return_value = "<h1>Not JSON</h1>"
            result = find_available_interfaces()
            self.assertEqual([], result)

            check_output.side_effect = Exception("Something went wrong :-/")
            result = find_available_interfaces()
            self.assertEqual([], result)


if __name__ == "__main__":
    unittest.main()