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