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