|
3 | 3 | """
|
4 | 4 | Test functions in `can.interfaces.socketcan.socketcan`.
|
5 | 5 | """
|
6 |
| -import ctypes |
7 |
| -import struct |
8 | 6 | import unittest
|
| 7 | + |
| 8 | +from unittest.mock import Mock |
9 | 9 | from unittest.mock import patch
|
| 10 | +from unittest.mock import call |
| 11 | + |
| 12 | +import ctypes |
10 | 13 |
|
11 | 14 | from can.interfaces.socketcan.socketcan import (
|
12 | 15 | bcm_header_factory,
|
@@ -237,25 +240,51 @@ def side_effect_ctypes_alignment(value):
|
237 | 240 | ]
|
238 | 241 | self.assertEqual(expected_fields, BcmMsgHead._fields_)
|
239 | 242 |
|
240 |
| - def test_build_bcm_header(self): |
241 |
| - def _find_u32_fmt_char() -> str: |
242 |
| - for _fmt in ("H", "I", "L", "Q"): |
243 |
| - if struct.calcsize(_fmt) == 4: |
244 |
| - return _fmt |
| 243 | + @unittest.skipIf( |
| 244 | + not ( |
| 245 | + ctypes.sizeof(ctypes.c_long) == 4 and ctypes.alignment(ctypes.c_long) == 4 |
| 246 | + ), |
| 247 | + "Should only run on platforms where sizeof(long) == 4 and alignof(long) == 4", |
| 248 | + ) |
| 249 | + def test_build_bcm_header_sizeof_long_4_alignof_long_4(self): |
| 250 | + expected_result = ( |
| 251 | + b"\x02\x00\x00\x00\x00\x00\x00\x00" |
| 252 | + b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 253 | + b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 254 | + b"\x00\x00\x00\x00\x01\x04\x00\x00" |
| 255 | + b"\x01\x00\x00\x00\x00\x00\x00\x00" |
| 256 | + ) |
245 | 257 |
|
246 |
| - def _standard_size_little_endian_to_native(data: bytes) -> bytes: |
247 |
| - std_le_fmt = "<IIIllllII" |
248 |
| - native_fmt = "@" + std_le_fmt[1:].replace("I", _find_u32_fmt_char()) |
249 |
| - aligned_data = struct.pack(native_fmt, *struct.unpack(std_le_fmt, data)) |
250 |
| - padded_data = aligned_data + b"\x00" * ((8 - len(aligned_data) % 8) % 8) |
251 |
| - return padded_data |
| 258 | + self.assertEqual( |
| 259 | + expected_result, |
| 260 | + build_bcm_header( |
| 261 | + opcode=CAN_BCM_TX_DELETE, |
| 262 | + flags=0, |
| 263 | + count=0, |
| 264 | + ival1_seconds=0, |
| 265 | + ival1_usec=0, |
| 266 | + ival2_seconds=0, |
| 267 | + ival2_usec=0, |
| 268 | + can_id=0x401, |
| 269 | + nframes=1, |
| 270 | + ), |
| 271 | + ) |
252 | 272 |
|
253 |
| - expected_result = _standard_size_little_endian_to_native( |
| 273 | + @unittest.skipIf( |
| 274 | + not ( |
| 275 | + ctypes.sizeof(ctypes.c_long) == 8 and ctypes.alignment(ctypes.c_long) == 8 |
| 276 | + ), |
| 277 | + "Should only run on platforms where sizeof(long) == 8 and alignof(long) == 8", |
| 278 | + ) |
| 279 | + def test_build_bcm_header_sizeof_long_8_alignof_long_8(self): |
| 280 | + expected_result = ( |
254 | 281 | b"\x02\x00\x00\x00\x00\x00\x00\x00"
|
255 | 282 | b"\x00\x00\x00\x00\x00\x00\x00\x00"
|
256 | 283 | b"\x00\x00\x00\x00\x00\x00\x00\x00"
|
257 |
| - b"\x00\x00\x00\x00\x01\x04\x00\x00" |
258 |
| - b"\x01\x00\x00\x00" |
| 284 | + b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 285 | + b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 286 | + b"\x00\x00\x00\x00\x00\x00\x00\x00" |
| 287 | + b"\x01\x04\x00\x00\x01\x00\x00\x00" |
259 | 288 | )
|
260 | 289 |
|
261 | 290 | self.assertEqual(
|
|
0 commit comments