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