1
|
|
|
"""Test OXM-related implementations.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
|
4
|
|
|
from pyof.v0x04.common.flow_match import ( |
5
|
|
|
Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestMatch(TestCase): |
9
|
|
|
"""Test Match class.""" |
10
|
|
|
|
11
|
|
|
def test_unpacked_pack(self): |
12
|
|
|
"""Pack and then unpack the result and check for equality. |
13
|
|
|
|
14
|
|
|
Use two TLVs to also test match-field list packing/unpacking. |
15
|
|
|
""" |
16
|
|
|
tlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, |
17
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, |
18
|
|
|
oxm_hasmask=True, oxm_value=b'abc') |
19
|
|
|
tlv2 = OxmTLV(oxm_class=OxmClass.OFPXMC_EXPERIMENTER, |
20
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_METADATA, |
21
|
|
|
oxm_hasmask=False, oxm_value=b'abcdef') |
22
|
|
|
|
23
|
|
|
match = Match(match_type=MatchType.OFPMT_OXM, |
24
|
|
|
oxm_match_fields=[tlv1, tlv2]) |
25
|
|
|
unpacked = Match() |
26
|
|
|
unpacked.unpack(match.pack()) |
27
|
|
|
self.assertEqual(match, unpacked) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class TestOxmTLV(TestCase): |
31
|
|
|
"""Test OXM TLV pack and unpack.""" |
32
|
|
|
|
33
|
|
|
def setUp(self): |
34
|
|
|
"""Instantiate an OXM TLV struct.""" |
35
|
|
|
self.tlv = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, |
36
|
|
|
oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, |
37
|
|
|
oxm_hasmask=False, oxm_value=b'') |
38
|
|
|
|
39
|
|
|
def test_different_class_types(self): |
40
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
41
|
|
|
for oxm_class in (OxmClass.OFPXMC_OPENFLOW_BASIC, |
42
|
|
|
OxmClass.OFPXMC_EXPERIMENTER): |
43
|
|
|
self.tlv.oxm_class = oxm_class |
44
|
|
|
unpacked = self._create_from_pack() |
45
|
|
|
self.assertEqual(oxm_class, unpacked.oxm_class) |
46
|
|
|
|
47
|
|
|
def test_different_fields(self): |
48
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
49
|
|
|
for oxm_field in (OxmOfbMatchField.OFPXMT_OFB_IN_PORT, |
50
|
|
|
OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR): |
51
|
|
|
self.tlv.oxm_field = oxm_field |
52
|
|
|
unpacked = self._create_from_pack() |
53
|
|
|
self.assertEqual(oxm_field, unpacked.oxm_field) |
54
|
|
|
|
55
|
|
|
def test_hasmask_bit(self): |
56
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
57
|
|
|
for oxm_hasmask in True, False: |
58
|
|
|
self.tlv.oxm_hasmask = oxm_hasmask |
59
|
|
|
unpacked = self._create_from_pack() |
60
|
|
|
self.assertEqual(oxm_hasmask, unpacked.oxm_hasmask) |
61
|
|
|
|
62
|
|
|
def test_different_values(self): |
63
|
|
|
"""Pack, unpack the result and assert the values are equal.""" |
64
|
|
|
for oxm_value in b'', b'abc': |
65
|
|
|
self.tlv.oxm_value = oxm_value |
66
|
|
|
unpacked = self._create_from_pack() |
67
|
|
|
self.assertEqual(oxm_value, unpacked.oxm_value) |
68
|
|
|
|
69
|
|
|
def _create_from_pack(self): |
70
|
|
|
"""Return a new instance by unpacking self.tlv.pack().""" |
71
|
|
|
unpacked = OxmTLV() |
72
|
|
|
unpacked.unpack(self.tlv.pack()) |
73
|
|
|
return unpacked |
74
|
|
|
|