blob: 98d999b8e61a238cc390ffe9a8572573dc8d0ad3 [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 Lanebe90eae2013-07-22 16:44:26 -070081 expected_failures = [
82 ofp.action.set_field, # field defaults to None
83 ]
Rich Lanea22233e2013-04-25 13:18:41 -070084 for klass in self.klasses:
85 def fn():
86 obj = klass()
87 if hasattr(obj, "xid"): obj.xid = 42
88 buf = obj.pack()
89 obj2 = klass.unpack(buf)
90 self.assertEquals(obj, obj2)
91 if klass in expected_failures:
92 self.assertRaises(Exception, fn)
93 else:
94 fn()
95
Rich Lane65790d22013-06-14 00:08:52 -070096 def test_parse_message(self):
Rich Laneefa54002013-06-14 07:26:27 -070097 expected_failures = []
Rich Lane65790d22013-06-14 00:08:52 -070098 for klass in self.klasses:
99 if not issubclass(klass, ofp.message.Message):
100 continue
101 def fn():
102 obj = klass(xid=42)
103 buf = obj.pack()
104 obj2 = ofp.message.parse_message(buf)
105 self.assertEquals(obj, obj2)
106 if klass in expected_failures:
107 self.assertRaises(Exception, fn)
108 else:
109 fn()
110
Rich Lanea22233e2013-04-25 13:18:41 -0700111 def test_show(self):
Rich Laneadb79832013-05-02 17:14:33 -0700112 expected_failures = []
Rich Lanea22233e2013-04-25 13:18:41 -0700113 for klass in self.klasses:
114 def fn():
115 obj = klass()
116 if hasattr(obj, "xid"): obj.xid = 42
117 obj.show()
118 if klass in expected_failures:
119 self.assertRaises(Exception, fn)
120 else:
121 fn()
122
123if __name__ == '__main__':
124 unittest.main()