blob: 2f144b5365868b4272dedc2290a046be0c50f7c7 [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
29
30try:
31 import loxi.of12 as ofp
32except ImportError:
33 exit("loxi package not found. Try setting PYTHONPATH.")
34
35class TestImports(unittest.TestCase):
36 def test_toplevel(self):
37 import loxi
38 self.assertTrue(hasattr(loxi, "ProtocolError"))
Rich Lane00549ea2013-04-25 13:33:16 -070039 self.assertEquals(loxi.version_names[3], "1.2")
Rich Lanea22233e2013-04-25 13:18:41 -070040 ofp = loxi.protocol(3)
41 self.assertEquals(ofp.OFP_VERSION, 3)
42 self.assertTrue(hasattr(ofp, "action"))
43 self.assertTrue(hasattr(ofp, "common"))
44 self.assertTrue(hasattr(ofp, "const"))
45 self.assertTrue(hasattr(ofp, "message"))
46 self.assertTrue(hasattr(ofp, "oxm"))
47
48 def test_version(self):
49 import loxi
50 self.assertTrue(hasattr(loxi.of12, "ProtocolError"))
51 self.assertTrue(hasattr(loxi.of12, "OFP_VERSION"))
52 self.assertEquals(loxi.of12.OFP_VERSION, 3)
53 self.assertTrue(hasattr(loxi.of12, "action"))
54 self.assertTrue(hasattr(loxi.of12, "common"))
55 self.assertTrue(hasattr(loxi.of12, "const"))
56 self.assertTrue(hasattr(loxi.of12, "message"))
57 self.assertTrue(hasattr(loxi.of12, "oxm"))
58
59class TestOXM(unittest.TestCase):
60 def test_oxm_in_phy_port_pack(self):
61 import loxi.of12 as ofp
62 obj = ofp.oxm.in_phy_port(value=42)
63 expected = ''.join([
64 '\x80\x00', # class
65 '\x02', # type/masked
66 '\x08', # length
67 '\x00\x00\x00\x2a' # value
68 ])
69 self.assertEquals(expected, obj.pack())
70
71 def test_oxm_in_phy_port_masked_pack(self):
72 import loxi.of12 as ofp
73 obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
74 expected = ''.join([
75 '\x80\x00', # class
76 '\x03', # type/masked
77 '\x0c', # length
78 '\x00\x00\x00\x2a', # value
79 '\xaa\xbb\xcc\xdd' # mask
80 ])
81 self.assertEquals(expected, obj.pack())
82
83 def test_oxm_ipv6_dst_pack(self):
84 import loxi.of12 as ofp
85 obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f')
86 expected = ''.join([
87 '\x80\x00', # class
88 '\x36', # type/masked
89 '\x14', # length
90 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value
91 ])
92 self.assertEquals(expected, obj.pack())
93
94class TestAllOF12(unittest.TestCase):
95 """
96 Round-trips every class through serialization/deserialization.
97 Not a replacement for handcoded tests because it only uses the
98 default member values.
99 """
100
101 def setUp(self):
102 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
103 self.klasses = [klass for mod in mods
104 for klass in mod.__dict__.values()
105 if hasattr(klass, 'show')]
106 self.klasses.sort(key=lambda x: str(x))
107
108 def test_serialization(self):
109 expected_failures = [
110 ofp.common.flow_stats_entry,
111 ofp.common.group_desc_stats_entry,
112 ofp.common.instruction,
113 ofp.common.instruction_apply_actions,
114 ofp.common.instruction_clear_actions,
115 ofp.common.instruction_experimenter,
116 ofp.common.instruction_goto_table,
117 ofp.common.instruction_header,
118 ofp.common.instruction_write_actions,
119 ofp.common.instruction_write_metadata,
120 ofp.common.match_v3,
121 ofp.common.table_stats_entry,
122 ofp.message.aggregate_stats_request,
123 ofp.message.flow_add,
124 ofp.message.flow_delete,
125 ofp.message.flow_delete_strict,
126 ofp.message.flow_modify,
127 ofp.message.flow_modify_strict,
128 ofp.message.flow_removed,
129 ofp.message.flow_stats_request,
130 ofp.message.group_desc_stats_reply,
131 ofp.message.group_mod,
132 ofp.message.group_stats_reply,
133 ofp.message.packet_in,
134 ]
135 for klass in self.klasses:
136 def fn():
137 obj = klass()
138 if hasattr(obj, "xid"): obj.xid = 42
139 buf = obj.pack()
140 obj2 = klass.unpack(buf)
141 self.assertEquals(obj, obj2)
142 if klass in expected_failures:
143 self.assertRaises(Exception, fn)
144 else:
145 fn()
146
147 def test_show(self):
148 expected_failures = [
149 ofp.common.flow_stats_entry,
Rich Lanea22233e2013-04-25 13:18:41 -0700150 ofp.common.table_stats_entry,
151 ofp.message.aggregate_stats_request,
152 ofp.message.flow_add,
153 ofp.message.flow_delete,
154 ofp.message.flow_delete_strict,
155 ofp.message.flow_modify,
156 ofp.message.flow_modify_strict,
157 ofp.message.flow_removed,
158 ofp.message.flow_stats_request,
159 ofp.message.packet_in,
160 ]
161 for klass in self.klasses:
162 def fn():
163 obj = klass()
164 if hasattr(obj, "xid"): obj.xid = 42
165 obj.show()
166 if klass in expected_failures:
167 self.assertRaises(Exception, fn)
168 else:
169 fn()
170
171if __name__ == '__main__':
172 unittest.main()