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