Passed
Pull Request — master (#404)
by
unknown
01:53
created

TestHWAddressMac.test_address_value()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
"""Tests for Python-openflow BasicTypes."""
2
import unittest
3
4
from pyof.foundation import basic_types
5
from pyof.foundation.basic_types import BinaryData
6
from tests.test_struct import TestStructDump
7
8
9
class TestUBInt8(TestStructDump):
10
    """Test UBInt8."""
11
12
    dump = b'\xff'
13
    obj = basic_types.UBInt8(2**8 - 1)
14
    min_size = 1
15
16
17
class TestUBInt16(TestStructDump):
18
    """Test UBInt16."""
19
20
    dump = b'\xff\xff'
21
    obj = basic_types.UBInt16(2**16 - 1)
22
    min_size = 2
23
24
25
class TestUBInt32(TestStructDump):
26
    """Test UBInt32."""
27
28
    dump = b'\xff\xff\xff\xff'
29
    obj = basic_types.UBInt32(2**32 - 1)
30
    min_size = 4
31
32
33
class TestChar3(TestStructDump):
34
    """Test Char with length 3."""
35
36
    dump = b'fo\x00'
37
    obj = basic_types.Char('foo', length=3)
38
    min_size = 3
39
40
41
class TestChar5(TestStructDump):
42
    """Test Char with length 5."""
43
44
    dump = b'foo\x00\x00'
45
    obj = basic_types.Char('foo', length=5)
46
    min_size = 5
47
48
49
class TestHWAddressMac(TestStructDump):
50
    """Test HWAddress mac value."""
51
52
    mac = '0a:d3:98:a5:30:47'
53
    dump = b'\x0a\xd3\x98\xa5\x30\x47'
54
    obj = basic_types.HWAddress(mac)
55
    min_size = 6
56
57
    def test_address_value(self):
58
        """Test HWAddress mac value."""
59
        self.assertEqual(self.obj.value, self.mac)
60
61
62
class TestIPAddressNetmask(TestStructDump):
63
    """Test IPAddress and its default netmask value."""
64
65
    dump = b'\xc0\xa8\x00\x01'
66
    obj = basic_types.IPAddress('192.168.0.1')
67
    netmask = 32
68
    min_size = 4
69
70
    def test_netmask(self):
71
        """Test IPAddress netmask value."""
72
        self.assertEqual(self.obj.netmask, self.netmask)
73
74
75
class TestIPAddressNoNetmask(TestIPAddressNetmask):
76
    """Test IPAdress and netmask value 16."""
77
78
    dump = b'\xc0\xa8\x00\x01'
79
    obj = basic_types.IPAddress('192.168.0.1/16')
80
    netmask = 16
81
    min_size = 4
82
83
84
class TestBinaryDataEmpty(TestStructDump):
85
    """Test empty BinaryData."""
86
87
    dump = b''
88
    obj = BinaryData()
89
    min_size = 0
90
91
92
class TestBinaryDataBytes(TestStructDump):
93
    """Test 'bytes' BinaryData."""
94
95
    dump = b'bytes'
96
    obj = BinaryData(b'bytes')
97
    min_size = 0
98
99
100
class TestIPAddress(unittest.TestCase):
101
    """Test of IPAddress BasicType max_prefix."""
102
103
    def test_max_prefix(self):
104
        """Testing get max_prefix from IPAddress."""
105
        ip_addr = basic_types.IPAddress()
106
        self.assertEqual(ip_addr.max_prefix, 32)
107
        ip_addr = basic_types.IPAddress('192.168.0.35/16')
108
        self.assertEqual(ip_addr.max_prefix, 32)
109
110
111
class TestBinaryData(unittest.TestCase):
112
    """Test Binary data type cannot accept string."""
113
114
    def test_unexpected_value(self):
115
        """Should raise ValueError if constructor value is not bytes."""
116
        self.assertRaises(ValueError, BinaryData, "can't be string")
117
118
    def test_unexpected_value_as_parameter(self):
119
        """Should raise ValueError if pack value is not bytes."""
120
        self.assertRaises(ValueError, BinaryData().pack, "can't be string")
121