blob: b30a40c1f03ad83f4ac5cf2c5dd279b7f1bdf71d [file] [log] [blame]
Rich Lanea22233e2013-04-25 13:18:41 -07001#!/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.
28import unittest
29
30try:
Rich Lane9d98adf2013-11-29 18:37:24 -080031 import loxi
Rich Lanea22233e2013-04-25 13:18:41 -070032 import loxi.of11 as ofp
33except ImportError:
34 exit("loxi package not found. Try setting PYTHONPATH.")
35
36class TestImports(unittest.TestCase):
37 def test_toplevel(self):
38 import loxi
39 self.assertTrue(hasattr(loxi, "ProtocolError"))
Rich Lane00549ea2013-04-25 13:33:16 -070040 self.assertEquals(loxi.version_names[2], "1.1")
Rich Lanea22233e2013-04-25 13:18:41 -070041 ofp = loxi.protocol(2)
42 self.assertEquals(ofp.OFP_VERSION, 2)
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
48 def test_version(self):
49 import loxi
50 self.assertTrue(hasattr(loxi.of11, "ProtocolError"))
51 self.assertTrue(hasattr(loxi.of11, "OFP_VERSION"))
52 self.assertEquals(loxi.of11.OFP_VERSION, 2)
53 self.assertTrue(hasattr(loxi.of11, "action"))
54 self.assertTrue(hasattr(loxi.of11, "common"))
55 self.assertTrue(hasattr(loxi.of11, "const"))
56 self.assertTrue(hasattr(loxi.of11, "message"))
57
58class TestAllOF11(unittest.TestCase):
59 """
60 Round-trips every class through serialization/deserialization.
61 Not a replacement for handcoded tests because it only uses the
62 default member values.
63 """
64
65 def setUp(self):
66 mods = [ofp.action,ofp.message,ofp.common]
67 self.klasses = [klass for mod in mods
68 for klass in mod.__dict__.values()
Rich Lane9d98adf2013-11-29 18:37:24 -080069 if isinstance(klass, type) and
70 issubclass(klass, loxi.OFObject) and
71 hasattr(klass, 'pack')]
Rich Lanea22233e2013-04-25 13:18:41 -070072 self.klasses.sort(key=lambda x: str(x))
73
74 def test_serialization(self):
Rich Laneadb79832013-05-02 17:14:33 -070075 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -070076 for klass in self.klasses:
77 def fn():
78 obj = klass()
79 if hasattr(obj, "xid"): obj.xid = 42
80 buf = obj.pack()
81 obj2 = klass.unpack(buf)
82 self.assertEquals(obj, obj2)
83 if klass in expected_failures:
84 self.assertRaises(Exception, fn)
85 else:
86 fn()
87
Rich Lane65790d22013-06-14 00:08:52 -070088 def test_parse_message(self):
Rich Laneefa54002013-06-14 07:26:27 -070089 expected_failures = []
Rich Lane65790d22013-06-14 00:08:52 -070090 for klass in self.klasses:
Rich Lane9d98adf2013-11-29 18:37:24 -080091 if not issubclass(klass, ofp.message.message):
Rich Lane65790d22013-06-14 00:08:52 -070092 continue
93 def fn():
94 obj = klass(xid=42)
95 buf = obj.pack()
96 obj2 = ofp.message.parse_message(buf)
97 self.assertEquals(obj, obj2)
98 if klass in expected_failures:
99 self.assertRaises(Exception, fn)
100 else:
101 fn()
102
Rich Lanea22233e2013-04-25 13:18:41 -0700103 def test_show(self):
Rich Laneadb79832013-05-02 17:14:33 -0700104 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -0700105 for klass in self.klasses:
106 def fn():
107 obj = klass()
108 if hasattr(obj, "xid"): obj.xid = 42
109 obj.show()
110 if klass in expected_failures:
111 self.assertRaises(Exception, fn)
112 else:
113 fn()
114
115if __name__ == '__main__':
116 unittest.main()