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 |
| 29 | |
| 30 | try: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 31 | import loxi |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 32 | import loxi.of11 as ofp |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 33 | from loxi.generic_util import OFReader |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 34 | except ImportError: |
| 35 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 36 | |
| 37 | class TestImports(unittest.TestCase): |
| 38 | def test_toplevel(self): |
| 39 | import loxi |
| 40 | self.assertTrue(hasattr(loxi, "ProtocolError")) |
Rich Lane | 00549ea | 2013-04-25 13:33:16 -0700 | [diff] [blame] | 41 | self.assertEquals(loxi.version_names[2], "1.1") |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 42 | ofp = loxi.protocol(2) |
| 43 | self.assertEquals(ofp.OFP_VERSION, 2) |
| 44 | self.assertTrue(hasattr(ofp, "action")) |
| 45 | self.assertTrue(hasattr(ofp, "common")) |
| 46 | self.assertTrue(hasattr(ofp, "const")) |
| 47 | self.assertTrue(hasattr(ofp, "message")) |
| 48 | |
| 49 | def test_version(self): |
| 50 | import loxi |
| 51 | self.assertTrue(hasattr(loxi.of11, "ProtocolError")) |
| 52 | self.assertTrue(hasattr(loxi.of11, "OFP_VERSION")) |
| 53 | self.assertEquals(loxi.of11.OFP_VERSION, 2) |
| 54 | self.assertTrue(hasattr(loxi.of11, "action")) |
| 55 | self.assertTrue(hasattr(loxi.of11, "common")) |
| 56 | self.assertTrue(hasattr(loxi.of11, "const")) |
| 57 | self.assertTrue(hasattr(loxi.of11, "message")) |
| 58 | |
| 59 | class TestAllOF11(unittest.TestCase): |
| 60 | """ |
| 61 | Round-trips every class through serialization/deserialization. |
| 62 | Not a replacement for handcoded tests because it only uses the |
| 63 | default member values. |
| 64 | """ |
| 65 | |
| 66 | def setUp(self): |
| 67 | mods = [ofp.action,ofp.message,ofp.common] |
| 68 | self.klasses = [klass for mod in mods |
| 69 | for klass in mod.__dict__.values() |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 70 | if isinstance(klass, type) and |
| 71 | issubclass(klass, loxi.OFObject) and |
Rich Lane | 038c9e6 | 2014-01-27 15:33:18 -0800 | [diff] [blame^] | 72 | not hasattr(klass, 'subtypes')] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 73 | self.klasses.sort(key=lambda x: str(x)) |
| 74 | |
| 75 | def test_serialization(self): |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 76 | expected_failures = [] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 77 | for klass in self.klasses: |
| 78 | def fn(): |
| 79 | obj = klass() |
| 80 | if hasattr(obj, "xid"): obj.xid = 42 |
| 81 | buf = obj.pack() |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame] | 82 | obj2 = klass.unpack(OFReader(buf)) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 83 | self.assertEquals(obj, obj2) |
| 84 | if klass in expected_failures: |
| 85 | self.assertRaises(Exception, fn) |
| 86 | else: |
| 87 | fn() |
| 88 | |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 89 | def test_parse_message(self): |
Rich Lane | efa5400 | 2013-06-14 07:26:27 -0700 | [diff] [blame] | 90 | expected_failures = [] |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 91 | for klass in self.klasses: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 92 | if not issubclass(klass, ofp.message.message): |
Rich Lane | 65790d2 | 2013-06-14 00:08:52 -0700 | [diff] [blame] | 93 | continue |
| 94 | def fn(): |
| 95 | obj = klass(xid=42) |
| 96 | buf = obj.pack() |
| 97 | obj2 = ofp.message.parse_message(buf) |
| 98 | self.assertEquals(obj, obj2) |
| 99 | if klass in expected_failures: |
| 100 | self.assertRaises(Exception, fn) |
| 101 | else: |
| 102 | fn() |
| 103 | |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 104 | def test_show(self): |
Rich Lane | adb7983 | 2013-05-02 17:14:33 -0700 | [diff] [blame] | 105 | expected_failures = [] |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 106 | for klass in self.klasses: |
| 107 | def fn(): |
| 108 | obj = klass() |
| 109 | if hasattr(obj, "xid"): obj.xid = 42 |
| 110 | obj.show() |
| 111 | if klass in expected_failures: |
| 112 | self.assertRaises(Exception, fn) |
| 113 | else: |
| 114 | fn() |
| 115 | |
| 116 | if __name__ == '__main__': |
| 117 | unittest.main() |