blob: fcdb0cf485ecc387ede1f358c357734a1693a2f3 [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
Rich Lanec9fc57d2013-05-16 16:39:12 -070029from testutil import add_datafiles_tests
Rich Lanea22233e2013-04-25 13:18:41 -070030
31try:
Rich Lane9d98adf2013-11-29 18:37:24 -080032 import loxi
Rich Lanea22233e2013-04-25 13:18:41 -070033 import loxi.of12 as ofp
34except ImportError:
35 exit("loxi package not found. Try setting PYTHONPATH.")
36
37class TestImports(unittest.TestCase):
38 def test_toplevel(self):
39 import loxi
40 self.assertTrue(hasattr(loxi, "ProtocolError"))
Rich Lane00549ea2013-04-25 13:33:16 -070041 self.assertEquals(loxi.version_names[3], "1.2")
Rich Lanea22233e2013-04-25 13:18:41 -070042 ofp = loxi.protocol(3)
43 self.assertEquals(ofp.OFP_VERSION, 3)
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 self.assertTrue(hasattr(ofp, "oxm"))
49
50 def test_version(self):
51 import loxi
52 self.assertTrue(hasattr(loxi.of12, "ProtocolError"))
53 self.assertTrue(hasattr(loxi.of12, "OFP_VERSION"))
54 self.assertEquals(loxi.of12.OFP_VERSION, 3)
55 self.assertTrue(hasattr(loxi.of12, "action"))
56 self.assertTrue(hasattr(loxi.of12, "common"))
57 self.assertTrue(hasattr(loxi.of12, "const"))
58 self.assertTrue(hasattr(loxi.of12, "message"))
59 self.assertTrue(hasattr(loxi.of12, "oxm"))
60
Rich Lanec9fc57d2013-05-16 16:39:12 -070061# The majority of the serialization tests are created here using the files in
62# the test_data directory.
63class TestDataFiles(unittest.TestCase):
64 pass
65add_datafiles_tests(TestDataFiles, 'of12/', ofp)
Rich Lanea22233e2013-04-25 13:18:41 -070066
67class TestAllOF12(unittest.TestCase):
68 """
69 Round-trips every class through serialization/deserialization.
70 Not a replacement for handcoded tests because it only uses the
71 default member values.
72 """
73
74 def setUp(self):
75 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
76 self.klasses = [klass for mod in mods
77 for klass in mod.__dict__.values()
Rich Lane9d98adf2013-11-29 18:37:24 -080078 if isinstance(klass, type) and
79 issubclass(klass, loxi.OFObject) and
80 hasattr(klass, 'pack')]
Rich Lanea22233e2013-04-25 13:18:41 -070081 self.klasses.sort(key=lambda x: str(x))
82
83 def test_serialization(self):
Rich Lanebe90eae2013-07-22 16:44:26 -070084 expected_failures = [
85 ofp.action.set_field, # field defaults to None
86 ]
Rich Lanea22233e2013-04-25 13:18:41 -070087 for klass in self.klasses:
88 def fn():
89 obj = klass()
90 if hasattr(obj, "xid"): obj.xid = 42
91 buf = obj.pack()
92 obj2 = klass.unpack(buf)
93 self.assertEquals(obj, obj2)
94 if klass in expected_failures:
95 self.assertRaises(Exception, fn)
96 else:
97 fn()
98
Rich Lane65790d22013-06-14 00:08:52 -070099 def test_parse_message(self):
Rich Laneefa54002013-06-14 07:26:27 -0700100 expected_failures = []
Rich Lane65790d22013-06-14 00:08:52 -0700101 for klass in self.klasses:
Rich Lane9d98adf2013-11-29 18:37:24 -0800102 if not issubclass(klass, ofp.message.message):
Rich Lane65790d22013-06-14 00:08:52 -0700103 continue
104 def fn():
105 obj = klass(xid=42)
106 buf = obj.pack()
107 obj2 = ofp.message.parse_message(buf)
108 self.assertEquals(obj, obj2)
109 if klass in expected_failures:
110 self.assertRaises(Exception, fn)
111 else:
112 fn()
113
Rich Lanea22233e2013-04-25 13:18:41 -0700114 def test_show(self):
Rich Laneadb79832013-05-02 17:14:33 -0700115 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -0700116 for klass in self.klasses:
117 def fn():
118 obj = klass()
119 if hasattr(obj, "xid"): obj.xid = 42
120 obj.show()
121 if klass in expected_failures:
122 self.assertRaises(Exception, fn)
123 else:
124 fn()
125
126if __name__ == '__main__':
127 unittest.main()