|
3 | 3 | """ |
4 | 4 | Test functions in `can.interfaces.socketcan.socketcan`. |
5 | 5 | """ |
| 6 | +import ctypes |
| 7 | +import struct |
6 | 8 | 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 |
13 | 10 |
|
14 | 11 | from can.interfaces.socketcan.socketcan import ( |
15 | 12 | bcm_header_factory, |
@@ -240,51 +237,25 @@ def side_effect_ctypes_alignment(value): |
240 | 237 | ] |
241 | 238 | self.assertEqual(expected_fields, BcmMsgHead._fields_) |
242 | 239 |
|
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 | | - ) |
| 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 |
257 | 245 |
|
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 | | - ) |
| 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 |
272 | 252 |
|
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 = ( |
| 253 | + expected_result = _standard_size_little_endian_to_native( |
281 | 254 | b"\x02\x00\x00\x00\x00\x00\x00\x00" |
282 | 255 | b"\x00\x00\x00\x00\x00\x00\x00\x00" |
283 | 256 | b"\x00\x00\x00\x00\x00\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" |
| 257 | + b"\x00\x00\x00\x00\x01\x04\x00\x00" |
| 258 | + b"\x01\x00\x00\x00" |
288 | 259 | ) |
289 | 260 |
|
290 | 261 | self.assertEqual( |
|
0 commit comments