blob: c463c50b9eb8174a1f6eaebb3ad7c989a2568fa0 [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
Rich Lane5b2745c2013-11-30 00:36:24 -080034 from loxi.generic_util import OFReader
Rich Lanea22233e2013-04-25 13:18:41 -070035except ImportError:
36 exit("loxi package not found. Try setting PYTHONPATH.")
37
38class TestImports(unittest.TestCase):
39 def test_toplevel(self):
40 import loxi
41 self.assertTrue(hasattr(loxi, "ProtocolError"))
Rich Lane00549ea2013-04-25 13:33:16 -070042 self.assertEquals(loxi.version_names[3], "1.2")
Rich Lanea22233e2013-04-25 13:18:41 -070043 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 Lanec9fc57d2013-05-16 16:39:12 -070062# The majority of the serialization tests are created here using the files in
63# the test_data directory.
64class TestDataFiles(unittest.TestCase):
65 pass
66add_datafiles_tests(TestDataFiles, 'of12/', ofp)
Rich Lanea22233e2013-04-25 13:18:41 -070067
68class 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 Lane9d98adf2013-11-29 18:37:24 -080079 if isinstance(klass, type) and
80 issubclass(klass, loxi.OFObject) and
Rich Lane038c9e62014-01-27 15:33:18 -080081 not hasattr(klass, 'subtypes')]
Rich Lanea22233e2013-04-25 13:18:41 -070082 self.klasses.sort(key=lambda x: str(x))
83
84 def test_serialization(self):
Rich Lanebe90eae2013-07-22 16:44:26 -070085 expected_failures = [
86 ofp.action.set_field, # field defaults to None
87 ]
Rich Lanea22233e2013-04-25 13:18:41 -070088 for klass in self.klasses:
89 def fn():
90 obj = klass()
91 if hasattr(obj, "xid"): obj.xid = 42
92 buf = obj.pack()
Rich Lane5b2745c2013-11-30 00:36:24 -080093 obj2 = klass.unpack(OFReader(buf))
Rich Lanea22233e2013-04-25 13:18:41 -070094 self.assertEquals(obj, obj2)
95 if klass in expected_failures:
96 self.assertRaises(Exception, fn)
97 else:
98 fn()
99
Rich Lane65790d22013-06-14 00:08:52 -0700100 def test_parse_message(self):
Rich Laneefa54002013-06-14 07:26:27 -0700101 expected_failures = []
Rich Lane65790d22013-06-14 00:08:52 -0700102 for klass in self.klasses:
Rich Lane9d98adf2013-11-29 18:37:24 -0800103 if not issubclass(klass, ofp.message.message):
Rich Lane65790d22013-06-14 00:08:52 -0700104 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 Lanea22233e2013-04-25 13:18:41 -0700115 def test_show(self):
Rich Laneadb79832013-05-02 17:14:33 -0700116 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -0700117 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
127if __name__ == '__main__':
128 unittest.main()