blob: 3b0aefbdb3bc778450c6be8d7735ac2a113ade67 [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
Rich Lane3005cf92013-05-01 12:33:35 -070059class TestCommon(unittest.TestCase):
60 sample_empty_match_buf = ''.join([
61 '\x00\x01', # type
62 '\x00\x04', # length
63 '\x00\x00\x00\x00', # padding
64 ])
65
66 def test_empty_match_pack(self):
67 obj = ofp.match()
68 self.assertEquals(self.sample_empty_match_buf, obj.pack())
69
70 def test_empty_match_unpack(self):
71 obj = ofp.match.unpack(self.sample_empty_match_buf)
72 self.assertEquals(len(obj.oxm_list), 0)
73
Rich Lanea22233e2013-04-25 13:18:41 -070074class TestOXM(unittest.TestCase):
75 def test_oxm_in_phy_port_pack(self):
76 import loxi.of12 as ofp
77 obj = ofp.oxm.in_phy_port(value=42)
78 expected = ''.join([
79 '\x80\x00', # class
80 '\x02', # type/masked
81 '\x08', # length
82 '\x00\x00\x00\x2a' # value
83 ])
84 self.assertEquals(expected, obj.pack())
85
86 def test_oxm_in_phy_port_masked_pack(self):
87 import loxi.of12 as ofp
88 obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
89 expected = ''.join([
90 '\x80\x00', # class
91 '\x03', # type/masked
92 '\x0c', # length
93 '\x00\x00\x00\x2a', # value
94 '\xaa\xbb\xcc\xdd' # mask
95 ])
96 self.assertEquals(expected, obj.pack())
97
98 def test_oxm_ipv6_dst_pack(self):
99 import loxi.of12 as ofp
100 obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f')
101 expected = ''.join([
102 '\x80\x00', # class
103 '\x36', # type/masked
104 '\x14', # length
105 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value
106 ])
107 self.assertEquals(expected, obj.pack())
108
109class TestAllOF12(unittest.TestCase):
110 """
111 Round-trips every class through serialization/deserialization.
112 Not a replacement for handcoded tests because it only uses the
113 default member values.
114 """
115
116 def setUp(self):
117 mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
118 self.klasses = [klass for mod in mods
119 for klass in mod.__dict__.values()
120 if hasattr(klass, 'show')]
121 self.klasses.sort(key=lambda x: str(x))
122
123 def test_serialization(self):
124 expected_failures = [
125 ofp.common.flow_stats_entry,
126 ofp.common.group_desc_stats_entry,
127 ofp.common.instruction,
128 ofp.common.instruction_apply_actions,
129 ofp.common.instruction_clear_actions,
130 ofp.common.instruction_experimenter,
131 ofp.common.instruction_goto_table,
132 ofp.common.instruction_header,
133 ofp.common.instruction_write_actions,
134 ofp.common.instruction_write_metadata,
Rich Lanea22233e2013-04-25 13:18:41 -0700135 ofp.common.table_stats_entry,
Rich Lanea22233e2013-04-25 13:18:41 -0700136 ofp.message.flow_add,
137 ofp.message.flow_delete,
138 ofp.message.flow_delete_strict,
139 ofp.message.flow_modify,
140 ofp.message.flow_modify_strict,
Rich Lanea22233e2013-04-25 13:18:41 -0700141 ofp.message.group_desc_stats_reply,
142 ofp.message.group_mod,
143 ofp.message.group_stats_reply,
144 ofp.message.packet_in,
145 ]
146 for klass in self.klasses:
147 def fn():
148 obj = klass()
149 if hasattr(obj, "xid"): obj.xid = 42
150 buf = obj.pack()
151 obj2 = klass.unpack(buf)
152 self.assertEquals(obj, obj2)
153 if klass in expected_failures:
154 self.assertRaises(Exception, fn)
155 else:
156 fn()
157
158 def test_show(self):
159 expected_failures = [
Rich Lanea22233e2013-04-25 13:18:41 -0700160 ofp.common.table_stats_entry,
Rich Lanea22233e2013-04-25 13:18:41 -0700161 ]
162 for klass in self.klasses:
163 def fn():
164 obj = klass()
165 if hasattr(obj, "xid"): obj.xid = 42
166 obj.show()
167 if klass in expected_failures:
168 self.assertRaises(Exception, fn)
169 else:
170 fn()
171
172if __name__ == '__main__':
173 unittest.main()