blob: 69263827d6eb9cc3411b1dd4ffae9ab13fd6511d [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:
32 import loxi.of12 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[3], "1.2")
Rich Lanea22233e2013-04-25 13:18:41 -070041 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 Lanec9fc57d2013-05-16 16:39:12 -070060# The majority of the serialization tests are created here using the files in
61# the test_data directory.
62class TestDataFiles(unittest.TestCase):
63 pass
64add_datafiles_tests(TestDataFiles, 'of12/', ofp)
Rich Lanea22233e2013-04-25 13:18:41 -070065
66class 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 Laneadb79832013-05-02 17:14:33 -070081 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -070082 for klass in self.klasses:
83 def fn():
84 obj = klass()
85 if hasattr(obj, "xid"): obj.xid = 42
86 buf = obj.pack()
87 obj2 = klass.unpack(buf)
88 self.assertEquals(obj, obj2)
89 if klass in expected_failures:
90 self.assertRaises(Exception, fn)
91 else:
92 fn()
93
94 def test_show(self):
Rich Laneadb79832013-05-02 17:14:33 -070095 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -070096 for klass in self.klasses:
97 def fn():
98 obj = klass()
99 if hasattr(obj, "xid"): obj.xid = 42
100 obj.show()
101 if klass in expected_failures:
102 self.assertRaises(Exception, fn)
103 else:
104 fn()
105
106if __name__ == '__main__':
107 unittest.main()