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

Teststruct improvement application #404

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
e9842dc
introduce general unpack method (comes from of_core.utils)
erickvermot Jul 7, 2017
883358a
proposes more flexible TestStruct
erickvermot Jul 7, 2017
5d6420e
remove comment as requested...
erickvermot Jul 13, 2017
acab250
apply new test implementation for basic types
erickvermot Jul 11, 2017
990c121
apply new test implementation for basic types - remove unused
erickvermot Jul 11, 2017
e20298e
apply new test implementation for network types
erickvermot Jul 11, 2017
79c6d54
apply new test implementation for network types - remove unused
erickvermot Jul 11, 2017
6889fb6
apply new test implementation for error msg
erickvermot Jul 11, 2017
f62087d
errormsg packet dump fix
erickvermot Jul 11, 2017
25db9a1
apply new test implementation for error msg - remove unused
erickvermot Jul 11, 2017
98a6d87
apply new test implementation for flow removed
erickvermot Jul 11, 2017
9267820
apply new test implementation for PacketIn
erickvermot Jul 11, 2017
7accf0a
apply new test implementation for PortStatus
erickvermot Jul 11, 2017
6509eb1
apply new test implementation for actions
erickvermot Jul 11, 2017
d616fe5
apply new test implementation for actions
erickvermot Jul 11, 2017
e2209c9
apply new test implementation for Match class
erickvermot Jul 11, 2017
40efb0a
apply new test implementation for Header class
erickvermot Jul 11, 2017
0c7912e
apply new test implementation for PhyPort class
erickvermot Jul 11, 2017
eb31213
apply new test implementation for queues
erickvermot Jul 11, 2017
af85d5b
apply new test implementation for agg stats reply
erickvermot Jul 11, 2017
3fb8844
apply new test implementation for agg stats request
erickvermot Jul 11, 2017
4190b19
apply new test implementation for BarrierReply class
erickvermot Jul 11, 2017
baa5104
apply new test implementation for BarrierRequest class
erickvermot Jul 11, 2017
740ad26
apply new test implementation for DescStats class
erickvermot Jul 11, 2017
4f7db61
apply new test implementation for FeaturesReply class
erickvermot Jul 11, 2017
a09632f
apply new test implementation for FeaturesRequest class
erickvermot Jul 11, 2017
16bd12f
apply new test implementation for FlowMod class
erickvermot Jul 11, 2017
9f846c9
apply new test implementation for flow stats (StatsReply msg)
erickvermot Jul 11, 2017
d4be345
apply new test implementation for flow stats request
erickvermot Jul 11, 2017
db73a7d
apply new test implementation for get config reply
erickvermot Jul 11, 2017
ef4f5ba
apply new test implementation for get config request
erickvermot Jul 11, 2017
4f5e66d
apply new test implementation for packet out
erickvermot Jul 11, 2017
51f77d5
apply new test implementation for port mod
erickvermot Jul 11, 2017
0dcf972
apply new test implementation for port stats
erickvermot Jul 11, 2017
fb289de
apply new test implementation for port stats request
erickvermot Jul 11, 2017
4d30809
apply new test implementation for get config reply
erickvermot Jul 11, 2017
11d34b3
apply new test implementation for get config request
erickvermot Jul 11, 2017
5cddf6e
test get config request wrong min size
erickvermot Jul 11, 2017
82b52a2
apply new test implementation for queue stats
erickvermot Jul 11, 2017
5600802
apply new test implementation for queue stats request
erickvermot Jul 11, 2017
988d268
apply new test implementation for set config
erickvermot Jul 11, 2017
d1a1770
apply new test implementation for stats reply
erickvermot Jul 11, 2017
883c637
apply new test implementation for stats request
erickvermot Jul 11, 2017
bc30ea4
apply new test implementation for table stats
erickvermot Jul 11, 2017
11d7af4
apply new test implementation for echo reply
erickvermot Jul 11, 2017
a6fe4f9
apply new test implementation for echo request
erickvermot Jul 11, 2017
0d56152
apply new test implementation for hello
erickvermot Jul 11, 2017
897653b
apply new test implementation for vendor header
erickvermot Jul 11, 2017
062e481
test port status isort
erickvermot Jul 11, 2017
f3c69ae
Requested changes on tests names, line breaks and such.
erickvermot Jul 13, 2017
d294d46
include dump object in TestStructDump docstring
erickvermot Jul 13, 2017
f2a02e5
make type(Char(length))(value) return new class but with same length
erickvermot Jul 14, 2017
089b921
Include min_size tests for a few basic_types
erickvermot Jul 14, 2017
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
28 changes: 15 additions & 13 deletions pyof/foundation/basic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,8 @@ def unpack(self, buff, offset=0):
self._value = ':'.join(hexas)


class Char(GenericType):
"""Build a double char type according to the length."""

def __init__(self, value=None, length=0):
"""The constructor takes the optional parameters below.

Args:
value: The character to be build.
length (int): Character size.
"""
super().__init__(value)
self.length = length
self._fmt = '!{}{}'.format(self.length, 's')
class CharStringBase(GenericType):
"""A null terminated ascii char string."""

def pack(self, value=None):
"""Pack the value as a binary representation.
Expand Down Expand Up @@ -231,6 +220,19 @@ def unpack(self, buff, offset=0):
self._value = unpacked_data.decode('ascii').rstrip('\0')


def Char(value=None, length=0):
"""Return a CharString class with given length and initial value."""
string_length = length

class CharString(CharStringBase):
"""Class for null terminated ascii strings of fixed length."""

length = string_length
_fmt = '!{}{}'.format(length, 's')

return CharString(value)


class IPAddress(GenericType):
"""Defines a IP address."""

Expand Down
37 changes: 37 additions & 0 deletions pyof/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""General Unpack utils for python-openflow."""
import pyof.v0x01.common.header
import pyof.v0x01.common.utils
import pyof.v0x04.common.header
import pyof.v0x04.common.utils
from pyof.foundation.exceptions import UnpackException

pyof_version_libs = {0x01: pyof.v0x01,
0x04: pyof.v0x04}


def unpack(packet):
"""Unpack the OpenFlow Packet and returns a message."""
try:
version = packet[0]
except IndexError:
raise UnpackException('null packet')

try:
pyof_lib = pyof_version_libs[version]
except KeyError:
raise UnpackException('Version not supported')

try:
header = pyof_lib.common.header.Header()
header.unpack(packet[:8])
message = pyof_lib.common.utils.new_message_from_header(header)
binary_data = packet[8:]
if binary_data:
if len(binary_data) == header.length - 8:
message.unpack(binary_data)
else:
raise UnpackException(
'packet size does not match packet length field')
return message
except (UnpackException, ValueError) as e:
raise UnpackException(e)
203 changes: 65 additions & 138 deletions tests/test_foundation/test_basic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,152 +3,102 @@

from pyof.foundation import basic_types
from pyof.foundation.basic_types import BinaryData
from tests.test_struct import TestStructDump


class TestUBInt8(unittest.TestCase):
"""Test of UBInt8 BasicType."""
class TestUBInt8(TestStructDump):
"""Test UBInt8."""

def setUp(self):
"""Basic test setup."""
self.ubint8 = basic_types.UBInt8()
dump = b'\xff'
obj = basic_types.UBInt8(2**8 - 1)
min_size = 1

def test_get_size(self):
"""[Foundation/BasicTypes/UBInt8] - size 1."""
self.assertEqual(self.ubint8.get_size(), 1)

@unittest.skip('Not yet implemented')
def test_pack(self):
"""[Foundation/BasicTypes/UBInt8] - packing."""
pass
class TestUBInt16(TestStructDump):
"""Test UBInt16."""

@unittest.skip('Not yet implemented')
def test_unpack(self):
"""[Foundation/BasicTypes/UBInt8] - unpacking."""
pass
dump = b'\xff\xff'
obj = basic_types.UBInt16(2**16 - 1)
min_size = 2


class TestUBInt16(unittest.TestCase):
"""Test of UBInt16 BasicType."""
class TestUBInt32(TestStructDump):
"""Test UBInt32."""

def setUp(self):
"""Basic test setup."""
self.ubint16 = basic_types.UBInt16()
dump = b'\xff\xff\xff\xff'
obj = basic_types.UBInt32(2**32 - 1)
min_size = 4

def test_get_size(self):
"""[Foundation/BasicTypes/UBInt16] - size 2."""
self.assertEqual(self.ubint16.get_size(), 2)

@unittest.skip('Not yet implemented')
def test_pack(self):
"""[Foundation/BasicTypes/UBInt16] - packing."""
pass
class TestChar3(TestStructDump):
"""Test Char with length 3."""

@unittest.skip('Not yet implemented')
def test_unpack(self):
"""[Foundation/BasicTypes/UBInt16] - unpacking."""
pass
dump = b'fo\x00'
obj = basic_types.Char('foo', length=3)
min_size = 3


class TestUBInt32(unittest.TestCase):
"""Test of UBInt32 BasicType."""
class TestChar5(TestStructDump):
"""Test Char with length 5."""

def setUp(self):
"""Basic test setup."""
self.ubint32 = basic_types.UBInt32()
dump = b'foo\x00\x00'
obj = basic_types.Char('foo', length=5)
min_size = 5

def test_get_size(self):
"""[Foundation/BasicTypes/UBInt32] - size 4."""
self.assertEqual(self.ubint32.get_size(), 4)

@unittest.skip('Not yet implemented')
def test_pack(self):
"""[Foundation/BasicTypes/UBInt32] - packing."""
pass
class TestHWAddressMac(TestStructDump):
"""Test HWAddress mac value."""

@unittest.skip('Not yet implemented')
def test_unpack(self):
"""[Foundation/BasicTypes/UBInt32] - unpacking."""
pass
mac = '0a:d3:98:a5:30:47'
dump = b'\x0a\xd3\x98\xa5\x30\x47'
obj = basic_types.HWAddress(mac)
min_size = 6

def test_address_value(self):
"""Test HWAddress mac value."""
self.assertEqual(self.obj.value, self.mac)

class TestChar(unittest.TestCase):
"""Test of Char BasicType."""

def setUp(self):
"""Basic test setup."""
self.char1 = basic_types.Char('foo', length=3)
self.char2 = basic_types.Char('foo', length=5)
class TestIPAddressNetmask(TestStructDump):
"""Test IPAddress and its default netmask value."""

def test_get_size(self):
"""[Foundation/BasicTypes/Char] - get_size."""
self.assertEqual(self.char1.get_size(), 3)
self.assertEqual(self.char2.get_size(), 5)
dump = b'\xc0\xa8\x00\x01'
obj = basic_types.IPAddress('192.168.0.1')
netmask = 32
min_size = 4

def test_pack(self):
"""[Foundation/BasicTypes/Char] - packing."""
self.assertEqual(self.char1.pack(), b'fo\x00')
self.assertEqual(self.char2.pack(), b'foo\x00\x00')
def test_netmask(self):
"""Test IPAddress netmask value."""
self.assertEqual(self.obj.netmask, self.netmask)

def test_unpack(self):
"""[Foundation/BasicTypes/Char] - unpacking."""
char1 = basic_types.Char(length=3)
char2 = basic_types.Char(length=5)
char1.unpack(b'fo\x00')
char2.unpack(b'foo\x00\x00')

self.assertEqual(char1.value, 'fo')
self.assertEqual(char2.value, 'foo')
class TestIPAddressNoNetmask(TestIPAddressNetmask):
"""Test IPAdress and netmask value 16."""

dump = b'\xc0\xa8\x00\x01'
obj = basic_types.IPAddress('192.168.0.1/16')
netmask = 16
min_size = 4

class TestHWaddress(unittest.TestCase):
"""Test of HWAddress BasicType."""

def test_unpack_packed(self):
"""Testing unpack of packed HWAddress."""
mac = '0a:d3:98:a5:30:47'
hw_addr = basic_types.HWAddress(mac)
packed = hw_addr.pack()
unpacked = basic_types.HWAddress()
unpacked.unpack(packed)
self.assertEqual(mac, unpacked.value)
class TestBinaryDataEmpty(TestStructDump):
"""Test empty BinaryData."""

def test_default_value(self):
"""Testing default_value for HWAddress."""
mac = '00:00:00:00:00:00'
hw_addr = basic_types.HWAddress()
packed = hw_addr.pack()
unpacked = basic_types.HWAddress()
unpacked.unpack(packed)
self.assertEqual(mac, unpacked.value)
dump = b''
obj = BinaryData()
min_size = 0


class TestIPAddress(unittest.TestCase):
"""Test of IPAddress BasicType."""

def test_unpack_packed(self):
"""Test unpacking of packed IPAddress."""
ip_addr = basic_types.IPAddress('192.168.0.1')
packed = ip_addr.pack()
unpacked = basic_types.IPAddress()
unpacked.unpack(packed)
self.assertEqual(ip_addr.value, unpacked.value)

def test_unpack_packed_with_netmask(self):
"""Testing unpack of packed IPAddress with netmask."""
ip_addr = basic_types.IPAddress('192.168.0.1/16')
packed = ip_addr.pack()
unpacked = basic_types.IPAddress()
unpacked.unpack(packed)
self.assertEqual(ip_addr.value, unpacked.value)
class TestBinaryDataBytes(TestStructDump):
"""Test 'bytes' BinaryData."""

dump = b'bytes'
obj = BinaryData(b'bytes')
min_size = 0

def test_netmask(self):
"""Testing get netmask from IPAddress."""
ip_addr = basic_types.IPAddress('192.168.0.1/24')
self.assertEqual(ip_addr.netmask, 24)
ip_addr = basic_types.IPAddress('192.168.0.1/16')
self.assertEqual(ip_addr.netmask, 16)
ip_addr = basic_types.IPAddress('192.168.0.1')
self.assertEqual(ip_addr.netmask, 32)

class TestIPAddress(unittest.TestCase):
"""Test of IPAddress BasicType max_prefix."""

def test_max_prefix(self):
"""Testing get max_prefix from IPAddress."""
Expand All @@ -157,32 +107,9 @@ def test_max_prefix(self):
ip_addr = basic_types.IPAddress('192.168.0.35/16')
self.assertEqual(ip_addr.max_prefix, 32)

def test_get_size(self):
"""Testing get_size from IPAddress."""
ip_addr = basic_types.IPAddress('192.168.0.1/24')
self.assertEqual(ip_addr.get_size(), 4)


class TestBinaryData(unittest.TestCase):
"""Test Binary data type."""

def test_default_value(self):
"""Default packed value should be an empty byte."""
expected = b''
actual = BinaryData().pack()
self.assertEqual(expected, actual)

def test_pack_bytes(self):
"""Test packing some bytes."""
expected = b'forty two'
actual = BinaryData(expected).pack()
self.assertEqual(expected, actual)

def test_pack_empty_bytes(self):
"""Test packing empty bytes."""
expected = b''
actual = BinaryData(expected).pack()
self.assertEqual(expected, actual)
"""Test Binary data type cannot accept string."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you removed this other tests here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because they are already being performed.


def test_unexpected_value(self):
"""Should raise ValueError if constructor value is not bytes."""
Expand Down
Loading