Passed
Pull Request — master (#412)
by
unknown
01:59
created

TestEthernet.test_Ethernet_unpack()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
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 VLAN, Ethernet, 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 TestEthernet(unittest.TestCase):
21
    """Test Ethernet frames."""
22
23
    def test_Ethernet_pack(self):
24
        """Test pack method of Ethernet class without VLAN tag."""
25
        ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf',
26
                            source='00:15:af:d5:38:98', ether_type=0x800,
27
                            data=b'testdata')
28
        packed = ethernet.pack()
29
        expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58\x98\x08\x00testdata'
30
        self.assertEqual(packed, expected)
31
32
    def test_Ethernet_unpack(self):
33
        """Test pack method of Ethernet class without VLAN tag."""
34
        raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata'
35
        expected = Ethernet(destination='00:15:af:d5:38:98',
36
                            source='00:1f:3a:3e:9a:cf', ether_type=0x800,
37
                            data=b'testdata')
38
        expected.pack()
39
        unpacked = Ethernet()
40
        unpacked.unpack(raw)
41
        self.assertEqual(unpacked, expected)
42
43
    def test_Tagged_Ethernet_pack(self):
44
        """Test pack method of Ethernet class including VLAN tag."""
45
        ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf',
46
                            source='00:15:af:d5:38:98', vlan=VLAN(vid=200),
47
                            ether_type=0x800, data=b'testdata')
48
        packed = ethernet.pack()
49
        expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58'
50
        expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata'
51
        self.assertEqual(packed, expected)
52
53
    def test_Tagged_Ethernet_unpack(self):
54
        """Test pack method of Ethernet class including VLAN tag."""
55
        raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>'
56
        raw += b'\x9a\xcf\x81\x00!^\x08\x00testdata'
57
        expected = Ethernet(destination='00:15:af:d5:38:98',
58
                            source='00:1f:3a:3e:9a:cf', vlan=VLAN(pcp=1,
59
                                                                  vid=350),
60
                            ether_type=0x800, data=b'testdata')
61
        expected.pack()
62
        unpacked = Ethernet()
63
        unpacked.unpack(raw)
64
        self.assertEqual(unpacked, expected)
65
66
67
class TestVLAN(unittest.TestCase):
68
    """Test VLAN headers."""
69
70
    def test_VLAN_pack(self):
71
        """Test pack method of VLAN class."""
72
        vlan = VLAN(pcp=3, vid=20)
73
        packed = vlan.pack()
74
        expected = b'\x81\x00`\x14'
75
        self.assertEqual(packed, expected)
76
77
    def test_VLAN_unpack(self):
78
        """Test unpack method of VLAN class."""
79
        raw = b'\x81\x00\xa0{'
80
        expected = VLAN(pcp=5, vid=123)
81
        unpacked = VLAN()
82
        unpacked.unpack(raw)
83
        self.assertEqual(unpacked, expected)
84
85
86
class TestIPv4(unittest.TestCase):
87
    """Test IPv4 packets."""
88
89
    def test_IPv4_pack(self):
90
        """Test pack/unpack of IPv4 class."""
91
        packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",
92
                      destination="172.16.10.30", options=b'1000',
93
                      data=b'testdata')
94
        packed = packet.pack()
95
        expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02'
96
        expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata'
97
        self.assertEqual(packed, expected)
98
99
    def test_IPv4_unpack(self):
100
        """Test unpack of IPv4 binary packet."""
101
        raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W'
102
        raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata'
103
        expected = IPv4(dscp=20, ttl=128, protocol=6, source="10.154.78.129",
104
                        destination="192.168.199.204", options=b'1000',
105
                        data=b'somemoredata')
106
        expected.pack()
107
        unpacked = IPv4()
108
        unpacked.unpack(raw)
109
        self.assertEqual(unpacked, expected)
110
111
    def test_IPv4_size(self):
112
        """Test Header size for IPv4 packet."""
113
        packet = IPv4()
114
        packet.pack()
115
        self.assertEqual(20, packet.get_size())
116
        self.assertEqual(20, packet.length)
117
        self.assertEqual(20, packet.ihl * 4)
118
119
    def test_IPv4_checksum(self):
120
        """Test if the IPv4 checksum is being calculated correclty."""
121
        packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10",
122
                      destination="172.16.10.30", options=b'1000',
123
                      data=b'testdata')
124
        packet.pack()
125
        self.assertEqual(packet.checksum, 709)
126