blob: 0294684510aa4c714060f9b826b7e5fe594f9eaf [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 Laneea693752013-03-18 11:05:45 -070056class TestOXM(unittest.TestCase):
57 def test_oxm_in_phy_port_pack(self):
58 import loxi.of13 as ofp
59 obj = ofp.oxm.in_phy_port(value=42)
60 expected = ''.join([
61 '\x80\x00', # class
62 '\x02', # type/masked
63 '\x08', # length
64 '\x00\x00\x00\x2a' # value
65 ])
66 self.assertEquals(expected, obj.pack())
67
68 def test_oxm_in_phy_port_masked_pack(self):
69 import loxi.of13 as ofp
70 obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
71 expected = ''.join([
72 '\x80\x00', # class
73 '\x03', # type/masked
74 '\x0c', # length
75 '\x00\x00\x00\x2a', # value
76 '\xaa\xbb\xcc\xdd' # mask
77 ])
78 self.assertEquals(expected, obj.pack())
79
Rich Lane41805642013-03-19 15:00:26 -070080 def test_oxm_ipv6_dst_pack(self):
81 import loxi.of13 as ofp
82 obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f')
83 expected = ''.join([
84 '\x80\x00', # class
85 '\x36', # type/masked
86 '\x14', # length
87 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value
88 ])
89 self.assertEquals(expected, obj.pack())
90
Rich Lane3f075972013-03-15 22:56:29 -070091class TestAllOF13(unittest.TestCase):
92 """
93 Round-trips every class through serialization/deserialization.
94 Not a replacement for handcoded tests because it only uses the
95 default member values.
96 """
97
98 def setUp(self):
Rich Laneea693752013-03-18 11:05:45 -070099 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
Rich Lane3f075972013-03-15 22:56:29 -0700100 self.klasses = [klass for mod in mods
101 for klass in mod.__dict__.values()
102 if hasattr(klass, 'show')]
103 self.klasses.sort(key=lambda x: str(x))
104
105 def test_serialization(self):
106 expected_failures = [
107 ofp.common.action_id,
108 ofp.common.action_id_bsn_mirror,
109 ofp.common.action_id_bsn_set_tunnel_dst,
110 ofp.common.action_id_copy_ttl_in,
111 ofp.common.action_id_copy_ttl_out,
112 ofp.common.action_id_dec_mpls_ttl,
113 ofp.common.action_id_dec_nw_ttl,
114 ofp.common.action_id_experimenter,
115 ofp.common.action_id_group,
116 ofp.common.action_id_header,
117 ofp.common.action_id_nicira_dec_ttl,
118 ofp.common.action_id_output,
119 ofp.common.action_id_pop_mpls,
120 ofp.common.action_id_pop_pbb,
121 ofp.common.action_id_pop_vlan,
122 ofp.common.action_id_push_mpls,
123 ofp.common.action_id_push_pbb,
124 ofp.common.action_id_push_vlan,
125 ofp.common.action_id_set_field,
126 ofp.common.action_id_set_mpls_ttl,
127 ofp.common.action_id_set_nw_ttl,
128 ofp.common.action_id_set_queue,
129 ofp.common.flow_stats_entry,
130 ofp.common.group_desc_stats_entry,
131 ofp.common.hello_elem,
132 ofp.common.hello_elem_header,
133 ofp.common.hello_elem_versionbitmap,
134 ofp.common.instruction,
135 ofp.common.instruction_apply_actions,
136 ofp.common.instruction_clear_actions,
137 ofp.common.instruction_experimenter,
138 ofp.common.instruction_goto_table,
139 ofp.common.instruction_header,
140 ofp.common.instruction_meter,
141 ofp.common.instruction_write_actions,
142 ofp.common.instruction_write_metadata,
143 ofp.common.match_v3,
144 ofp.common.meter_band,
145 ofp.common.meter_band_drop,
146 ofp.common.meter_band_dscp_remark,
147 ofp.common.meter_band_experimenter,
148 ofp.common.meter_band_header,
Rich Lane3f075972013-03-15 22:56:29 -0700149 ofp.common.table_feature_prop,
150 ofp.common.table_feature_prop_apply_actions,
151 ofp.common.table_feature_prop_apply_actions_miss,
152 ofp.common.table_feature_prop_apply_setfield,
153 ofp.common.table_feature_prop_apply_setfield_miss,
154 ofp.common.table_feature_prop_experimenter,
155 ofp.common.table_feature_prop_header,
156 ofp.common.table_feature_prop_instructions,
157 ofp.common.table_feature_prop_instructions_miss,
158 ofp.common.table_feature_prop_match,
159 ofp.common.table_feature_prop_next_tables,
160 ofp.common.table_feature_prop_next_tables_miss,
161 ofp.common.table_feature_prop_wildcards,
162 ofp.common.table_feature_prop_write_actions,
163 ofp.common.table_feature_prop_write_actions_miss,
164 ofp.common.table_feature_prop_write_setfield,
165 ofp.common.table_feature_prop_write_setfield_miss,
166 ofp.message.aggregate_stats_request,
167 ofp.message.flow_add,
168 ofp.message.flow_delete,
169 ofp.message.flow_delete_strict,
170 ofp.message.flow_modify,
171 ofp.message.flow_modify_strict,
172 ofp.message.flow_removed,
173 ofp.message.flow_stats_request,
174 ofp.message.group_desc_stats_reply,
175 ofp.message.group_mod,
176 ofp.message.group_stats_reply,
177 ofp.message.meter_features_stats_reply,
178 ofp.message.meter_stats_reply,
179 ofp.message.packet_in,
180 ofp.message.table_features_stats_reply,
181 ofp.message.table_features_stats_request,
182 ]
183 for klass in self.klasses:
184 def fn():
185 obj = klass()
186 if hasattr(obj, "xid"): obj.xid = 42
187 buf = obj.pack()
188 obj2 = klass.unpack(buf)
189 self.assertEquals(obj, obj2)
190 if klass in expected_failures:
191 self.assertRaises(Exception, fn)
192 else:
193 fn()
194
195 def test_show(self):
196 expected_failures = [
197 ofp.common.action_id,
198 ofp.common.action_id_bsn_mirror,
199 ofp.common.action_id_bsn_set_tunnel_dst,
200 ofp.common.action_id_copy_ttl_in,
201 ofp.common.action_id_copy_ttl_out,
202 ofp.common.action_id_dec_mpls_ttl,
203 ofp.common.action_id_dec_nw_ttl,
204 ofp.common.action_id_experimenter,
205 ofp.common.action_id_group,
206 ofp.common.action_id_header,
207 ofp.common.action_id_nicira_dec_ttl,
208 ofp.common.action_id_output,
209 ofp.common.action_id_pop_mpls,
210 ofp.common.action_id_pop_pbb,
211 ofp.common.action_id_pop_vlan,
212 ofp.common.action_id_push_mpls,
213 ofp.common.action_id_push_pbb,
214 ofp.common.action_id_push_vlan,
215 ofp.common.action_id_set_field,
216 ofp.common.action_id_set_mpls_ttl,
217 ofp.common.action_id_set_nw_ttl,
218 ofp.common.action_id_set_queue,
219 ofp.common.flow_stats_entry,
220 ofp.common.group_desc_stats_entry,
221 ofp.common.hello_elem,
222 ofp.common.hello_elem_header,
223 ofp.common.hello_elem_versionbitmap,
224 ofp.common.instruction,
225 ofp.common.instruction_apply_actions,
226 ofp.common.instruction_clear_actions,
227 ofp.common.instruction_experimenter,
228 ofp.common.instruction_goto_table,
229 ofp.common.instruction_header,
230 ofp.common.instruction_meter,
231 ofp.common.instruction_write_actions,
232 ofp.common.instruction_write_metadata,
233 ofp.common.match_v3,
234 ofp.common.meter_band,
235 ofp.common.meter_band_drop,
236 ofp.common.meter_band_dscp_remark,
237 ofp.common.meter_band_experimenter,
238 ofp.common.meter_band_header,
Rich Lane3f075972013-03-15 22:56:29 -0700239 ofp.common.table_feature_prop,
240 ofp.common.table_feature_prop_apply_actions,
241 ofp.common.table_feature_prop_apply_actions_miss,
242 ofp.common.table_feature_prop_apply_setfield,
243 ofp.common.table_feature_prop_apply_setfield_miss,
244 ofp.common.table_feature_prop_experimenter,
245 ofp.common.table_feature_prop_header,
246 ofp.common.table_feature_prop_instructions,
247 ofp.common.table_feature_prop_instructions_miss,
248 ofp.common.table_feature_prop_match,
249 ofp.common.table_feature_prop_next_tables,
250 ofp.common.table_feature_prop_next_tables_miss,
251 ofp.common.table_feature_prop_wildcards,
252 ofp.common.table_feature_prop_write_actions,
253 ofp.common.table_feature_prop_write_actions_miss,
254 ofp.common.table_feature_prop_write_setfield,
255 ofp.common.table_feature_prop_write_setfield_miss,
256 ofp.message.aggregate_stats_request,
257 ofp.message.flow_add,
258 ofp.message.flow_delete,
259 ofp.message.flow_delete_strict,
260 ofp.message.flow_modify,
261 ofp.message.flow_modify_strict,
262 ofp.message.flow_removed,
263 ofp.message.flow_stats_request,
264 ofp.message.packet_in,
265 ]
266 for klass in self.klasses:
267 def fn():
268 obj = klass()
269 if hasattr(obj, "xid"): obj.xid = 42
270 obj.show()
271 if klass in expected_failures:
272 self.assertRaises(Exception, fn)
273 else:
274 fn()
275
276if __name__ == '__main__':
277 unittest.main()