Skip to content

Commit a344a13

Browse files
authored
Fix #1376 (#1412)
* try to fix test * find u32
1 parent c9d6d4e commit a344a13

File tree

1 file changed

+16
-45
lines changed

1 file changed

+16
-45
lines changed

test/test_socketcan.py

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
"""
44
Test functions in `can.interfaces.socketcan.socketcan`.
55
"""
6+
import ctypes
7+
import struct
68
import unittest
7-
8-
from unittest.mock import Mock
99
from unittest.mock import patch
10-
from unittest.mock import call
11-
12-
import ctypes
1310

1411
from can.interfaces.socketcan.socketcan import (
1512
bcm_header_factory,
@@ -240,51 +237,25 @@ def side_effect_ctypes_alignment(value):
240237
]
241238
self.assertEqual(expected_fields, BcmMsgHead._fields_)
242239

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
257245

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
272252

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(
281254
b"\x02\x00\x00\x00\x00\x00\x00\x00"
282255
b"\x00\x00\x00\x00\x00\x00\x00\x00"
283256
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"
288259
)
289260

290261
self.assertEqual(

0 commit comments

Comments
 (0)