blob: 68cbb1ad8dbb75098a77c39eef34c809b9ec7b16 [file] [log] [blame]
Rich Lane3f075972013-03-15 22:56:29 -07001#!/usr/bin/env python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28import unittest
29
30try:
31 import loxi.of13 as ofp
32except ImportError:
33 exit("loxi package not found. Try setting PYTHONPATH.")
34
35class TestImports(unittest.TestCase):
36 def test_toplevel(self):
37 import loxi
38 self.assertTrue(hasattr(loxi, "ProtocolError"))
39 ofp = loxi.protocol(4)
40 self.assertEquals(ofp.OFP_VERSION, 4)
41 self.assertTrue(hasattr(ofp, "action"))
42 self.assertTrue(hasattr(ofp, "common"))
43 self.assertTrue(hasattr(ofp, "const"))
44 self.assertTrue(hasattr(ofp, "message"))
45
46 def test_version(self):
47 import loxi
48 self.assertTrue(hasattr(loxi.of13, "ProtocolError"))
49 self.assertTrue(hasattr(loxi.of13, "OFP_VERSION"))
50 self.assertEquals(loxi.of13.OFP_VERSION, 4)
51 self.assertTrue(hasattr(loxi.of13, "action"))
52 self.assertTrue(hasattr(loxi.of13, "common"))
53 self.assertTrue(hasattr(loxi.of13, "const"))
54 self.assertTrue(hasattr(loxi.of13, "message"))
55
Rich Lanee90685c2013-04-05 17:27:41 -070056class TestCommon(unittest.TestCase):
57 sample_hello_elem_buf = ''.join([
58 '\x00\x01', # type
59 '\x00\x0c', # length
60 '\x01\x23\x45\x67', # bitmaps[0]
61 '\x89\xab\xcd\xef', # bitmaps[1]
62 ])
63
64 def test_hello_elem_versionbitmap_pack(self):
65 obj = ofp.hello_elem_versionbitmap(bitmaps=[ofp.uint32(0x01234567),ofp.uint32(0x89abcdef)])
66 self.assertEquals(self.sample_hello_elem_buf, obj.pack())
67
68 def test_hello_elem_versionbitmap_unpack(self):
69 obj = ofp.hello_elem_versionbitmap.unpack(self.sample_hello_elem_buf)
70 self.assertEquals(len(obj.bitmaps), 2)
71 self.assertEquals(obj.bitmaps[0], ofp.uint32(0x01234567))
72 self.assertEquals(obj.bitmaps[1], ofp.uint32(0x89abcdef))
73
74 def test_list_hello_elem_unpack(self):
75 buf = ''.join([
76 '\x00\x01\x00\x04', # versionbitmap
77 '\x00\x00\x00\x04', # unknown type
78 '\x00\x01\x00\x04', # versionbitmap
79 ])
80 l = ofp.unpack_list_hello_elem(buf)
81 self.assertEquals(len(l), 2)
82 self.assertTrue(isinstance(l[0], ofp.hello_elem_versionbitmap))
83 self.assertTrue(isinstance(l[1], ofp.hello_elem_versionbitmap))
84
Rich Laneea693752013-03-18 11:05:45 -070085class TestOXM(unittest.TestCase):
86 def test_oxm_in_phy_port_pack(self):
87 import loxi.of13 as ofp
88 obj = ofp.oxm.in_phy_port(value=42)
89 expected = ''.join([
90 '\x80\x00', # class
91 '\x02', # type/masked
92 '\x08', # length
93 '\x00\x00\x00\x2a' # value
94 ])
95 self.assertEquals(expected, obj.pack())
96
97 def test_oxm_in_phy_port_masked_pack(self):
98 import loxi.of13 as ofp
99 obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
100 expected = ''.join([
101 '\x80\x00', # class
102 '\x03', # type/masked
103 '\x0c', # length
104 '\x00\x00\x00\x2a', # value
105 '\xaa\xbb\xcc\xdd' # mask
106 ])
107 self.assertEquals(expected, obj.pack())
108
Rich Lane41805642013-03-19 15:00:26 -0700109 def test_oxm_ipv6_dst_pack(self):
110 import loxi.of13 as ofp
111 obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f')
112 expected = ''.join([
113 '\x80\x00', # class
114 '\x36', # type/masked
115 '\x14', # length
116 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value
117 ])
118 self.assertEquals(expected, obj.pack())
119
Rich Lane3f075972013-03-15 22:56:29 -0700120class TestAllOF13(unittest.TestCase):
121 """
122 Round-trips every class through serialization/deserialization.
123 Not a replacement for handcoded tests because it only uses the
124 default member values.
125 """
126
127 def setUp(self):
Rich Laneea693752013-03-18 11:05:45 -0700128 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
Rich Lane3f075972013-03-15 22:56:29 -0700129 self.klasses = [klass for mod in mods
130 for klass in mod.__dict__.values()
131 if hasattr(klass, 'show')]
132 self.klasses.sort(key=lambda x: str(x))
133
134 def test_serialization(self):
135 expected_failures = [
136 ofp.common.action_id,
137 ofp.common.action_id_bsn_mirror,
138 ofp.common.action_id_bsn_set_tunnel_dst,
139 ofp.common.action_id_copy_ttl_in,
140 ofp.common.action_id_copy_ttl_out,
141 ofp.common.action_id_dec_mpls_ttl,
142 ofp.common.action_id_dec_nw_ttl,
143 ofp.common.action_id_experimenter,
144 ofp.common.action_id_group,
145 ofp.common.action_id_header,
146 ofp.common.action_id_nicira_dec_ttl,
147 ofp.common.action_id_output,
148 ofp.common.action_id_pop_mpls,
149 ofp.common.action_id_pop_pbb,
150 ofp.common.action_id_pop_vlan,
151 ofp.common.action_id_push_mpls,
152 ofp.common.action_id_push_pbb,
153 ofp.common.action_id_push_vlan,
154 ofp.common.action_id_set_field,
155 ofp.common.action_id_set_mpls_ttl,
156 ofp.common.action_id_set_nw_ttl,
157 ofp.common.action_id_set_queue,
158 ofp.common.flow_stats_entry,
159 ofp.common.group_desc_stats_entry,
Rich Lane3f075972013-03-15 22:56:29 -0700160 ofp.common.instruction,
161 ofp.common.instruction_apply_actions,
162 ofp.common.instruction_clear_actions,
163 ofp.common.instruction_experimenter,
164 ofp.common.instruction_goto_table,
165 ofp.common.instruction_header,
166 ofp.common.instruction_meter,
167 ofp.common.instruction_write_actions,
168 ofp.common.instruction_write_metadata,
169 ofp.common.match_v3,
170 ofp.common.meter_band,
171 ofp.common.meter_band_drop,
172 ofp.common.meter_band_dscp_remark,
173 ofp.common.meter_band_experimenter,
174 ofp.common.meter_band_header,
Rich Lane3f075972013-03-15 22:56:29 -0700175 ofp.common.table_feature_prop,
176 ofp.common.table_feature_prop_apply_actions,
177 ofp.common.table_feature_prop_apply_actions_miss,
178 ofp.common.table_feature_prop_apply_setfield,
179 ofp.common.table_feature_prop_apply_setfield_miss,
180 ofp.common.table_feature_prop_experimenter,
181 ofp.common.table_feature_prop_header,
182 ofp.common.table_feature_prop_instructions,
183 ofp.common.table_feature_prop_instructions_miss,
184 ofp.common.table_feature_prop_match,
185 ofp.common.table_feature_prop_next_tables,
186 ofp.common.table_feature_prop_next_tables_miss,
187 ofp.common.table_feature_prop_wildcards,
188 ofp.common.table_feature_prop_write_actions,
189 ofp.common.table_feature_prop_write_actions_miss,
190 ofp.common.table_feature_prop_write_setfield,
191 ofp.common.table_feature_prop_write_setfield_miss,
192 ofp.message.aggregate_stats_request,
193 ofp.message.flow_add,
194 ofp.message.flow_delete,
195 ofp.message.flow_delete_strict,
196 ofp.message.flow_modify,
197 ofp.message.flow_modify_strict,
198 ofp.message.flow_removed,
199 ofp.message.flow_stats_request,
200 ofp.message.group_desc_stats_reply,
201 ofp.message.group_mod,
202 ofp.message.group_stats_reply,
203 ofp.message.meter_features_stats_reply,
204 ofp.message.meter_stats_reply,
205 ofp.message.packet_in,
206 ofp.message.table_features_stats_reply,
207 ofp.message.table_features_stats_request,
208 ]
209 for klass in self.klasses:
210 def fn():
211 obj = klass()
212 if hasattr(obj, "xid"): obj.xid = 42
213 buf = obj.pack()
214 obj2 = klass.unpack(buf)
215 self.assertEquals(obj, obj2)
216 if klass in expected_failures:
217 self.assertRaises(Exception, fn)
218 else:
219 fn()
220
221 def test_show(self):
222 expected_failures = [
223 ofp.common.action_id,
224 ofp.common.action_id_bsn_mirror,
225 ofp.common.action_id_bsn_set_tunnel_dst,
226 ofp.common.action_id_copy_ttl_in,
227 ofp.common.action_id_copy_ttl_out,
228 ofp.common.action_id_dec_mpls_ttl,
229 ofp.common.action_id_dec_nw_ttl,
230 ofp.common.action_id_experimenter,
231 ofp.common.action_id_group,
232 ofp.common.action_id_header,
233 ofp.common.action_id_nicira_dec_ttl,
234 ofp.common.action_id_output,
235 ofp.common.action_id_pop_mpls,
236 ofp.common.action_id_pop_pbb,
237 ofp.common.action_id_pop_vlan,
238 ofp.common.action_id_push_mpls,
239 ofp.common.action_id_push_pbb,
240 ofp.common.action_id_push_vlan,
241 ofp.common.action_id_set_field,
242 ofp.common.action_id_set_mpls_ttl,
243 ofp.common.action_id_set_nw_ttl,
244 ofp.common.action_id_set_queue,
245 ofp.common.flow_stats_entry,
246 ofp.common.group_desc_stats_entry,
Rich Lane3f075972013-03-15 22:56:29 -0700247 ofp.common.instruction,
248 ofp.common.instruction_apply_actions,
249 ofp.common.instruction_clear_actions,
250 ofp.common.instruction_experimenter,
251 ofp.common.instruction_goto_table,
252 ofp.common.instruction_header,
253 ofp.common.instruction_meter,
254 ofp.common.instruction_write_actions,
255 ofp.common.instruction_write_metadata,
256 ofp.common.match_v3,
257 ofp.common.meter_band,
258 ofp.common.meter_band_drop,
259 ofp.common.meter_band_dscp_remark,
260 ofp.common.meter_band_experimenter,
261 ofp.common.meter_band_header,
Rich Lane3f075972013-03-15 22:56:29 -0700262 ofp.common.table_feature_prop,
263 ofp.common.table_feature_prop_apply_actions,
264 ofp.common.table_feature_prop_apply_actions_miss,
265 ofp.common.table_feature_prop_apply_setfield,
266 ofp.common.table_feature_prop_apply_setfield_miss,
267 ofp.common.table_feature_prop_experimenter,
268 ofp.common.table_feature_prop_header,
269 ofp.common.table_feature_prop_instructions,
270 ofp.common.table_feature_prop_instructions_miss,
271 ofp.common.table_feature_prop_match,
272 ofp.common.table_feature_prop_next_tables,
273 ofp.common.table_feature_prop_next_tables_miss,
274 ofp.common.table_feature_prop_wildcards,
275 ofp.common.table_feature_prop_write_actions,
276 ofp.common.table_feature_prop_write_actions_miss,
277 ofp.common.table_feature_prop_write_setfield,
278 ofp.common.table_feature_prop_write_setfield_miss,
279 ofp.message.aggregate_stats_request,
280 ofp.message.flow_add,
281 ofp.message.flow_delete,
282 ofp.message.flow_delete_strict,
283 ofp.message.flow_modify,
284 ofp.message.flow_modify_strict,
285 ofp.message.flow_removed,
286 ofp.message.flow_stats_request,
287 ofp.message.packet_in,
288 ]
289 for klass in self.klasses:
290 def fn():
291 obj = klass()
292 if hasattr(obj, "xid"): obj.xid = 42
293 obj.show()
294 if klass in expected_failures:
295 self.assertRaises(Exception, fn)
296 else:
297 fn()
298
299if __name__ == '__main__':
300 unittest.main()