Passed
Push — master ( 1eeec4...42d182 )
by Diego Rabatone
01:50
created

TestHeader.test_random_xid()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
1
"""Testing Header structure."""
2
import os
3
import unittest
4
from unittest.mock import patch
0 ignored issues
show
Unused Code introduced by
Unused patch imported from unittest.mock
Loading history...
5
6
from pyof.v0x01.common.header import Header, Type
7
8
9
class TestHeader(unittest.TestCase):
10
    """Test the message Header."""
11
12
    def setUp(self):
13
        """Setup the TestHeader Class instantiating a HELLO header."""
14
        self.message = Header()
15
        self.message.message_type = Type.OFPT_HELLO
16
        self.message.xid = 1
17
        self.message.length = 0
18
19
    def test_size(self):
20
        """[Common/Header] - size 8."""
21
        self.assertEqual(self.message.get_size(), 8)
22
23
    @unittest.expectedFailure
24
    def test_pack_empty(self):
25
        """[Common/Header] - packing empty header."""
26
        self.assertRaises(TypeError,
27
                          Header().pack())
28
29
    def test_pack(self):
30
        """[Common/Header] - packing Hello."""
31
        packed_header = b'\x01\x00\x00\x00\x00\x00\x00\x01'
32
        self.assertEqual(self.message.pack(), packed_header)
33
34 View Code Duplication
    def test_unpack(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
        """[Common/Header] - unpacking Hello."""
36
        filename = os.path.join(os.path.dirname(os.path.realpath('__file__')),
37
                                'raw/v0x01/ofpt_hello.dat')
38
        f = open(filename, 'rb')
39
        self.message.unpack(f.read(8))
40
41
        self.assertEqual(self.message.length, 8)
42
        self.assertEqual(self.message.xid, 1)
43
        self.assertEqual(self.message.message_type, Type.OFPT_HELLO)
44
        self.assertEqual(self.message.version, 1)
45
46
        f.close()
47