Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions canopen/objectdictionary/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import operator
import struct


Expand Down Expand Up @@ -83,7 +84,11 @@ def unpack(self, buffer):
return super().unpack(buffer + b'\x00' * (super().size - self.size))

def pack(self, *v):
return super().pack(*v)[:self.size]
packed = super().pack(*v)
value = operator.index(v[0])
if not 0 <= value < (1 << self.width):
raise struct.error("unsigned value out of range for data type")
return packed[:self.size]

@property
def size(self) -> int:
Expand Down Expand Up @@ -120,7 +125,13 @@ def unpack(self, buffer):
)

def pack(self, *v):
return super().pack(*v)[:self.size]
packed = super().pack(*v)
value = operator.index(v[0])
lower = -(1 << (self.width - 1))
upper = 1 << (self.width - 1)
if not lower <= value < upper:
raise struct.error("signed value out of range for data type")
return packed[:self.size]

@property
def size(self) -> int:
Expand Down
47 changes: 47 additions & 0 deletions test/test_od.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
import struct
import unittest

from canopen import objectdictionary as od


class TestDataConversions(unittest.TestCase):

def test_nonstandard_unsigned_width_rejects_overflow(self):
from canopen.objectdictionary.datatypes import UnsignedN
for width in (24, 40, 48, 56):
with self.subTest(width=width):
datatype = UnsignedN(width)
max_value = (1 << width) - 1
self.assertEqual(datatype.unpack(datatype.pack(max_value))[0], max_value)
self.assertEqual(datatype.unpack(datatype.pack(0))[0], 0)
with self.assertRaises(struct.error):
datatype.pack(1 << width)
with self.assertRaises(struct.error):
datatype.pack(-1)

def test_nonstandard_signed_width_rejects_overflow(self):
from canopen.objectdictionary.datatypes import IntegerN
for width in (24, 40, 48, 56):
with self.subTest(width=width):
datatype = IntegerN(width)
min_value = -(1 << (width - 1))
max_value = (1 << (width - 1)) - 1
self.assertEqual(datatype.unpack(datatype.pack(min_value))[0], min_value)
self.assertEqual(datatype.unpack(datatype.pack(max_value))[0], max_value)
with self.assertRaises(struct.error):
datatype.pack(max_value + 1)
with self.assertRaises(struct.error):
datatype.pack(min_value - 1)


def test_variable_encoding_rejects_narrow_width_overflow(self):
unsigned = (od.UNSIGNED24, od.UNSIGNED40, od.UNSIGNED48, od.UNSIGNED56)
signed = (od.INTEGER24, od.INTEGER40, od.INTEGER48, od.INTEGER56)
for width, data_type in zip((24, 40, 48, 56), unsigned):
with self.subTest(kind="unsigned", width=width):
var = od.ODVariable("Narrow unsigned", 0x1000)
var.data_type = data_type
with self.assertRaisesRegex(ValueError, "Value does not fit"):
var.encode_raw(1 << width)
for width, data_type in zip((24, 40, 48, 56), signed):
with self.subTest(kind="signed", width=width):
var = od.ODVariable("Narrow signed", 0x1000)
var.data_type = data_type
with self.assertRaisesRegex(ValueError, "Value does not fit"):
var.encode_raw(1 << (width - 1))
with self.assertRaisesRegex(ValueError, "Value does not fit"):
var.encode_raw(-(1 << (width - 1)) - 1)

def test_boolean(self):
var = od.ODVariable("Test BOOLEAN", 0x1000)
var.data_type = od.BOOLEAN
Expand Down