1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2015 YAMAMOTO Takashi <yamamoto at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 import ryu.ofproto.ofproto_v1_3 as ofp
22 class Test_OXM(unittest.TestCase):
23 def _test_encode(self, user, on_wire):
25 (n, v, m) = ofp.oxm_from_user(f, uv)
27 ofp.oxm_serialize(n, v, m, buf, 0)
28 self.assertEqual(on_wire, buf)
30 def _test_decode(self, user, on_wire):
31 (n, v, m, l) = ofp.oxm_parse(on_wire, 0)
32 self.assertEqual(len(on_wire), l)
33 (f, uv) = ofp.oxm_to_user(n, v, m)
34 self.assertEqual(user, (f, uv))
36 def _test_encode_header(self, user, on_wire):
38 n = ofp.oxm_from_user_header(f)
40 ofp.oxm_serialize_header(n, buf, 0)
41 self.assertEqual(on_wire, buf)
43 def _test_decode_header(self, user, on_wire):
44 (n, l) = ofp.oxm_parse_header(on_wire, 0)
45 self.assertEqual(len(on_wire), l)
46 f = ofp.oxm_to_user_header(n)
47 self.assertEqual(user, f)
49 def _test(self, user, on_wire, header_bytes):
50 self._test_encode(user, on_wire)
51 self._test_decode(user, on_wire)
52 if isinstance(user[1], tuple): # has mask?
55 on_wire_header = on_wire[:header_bytes]
56 self._test_decode_header(user_header, on_wire_header)
57 if user_header.startswith('field_'):
58 return # not supported
59 self._test_encode_header(user_header, on_wire_header)
61 def test_basic_nomask(self):
62 user = ('ipv4_src', '192.0.2.1')
67 self._test(user, on_wire, 4)
69 def test_basic_mask(self):
70 user = ('ipv4_src', ('192.0.2.1', '255.255.0.0'))
76 self._test(user, on_wire, 4)
78 def test_exp_nomask(self):
79 user = ('_dp_hash', 0x12345678)
82 b'\x00\x00\x23\x20' # Nicira
85 self._test(user, on_wire, 8)
87 def test_exp_mask(self):
88 user = ('_dp_hash', (0x12345678, 0x7fffffff))
91 b'\x00\x00\x23\x20' # Nicira
95 self._test(user, on_wire, 8)
97 def test_exp_nomask_2(self):
98 user = ('tcp_flags', 0x876)
101 b'\x4f\x4e\x46\x00' # ONF
104 self._test(user, on_wire, 8)
106 def test_exp_mask_2(self):
107 user = ('tcp_flags', (0x876, 0x7ff))
110 b'\x4f\x4e\x46\x00' # ONF
114 self._test(user, on_wire, 8)
116 def test_exp_nomask_3(self):
117 user = ('actset_output', 0x98765432)
120 b'\x4f\x4e\x46\x00' # ONF
123 self._test(user, on_wire, 8)
125 def test_exp_mask_3(self):
126 user = ('actset_output', (0x98765432, 0xfffffffe))
129 b'\x4f\x4e\x46\x00' # ONF
133 self._test(user, on_wire, 8)
135 def test_nxm_1_nomask(self):
136 user = ('tun_ipv4_src', '192.0.2.1')
141 self._test(user, on_wire, 4)
143 def test_nxm_1_mask(self):
144 user = ('tun_ipv4_src', ('192.0.2.1', '255.255.0.0'))
150 self._test(user, on_wire, 4)
152 def test_ext_256_nomask(self):
153 user = ('pbb_uca', 50)
156 b'\x4f\x4e\x46\x00' # ONF
160 self._test(user, on_wire, 10)
162 def test_ext_256_mask(self):
163 user = ('pbb_uca', (50, 51))
166 b'\x4f\x4e\x46\x00' # ONF
171 self._test(user, on_wire, 10)
173 def test_basic_unknown_nomask(self):
174 user = ('field_100', 'aG9nZWhvZ2U=')
179 self._test(user, on_wire, 4)
181 def test_basic_unknown_mask(self):
182 user = ('field_100', ('aG9nZWhvZ2U=', 'ZnVnYWZ1Z2E='))
188 self._test(user, on_wire, 4)