Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -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 |
Rich Lane | 9a3f1fd | 2013-05-10 16:29:17 -0700 | [diff] [blame] | 29 | import test_data |
Rich Lane | 5c4446e | 2013-05-16 13:53:51 -0700 | [diff] [blame] | 30 | from testutil import add_datafiles_tests |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 31 | |
| 32 | try: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 33 | import loxi |
Rich Lane | fb7de0e | 2013-03-29 16:50:29 -0700 | [diff] [blame] | 34 | import loxi.of10 as ofp |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 35 | from loxi.generic_util import OFReader |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | except ImportError: |
| 37 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 38 | |
| 39 | class TestImports(unittest.TestCase): |
| 40 | def test_toplevel(self): |
| 41 | import loxi |
| 42 | self.assertTrue(hasattr(loxi, "ProtocolError")) |
Rich Lane | 00549ea | 2013-04-25 13:33:16 -0700 | [diff] [blame] | 43 | self.assertEquals(loxi.version_names[1], "1.0") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 44 | ofp = loxi.protocol(1) |
| 45 | self.assertEquals(ofp.OFP_VERSION, 1) |
| 46 | self.assertTrue(hasattr(ofp, "action")) |
| 47 | self.assertTrue(hasattr(ofp, "common")) |
| 48 | self.assertTrue(hasattr(ofp, "const")) |
| 49 | self.assertTrue(hasattr(ofp, "message")) |
| 50 | |
| 51 | def test_version(self): |
Rich Lane | a94fb08 | 2013-04-04 17:04:17 -0700 | [diff] [blame] | 52 | import loxi |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 53 | self.assertTrue(hasattr(loxi.of10, "ProtocolError")) |
| 54 | self.assertTrue(hasattr(loxi.of10, "OFP_VERSION")) |
| 55 | self.assertEquals(loxi.of10.OFP_VERSION, 1) |
| 56 | self.assertTrue(hasattr(loxi.of10, "action")) |
| 57 | self.assertTrue(hasattr(loxi.of10, "common")) |
| 58 | self.assertTrue(hasattr(loxi.of10, "const")) |
| 59 | self.assertTrue(hasattr(loxi.of10, "message")) |
| 60 | |
| 61 | class TestActions(unittest.TestCase): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 62 | def test_output_equality(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 63 | action = ofp.action.output(port=1, max_len=0x1234) |
| 64 | action2 = ofp.action.output(port=1, max_len=0x1234) |
| 65 | self.assertEquals(action, action2) |
| 66 | |
| 67 | action2.port = 2 |
| 68 | self.assertNotEquals(action, action2) |
| 69 | action2.port = 1 |
| 70 | |
| 71 | action2.max_len = 0xffff |
| 72 | self.assertNotEquals(action, action2) |
| 73 | action2.max_len = 0x1234 |
| 74 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 75 | # Assumes action serialization/deserialization works |
| 76 | class TestActionList(unittest.TestCase): |
| 77 | def test_normal(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 78 | expected = [] |
| 79 | bufs = [] |
| 80 | |
| 81 | def add(action): |
| 82 | expected.append(action) |
| 83 | bufs.append(action.pack()) |
| 84 | |
| 85 | add(ofp.action.output(port=1, max_len=0xffff)) |
| 86 | add(ofp.action.output(port=2, max_len=0xffff)) |
| 87 | add(ofp.action.output(port=ofp.OFPP_IN_PORT, max_len=0xffff)) |
| 88 | add(ofp.action.bsn_set_tunnel_dst(dst=0x12345678)) |
| 89 | add(ofp.action.nicira_dec_ttl()) |
| 90 | |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 91 | actions = loxi.generic_util.unpack_list(OFReader(''.join(bufs)), ofp.action.action.unpack) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 92 | self.assertEquals(actions, expected) |
| 93 | |
| 94 | def test_empty_list(self): |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 95 | self.assertEquals(loxi.generic_util.unpack_list(OFReader(''), ofp.action.action.unpack), []) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 96 | |
| 97 | def test_invalid_list_length(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 98 | buf = '\x00' * 9 |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 99 | with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'): |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 100 | loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 101 | |
| 102 | def test_invalid_action_length(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 103 | buf = '\x00' * 8 |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 104 | with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'): |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 105 | loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 106 | |
| 107 | buf = '\x00\x00\x00\x04' |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 108 | with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'): |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 109 | loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | |
| 111 | buf = '\x00\x00\x00\x10\x00\x00\x00\x00' |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 112 | with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'): |
Rich Lane | 5bcc7c7 | 2013-12-01 15:49:49 -0800 | [diff] [blame] | 113 | loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 114 | |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 115 | def test_unknown_action_type(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 116 | buf = '\xff\xfe\x00\x08\x00\x00\x00\x00' |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 117 | result = loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack) |
| 118 | self.assertEquals(result, [ofp.action.action(type=0xfffe)]) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 119 | |
| 120 | class TestConstants(unittest.TestCase): |
| 121 | def test_ports(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 122 | self.assertEquals(0xffff, ofp.OFPP_NONE) |
| 123 | |
| 124 | def test_wildcards(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 125 | self.assertEquals(0xfc000, ofp.OFPFW_NW_DST_MASK) |
| 126 | |
| 127 | class TestCommon(unittest.TestCase): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 128 | def test_match(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 129 | match = ofp.match() |
| 130 | self.assertEquals(match.wildcards, ofp.OFPFW_ALL) |
| 131 | self.assertEquals(match.tcp_src, 0) |
| 132 | buf = match.pack() |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 133 | match2 = ofp.match.unpack(OFReader(buf)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 134 | self.assertEquals(match, match2) |
| 135 | |
| 136 | class TestMessages(unittest.TestCase): |
| 137 | def test_hello_construction(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 138 | msg = ofp.message.hello() |
| 139 | self.assertEquals(msg.version, ofp.OFP_VERSION) |
| 140 | self.assertEquals(msg.type, ofp.OFPT_HELLO) |
| 141 | self.assertEquals(msg.xid, None) |
| 142 | |
| 143 | msg = ofp.message.hello(xid=123) |
| 144 | self.assertEquals(msg.xid, 123) |
| 145 | |
| 146 | # 0 is a valid xid distinct from None |
| 147 | msg = ofp.message.hello(xid=0) |
| 148 | self.assertEquals(msg.xid, 0) |
| 149 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 150 | def test_echo_request_construction(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 151 | msg = ofp.message.echo_request(data="abc") |
| 152 | self.assertEquals(msg.data, "abc") |
| 153 | |
Rich Lane | 5c4446e | 2013-05-16 13:53:51 -0700 | [diff] [blame] | 154 | def test_echo_request_invalid_length(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 155 | buf = "\x01\x02\x00\x07\x12\x34\x56" |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 156 | with self.assertRaisesRegexp(ofp.ProtocolError, "Buffer too short"): |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 157 | ofp.message.echo_request.unpack(OFReader(buf)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 158 | |
| 159 | def test_echo_request_equality(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 160 | msg = ofp.message.echo_request(xid=0x12345678, data="abc") |
| 161 | msg2 = ofp.message.echo_request(xid=0x12345678, data="abc") |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 162 | self.assertEquals(msg, msg2) |
| 163 | |
| 164 | msg2.xid = 1 |
| 165 | self.assertNotEquals(msg, msg2) |
| 166 | msg2.xid = msg.xid |
| 167 | |
| 168 | msg2.data = "a" |
| 169 | self.assertNotEquals(msg, msg2) |
| 170 | msg2.data = msg.data |
| 171 | |
Rich Lane | b1f347d | 2013-05-16 16:39:12 -0700 | [diff] [blame] | 172 | # The majority of the serialization tests are created here using the files in |
| 173 | # the test_data directory. |
Rich Lane | 5c4446e | 2013-05-16 13:53:51 -0700 | [diff] [blame] | 174 | class TestDataFiles(unittest.TestCase): |
| 175 | pass |
Rich Lane | 5c4446e | 2013-05-16 13:53:51 -0700 | [diff] [blame] | 176 | add_datafiles_tests(TestDataFiles, 'of10/', ofp) |
| 177 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 178 | class TestParse(unittest.TestCase): |
| 179 | def test_parse_header(self): |
| 180 | import loxi |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 181 | |
| 182 | msg_ver, msg_type, msg_len, msg_xid = ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56\x78") |
| 183 | self.assertEquals(1, msg_ver) |
| 184 | self.assertEquals(4, msg_type) |
| 185 | self.assertEquals(45032, msg_len) |
| 186 | self.assertEquals(0x12345678, msg_xid) |
| 187 | |
| 188 | with self.assertRaisesRegexp(loxi.ProtocolError, "too short"): |
| 189 | ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56") |
| 190 | |
| 191 | def test_parse_message(self): |
| 192 | import loxi |
| 193 | import loxi.of10 as ofp |
| 194 | |
| 195 | buf = "\x01\x00\x00\x08\x12\x34\x56\x78" |
| 196 | msg = ofp.message.parse_message(buf) |
| 197 | assert(msg.xid == 0x12345678) |
| 198 | |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 199 | # Get a list of all concrete message classes |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 200 | test_klasses = [x for x in ofp.message.__dict__.values() |
| 201 | if type(x) == type |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 202 | and issubclass(x, ofp.message.message) |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 203 | and not hasattr(x, 'subtypes')] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 204 | |
| 205 | for klass in test_klasses: |
| 206 | self.assertIsInstance(ofp.message.parse_message(klass(xid=1).pack()), klass) |
| 207 | |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 208 | def test_parse_unknown_message(self): |
| 209 | import loxi |
| 210 | import loxi.of10 as ofp |
| 211 | |
| 212 | buf = "\x01\xfe\x00\x08\x12\x34\x56\x78" |
| 213 | msg = ofp.message.parse_message(buf) |
| 214 | self.assertIsInstance(msg, ofp.message.message) |
| 215 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 216 | class TestUtils(unittest.TestCase): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 217 | def test_pretty_wildcards(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 218 | self.assertEquals("OFPFW_ALL", ofp.util.pretty_wildcards(ofp.OFPFW_ALL)) |
| 219 | self.assertEquals("0", ofp.util.pretty_wildcards(0)) |
| 220 | self.assertEquals("OFPFW_DL_SRC|OFPFW_DL_DST", |
| 221 | ofp.util.pretty_wildcards(ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST)) |
| 222 | self.assertEquals("OFPFW_NW_SRC_MASK&0x2000", |
| 223 | ofp.util.pretty_wildcards(ofp.OFPFW_NW_SRC_ALL)) |
| 224 | self.assertEquals("OFPFW_NW_SRC_MASK&0x1a00", |
| 225 | ofp.util.pretty_wildcards(0x00001a00)) |
| 226 | self.assertEquals("OFPFW_IN_PORT|0x80000000", |
| 227 | ofp.util.pretty_wildcards(ofp.OFPFW_IN_PORT|0x80000000)) |
| 228 | |
| 229 | class TestAll(unittest.TestCase): |
| 230 | """ |
| 231 | Round-trips every class through serialization/deserialization. |
| 232 | Not a replacement for handcoded tests because it only uses the |
| 233 | default member values. |
| 234 | """ |
| 235 | |
| 236 | def setUp(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 237 | mods = [ofp.action,ofp.message,ofp.common] |
| 238 | self.klasses = [klass for mod in mods |
| 239 | for klass in mod.__dict__.values() |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 240 | if isinstance(klass, type) and |
| 241 | issubclass(klass, loxi.OFObject) and |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 242 | not hasattr(klass, 'subtypes')] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 243 | self.klasses.sort(key=lambda x: str(x)) |
| 244 | |
| 245 | def test_serialization(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 246 | expected_failures = [] |
| 247 | for klass in self.klasses: |
| 248 | def fn(): |
| 249 | obj = klass() |
| 250 | if hasattr(obj, "xid"): obj.xid = 42 |
| 251 | buf = obj.pack() |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 252 | obj2 = klass.unpack(OFReader(buf)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 253 | self.assertEquals(obj, obj2) |
| 254 | if klass in expected_failures: |
| 255 | self.assertRaises(Exception, fn) |
| 256 | else: |
| 257 | fn() |
| 258 | |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 259 | def test_parse_message(self): |
| 260 | expected_failures = [] |
| 261 | for klass in self.klasses: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 262 | if not issubclass(klass, ofp.message.message): |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 263 | continue |
| 264 | def fn(): |
| 265 | obj = klass(xid=42) |
| 266 | buf = obj.pack() |
| 267 | obj2 = ofp.message.parse_message(buf) |
| 268 | self.assertEquals(obj, obj2) |
| 269 | if klass in expected_failures: |
| 270 | self.assertRaises(Exception, fn) |
| 271 | else: |
| 272 | fn() |
| 273 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 274 | def test_show(self): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 275 | expected_failures = [] |
| 276 | for klass in self.klasses: |
| 277 | def fn(): |
| 278 | obj = klass() |
| 279 | if hasattr(obj, "xid"): obj.xid = 42 |
| 280 | obj.show() |
| 281 | if klass in expected_failures: |
| 282 | self.assertRaises(Exception, fn) |
| 283 | else: |
| 284 | fn() |
| 285 | |
| 286 | if __name__ == '__main__': |
| 287 | unittest.main() |