|
| 1 | +""" |
| 2 | +Test functions in `can.interfaces.socketcan.socketcan`. |
| 3 | +""" |
| 4 | +import unittest |
| 5 | + |
| 6 | +try: |
| 7 | + from unittest.mock import Mock |
| 8 | + from unittest.mock import patch |
| 9 | + from unittest.mock import call |
| 10 | +except ImportError: |
| 11 | + from mock import Mock |
| 12 | + from mock import patch |
| 13 | + from mock import call |
| 14 | + |
| 15 | +import ctypes |
| 16 | + |
| 17 | +from can.interfaces.socketcan.socketcan import bcm_header_factory |
| 18 | + |
| 19 | + |
| 20 | +class SocketCANTest(unittest.TestCase): |
| 21 | + def setUp(self): |
| 22 | + self._ctypes_sizeof = ctypes.sizeof |
| 23 | + self._ctypes_alignment = ctypes.alignment |
| 24 | + |
| 25 | + @patch("ctypes.sizeof") |
| 26 | + @patch("ctypes.alignment") |
| 27 | + def test_bcm_header_factory_32_bit_sizeof_long_4_alignof_long_4( |
| 28 | + self, ctypes_sizeof, ctypes_alignment |
| 29 | + ): |
| 30 | + """This tests a 32-bit platform (ex. Debian Stretch on i386), where: |
| 31 | +
|
| 32 | + * sizeof(long) == 4 |
| 33 | + * sizeof(long long) == 8 |
| 34 | + * alignof(long) == 4 |
| 35 | + * alignof(long long) == 4 |
| 36 | + """ |
| 37 | + |
| 38 | + def side_effect_ctypes_sizeof(value): |
| 39 | + type_to_size = { |
| 40 | + ctypes.c_longlong: 8, |
| 41 | + ctypes.c_long: 4, |
| 42 | + ctypes.c_uint8: 1, |
| 43 | + ctypes.c_uint16: 2, |
| 44 | + ctypes.c_uint32: 4, |
| 45 | + ctypes.c_uint64: 8, |
| 46 | + } |
| 47 | + return type_to_size[value] |
| 48 | + |
| 49 | + def side_effect_ctypes_alignment(value): |
| 50 | + type_to_alignment = { |
| 51 | + ctypes.c_longlong: 4, |
| 52 | + ctypes.c_long: 4, |
| 53 | + ctypes.c_uint8: 1, |
| 54 | + ctypes.c_uint16: 2, |
| 55 | + ctypes.c_uint32: 4, |
| 56 | + ctypes.c_uint64: 4, |
| 57 | + } |
| 58 | + return type_to_alignment[value] |
| 59 | + |
| 60 | + ctypes_sizeof.side_effect = side_effect_ctypes_sizeof |
| 61 | + ctypes_alignment.side_effect = side_effect_ctypes_alignment |
| 62 | + |
| 63 | + fields = [ |
| 64 | + ("opcode", ctypes.c_uint32), |
| 65 | + ("flags", ctypes.c_uint32), |
| 66 | + ("count", ctypes.c_uint32), |
| 67 | + ("ival1_tv_sec", ctypes.c_long), |
| 68 | + ("ival1_tv_usec", ctypes.c_long), |
| 69 | + ("ival2_tv_sec", ctypes.c_long), |
| 70 | + ("ival2_tv_usec", ctypes.c_long), |
| 71 | + ("can_id", ctypes.c_uint32), |
| 72 | + ("nframes", ctypes.c_uint32), |
| 73 | + ] |
| 74 | + BcmMsgHead = bcm_header_factory(fields) |
| 75 | + |
| 76 | + expected_fields = [ |
| 77 | + ("opcode", ctypes.c_uint32), |
| 78 | + ("flags", ctypes.c_uint32), |
| 79 | + ("count", ctypes.c_uint32), |
| 80 | + ("ival1_tv_sec", ctypes.c_long), |
| 81 | + ("ival1_tv_usec", ctypes.c_long), |
| 82 | + ("ival2_tv_sec", ctypes.c_long), |
| 83 | + ("ival2_tv_usec", ctypes.c_long), |
| 84 | + ("can_id", ctypes.c_uint32), |
| 85 | + ("nframes", ctypes.c_uint32), |
| 86 | + # We expect 4 bytes of padding |
| 87 | + ("pad_0", ctypes.c_uint8), |
| 88 | + ("pad_1", ctypes.c_uint8), |
| 89 | + ("pad_2", ctypes.c_uint8), |
| 90 | + ("pad_3", ctypes.c_uint8), |
| 91 | + ] |
| 92 | + self.assertEqual(expected_fields, BcmMsgHead._fields_) |
| 93 | + |
| 94 | + @patch("ctypes.sizeof") |
| 95 | + @patch("ctypes.alignment") |
| 96 | + def test_bcm_header_factory_32_bit_sizeof_long_4_alignof_long_8( |
| 97 | + self, ctypes_sizeof, ctypes_alignment |
| 98 | + ): |
| 99 | + """This tests a 32-bit platform (ex. Raspbian Stretch on armv7l), where: |
| 100 | +
|
| 101 | + * sizeof(long) == 4 |
| 102 | + * sizeof(long long) == 8 |
| 103 | + * alignof(long) == 4 |
| 104 | + * alignof(long long) == 8 |
| 105 | + """ |
| 106 | + |
| 107 | + def side_effect_ctypes_sizeof(value): |
| 108 | + type_to_size = { |
| 109 | + ctypes.c_longlong: 8, |
| 110 | + ctypes.c_long: 4, |
| 111 | + ctypes.c_uint8: 1, |
| 112 | + ctypes.c_uint16: 2, |
| 113 | + ctypes.c_uint32: 4, |
| 114 | + ctypes.c_uint64: 8, |
| 115 | + } |
| 116 | + return type_to_size[value] |
| 117 | + |
| 118 | + def side_effect_ctypes_alignment(value): |
| 119 | + type_to_alignment = { |
| 120 | + ctypes.c_longlong: 8, |
| 121 | + ctypes.c_long: 4, |
| 122 | + ctypes.c_uint8: 1, |
| 123 | + ctypes.c_uint16: 2, |
| 124 | + ctypes.c_uint32: 4, |
| 125 | + ctypes.c_uint64: 8, |
| 126 | + } |
| 127 | + return type_to_alignment[value] |
| 128 | + |
| 129 | + ctypes_sizeof.side_effect = side_effect_ctypes_sizeof |
| 130 | + ctypes_alignment.side_effect = side_effect_ctypes_alignment |
| 131 | + |
| 132 | + fields = [ |
| 133 | + ("opcode", ctypes.c_uint32), |
| 134 | + ("flags", ctypes.c_uint32), |
| 135 | + ("count", ctypes.c_uint32), |
| 136 | + ("ival1_tv_sec", ctypes.c_long), |
| 137 | + ("ival1_tv_usec", ctypes.c_long), |
| 138 | + ("ival2_tv_sec", ctypes.c_long), |
| 139 | + ("ival2_tv_usec", ctypes.c_long), |
| 140 | + ("can_id", ctypes.c_uint32), |
| 141 | + ("nframes", ctypes.c_uint32), |
| 142 | + ] |
| 143 | + BcmMsgHead = bcm_header_factory(fields) |
| 144 | + |
| 145 | + expected_fields = [ |
| 146 | + ("opcode", ctypes.c_uint32), |
| 147 | + ("flags", ctypes.c_uint32), |
| 148 | + ("count", ctypes.c_uint32), |
| 149 | + ("ival1_tv_sec", ctypes.c_long), |
| 150 | + ("ival1_tv_usec", ctypes.c_long), |
| 151 | + ("ival2_tv_sec", ctypes.c_long), |
| 152 | + ("ival2_tv_usec", ctypes.c_long), |
| 153 | + ("can_id", ctypes.c_uint32), |
| 154 | + ("nframes", ctypes.c_uint32), |
| 155 | + # We expect 4 bytes of padding |
| 156 | + ("pad_0", ctypes.c_uint8), |
| 157 | + ("pad_1", ctypes.c_uint8), |
| 158 | + ("pad_2", ctypes.c_uint8), |
| 159 | + ("pad_3", ctypes.c_uint8), |
| 160 | + ] |
| 161 | + self.assertEqual(expected_fields, BcmMsgHead._fields_) |
| 162 | + |
| 163 | + @patch("ctypes.sizeof") |
| 164 | + @patch("ctypes.alignment") |
| 165 | + def test_bcm_header_factory_64_bit_sizeof_long_4_alignof_long_4( |
| 166 | + self, ctypes_sizeof, ctypes_alignment |
| 167 | + ): |
| 168 | + """This tests a 64-bit platform (ex. Ubuntu 18.04 on x86_64), where: |
| 169 | +
|
| 170 | + * sizeof(long) == 8 |
| 171 | + * sizeof(long long) == 8 |
| 172 | + * alignof(long) == 8 |
| 173 | + * alignof(long long) == 8 |
| 174 | + """ |
| 175 | + |
| 176 | + def side_effect_ctypes_sizeof(value): |
| 177 | + type_to_size = { |
| 178 | + ctypes.c_longlong: 8, |
| 179 | + ctypes.c_long: 8, |
| 180 | + ctypes.c_uint8: 1, |
| 181 | + ctypes.c_uint16: 2, |
| 182 | + ctypes.c_uint32: 4, |
| 183 | + ctypes.c_uint64: 8, |
| 184 | + } |
| 185 | + return type_to_size[value] |
| 186 | + |
| 187 | + def side_effect_ctypes_alignment(value): |
| 188 | + type_to_alignment = { |
| 189 | + ctypes.c_longlong: 8, |
| 190 | + ctypes.c_long: 8, |
| 191 | + ctypes.c_uint8: 1, |
| 192 | + ctypes.c_uint16: 2, |
| 193 | + ctypes.c_uint32: 4, |
| 194 | + ctypes.c_uint64: 8, |
| 195 | + } |
| 196 | + return type_to_alignment[value] |
| 197 | + |
| 198 | + ctypes_sizeof.side_effect = side_effect_ctypes_sizeof |
| 199 | + ctypes_alignment.side_effect = side_effect_ctypes_alignment |
| 200 | + |
| 201 | + fields = [ |
| 202 | + ("opcode", ctypes.c_uint32), |
| 203 | + ("flags", ctypes.c_uint32), |
| 204 | + ("count", ctypes.c_uint32), |
| 205 | + ("ival1_tv_sec", ctypes.c_long), |
| 206 | + ("ival1_tv_usec", ctypes.c_long), |
| 207 | + ("ival2_tv_sec", ctypes.c_long), |
| 208 | + ("ival2_tv_usec", ctypes.c_long), |
| 209 | + ("can_id", ctypes.c_uint32), |
| 210 | + ("nframes", ctypes.c_uint32), |
| 211 | + ] |
| 212 | + BcmMsgHead = bcm_header_factory(fields) |
| 213 | + |
| 214 | + expected_fields = [ |
| 215 | + ("opcode", ctypes.c_uint32), |
| 216 | + ("flags", ctypes.c_uint32), |
| 217 | + ("count", ctypes.c_uint32), |
| 218 | + # We expect 4 bytes of padding |
| 219 | + ("pad_0", ctypes.c_uint8), |
| 220 | + ("pad_1", ctypes.c_uint8), |
| 221 | + ("pad_2", ctypes.c_uint8), |
| 222 | + ("pad_3", ctypes.c_uint8), |
| 223 | + ("ival1_tv_sec", ctypes.c_long), |
| 224 | + ("ival1_tv_usec", ctypes.c_long), |
| 225 | + ("ival2_tv_sec", ctypes.c_long), |
| 226 | + ("ival2_tv_usec", ctypes.c_long), |
| 227 | + ("can_id", ctypes.c_uint32), |
| 228 | + ("nframes", ctypes.c_uint32), |
| 229 | + ] |
| 230 | + self.assertEqual(expected_fields, BcmMsgHead._fields_) |
| 231 | + |
| 232 | + |
| 233 | +if __name__ == "__main__": |
| 234 | + unittest.main() |
0 commit comments