|
1
|
|
|
"""Test Python-openflow network types.""" |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
from pyof.foundation.basic_types import BinaryData |
|
5
|
|
|
from pyof.foundation.network_types import GenericTLV, IPv4 |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestNetworkTypes(unittest.TestCase): |
|
9
|
|
|
"""Reproduce bugs found.""" |
|
10
|
|
|
|
|
11
|
|
|
def test_GenTLV_value_unpack(self): |
|
12
|
|
|
"""Value attribute should be the same after unpacking.""" |
|
13
|
|
|
value = BinaryData(b'test') |
|
14
|
|
|
tlv = GenericTLV(value=value) |
|
15
|
|
|
tlv_unpacked = GenericTLV() |
|
16
|
|
|
tlv_unpacked.unpack(tlv.pack()) |
|
17
|
|
|
self.assertEqual(tlv.value.value, tlv_unpacked.value.value) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class TestIPv4(unittest.TestCase): |
|
21
|
|
|
"""Test IPv4 packets.""" |
|
22
|
|
|
|
|
23
|
|
|
def test_IPv4_pack(self): |
|
24
|
|
|
"""Test pack/unpack of IPv4 class.""" |
|
25
|
|
|
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", |
|
26
|
|
|
destination="172.16.10.30", options=b'1000', |
|
27
|
|
|
data=b'testdata') |
|
28
|
|
|
packed = packet.pack() |
|
29
|
|
|
expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02' |
|
30
|
|
|
expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata' |
|
31
|
|
|
self.assertEqual(packed, expected) |
|
32
|
|
|
|
|
33
|
|
|
def test_IPv4_unpack(self): |
|
34
|
|
|
"""Test unpack of IPv4 binary packet.""" |
|
35
|
|
|
raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W' |
|
36
|
|
|
raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata' |
|
37
|
|
|
expected = IPv4(dscp=20, ttl=128, protocol=6, source="10.154.78.129", |
|
38
|
|
|
destination="192.168.199.204", options=b'1000', |
|
39
|
|
|
data=b'somemoredata') |
|
40
|
|
|
expected.pack() |
|
41
|
|
|
unpacked = IPv4() |
|
42
|
|
|
unpacked.unpack(raw) |
|
43
|
|
|
self.assertEqual(unpacked, expected) |
|
44
|
|
|
|
|
45
|
|
|
def test_IPv4_size(self): |
|
46
|
|
|
"""Test Header size for IPv4 packet.""" |
|
47
|
|
|
packet = IPv4() |
|
48
|
|
|
packet.pack() |
|
49
|
|
|
self.assertEqual(20, packet.get_size()) |
|
50
|
|
|
self.assertEqual(20, packet.length) |
|
51
|
|
|
self.assertEqual(20, packet.ihl * 4) |
|
52
|
|
|
|
|
53
|
|
|
def test_IPv4_checksum(self): |
|
54
|
|
|
"""Test if the IPv4 checksum is being calculated correclty.""" |
|
55
|
|
|
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", |
|
56
|
|
|
destination="172.16.10.30", options=b'1000', |
|
57
|
|
|
data=b'testdata') |
|
58
|
|
|
packet.pack() |
|
59
|
|
|
self.assertEqual(packet.checksum, 709) |
|
60
|
|
|
|