1
|
|
|
"""Test OXM-related implementations.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
|
4
|
|
|
from pyof.foundation.exceptions import PackException, UnpackException |
5
|
|
|
from pyof.v0x04.common.flow_match import ( |
6
|
|
|
Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestMatch(TestCase): |
10
|
|
|
"""Test Match class.""" |
11
|
|
|
|
12
|
|
|
tlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, |
13
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, |
14
|
|
|
oxm_hasmask=True, oxm_value=b'abc') |
15
|
|
|
tlv2 = OxmTLV(oxm_class=OxmClass.OFPXMC_EXPERIMENTER, |
16
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_METADATA, |
17
|
|
|
oxm_hasmask=False, oxm_value=b'abcdef') |
18
|
|
|
match = Match(match_type=MatchType.OFPMT_OXM, |
19
|
|
|
oxm_match_fields=[tlv1, tlv2]) |
20
|
|
|
|
21
|
|
|
def test_unpacked_pack(self): |
22
|
|
|
"""Pack and then unpack the result and check for equality. |
23
|
|
|
|
24
|
|
|
Use two TLVs to also test match-field list packing/unpacking. |
25
|
|
|
""" |
26
|
|
|
unpacked = Match() |
27
|
|
|
unpacked.unpack(self.match.pack()) |
28
|
|
|
self.assertEqual(self.match, unpacked) |
29
|
|
|
|
30
|
|
|
def test_pack_other_instance(self): |
31
|
|
|
"""Test packing another Match instance by using the value argument.""" |
32
|
|
|
expected = self.match.pack() |
33
|
|
|
valued_pack = Match().pack(self.match) |
34
|
|
|
self.assertEqual(expected, valued_pack) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class TestOxmTLV(TestCase): |
38
|
|
|
"""Test OXM TLV pack and unpack.""" |
39
|
|
|
|
40
|
|
|
def setUp(self): |
41
|
|
|
"""Instantiate an OXM TLV struct.""" |
42
|
|
|
self.tlv = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, |
43
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, |
44
|
|
|
oxm_hasmask=False, oxm_value=b'') |
45
|
|
|
|
46
|
|
|
def test_different_class_types(self): |
47
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
48
|
|
|
for oxm_class in (OxmClass.OFPXMC_OPENFLOW_BASIC, |
49
|
|
|
OxmClass.OFPXMC_EXPERIMENTER): |
50
|
|
|
self.tlv.oxm_class = oxm_class |
51
|
|
|
unpacked = self._create_from_pack() |
52
|
|
|
self.assertEqual(oxm_class, unpacked.oxm_class) |
53
|
|
|
|
54
|
|
|
def test_different_fields(self): |
55
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
56
|
|
|
for oxm_field in (OxmOfbMatchField.OFPXMT_OFB_IN_PORT, |
57
|
|
|
OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR): |
58
|
|
|
self.tlv.oxm_field = oxm_field |
59
|
|
|
unpacked = self._create_from_pack() |
60
|
|
|
self.assertEqual(oxm_field, unpacked.oxm_field) |
61
|
|
|
|
62
|
|
|
def test_hasmask_bit(self): |
63
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
64
|
|
|
for oxm_hasmask in True, False: |
65
|
|
|
self.tlv.oxm_hasmask = oxm_hasmask |
66
|
|
|
unpacked = self._create_from_pack() |
67
|
|
|
self.assertEqual(oxm_hasmask, unpacked.oxm_hasmask) |
68
|
|
|
|
69
|
|
|
def test_different_values(self): |
70
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
71
|
|
|
for oxm_value in b'', b'abc': |
72
|
|
|
self.tlv.oxm_value = oxm_value |
73
|
|
|
unpacked = self._create_from_pack() |
74
|
|
|
self.assertEqual(oxm_value, unpacked.oxm_value) |
75
|
|
|
|
76
|
|
|
def _create_from_pack(self): |
77
|
|
|
"""Return a new instance by unpacking self.tlv.pack().""" |
78
|
|
|
unpacked = OxmTLV() |
79
|
|
|
unpacked.unpack(self.tlv.pack()) |
80
|
|
|
return unpacked |
81
|
|
|
|
82
|
|
|
def test_pack_overflowed_field(self): |
83
|
|
|
"""Raise PackException if field is bigger than 7 bit.""" |
84
|
|
|
self.tlv.oxm_class = OxmClass.OFPXMC_EXPERIMENTER |
85
|
|
|
self.tlv.oxm_field = 2**7 |
86
|
|
|
with self.assertRaises(PackException): |
87
|
|
|
self.tlv.pack() |
88
|
|
|
|
89
|
|
|
def test_pack_invalid_field(self): |
90
|
|
|
"""Raise PackException if field is invalid for a class. |
91
|
|
|
|
92
|
|
|
Example: field 42 is invalid for oxm_class OFPXMC_OPENFLOW_BASIC. |
93
|
|
|
""" |
94
|
|
|
self.tlv.oxm_class = OxmClass.OFPXMC_OPENFLOW_BASIC |
95
|
|
|
self.tlv.oxm_field = 42 |
96
|
|
|
with self.assertRaises(PackException): |
97
|
|
|
self.tlv.pack() |
98
|
|
|
|
99
|
|
|
def test_unpack_invalid_field(self): |
100
|
|
|
"""Raise UnpackException if field is invalid for a class. |
101
|
|
|
|
102
|
|
|
Example: field 42 is invalid for oxm_class OFPXMC_OPENFLOW_BASIC. |
103
|
|
|
""" |
104
|
|
|
field42 = b'\x80\x00T\x00' |
105
|
|
|
tlv = OxmTLV() |
106
|
|
|
with self.assertRaises(UnpackException): |
107
|
|
|
tlv.unpack(field42) |
108
|
|
|
|
109
|
|
|
def test_max_field_value(self): |
110
|
|
|
"""Use all bits of oxm_field.""" |
111
|
|
|
self.tlv.oxm_class = OxmClass.OFPXMC_EXPERIMENTER |
112
|
|
|
self.tlv.oxm_field = 127 |
113
|
|
|
unpacked = OxmTLV() |
114
|
|
|
unpacked.unpack(self.tlv.pack()) |
115
|
|
|
self.assertEqual(self.tlv, unpacked) |
116
|
|
|
|