Passed
Pull Request — master (#374)
by macartur
02:27
created

TestBinaryData.test_default_value()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
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
7
8
class TestUBInt8(unittest.TestCase):
9
    """Test of UBInt8 BasicType."""
10
11
    def setUp(self):
12
        """Basic test setup."""
13
        self.ubint8 = basic_types.UBInt8()
14
15
    def test_get_size(self):
16
        """[Foundation/BasicTypes/UBInt8] - size 1."""
17
        self.assertEqual(self.ubint8.get_size(), 1)
18
19
    @unittest.skip('Not yet implemented')
20
    def test_pack(self):
21
        """[Foundation/BasicTypes/UBInt8] - packing."""
22
        pass
23
24
    @unittest.skip('Not yet implemented')
25
    def test_unpack(self):
26
        """[Foundation/BasicTypes/UBInt8] - unpacking."""
27
        pass
28
29
30
class TestUBInt16(unittest.TestCase):
31
    """Test of UBInt16 BasicType."""
32
33
    def setUp(self):
34
        """Basic test setup."""
35
        self.ubint16 = basic_types.UBInt16()
36
37
    def test_get_size(self):
38
        """[Foundation/BasicTypes/UBInt16] - size 2."""
39
        self.assertEqual(self.ubint16.get_size(), 2)
40
41
    @unittest.skip('Not yet implemented')
42
    def test_pack(self):
43
        """[Foundation/BasicTypes/UBInt16] - packing."""
44
        pass
45
46
    @unittest.skip('Not yet implemented')
47
    def test_unpack(self):
48
        """[Foundation/BasicTypes/UBInt16] - unpacking."""
49
        pass
50
51
52
class TestUBInt32(unittest.TestCase):
53
    """Test of UBInt32 BasicType."""
54
55
    def setUp(self):
56
        """Basic test setup."""
57
        self.ubint32 = basic_types.UBInt32()
58
59
    def test_get_size(self):
60
        """[Foundation/BasicTypes/UBInt32] - size 4."""
61
        self.assertEqual(self.ubint32.get_size(), 4)
62
63
    @unittest.skip('Not yet implemented')
64
    def test_pack(self):
65
        """[Foundation/BasicTypes/UBInt32] - packing."""
66
        pass
67
68
    @unittest.skip('Not yet implemented')
69
    def test_unpack(self):
70
        """[Foundation/BasicTypes/UBInt32] - unpacking."""
71
        pass
72
73
74
class TestChar(unittest.TestCase):
75
    """Test of Char BasicType."""
76
77
    def setUp(self):
78
        """Basic test setup."""
79
        self.char1 = basic_types.Char('foo', length=3)
80
        self.char2 = basic_types.Char('foo', length=5)
81
82
    def test_get_size(self):
83
        """[Foundation/BasicTypes/Char] - get_size."""
84
        self.assertEqual(self.char1.get_size(), 3)
85
        self.assertEqual(self.char2.get_size(), 5)
86
87
    def test_pack(self):
88
        """[Foundation/BasicTypes/Char] - packing."""
89
        self.assertEqual(self.char1.pack(), b'fo\x00')
90
        self.assertEqual(self.char2.pack(), b'foo\x00\x00')
91
92
    def test_unpack(self):
93
        """[Foundation/BasicTypes/Char] - unpacking."""
94
        char1 = basic_types.Char(length=3)
95
        char2 = basic_types.Char(length=5)
96
        char1.unpack(b'fo\x00')
97
        char2.unpack(b'foo\x00\x00')
98
99
        self.assertEqual(char1.value, 'fo')
100
        self.assertEqual(char2.value, 'foo')
101
102
103
class TestHWaddress(unittest.TestCase):
104
    """Test of HWAddress BasicType."""
105
106
    def test_unpack_packed(self):
107
        """Testing unpack of packed HWAddress."""
108
        mac = '0a:d3:98:a5:30:47'
109
        hw_addr = basic_types.HWAddress(mac)
110
        packed = hw_addr.pack()
111
        unpacked = basic_types.HWAddress()
112
        unpacked.unpack(packed)
113
        self.assertEqual(mac, unpacked.value)
114
115
    def test_default_value(self):
116
        """Testing default_value for HWAddress."""
117
        mac = '00:00:00:00:00:00'
118
        hw_addr = basic_types.HWAddress()
119
        packed = hw_addr.pack()
120
        unpacked = basic_types.HWAddress()
121
        unpacked.unpack(packed)
122
        self.assertEqual(mac, unpacked.value)
123
124
125
class TestIPAddress(unittest.TestCase):
126
    """Test of IPAddress BasicType."""
127
128
    def test_unpack_packed(self):
129
        """Test unpacking of packed IPAddress."""
130
        ip_addr = basic_types.IPAddress('192.168.0.1')
131
        packed = ip_addr.pack()
132
        unpacked = basic_types.IPAddress()
133
        unpacked.unpack(packed)
134
        self.assertEqual(ip_addr.value, unpacked.value)
135
136
    def test_unpack_packed_with_netmask(self):
137
        """Testing unpack of packed IPAddress with netmask."""
138
        ip_addr = basic_types.IPAddress('192.168.0.1/16')
139
        packed = ip_addr.pack()
140
        unpacked = basic_types.IPAddress()
141
        unpacked.unpack(packed)
142
        self.assertEqual(ip_addr.value, unpacked.value)
143
144
    def test_netmask(self):
145
        """Testing get netmask from IPAddress."""
146
        ip_addr = basic_types.IPAddress('192.168.0.1/24')
147
        self.assertEqual(ip_addr.netmask, 24)
148
        ip_addr = basic_types.IPAddress('192.168.0.1/16')
149
        self.assertEqual(ip_addr.netmask, 16)
150
        ip_addr = basic_types.IPAddress('192.168.0.1')
151
        self.assertEqual(ip_addr.netmask, 32)
152
153
    def test_max_prefix(self):
154
        """Testing get max_prefix from IPAddress."""
155
        ip_addr = basic_types.IPAddress()
156
        self.assertEqual(ip_addr.max_prefix, 32)
157
        ip_addr = basic_types.IPAddress('192.168.0.35/16')
158
        self.assertEqual(ip_addr.max_prefix, 32)
159
160
    def test_get_size(self):
161
        """Testing get_size from IPAddress."""
162
        ip_addr = basic_types.IPAddress('192.168.0.1/24')
163
        self.assertEqual(ip_addr.get_size(), 4)
164
165
166
class TestBinaryData(unittest.TestCase):
167
    """Test Binary data type."""
168
169
    def test_default_value(self):
170
        """Default packed value should be an empty byte."""
171
        expected = b''
172
        actual = BinaryData().pack()
173
        self.assertEqual(expected, actual)
174
175
    def test_pack_bytes(self):
176
        """Test packing some bytes."""
177
        expected = b'forty two'
178
        actual = BinaryData(expected).pack()
179
        self.assertEqual(expected, actual)
180
181
    def test_pack_empty_bytes(self):
182
        """Test packing empty bytes."""
183
        expected = b''
184
        actual = BinaryData(expected).pack()
185
        self.assertEqual(expected, actual)
186
187
    def test_unexpected_value(self):
188
        """Should raise ValueError if constructor value is not bytes."""
189
        self.assertRaises(ValueError, BinaryData, "can't be string")
190
191
    def test_unexpected_value_as_parameter(self):
192
        """Should raise ValueError if pack value is not bytes."""
193
        self.assertRaises(ValueError, BinaryData().pack, "can't be string")
194