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 |
Rich Lane | 1d5b010 | 2013-05-10 16:28:17 -0700 | [diff] [blame] | 29 | from testutil import test_serialization |
Rich Lane | c9fc57d | 2013-05-16 16:39:12 -0700 | [diff] [blame] | 30 | from testutil import add_datafiles_tests |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 31 | |
| 32 | try: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 33 | import loxi |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 34 | import loxi.of13 as ofp |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 35 | from loxi.generic_util import OFReader |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -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[4], "1.3") |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 44 | ofp = loxi.protocol(4) |
| 45 | self.assertEquals(ofp.OFP_VERSION, 4) |
| 46 | self.assertTrue(hasattr(ofp, "action")) |
| 47 | self.assertTrue(hasattr(ofp, "common")) |
| 48 | self.assertTrue(hasattr(ofp, "const")) |
| 49 | self.assertTrue(hasattr(ofp, "message")) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 50 | self.assertTrue(hasattr(ofp, "oxm")) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 51 | |
| 52 | def test_version(self): |
| 53 | import loxi |
| 54 | self.assertTrue(hasattr(loxi.of13, "ProtocolError")) |
| 55 | self.assertTrue(hasattr(loxi.of13, "OFP_VERSION")) |
| 56 | self.assertEquals(loxi.of13.OFP_VERSION, 4) |
| 57 | self.assertTrue(hasattr(loxi.of13, "action")) |
| 58 | self.assertTrue(hasattr(loxi.of13, "common")) |
| 59 | self.assertTrue(hasattr(loxi.of13, "const")) |
| 60 | self.assertTrue(hasattr(loxi.of13, "message")) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 61 | self.assertTrue(hasattr(loxi.of13, "oxm")) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 62 | |
Rich Lane | c9fc57d | 2013-05-16 16:39:12 -0700 | [diff] [blame] | 63 | # The majority of the serialization tests are created here using the files in |
| 64 | # the test_data directory. |
| 65 | class TestDataFiles(unittest.TestCase): |
| 66 | pass |
| 67 | add_datafiles_tests(TestDataFiles, 'of13/', ofp) |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 68 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 69 | class TestAllOF13(unittest.TestCase): |
| 70 | """ |
| 71 | Round-trips every class through serialization/deserialization. |
| 72 | Not a replacement for handcoded tests because it only uses the |
| 73 | default member values. |
| 74 | """ |
| 75 | |
| 76 | def setUp(self): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 77 | mods = [ofp.action,ofp.message,ofp.common,ofp.oxm] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 78 | self.klasses = [klass for mod in mods |
| 79 | for klass in mod.__dict__.values() |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 80 | if isinstance(klass, type) and |
| 81 | issubclass(klass, loxi.OFObject) and |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 82 | not hasattr(klass, 'subtypes')] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 83 | self.klasses.sort(key=lambda x: str(x)) |
| 84 | |
| 85 | def test_serialization(self): |
| 86 | expected_failures = [ |
Rich Lane | be90eae | 2013-07-22 16:44:26 -0700 | [diff] [blame] | 87 | ofp.action.set_field, # field defaults to None |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 88 | ] |
| 89 | for klass in self.klasses: |
| 90 | def fn(): |
| 91 | obj = klass() |
| 92 | if hasattr(obj, "xid"): obj.xid = 42 |
| 93 | buf = obj.pack() |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 94 | obj2 = klass.unpack(OFReader(buf)) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 95 | self.assertEquals(obj, obj2) |
| 96 | if klass in expected_failures: |
| 97 | self.assertRaises(Exception, fn) |
| 98 | else: |
| 99 | fn() |
| 100 | |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 101 | def test_parse_message(self): |
| 102 | expected_failures = [ |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 103 | ] |
| 104 | for klass in self.klasses: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 105 | if not issubclass(klass, ofp.message.message): |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 106 | continue |
| 107 | def fn(): |
| 108 | obj = klass(xid=42) |
| 109 | buf = obj.pack() |
| 110 | obj2 = ofp.message.parse_message(buf) |
| 111 | self.assertEquals(obj, obj2) |
| 112 | if klass in expected_failures: |
| 113 | self.assertRaises(Exception, fn) |
| 114 | else: |
| 115 | fn() |
| 116 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 117 | def test_show(self): |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 118 | expected_failures = [] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 119 | for klass in self.klasses: |
| 120 | def fn(): |
| 121 | obj = klass() |
| 122 | if hasattr(obj, "xid"): obj.xid = 42 |
| 123 | obj.show() |
| 124 | if klass in expected_failures: |
| 125 | self.assertRaises(Exception, fn) |
| 126 | else: |
| 127 | fn() |
| 128 | |
Rich Lane | 3009c1a | 2014-11-13 15:43:09 -0800 | [diff] [blame] | 129 | class TestUtils(unittest.TestCase): |
| 130 | def check_bitmap_512(self, value, data): |
| 131 | self.assertEquals(data, ofp.util.pack_bitmap_512(set(value))) |
| 132 | self.assertEquals(ofp.util.unpack_bitmap_512(OFReader(data)), set(value)) |
| 133 | |
| 134 | def test_bitmap_512(self): |
| 135 | self.check_bitmap_512([0], "\x00" * 63 + "\x01") |
| 136 | self.check_bitmap_512([8], "\x00" * 62 + "\x01\x00") |
| 137 | self.check_bitmap_512([63], "\x00" * 56 + "\x80" + "\x00" * 7) |
| 138 | self.check_bitmap_512([64], "\x00" * 55 + "\x01" + "\x00" * 8) |
| 139 | self.check_bitmap_512([511], "\x80" + "\x00" * 63) |
| 140 | self.check_bitmap_512([5, 67, 90], "\x00" * 52 + "\x04\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x20") |
| 141 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 142 | if __name__ == '__main__': |
| 143 | unittest.main() |