Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 2e8a94a

Browse files
committed
BinaryData: raise instead of discarding data
Before, packed BinaryData would be empty if given a non-byte value. E.g.: BinaryData('string').pack() == b''. Now, we raise a ValueError.
1 parent 43feaed commit 2e8a94a

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

pyof/foundation/basic_types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,13 @@ def __init__(self, value=b''): # noqa
411411
"""The constructor takes the parameter below.
412412
413413
Args:
414-
value (bytes): The binary data. Defaults to an empty value.
414+
value (bytes, str): The binary data. Defaults to an empty value.
415+
416+
Raises:
417+
ValueError: If given value is not bytes.
415418
"""
419+
if not isinstance(value, bytes):
420+
raise ValueError('BinaryData must contain bytes.')
416421
super().__init__(value)
417422

418423
def pack(self, value=None):
@@ -430,8 +435,10 @@ def pack(self, value=None):
430435
if value is None:
431436
value = self._value
432437

433-
if isinstance(value, bytes) and value:
434-
return value
438+
if value:
439+
if isinstance(value, bytes):
440+
return value
441+
raise ValueError('BinaryData must contain bytes.')
435442

436443
return b''
437444

tests/test_foundation/test_basic_types.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
from pyof.foundation import basic_types
5+
from pyof.foundation.basic_types import BinaryData
56

67

78
class TestUBInt8(unittest.TestCase):
@@ -160,3 +161,33 @@ def test_get_size(self):
160161
"""Testing get_size from IPAddress."""
161162
ip_addr = basic_types.IPAddress('192.168.0.1/24')
162163
self.assertEqual(ip_addr.get_size(), 4)
164+
165+
166+
class TestBinaryData(unittest.TestCase):
167+
"""Test Binary data type."""
168+
169+
def test_default_value(self):
170+
"""Default packed value should be an empty byte."""
171+
expected = b''
172+
actual = BinaryData().pack()
173+
self.assertEqual(expected, actual)
174+
175+
def test_pack_bytes(self):
176+
"""Test packing some bytes."""
177+
expected = b'forty two'
178+
actual = BinaryData(expected).pack()
179+
self.assertEqual(expected, actual)
180+
181+
def test_pack_empty_bytes(self):
182+
"""Test packing empty bytes."""
183+
expected = b''
184+
actual = BinaryData(expected).pack()
185+
self.assertEqual(expected, actual)
186+
187+
def test_unexpected_value(self):
188+
"""Should raise ValueError if constructor value is not bytes."""
189+
self.assertRaises(ValueError, BinaryData, "can't be string")
190+
191+
def test_unexpected_value_as_parameter(self):
192+
"""Should raise ValueError if pack value is not bytes."""
193+
self.assertRaises(ValueError, BinaryData().pack, "can't be string")

tests/v0x01/test_asynchronous/test_error_msg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def setUpClass(cls):
1717
super().set_raw_dump_object(ErrorMsg, xid=12,
1818
error_type=ErrorType.OFPET_BAD_REQUEST,
1919
code=BadRequestCode.OFPBRC_BAD_STAT,
20-
data=BinaryData('object_test'))
20+
data=BinaryData(b'object_test'))
2121
super().set_minimum_size(12)

0 commit comments

Comments
 (0)