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 |
Rich Lane | c9fc57d | 2013-05-16 16:39:12 -0700 | [diff] [blame] | 29 | from testutil import add_datafiles_tests |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 30 | |
| 31 | try: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 32 | import loxi |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 33 | import loxi.of12 as ofp |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 34 | from loxi.generic_util import OFReader |
Rich Lane | a22233e | 2013-04-25 13:18:41 -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[3], "1.2") |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 43 | ofp = loxi.protocol(3) |
| 44 | self.assertEquals(ofp.OFP_VERSION, 3) |
| 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 | self.assertTrue(hasattr(ofp, "oxm")) |
| 50 | |
| 51 | def test_version(self): |
| 52 | import loxi |
| 53 | self.assertTrue(hasattr(loxi.of12, "ProtocolError")) |
| 54 | self.assertTrue(hasattr(loxi.of12, "OFP_VERSION")) |
| 55 | self.assertEquals(loxi.of12.OFP_VERSION, 3) |
| 56 | self.assertTrue(hasattr(loxi.of12, "action")) |
| 57 | self.assertTrue(hasattr(loxi.of12, "common")) |
| 58 | self.assertTrue(hasattr(loxi.of12, "const")) |
| 59 | self.assertTrue(hasattr(loxi.of12, "message")) |
| 60 | self.assertTrue(hasattr(loxi.of12, "oxm")) |
| 61 | |
Rich Lane | c9fc57d | 2013-05-16 16:39:12 -0700 | [diff] [blame] | 62 | # The majority of the serialization tests are created here using the files in |
| 63 | # the test_data directory. |
| 64 | class TestDataFiles(unittest.TestCase): |
| 65 | pass |
| 66 | add_datafiles_tests(TestDataFiles, 'of12/', ofp) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 67 | |
| 68 | class TestAllOF12(unittest.TestCase): |
| 69 | """ |
| 70 | Round-trips every class through serialization/deserialization. |
| 71 | Not a replacement for handcoded tests because it only uses the |
| 72 | default member values. |
| 73 | """ |
| 74 | |
| 75 | def setUp(self): |
| 76 | mods = [ofp.action,ofp.message,ofp.common,ofp.oxm] |
| 77 | self.klasses = [klass for mod in mods |
| 78 | for klass in mod.__dict__.values() |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 79 | if isinstance(klass, type) and |
| 80 | issubclass(klass, loxi.OFObject) and |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame] | 81 | not hasattr(klass, 'subtypes')] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 82 | self.klasses.sort(key=lambda x: str(x)) |
| 83 | |
| 84 | def test_serialization(self): |
Rich Lane | be90eae | 2013-07-22 16:44:26 -0700 | [diff] [blame] | 85 | expected_failures = [ |
| 86 | ofp.action.set_field, # field defaults to None |
| 87 | ] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 88 | for klass in self.klasses: |
| 89 | def fn(): |
| 90 | obj = klass() |
| 91 | if hasattr(obj, "xid"): obj.xid = 42 |
| 92 | buf = obj.pack() |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 93 | obj2 = klass.unpack(OFReader(buf)) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 94 | self.assertEquals(obj, obj2) |
| 95 | if klass in expected_failures: |
| 96 | self.assertRaises(Exception, fn) |
| 97 | else: |
| 98 | fn() |
| 99 | |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 100 | def test_parse_message(self): |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 101 | expected_failures = [] |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 102 | for klass in self.klasses: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 103 | if not issubclass(klass, ofp.message.message): |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 104 | continue |
| 105 | def fn(): |
| 106 | obj = klass(xid=42) |
| 107 | buf = obj.pack() |
| 108 | obj2 = ofp.message.parse_message(buf) |
| 109 | self.assertEquals(obj, obj2) |
| 110 | if klass in expected_failures: |
| 111 | self.assertRaises(Exception, fn) |
| 112 | else: |
| 113 | fn() |
| 114 | |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 115 | def test_show(self): |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 116 | expected_failures = [] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 117 | for klass in self.klasses: |
| 118 | def fn(): |
| 119 | obj = klass() |
| 120 | if hasattr(obj, "xid"): obj.xid = 42 |
| 121 | obj.show() |
| 122 | if klass in expected_failures: |
| 123 | self.assertRaises(Exception, fn) |
| 124 | else: |
| 125 | fn() |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | unittest.main() |