blob: 3fdfb8e6a07d333eda45c8c875e1046f61e7bf72 [file] [log] [blame]
Rich Lane2dd8ed22014-10-03 16:17:09 -07001#!/usr/bin/env python
2# Copyright 2014, 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 2014, 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
29from testutil import test_serialization
30from testutil import add_datafiles_tests
31
32try:
33 import loxi
34 import loxi.of14 as ofp
35 from loxi.generic_util import OFReader
36except ImportError:
37 exit("loxi package not found. Try setting PYTHONPATH.")
38
39class TestImports(unittest.TestCase):
40 def test_toplevel(self):
41 import loxi
42 self.assertTrue(hasattr(loxi, "ProtocolError"))
43 self.assertEquals(loxi.version_names[5], "1.4")
44 ofp = loxi.protocol(5)
45 self.assertEquals(ofp.OFP_VERSION, 5)
46 self.assertTrue(hasattr(ofp, "action"))
47 self.assertTrue(hasattr(ofp, "common"))
48 self.assertTrue(hasattr(ofp, "const"))
49 self.assertTrue(hasattr(ofp, "message"))
50 self.assertTrue(hasattr(ofp, "oxm"))
51
52 def test_version(self):
53 import loxi
54 self.assertTrue(hasattr(loxi.of14, "ProtocolError"))
55 self.assertTrue(hasattr(loxi.of14, "OFP_VERSION"))
56 self.assertEquals(loxi.of14.OFP_VERSION, 5)
57 self.assertTrue(hasattr(loxi.of14, "action"))
58 self.assertTrue(hasattr(loxi.of14, "common"))
59 self.assertTrue(hasattr(loxi.of14, "const"))
60 self.assertTrue(hasattr(loxi.of14, "message"))
61 self.assertTrue(hasattr(loxi.of14, "oxm"))
62
63# The majority of the serialization tests are created here using the files in
64# the test_data directory.
65class TestDataFiles(unittest.TestCase):
66 pass
67add_datafiles_tests(TestDataFiles, 'of14/', ofp)
68
69class TestAllof14(unittest.TestCase):
70 """
71 Round-trips every class through serialization/deserialization.
72 Not a replacement for handcoded tests because it only uses the
73 default member values.
74 """
75
76 def setUp(self):
77 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
78 self.klasses = [klass for mod in mods
79 for klass in mod.__dict__.values()
80 if isinstance(klass, type) and
81 issubclass(klass, loxi.OFObject) and
82 not hasattr(klass, 'subtypes')]
83 self.klasses.sort(key=lambda x: str(x))
84
85 def test_serialization(self):
86 expected_failures = [
87 ofp.action.set_field, # field defaults to None
88 ]
89 for klass in self.klasses:
90 def fn():
91 obj = klass()
92 if hasattr(obj, "xid"): obj.xid = 42
93 buf = obj.pack()
94 obj2 = klass.unpack(OFReader(buf))
95 self.assertEquals(obj, obj2)
96 if klass in expected_failures:
97 self.assertRaises(Exception, fn)
98 else:
99 fn()
100
101 def test_parse_message(self):
102 expected_failures = [
103 ]
104 for klass in self.klasses:
105 if not issubclass(klass, ofp.message.message):
106 continue
107 def fn():
108 obj = klass(xid=42)
109 buf = obj.pack()
110 obj2 = ofp.message.parse_message(buf)
111 self.assertEquals(obj, obj2)
112 if klass in expected_failures:
113 self.assertRaises(Exception, fn)
114 else:
115 fn()
116
117 def test_show(self):
118 expected_failures = []
119 for klass in self.klasses:
120 def fn():
121 obj = klass()
122 if hasattr(obj, "xid"): obj.xid = 42
123 obj.show()
124 if klass in expected_failures:
125 self.assertRaises(Exception, fn)
126 else:
127 fn()
128
129if __name__ == '__main__':
130 unittest.main()