|
1
|
|
|
"""Test Python-openflow network types.""" |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
from pyof.foundation.basic_types import BinaryData |
|
5
|
|
|
from pyof.foundation.exceptions import UnpackException |
|
6
|
|
|
from pyof.foundation.network_types import VLAN, Ethernet, GenericTLV, IPv4 |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class TestNetworkTypes(unittest.TestCase): |
|
10
|
|
|
"""Reproduce bugs found.""" |
|
11
|
|
|
|
|
12
|
|
|
def test_GenTLV_value_unpack(self): |
|
13
|
|
|
"""Value attribute should be the same after unpacking.""" |
|
14
|
|
|
value = BinaryData(b'test') |
|
15
|
|
|
tlv = GenericTLV(value=value) |
|
16
|
|
|
tlv_unpacked = GenericTLV() |
|
17
|
|
|
tlv_unpacked.unpack(tlv.pack()) |
|
18
|
|
|
self.assertEqual(tlv.value.value, tlv_unpacked.value.value) |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class TestEthernet(unittest.TestCase): |
|
22
|
|
|
"""Test Ethernet frames.""" |
|
23
|
|
|
|
|
24
|
|
|
def test_Ethernet_pack(self): |
|
25
|
|
|
"""Test pack method of Ethernet class without VLAN tag.""" |
|
26
|
|
|
ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf', |
|
27
|
|
|
source='00:15:af:d5:38:98', ether_type=0x800, |
|
28
|
|
|
data=b'testdata') |
|
29
|
|
|
packed = ethernet.pack() |
|
30
|
|
|
expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58\x98\x08\x00testdata' |
|
31
|
|
|
self.assertEqual(packed, expected) |
|
32
|
|
|
|
|
33
|
|
|
def test_Ethernet_unpack(self): |
|
34
|
|
|
"""Test pack method of Ethernet class without VLAN tag.""" |
|
35
|
|
|
raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata' |
|
36
|
|
|
expected = Ethernet(destination='00:15:af:d5:38:98', |
|
37
|
|
|
source='00:1f:3a:3e:9a:cf', ether_type=0x800, |
|
38
|
|
|
data=b'testdata') |
|
39
|
|
|
expected.pack() |
|
40
|
|
|
unpacked = Ethernet() |
|
41
|
|
|
unpacked.unpack(raw) |
|
42
|
|
|
self.assertEqual(unpacked, expected) |
|
43
|
|
|
|
|
44
|
|
|
def test_Tagged_Ethernet_pack(self): |
|
45
|
|
|
"""Test pack method of Ethernet class including VLAN tag.""" |
|
46
|
|
|
ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf', |
|
47
|
|
|
source='00:15:af:d5:38:98', vlan=VLAN(vid=200), |
|
48
|
|
|
ether_type=0x800, data=b'testdata') |
|
49
|
|
|
packed = ethernet.pack() |
|
50
|
|
|
expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58' |
|
51
|
|
|
expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata' |
|
52
|
|
|
self.assertEqual(packed, expected) |
|
53
|
|
|
|
|
54
|
|
|
def test_Tagged_Ethernet_unpack(self): |
|
55
|
|
|
"""Test pack method of Ethernet class including VLAN tag.""" |
|
56
|
|
|
raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>' |
|
57
|
|
|
raw += b'\x9a\xcf\x81\x00!^\x08\x00testdata' |
|
58
|
|
|
expected = Ethernet(destination='00:15:af:d5:38:98', |
|
59
|
|
|
source='00:1f:3a:3e:9a:cf', vlan=VLAN(pcp=1, |
|
60
|
|
|
vid=350), |
|
61
|
|
|
ether_type=0x800, data=b'testdata') |
|
62
|
|
|
expected.pack() |
|
63
|
|
|
unpacked = Ethernet() |
|
64
|
|
|
unpacked.unpack(raw) |
|
65
|
|
|
self.assertEqual(unpacked, expected) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
class TestVLAN(unittest.TestCase): |
|
69
|
|
|
"""Test VLAN headers.""" |
|
70
|
|
|
|
|
71
|
|
|
def test_VLAN_pack(self): |
|
72
|
|
|
"""Test pack method of VLAN class.""" |
|
73
|
|
|
vlan = VLAN(pcp=3, vid=20) |
|
74
|
|
|
packed = vlan.pack() |
|
75
|
|
|
expected = b'\x81\x00`\x14' |
|
76
|
|
|
self.assertEqual(packed, expected) |
|
77
|
|
|
|
|
78
|
|
|
def test_VLAN_unpack(self): |
|
79
|
|
|
"""Test unpack method of VLAN class.""" |
|
80
|
|
|
raw = b'\x81\x00\xa0{' |
|
81
|
|
|
expected = VLAN(pcp=5, vid=123) |
|
82
|
|
|
unpacked = VLAN() |
|
83
|
|
|
unpacked.unpack(raw) |
|
84
|
|
|
self.assertEqual(unpacked, expected) |
|
85
|
|
|
|
|
86
|
|
|
def test_unpack_wrong_tpid(self): |
|
87
|
|
|
"""Raise UnpackException if the tpid is not VLAN_TPID.""" |
|
88
|
|
|
raw = b'\x12\x34\xa0{' |
|
89
|
|
|
vlan = VLAN() |
|
90
|
|
|
with self.assertRaises(UnpackException): |
|
91
|
|
|
vlan.unpack(raw) |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
class TestIPv4(unittest.TestCase): |
|
95
|
|
|
"""Test IPv4 packets.""" |
|
96
|
|
|
|
|
97
|
|
|
def test_IPv4_pack(self): |
|
98
|
|
|
"""Test pack/unpack of IPv4 class.""" |
|
99
|
|
|
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", |
|
100
|
|
|
destination="172.16.10.30", options=b'1000', |
|
101
|
|
|
data=b'testdata') |
|
102
|
|
|
packed = packet.pack() |
|
103
|
|
|
expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02' |
|
104
|
|
|
expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata' |
|
105
|
|
|
self.assertEqual(packed, expected) |
|
106
|
|
|
|
|
107
|
|
|
def test_IPv4_unpack(self): |
|
108
|
|
|
"""Test unpack of IPv4 binary packet.""" |
|
109
|
|
|
raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W' |
|
110
|
|
|
raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata' |
|
111
|
|
|
expected = IPv4(dscp=20, ttl=128, protocol=6, source="10.154.78.129", |
|
112
|
|
|
destination="192.168.199.204", options=b'1000', |
|
113
|
|
|
data=b'somemoredata') |
|
114
|
|
|
expected.pack() |
|
115
|
|
|
unpacked = IPv4() |
|
116
|
|
|
unpacked.unpack(raw) |
|
117
|
|
|
self.assertEqual(unpacked, expected) |
|
118
|
|
|
|
|
119
|
|
|
def test_IPv4_size(self): |
|
120
|
|
|
"""Test Header size for IPv4 packet.""" |
|
121
|
|
|
packet = IPv4() |
|
122
|
|
|
packet.pack() |
|
123
|
|
|
self.assertEqual(20, packet.get_size()) |
|
124
|
|
|
self.assertEqual(20, packet.length) |
|
125
|
|
|
self.assertEqual(20, packet.ihl * 4) |
|
126
|
|
|
|
|
127
|
|
|
def test_IPv4_checksum(self): |
|
128
|
|
|
"""Test if the IPv4 checksum is being calculated correclty.""" |
|
129
|
|
|
packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", |
|
130
|
|
|
destination="172.16.10.30", options=b'1000', |
|
131
|
|
|
data=b'testdata') |
|
132
|
|
|
packet.pack() |
|
133
|
|
|
self.assertEqual(packet.checksum, 709) |
|
134
|
|
|
|