blob: ab1b4d683322d8b50615beaed7d5febd3caf76ac [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"))
Rich Lane00549ea2013-04-25 13:33:16 -070039 self.assertEquals(loxi.version_names[4], "1.3")
Rich Lane3f075972013-03-15 22:56:29 -070040 ofp = loxi.protocol(4)
41 self.assertEquals(ofp.OFP_VERSION, 4)
42 self.assertTrue(hasattr(ofp, "action"))
43 self.assertTrue(hasattr(ofp, "common"))
44 self.assertTrue(hasattr(ofp, "const"))
45 self.assertTrue(hasattr(ofp, "message"))
Rich Lanea22233e2013-04-25 13:18:41 -070046 self.assertTrue(hasattr(ofp, "oxm"))
Rich Lane3f075972013-03-15 22:56:29 -070047
48 def test_version(self):
49 import loxi
50 self.assertTrue(hasattr(loxi.of13, "ProtocolError"))
51 self.assertTrue(hasattr(loxi.of13, "OFP_VERSION"))
52 self.assertEquals(loxi.of13.OFP_VERSION, 4)
53 self.assertTrue(hasattr(loxi.of13, "action"))
54 self.assertTrue(hasattr(loxi.of13, "common"))
55 self.assertTrue(hasattr(loxi.of13, "const"))
56 self.assertTrue(hasattr(loxi.of13, "message"))
Rich Lanea22233e2013-04-25 13:18:41 -070057 self.assertTrue(hasattr(loxi.of13, "oxm"))
Rich Lane3f075972013-03-15 22:56:29 -070058
Rich Lanee90685c2013-04-05 17:27:41 -070059class TestCommon(unittest.TestCase):
60 sample_hello_elem_buf = ''.join([
61 '\x00\x01', # type
62 '\x00\x0c', # length
63 '\x01\x23\x45\x67', # bitmaps[0]
64 '\x89\xab\xcd\xef', # bitmaps[1]
65 ])
66
67 def test_hello_elem_versionbitmap_pack(self):
68 obj = ofp.hello_elem_versionbitmap(bitmaps=[ofp.uint32(0x01234567),ofp.uint32(0x89abcdef)])
69 self.assertEquals(self.sample_hello_elem_buf, obj.pack())
70
71 def test_hello_elem_versionbitmap_unpack(self):
72 obj = ofp.hello_elem_versionbitmap.unpack(self.sample_hello_elem_buf)
73 self.assertEquals(len(obj.bitmaps), 2)
74 self.assertEquals(obj.bitmaps[0], ofp.uint32(0x01234567))
75 self.assertEquals(obj.bitmaps[1], ofp.uint32(0x89abcdef))
76
77 def test_list_hello_elem_unpack(self):
78 buf = ''.join([
79 '\x00\x01\x00\x04', # versionbitmap
80 '\x00\x00\x00\x04', # unknown type
81 '\x00\x01\x00\x04', # versionbitmap
82 ])
83 l = ofp.unpack_list_hello_elem(buf)
84 self.assertEquals(len(l), 2)
85 self.assertTrue(isinstance(l[0], ofp.hello_elem_versionbitmap))
86 self.assertTrue(isinstance(l[1], ofp.hello_elem_versionbitmap))
87
Rich Laneea693752013-03-18 11:05:45 -070088class TestOXM(unittest.TestCase):
89 def test_oxm_in_phy_port_pack(self):
90 import loxi.of13 as ofp
91 obj = ofp.oxm.in_phy_port(value=42)
92 expected = ''.join([
93 '\x80\x00', # class
94 '\x02', # type/masked
95 '\x08', # length
96 '\x00\x00\x00\x2a' # value
97 ])
98 self.assertEquals(expected, obj.pack())
99
100 def test_oxm_in_phy_port_masked_pack(self):
101 import loxi.of13 as ofp
102 obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
103 expected = ''.join([
104 '\x80\x00', # class
105 '\x03', # type/masked
106 '\x0c', # length
107 '\x00\x00\x00\x2a', # value
108 '\xaa\xbb\xcc\xdd' # mask
109 ])
110 self.assertEquals(expected, obj.pack())
111
Rich Lane41805642013-03-19 15:00:26 -0700112 def test_oxm_ipv6_dst_pack(self):
113 import loxi.of13 as ofp
114 obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f')
115 expected = ''.join([
116 '\x80\x00', # class
117 '\x36', # type/masked
118 '\x14', # length
119 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value
120 ])
121 self.assertEquals(expected, obj.pack())
122
Rich Lane3f075972013-03-15 22:56:29 -0700123class TestAllOF13(unittest.TestCase):
124 """
125 Round-trips every class through serialization/deserialization.
126 Not a replacement for handcoded tests because it only uses the
127 default member values.
128 """
129
130 def setUp(self):
Rich Laneea693752013-03-18 11:05:45 -0700131 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
Rich Lane3f075972013-03-15 22:56:29 -0700132 self.klasses = [klass for mod in mods
133 for klass in mod.__dict__.values()
134 if hasattr(klass, 'show')]
135 self.klasses.sort(key=lambda x: str(x))
136
137 def test_serialization(self):
138 expected_failures = [
139 ofp.common.action_id,
140 ofp.common.action_id_bsn_mirror,
141 ofp.common.action_id_bsn_set_tunnel_dst,
142 ofp.common.action_id_copy_ttl_in,
143 ofp.common.action_id_copy_ttl_out,
144 ofp.common.action_id_dec_mpls_ttl,
145 ofp.common.action_id_dec_nw_ttl,
146 ofp.common.action_id_experimenter,
147 ofp.common.action_id_group,
148 ofp.common.action_id_header,
149 ofp.common.action_id_nicira_dec_ttl,
150 ofp.common.action_id_output,
151 ofp.common.action_id_pop_mpls,
152 ofp.common.action_id_pop_pbb,
153 ofp.common.action_id_pop_vlan,
154 ofp.common.action_id_push_mpls,
155 ofp.common.action_id_push_pbb,
156 ofp.common.action_id_push_vlan,
157 ofp.common.action_id_set_field,
158 ofp.common.action_id_set_mpls_ttl,
159 ofp.common.action_id_set_nw_ttl,
160 ofp.common.action_id_set_queue,
161 ofp.common.flow_stats_entry,
162 ofp.common.group_desc_stats_entry,
Rich Lane3f075972013-03-15 22:56:29 -0700163 ofp.common.instruction,
164 ofp.common.instruction_apply_actions,
165 ofp.common.instruction_clear_actions,
166 ofp.common.instruction_experimenter,
167 ofp.common.instruction_goto_table,
168 ofp.common.instruction_header,
169 ofp.common.instruction_meter,
170 ofp.common.instruction_write_actions,
171 ofp.common.instruction_write_metadata,
172 ofp.common.match_v3,
173 ofp.common.meter_band,
174 ofp.common.meter_band_drop,
175 ofp.common.meter_band_dscp_remark,
176 ofp.common.meter_band_experimenter,
177 ofp.common.meter_band_header,
Rich Lane3f075972013-03-15 22:56:29 -0700178 ofp.common.table_feature_prop,
179 ofp.common.table_feature_prop_apply_actions,
180 ofp.common.table_feature_prop_apply_actions_miss,
181 ofp.common.table_feature_prop_apply_setfield,
182 ofp.common.table_feature_prop_apply_setfield_miss,
183 ofp.common.table_feature_prop_experimenter,
184 ofp.common.table_feature_prop_header,
185 ofp.common.table_feature_prop_instructions,
186 ofp.common.table_feature_prop_instructions_miss,
187 ofp.common.table_feature_prop_match,
188 ofp.common.table_feature_prop_next_tables,
189 ofp.common.table_feature_prop_next_tables_miss,
190 ofp.common.table_feature_prop_wildcards,
191 ofp.common.table_feature_prop_write_actions,
192 ofp.common.table_feature_prop_write_actions_miss,
193 ofp.common.table_feature_prop_write_setfield,
194 ofp.common.table_feature_prop_write_setfield_miss,
195 ofp.message.aggregate_stats_request,
196 ofp.message.flow_add,
197 ofp.message.flow_delete,
198 ofp.message.flow_delete_strict,
199 ofp.message.flow_modify,
200 ofp.message.flow_modify_strict,
201 ofp.message.flow_removed,
202 ofp.message.flow_stats_request,
203 ofp.message.group_desc_stats_reply,
204 ofp.message.group_mod,
205 ofp.message.group_stats_reply,
206 ofp.message.meter_features_stats_reply,
207 ofp.message.meter_stats_reply,
208 ofp.message.packet_in,
209 ofp.message.table_features_stats_reply,
210 ofp.message.table_features_stats_request,
211 ]
212 for klass in self.klasses:
213 def fn():
214 obj = klass()
215 if hasattr(obj, "xid"): obj.xid = 42
216 buf = obj.pack()
217 obj2 = klass.unpack(buf)
218 self.assertEquals(obj, obj2)
219 if klass in expected_failures:
220 self.assertRaises(Exception, fn)
221 else:
222 fn()
223
224 def test_show(self):
Rich Lane8ca3b772013-04-30 13:36:55 -0700225 expected_failures = []
Rich Lane3f075972013-03-15 22:56:29 -0700226 for klass in self.klasses:
227 def fn():
228 obj = klass()
229 if hasattr(obj, "xid"): obj.xid = 42
230 obj.show()
231 if klass in expected_failures:
232 self.assertRaises(Exception, fn)
233 else:
234 fn()
235
236if __name__ == '__main__':
237 unittest.main()