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

Commit 1fecb55

Browse files
committed
Minor fixes in pyof/utils.py and add tests
Fix #381
1 parent ce81263 commit 1fecb55

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

pyof/utils.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
"""General Unpack utils for python-openflow."""
2-
import pyof.v0x01.common.header
3-
import pyof.v0x01.common.utils
4-
import pyof.v0x04.common.header
5-
import pyof.v0x04.common.utils
1+
"""General Unpack utils for python-openflow.
2+
3+
This package was moved from kytos/of_core for the purpose of creating a generic
4+
method to perform package unpack independent of the OpenFlow version.
5+
"""
6+
from pyof import v0x01, v0x04
67
from pyof.foundation.exceptions import UnpackException
78

8-
pyof_version_libs = {0x01: pyof.v0x01,
9-
0x04: pyof.v0x04}
9+
PYOF_VERSION_LIBS = {0x01: v0x01,
10+
0x04: v0x04}
1011

1112

1213
def validate_packet(packet):
13-
"""Check if packet is valid OF packet."""
14+
"""Check if packet is valid OF packet.
15+
16+
Raises:
17+
UnpackException: If the packet is invalid.
18+
"""
1419
if not isinstance(packet, bytes):
1520
raise UnpackException('invalid packet')
1621

@@ -29,22 +34,32 @@ def validate_packet(packet):
2934

3035

3136
def unpack(packet):
32-
"""Unpack the OpenFlow Packet and returns a message."""
37+
"""Unpack the OpenFlow Packet and returns a message.
38+
39+
Returns:
40+
GenericMessage: Message unpacked based on openflow packet.
41+
42+
Raises:
43+
UnpackException: if the packet can't be unpacked.
44+
"""
3345
validate_packet(packet)
3446

3547
version = packet[0]
3648
try:
37-
pyof_lib = pyof_version_libs[version]
49+
pyof_lib = PYOF_VERSION_LIBS[version]
3850
except KeyError:
3951
raise UnpackException('Version not supported')
4052

4153
try:
4254
header = pyof_lib.common.header.Header()
43-
header.unpack(packet[:8])
55+
header.unpack(packet[:header.get_size()])
4456
message = pyof_lib.common.utils.new_message_from_header(header)
45-
binary_data = packet[8:]
46-
if binary_data:
57+
58+
binary_data = packet[header.get_size():]
59+
binary_data_size = header.length - header.get_size()
60+
61+
if binary_data and len(binary_data) == binary_data_size:
4762
message.unpack(binary_data)
4863
return message
49-
except (UnpackException, ValueError) as e:
50-
raise UnpackException(e)
64+
except (UnpackException, ValueError) as exception:
65+
raise UnpackException(exception)

tests/test_utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Automate utils tests."""
2+
import unittest
3+
4+
from pyof.utils import UnpackException, unpack, validate_packet
5+
from pyof.v0x01.symmetric.hello import Hello as Hello_v0x01
6+
from pyof.v0x04.symmetric.hello import Hello as Hello_v0x04
7+
8+
9+
class TestUtils(unittest.TestCase):
10+
"""Run tests to verify unpack independent of version."""
11+
12+
def test_unpack_v0x01_packet(self):
13+
"""Test if the package in version v0x01 is properly unpacked."""
14+
data = Hello_v0x01().pack()
15+
expected = unpack(data)
16+
self.assertEqual(expected.pack(), data)
17+
18+
def test_unpack_v0x04_packet(self):
19+
"""Test if the package in version v0x04 is properly unpacked."""
20+
data = Hello_v0x04().pack()
21+
expected = unpack(data)
22+
self.assertEqual(expected.pack(), data)
23+
24+
def test_invalid_packet_with_version_more_then_128(self):
25+
"""Test validate a invalid packet with version more than 128."""
26+
hello = Hello_v0x04()
27+
hello.header.version = 129
28+
29+
self.assertRaises(UnpackException, validate_packet, hello.pack())
30+
31+
def test_validate_packet_with_invalid_packets(self):
32+
"""Test validate a invalid packet with invalid packets."""
33+
hello = Hello_v0x04()
34+
35+
hello.header.version = 128
36+
self.assertRaises(UnpackException, validate_packet, hello.pack())
37+
38+
hello.header.version = 0
39+
self.assertRaises(UnpackException, validate_packet, hello.pack())

0 commit comments

Comments
 (0)