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

Commit 848b698

Browse files
authored
Merge pull request #369 from cemsbr/binarydata
BinaryData: raise instead of discarding data
2 parents d0071fd + af7918e commit 848b698

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ Change log
33
##########
44
All notable changes to the python-openflow project is documented in this file.
55

6-
[UNRELEASED] - Under development
7-
********************************
6+
UNRELEASED - [2017.1b3] - "bethania" beta3 - 2017-06-1X
7+
*******************************************************
88
Added
99
=====
1010
- IPv4 packet pack/unpack support.
1111

1212
Changed
1313
=======
14+
- Raise ValueError if not using bytes (e.g. string) in BinaryData.
1415

1516
Deprecated
1617
==========

pyof/foundation/basic_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,12 @@ def __init__(self, value=b''): # noqa
412412
413413
Args:
414414
value (bytes): 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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def setUpClass(cls):
1616
error_type=ErrorType.OFPET_BAD_REQUEST,
1717
code=BadRequestCode.OFPBRC_BAD_STAT,
1818
data=b'')
19-
2019
super().set_minimum_size(12)
2120

2221
def test_unpack_error_msg(self):

0 commit comments

Comments
 (0)