|
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
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class TestChar5(TestStructDump): |
|
41
|
|
|
"""Test Char with length 5.""" |
|
42
|
|
|
|
|
43
|
|
|
dump = b'foo\x00\x00' |
|
44
|
|
|
obj = basic_types.Char('foo', length=5) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class TestHWAddressDefault(TestStructDump): |
|
48
|
|
|
"""Test HWAddress default value.""" |
|
49
|
|
|
|
|
50
|
|
|
dump = b'\x00\x00\x00\x00\x00\x00' |
|
51
|
|
|
obj = basic_types.HWAddress() |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
class TestHWAddressMac(TestStructDump): |
|
55
|
|
|
"""Test HWAddress mac value.""" |
|
56
|
|
|
|
|
57
|
|
|
mac = '00:00:00:00:00:00' |
|
58
|
|
|
dump = b'\x00\x00\x00\x00\x00\x00' |
|
59
|
|
|
obj = basic_types.HWAddress(mac) |
|
60
|
|
|
|
|
61
|
|
|
def test_address_value(self): |
|
62
|
|
|
"""Test HWAddress mac value.""" |
|
63
|
|
|
self.assertEqual(self.obj.value, self.mac) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
class TestHWAddressRandom(TestHWAddressMac): |
|
67
|
|
|
"""Test HWAddress mac value 0a:d3:98:a5:30:47.""" |
|
68
|
|
|
|
|
69
|
|
|
mac = '0a:d3:98:a5:30:47' |
|
70
|
|
|
dump = b'\x0a\xd3\x98\xa5\x30\x47' |
|
71
|
|
|
obj = basic_types.HWAddress(mac) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
class TestIPAddressNetmask(TestStructDump): |
|
75
|
|
|
"""Test IPAddress and its default netmask value.""" |
|
76
|
|
|
|
|
77
|
|
|
dump = b'\xc0\xa8\x00\x01' |
|
78
|
|
|
obj = basic_types.IPAddress('192.168.0.1') |
|
79
|
|
|
netmask = 32 |
|
80
|
|
|
|
|
81
|
|
|
def test_netmask(self): |
|
82
|
|
|
"""Test IPAddress netmask value.""" |
|
83
|
|
|
self.assertEqual(self.obj.netmask, self.netmask) |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
class TestIPAddressNoNetmask(TestIPAddressNetmask): |
|
87
|
|
|
"""Test IPAdress and netmask value 16.""" |
|
88
|
|
|
|
|
89
|
|
|
dump = b'\xc0\xa8\x00\x01' |
|
90
|
|
|
obj = basic_types.IPAddress('192.168.0.1/16') |
|
91
|
|
|
netmask = 16 |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
class TestBinaryDataEmpty(TestStructDump): |
|
95
|
|
|
"""Test empty BinaryData.""" |
|
96
|
|
|
|
|
97
|
|
|
dump = b'' |
|
98
|
|
|
obj = BinaryData() |
|
99
|
|
|
min_size = 0 |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
class TestBinaryDataBytes(TestStructDump): |
|
103
|
|
|
"""Test 'bytes' BinaryData.""" |
|
104
|
|
|
|
|
105
|
|
|
dump = b'bytes' |
|
106
|
|
|
obj = BinaryData(b'bytes') |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
class TestIPAddress(unittest.TestCase): |
|
110
|
|
|
"""Test of IPAddress BasicType max_prefix.""" |
|
111
|
|
|
|
|
112
|
|
|
def test_max_prefix(self): |
|
113
|
|
|
"""Testing get max_prefix from IPAddress.""" |
|
114
|
|
|
ip_addr = basic_types.IPAddress() |
|
115
|
|
|
self.assertEqual(ip_addr.max_prefix, 32) |
|
116
|
|
|
ip_addr = basic_types.IPAddress('192.168.0.35/16') |
|
117
|
|
|
self.assertEqual(ip_addr.max_prefix, 32) |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
class TestBinaryData(unittest.TestCase): |
|
121
|
|
|
"""Test Binary data type cannot accept string.""" |
|
122
|
|
|
|
|
123
|
|
|
def test_unexpected_value(self): |
|
124
|
|
|
"""Should raise ValueError if constructor value is not bytes.""" |
|
125
|
|
|
self.assertRaises(ValueError, BinaryData, "can't be string") |
|
126
|
|
|
|
|
127
|
|
|
def test_unexpected_value_as_parameter(self): |
|
128
|
|
|
"""Should raise ValueError if pack value is not bytes.""" |
|
129
|
|
|
self.assertRaises(ValueError, BinaryData().pack, "can't be string") |
|
130
|
|
|
|