From b71c392c9d724abc34d365cfc86c5dcb67db489c Mon Sep 17 00:00:00 2001 From: Lukas Magel Date: Sun, 11 Jun 2023 22:39:55 +0200 Subject: [PATCH 1/3] Implement test to trigger ASCII decoding error in Kvaser bus constructor --- test/test_kvaser.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/test_kvaser.py b/test/test_kvaser.py index 043f86f8c..509367c7d 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -3,6 +3,7 @@ """ """ +import ctypes import time import unittest from unittest.mock import Mock @@ -49,6 +50,26 @@ def test_bus_creation(self): self.assertTrue(canlib.canOpenChannel.called) self.assertTrue(canlib.canBusOn.called) + def test_bus_creation_illegal_channel_name(self): + # Test if the bus constructor is able to deal with non-ASCII characters + def canGetChannelDataMock( + channel: ctypes.c_int, + dtype: ctypes.c_int, + buf: ctypes.c_void_p, + bufsize: ctypes.c_size_t, + ): + if dtype == constants.canCHANNELDATA_DEVDESCR_ASCII: + obj = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char)) + for i, char in enumerate(b"hello\x7a\xcb"): + obj[i] = char + + canlib.canGetChannelData = canGetChannelDataMock + bus = can.Bus(channel=0, interface="kvaser") + + self.assertTrue(bus.channel_info.startswith("hello")) + + bus.shutdown() + def test_bus_shutdown(self): self.bus.shutdown() self.assertTrue(canlib.canBusOff.called) From d3de662c6c13052a376897ce70f70531b9ab7d57 Mon Sep 17 00:00:00 2001 From: Lukas Magel Date: Sun, 11 Jun 2023 22:51:04 +0200 Subject: [PATCH 2/3] Change handling of invalid chars in Kvaser device name Previously, illegal characters triggered an exception. With the new behavior, illegal characters will be replaced with a placeholder value. --- can/interfaces/kvaser/canlib.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 32d28059a..4e5e8c51b 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -727,7 +727,8 @@ def get_channel_info(channel): ctypes.sizeof(number), ) - return f"{name.value.decode('ascii')}, S/N {serial.value} (#{number.value + 1})" + name_decoded = name.value.decode("ascii", errors="replace") + return f"{name_decoded}, S/N {serial.value} (#{number.value + 1})" init_kvaser_library() From 2e26f7b9d22ec2d1e2946540a44ce77e43f59fa1 Mon Sep 17 00:00:00 2001 From: Lukas Magel Date: Sun, 11 Jun 2023 22:56:41 +0200 Subject: [PATCH 3/3] Rename variables in test --- test/test_kvaser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_kvaser.py b/test/test_kvaser.py index 509367c7d..1254f2fc7 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -54,14 +54,14 @@ def test_bus_creation_illegal_channel_name(self): # Test if the bus constructor is able to deal with non-ASCII characters def canGetChannelDataMock( channel: ctypes.c_int, - dtype: ctypes.c_int, + param: ctypes.c_int, buf: ctypes.c_void_p, bufsize: ctypes.c_size_t, ): - if dtype == constants.canCHANNELDATA_DEVDESCR_ASCII: - obj = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char)) + if param == constants.canCHANNELDATA_DEVDESCR_ASCII: + buf_char_ptr = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char)) for i, char in enumerate(b"hello\x7a\xcb"): - obj[i] = char + buf_char_ptr[i] = char canlib.canGetChannelData = canGetChannelDataMock bus = can.Bus(channel=0, interface="kvaser")