Rich Lane | a22233e | 2013-04-25 13:18:41 -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.of12 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(3) |
| 40 | self.assertEquals(ofp.OFP_VERSION, 3) |
| 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 | self.assertTrue(hasattr(ofp, "oxm")) |
| 46 | |
| 47 | def test_version(self): |
| 48 | import loxi |
| 49 | self.assertTrue(hasattr(loxi.of12, "ProtocolError")) |
| 50 | self.assertTrue(hasattr(loxi.of12, "OFP_VERSION")) |
| 51 | self.assertEquals(loxi.of12.OFP_VERSION, 3) |
| 52 | self.assertTrue(hasattr(loxi.of12, "action")) |
| 53 | self.assertTrue(hasattr(loxi.of12, "common")) |
| 54 | self.assertTrue(hasattr(loxi.of12, "const")) |
| 55 | self.assertTrue(hasattr(loxi.of12, "message")) |
| 56 | self.assertTrue(hasattr(loxi.of12, "oxm")) |
| 57 | |
| 58 | class TestOXM(unittest.TestCase): |
| 59 | def test_oxm_in_phy_port_pack(self): |
| 60 | import loxi.of12 as ofp |
| 61 | obj = ofp.oxm.in_phy_port(value=42) |
| 62 | expected = ''.join([ |
| 63 | '\x80\x00', # class |
| 64 | '\x02', # type/masked |
| 65 | '\x08', # length |
| 66 | '\x00\x00\x00\x2a' # value |
| 67 | ]) |
| 68 | self.assertEquals(expected, obj.pack()) |
| 69 | |
| 70 | def test_oxm_in_phy_port_masked_pack(self): |
| 71 | import loxi.of12 as ofp |
| 72 | obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd) |
| 73 | expected = ''.join([ |
| 74 | '\x80\x00', # class |
| 75 | '\x03', # type/masked |
| 76 | '\x0c', # length |
| 77 | '\x00\x00\x00\x2a', # value |
| 78 | '\xaa\xbb\xcc\xdd' # mask |
| 79 | ]) |
| 80 | self.assertEquals(expected, obj.pack()) |
| 81 | |
| 82 | def test_oxm_ipv6_dst_pack(self): |
| 83 | import loxi.of12 as ofp |
| 84 | obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f') |
| 85 | expected = ''.join([ |
| 86 | '\x80\x00', # class |
| 87 | '\x36', # type/masked |
| 88 | '\x14', # length |
| 89 | '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value |
| 90 | ]) |
| 91 | self.assertEquals(expected, obj.pack()) |
| 92 | |
| 93 | class TestAllOF12(unittest.TestCase): |
| 94 | """ |
| 95 | Round-trips every class through serialization/deserialization. |
| 96 | Not a replacement for handcoded tests because it only uses the |
| 97 | default member values. |
| 98 | """ |
| 99 | |
| 100 | def setUp(self): |
| 101 | mods = [ofp.action,ofp.message,ofp.common,ofp.oxm] |
| 102 | self.klasses = [klass for mod in mods |
| 103 | for klass in mod.__dict__.values() |
| 104 | if hasattr(klass, 'show')] |
| 105 | self.klasses.sort(key=lambda x: str(x)) |
| 106 | |
| 107 | def test_serialization(self): |
| 108 | expected_failures = [ |
| 109 | ofp.common.flow_stats_entry, |
| 110 | ofp.common.group_desc_stats_entry, |
| 111 | ofp.common.instruction, |
| 112 | ofp.common.instruction_apply_actions, |
| 113 | ofp.common.instruction_clear_actions, |
| 114 | ofp.common.instruction_experimenter, |
| 115 | ofp.common.instruction_goto_table, |
| 116 | ofp.common.instruction_header, |
| 117 | ofp.common.instruction_write_actions, |
| 118 | ofp.common.instruction_write_metadata, |
| 119 | ofp.common.match_v3, |
| 120 | ofp.common.table_stats_entry, |
| 121 | ofp.message.aggregate_stats_request, |
| 122 | ofp.message.flow_add, |
| 123 | ofp.message.flow_delete, |
| 124 | ofp.message.flow_delete_strict, |
| 125 | ofp.message.flow_modify, |
| 126 | ofp.message.flow_modify_strict, |
| 127 | ofp.message.flow_removed, |
| 128 | ofp.message.flow_stats_request, |
| 129 | ofp.message.group_desc_stats_reply, |
| 130 | ofp.message.group_mod, |
| 131 | ofp.message.group_stats_reply, |
| 132 | ofp.message.packet_in, |
| 133 | ] |
| 134 | for klass in self.klasses: |
| 135 | def fn(): |
| 136 | obj = klass() |
| 137 | if hasattr(obj, "xid"): obj.xid = 42 |
| 138 | buf = obj.pack() |
| 139 | obj2 = klass.unpack(buf) |
| 140 | self.assertEquals(obj, obj2) |
| 141 | if klass in expected_failures: |
| 142 | self.assertRaises(Exception, fn) |
| 143 | else: |
| 144 | fn() |
| 145 | |
| 146 | def test_show(self): |
| 147 | expected_failures = [ |
| 148 | ofp.common.flow_stats_entry, |
| 149 | ofp.common.group_desc_stats_entry, |
| 150 | ofp.common.instruction, |
| 151 | ofp.common.instruction_apply_actions, |
| 152 | ofp.common.instruction_clear_actions, |
| 153 | ofp.common.instruction_experimenter, |
| 154 | ofp.common.instruction_goto_table, |
| 155 | ofp.common.instruction_header, |
| 156 | ofp.common.instruction_write_actions, |
| 157 | ofp.common.instruction_write_metadata, |
| 158 | ofp.common.match_v3, |
| 159 | ofp.common.table_stats_entry, |
| 160 | ofp.message.aggregate_stats_request, |
| 161 | ofp.message.flow_add, |
| 162 | ofp.message.flow_delete, |
| 163 | ofp.message.flow_delete_strict, |
| 164 | ofp.message.flow_modify, |
| 165 | ofp.message.flow_modify_strict, |
| 166 | ofp.message.flow_removed, |
| 167 | ofp.message.flow_stats_request, |
| 168 | ofp.message.packet_in, |
| 169 | ] |
| 170 | for klass in self.klasses: |
| 171 | def fn(): |
| 172 | obj = klass() |
| 173 | if hasattr(obj, "xid"): obj.xid = 42 |
| 174 | obj.show() |
| 175 | if klass in expected_failures: |
| 176 | self.assertRaises(Exception, fn) |
| 177 | else: |
| 178 | fn() |
| 179 | |
| 180 | if __name__ == '__main__': |
| 181 | unittest.main() |