Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 1 | #!/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. |
| 28 | import unittest |
| 29 | |
| 30 | try: |
| 31 | import loxi.of13 as ofp |
| 32 | except ImportError: |
| 33 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 34 | |
| 35 | class 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 Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame^] | 56 | class 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 Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 80 | class TestAllOF13(unittest.TestCase): |
| 81 | """ |
| 82 | Round-trips every class through serialization/deserialization. |
| 83 | Not a replacement for handcoded tests because it only uses the |
| 84 | default member values. |
| 85 | """ |
| 86 | |
| 87 | def setUp(self): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame^] | 88 | mods = [ofp.action,ofp.message,ofp.common,ofp.oxm] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 89 | self.klasses = [klass for mod in mods |
| 90 | for klass in mod.__dict__.values() |
| 91 | if hasattr(klass, 'show')] |
| 92 | self.klasses.sort(key=lambda x: str(x)) |
| 93 | |
| 94 | def test_serialization(self): |
| 95 | expected_failures = [ |
| 96 | ofp.common.action_id, |
| 97 | ofp.common.action_id_bsn_mirror, |
| 98 | ofp.common.action_id_bsn_set_tunnel_dst, |
| 99 | ofp.common.action_id_copy_ttl_in, |
| 100 | ofp.common.action_id_copy_ttl_out, |
| 101 | ofp.common.action_id_dec_mpls_ttl, |
| 102 | ofp.common.action_id_dec_nw_ttl, |
| 103 | ofp.common.action_id_experimenter, |
| 104 | ofp.common.action_id_group, |
| 105 | ofp.common.action_id_header, |
| 106 | ofp.common.action_id_nicira_dec_ttl, |
| 107 | ofp.common.action_id_output, |
| 108 | ofp.common.action_id_pop_mpls, |
| 109 | ofp.common.action_id_pop_pbb, |
| 110 | ofp.common.action_id_pop_vlan, |
| 111 | ofp.common.action_id_push_mpls, |
| 112 | ofp.common.action_id_push_pbb, |
| 113 | ofp.common.action_id_push_vlan, |
| 114 | ofp.common.action_id_set_field, |
| 115 | ofp.common.action_id_set_mpls_ttl, |
| 116 | ofp.common.action_id_set_nw_ttl, |
| 117 | ofp.common.action_id_set_queue, |
| 118 | ofp.common.flow_stats_entry, |
| 119 | ofp.common.group_desc_stats_entry, |
| 120 | ofp.common.hello_elem, |
| 121 | ofp.common.hello_elem_header, |
| 122 | ofp.common.hello_elem_versionbitmap, |
| 123 | ofp.common.instruction, |
| 124 | ofp.common.instruction_apply_actions, |
| 125 | ofp.common.instruction_clear_actions, |
| 126 | ofp.common.instruction_experimenter, |
| 127 | ofp.common.instruction_goto_table, |
| 128 | ofp.common.instruction_header, |
| 129 | ofp.common.instruction_meter, |
| 130 | ofp.common.instruction_write_actions, |
| 131 | ofp.common.instruction_write_metadata, |
| 132 | ofp.common.match_v3, |
| 133 | ofp.common.meter_band, |
| 134 | ofp.common.meter_band_drop, |
| 135 | ofp.common.meter_band_dscp_remark, |
| 136 | ofp.common.meter_band_experimenter, |
| 137 | ofp.common.meter_band_header, |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 138 | ofp.common.table_feature_prop, |
| 139 | ofp.common.table_feature_prop_apply_actions, |
| 140 | ofp.common.table_feature_prop_apply_actions_miss, |
| 141 | ofp.common.table_feature_prop_apply_setfield, |
| 142 | ofp.common.table_feature_prop_apply_setfield_miss, |
| 143 | ofp.common.table_feature_prop_experimenter, |
| 144 | ofp.common.table_feature_prop_header, |
| 145 | ofp.common.table_feature_prop_instructions, |
| 146 | ofp.common.table_feature_prop_instructions_miss, |
| 147 | ofp.common.table_feature_prop_match, |
| 148 | ofp.common.table_feature_prop_next_tables, |
| 149 | ofp.common.table_feature_prop_next_tables_miss, |
| 150 | ofp.common.table_feature_prop_wildcards, |
| 151 | ofp.common.table_feature_prop_write_actions, |
| 152 | ofp.common.table_feature_prop_write_actions_miss, |
| 153 | ofp.common.table_feature_prop_write_setfield, |
| 154 | ofp.common.table_feature_prop_write_setfield_miss, |
| 155 | ofp.message.aggregate_stats_request, |
| 156 | ofp.message.flow_add, |
| 157 | ofp.message.flow_delete, |
| 158 | ofp.message.flow_delete_strict, |
| 159 | ofp.message.flow_modify, |
| 160 | ofp.message.flow_modify_strict, |
| 161 | ofp.message.flow_removed, |
| 162 | ofp.message.flow_stats_request, |
| 163 | ofp.message.group_desc_stats_reply, |
| 164 | ofp.message.group_mod, |
| 165 | ofp.message.group_stats_reply, |
| 166 | ofp.message.meter_features_stats_reply, |
| 167 | ofp.message.meter_stats_reply, |
| 168 | ofp.message.packet_in, |
| 169 | ofp.message.table_features_stats_reply, |
| 170 | ofp.message.table_features_stats_request, |
| 171 | ] |
| 172 | for klass in self.klasses: |
| 173 | def fn(): |
| 174 | obj = klass() |
| 175 | if hasattr(obj, "xid"): obj.xid = 42 |
| 176 | buf = obj.pack() |
| 177 | obj2 = klass.unpack(buf) |
| 178 | self.assertEquals(obj, obj2) |
| 179 | if klass in expected_failures: |
| 180 | self.assertRaises(Exception, fn) |
| 181 | else: |
| 182 | fn() |
| 183 | |
| 184 | def test_show(self): |
| 185 | expected_failures = [ |
| 186 | ofp.common.action_id, |
| 187 | ofp.common.action_id_bsn_mirror, |
| 188 | ofp.common.action_id_bsn_set_tunnel_dst, |
| 189 | ofp.common.action_id_copy_ttl_in, |
| 190 | ofp.common.action_id_copy_ttl_out, |
| 191 | ofp.common.action_id_dec_mpls_ttl, |
| 192 | ofp.common.action_id_dec_nw_ttl, |
| 193 | ofp.common.action_id_experimenter, |
| 194 | ofp.common.action_id_group, |
| 195 | ofp.common.action_id_header, |
| 196 | ofp.common.action_id_nicira_dec_ttl, |
| 197 | ofp.common.action_id_output, |
| 198 | ofp.common.action_id_pop_mpls, |
| 199 | ofp.common.action_id_pop_pbb, |
| 200 | ofp.common.action_id_pop_vlan, |
| 201 | ofp.common.action_id_push_mpls, |
| 202 | ofp.common.action_id_push_pbb, |
| 203 | ofp.common.action_id_push_vlan, |
| 204 | ofp.common.action_id_set_field, |
| 205 | ofp.common.action_id_set_mpls_ttl, |
| 206 | ofp.common.action_id_set_nw_ttl, |
| 207 | ofp.common.action_id_set_queue, |
| 208 | ofp.common.flow_stats_entry, |
| 209 | ofp.common.group_desc_stats_entry, |
| 210 | ofp.common.hello_elem, |
| 211 | ofp.common.hello_elem_header, |
| 212 | ofp.common.hello_elem_versionbitmap, |
| 213 | ofp.common.instruction, |
| 214 | ofp.common.instruction_apply_actions, |
| 215 | ofp.common.instruction_clear_actions, |
| 216 | ofp.common.instruction_experimenter, |
| 217 | ofp.common.instruction_goto_table, |
| 218 | ofp.common.instruction_header, |
| 219 | ofp.common.instruction_meter, |
| 220 | ofp.common.instruction_write_actions, |
| 221 | ofp.common.instruction_write_metadata, |
| 222 | ofp.common.match_v3, |
| 223 | ofp.common.meter_band, |
| 224 | ofp.common.meter_band_drop, |
| 225 | ofp.common.meter_band_dscp_remark, |
| 226 | ofp.common.meter_band_experimenter, |
| 227 | ofp.common.meter_band_header, |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 228 | ofp.common.table_feature_prop, |
| 229 | ofp.common.table_feature_prop_apply_actions, |
| 230 | ofp.common.table_feature_prop_apply_actions_miss, |
| 231 | ofp.common.table_feature_prop_apply_setfield, |
| 232 | ofp.common.table_feature_prop_apply_setfield_miss, |
| 233 | ofp.common.table_feature_prop_experimenter, |
| 234 | ofp.common.table_feature_prop_header, |
| 235 | ofp.common.table_feature_prop_instructions, |
| 236 | ofp.common.table_feature_prop_instructions_miss, |
| 237 | ofp.common.table_feature_prop_match, |
| 238 | ofp.common.table_feature_prop_next_tables, |
| 239 | ofp.common.table_feature_prop_next_tables_miss, |
| 240 | ofp.common.table_feature_prop_wildcards, |
| 241 | ofp.common.table_feature_prop_write_actions, |
| 242 | ofp.common.table_feature_prop_write_actions_miss, |
| 243 | ofp.common.table_feature_prop_write_setfield, |
| 244 | ofp.common.table_feature_prop_write_setfield_miss, |
| 245 | ofp.message.aggregate_stats_request, |
| 246 | ofp.message.flow_add, |
| 247 | ofp.message.flow_delete, |
| 248 | ofp.message.flow_delete_strict, |
| 249 | ofp.message.flow_modify, |
| 250 | ofp.message.flow_modify_strict, |
| 251 | ofp.message.flow_removed, |
| 252 | ofp.message.flow_stats_request, |
| 253 | ofp.message.packet_in, |
| 254 | ] |
| 255 | for klass in self.klasses: |
| 256 | def fn(): |
| 257 | obj = klass() |
| 258 | if hasattr(obj, "xid"): obj.xid = 42 |
| 259 | obj.show() |
| 260 | if klass in expected_failures: |
| 261 | self.assertRaises(Exception, fn) |
| 262 | else: |
| 263 | fn() |
| 264 | |
| 265 | if __name__ == '__main__': |
| 266 | unittest.main() |