blob: 7e10335c987194abe4208aed1225288182fcfe97 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -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 Lane9a3f1fd2013-05-10 16:29:17 -070029import test_data
Rich Lane5c4446e2013-05-16 13:53:51 -070030from testutil import add_datafiles_tests
Rich Lanea06d0c32013-03-25 08:52:03 -070031
32try:
Rich Lane9d98adf2013-11-29 18:37:24 -080033 import loxi
Rich Lanefb7de0e2013-03-29 16:50:29 -070034 import loxi.of10 as ofp
Rich Lane57026dc2013-05-01 10:13:16 -070035 from loxi.generic_util import OFReader
Rich Lanea06d0c32013-03-25 08:52:03 -070036except 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"))
Rich Lane00549ea2013-04-25 13:33:16 -070043 self.assertEquals(loxi.version_names[1], "1.0")
Rich Lanea06d0c32013-03-25 08:52:03 -070044 ofp = loxi.protocol(1)
45 self.assertEquals(ofp.OFP_VERSION, 1)
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
51 def test_version(self):
Rich Lanea94fb082013-04-04 17:04:17 -070052 import loxi
Rich Lanea06d0c32013-03-25 08:52:03 -070053 self.assertTrue(hasattr(loxi.of10, "ProtocolError"))
54 self.assertTrue(hasattr(loxi.of10, "OFP_VERSION"))
55 self.assertEquals(loxi.of10.OFP_VERSION, 1)
56 self.assertTrue(hasattr(loxi.of10, "action"))
57 self.assertTrue(hasattr(loxi.of10, "common"))
58 self.assertTrue(hasattr(loxi.of10, "const"))
59 self.assertTrue(hasattr(loxi.of10, "message"))
60
61class TestActions(unittest.TestCase):
Rich Lanea06d0c32013-03-25 08:52:03 -070062 def test_output_equality(self):
Rich Lanea06d0c32013-03-25 08:52:03 -070063 action = ofp.action.output(port=1, max_len=0x1234)
64 action2 = ofp.action.output(port=1, max_len=0x1234)
65 self.assertEquals(action, action2)
66
67 action2.port = 2
68 self.assertNotEquals(action, action2)
69 action2.port = 1
70
71 action2.max_len = 0xffff
72 self.assertNotEquals(action, action2)
73 action2.max_len = 0x1234
74
Rich Lanea06d0c32013-03-25 08:52:03 -070075# Assumes action serialization/deserialization works
76class TestActionList(unittest.TestCase):
77 def test_normal(self):
Rich Lanea06d0c32013-03-25 08:52:03 -070078 expected = []
79 bufs = []
80
81 def add(action):
82 expected.append(action)
83 bufs.append(action.pack())
84
85 add(ofp.action.output(port=1, max_len=0xffff))
86 add(ofp.action.output(port=2, max_len=0xffff))
87 add(ofp.action.output(port=ofp.OFPP_IN_PORT, max_len=0xffff))
88 add(ofp.action.bsn_set_tunnel_dst(dst=0x12345678))
89 add(ofp.action.nicira_dec_ttl())
90
Rich Lane5bcc7c72013-12-01 15:49:49 -080091 actions = loxi.generic_util.unpack_list(OFReader(''.join(bufs)), ofp.action.action.unpack)
Rich Lanea06d0c32013-03-25 08:52:03 -070092 self.assertEquals(actions, expected)
93
94 def test_empty_list(self):
Rich Lane5bcc7c72013-12-01 15:49:49 -080095 self.assertEquals(loxi.generic_util.unpack_list(OFReader(''), ofp.action.action.unpack), [])
Rich Lanea06d0c32013-03-25 08:52:03 -070096
97 def test_invalid_list_length(self):
Rich Lanea06d0c32013-03-25 08:52:03 -070098 buf = '\x00' * 9
Rich Lane57026dc2013-05-01 10:13:16 -070099 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800100 loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
Rich Lanea06d0c32013-03-25 08:52:03 -0700101
102 def test_invalid_action_length(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700103 buf = '\x00' * 8
Rich Lane57026dc2013-05-01 10:13:16 -0700104 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800105 loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
Rich Lanea06d0c32013-03-25 08:52:03 -0700106
107 buf = '\x00\x00\x00\x04'
Rich Lane57026dc2013-05-01 10:13:16 -0700108 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800109 loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
Rich Lanea06d0c32013-03-25 08:52:03 -0700110
111 buf = '\x00\x00\x00\x10\x00\x00\x00\x00'
Rich Lane57026dc2013-05-01 10:13:16 -0700112 with self.assertRaisesRegexp(ofp.ProtocolError, 'Buffer too short'):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800113 loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
Rich Lanea06d0c32013-03-25 08:52:03 -0700114
Rich Lane038c9e62014-01-27 15:33:18 -0800115 def test_unknown_action_type(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700116 buf = '\xff\xfe\x00\x08\x00\x00\x00\x00'
Rich Lane038c9e62014-01-27 15:33:18 -0800117 result = loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
118 self.assertEquals(result, [ofp.action.action(type=0xfffe)])
Rich Lanea06d0c32013-03-25 08:52:03 -0700119
120class TestConstants(unittest.TestCase):
121 def test_ports(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700122 self.assertEquals(0xffff, ofp.OFPP_NONE)
123
124 def test_wildcards(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700125 self.assertEquals(0xfc000, ofp.OFPFW_NW_DST_MASK)
126
127class TestCommon(unittest.TestCase):
Rich Lanea06d0c32013-03-25 08:52:03 -0700128 def test_match(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700129 match = ofp.match()
130 self.assertEquals(match.wildcards, ofp.OFPFW_ALL)
131 self.assertEquals(match.tcp_src, 0)
132 buf = match.pack()
Rich Lane5b2745c2013-11-30 00:36:24 -0800133 match2 = ofp.match.unpack(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700134 self.assertEquals(match, match2)
135
136class TestMessages(unittest.TestCase):
137 def test_hello_construction(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700138 msg = ofp.message.hello()
139 self.assertEquals(msg.version, ofp.OFP_VERSION)
140 self.assertEquals(msg.type, ofp.OFPT_HELLO)
141 self.assertEquals(msg.xid, None)
142
143 msg = ofp.message.hello(xid=123)
144 self.assertEquals(msg.xid, 123)
145
146 # 0 is a valid xid distinct from None
147 msg = ofp.message.hello(xid=0)
148 self.assertEquals(msg.xid, 0)
149
Rich Lanea06d0c32013-03-25 08:52:03 -0700150 def test_echo_request_construction(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700151 msg = ofp.message.echo_request(data="abc")
152 self.assertEquals(msg.data, "abc")
153
Rich Lane5c4446e2013-05-16 13:53:51 -0700154 def test_echo_request_invalid_length(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700155 buf = "\x01\x02\x00\x07\x12\x34\x56"
Rich Lane0f7f9d52013-11-29 17:00:49 -0800156 with self.assertRaisesRegexp(ofp.ProtocolError, "Buffer too short"):
Rich Lane5b2745c2013-11-30 00:36:24 -0800157 ofp.message.echo_request.unpack(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700158
159 def test_echo_request_equality(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700160 msg = ofp.message.echo_request(xid=0x12345678, data="abc")
161 msg2 = ofp.message.echo_request(xid=0x12345678, data="abc")
Rich Lanea06d0c32013-03-25 08:52:03 -0700162 self.assertEquals(msg, msg2)
163
164 msg2.xid = 1
165 self.assertNotEquals(msg, msg2)
166 msg2.xid = msg.xid
167
168 msg2.data = "a"
169 self.assertNotEquals(msg, msg2)
170 msg2.data = msg.data
171
Rich Laneb1f347d2013-05-16 16:39:12 -0700172# The majority of the serialization tests are created here using the files in
173# the test_data directory.
Rich Lane5c4446e2013-05-16 13:53:51 -0700174class TestDataFiles(unittest.TestCase):
175 pass
Rich Lane5c4446e2013-05-16 13:53:51 -0700176add_datafiles_tests(TestDataFiles, 'of10/', ofp)
177
Rich Lanea06d0c32013-03-25 08:52:03 -0700178class TestParse(unittest.TestCase):
179 def test_parse_header(self):
180 import loxi
Rich Lanea06d0c32013-03-25 08:52:03 -0700181
182 msg_ver, msg_type, msg_len, msg_xid = ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56\x78")
183 self.assertEquals(1, msg_ver)
184 self.assertEquals(4, msg_type)
185 self.assertEquals(45032, msg_len)
186 self.assertEquals(0x12345678, msg_xid)
187
188 with self.assertRaisesRegexp(loxi.ProtocolError, "too short"):
189 ofp.message.parse_header("\x01\x04\xAF\xE8\x12\x34\x56")
190
191 def test_parse_message(self):
192 import loxi
193 import loxi.of10 as ofp
194
195 buf = "\x01\x00\x00\x08\x12\x34\x56\x78"
196 msg = ofp.message.parse_message(buf)
197 assert(msg.xid == 0x12345678)
198
Rich Lane9d98adf2013-11-29 18:37:24 -0800199 # Get a list of all concrete message classes
Rich Lanea06d0c32013-03-25 08:52:03 -0700200 test_klasses = [x for x in ofp.message.__dict__.values()
201 if type(x) == type
Rich Lane9d98adf2013-11-29 18:37:24 -0800202 and issubclass(x, ofp.message.message)
Rich Lane038c9e62014-01-27 15:33:18 -0800203 and not hasattr(x, 'subtypes')]
Rich Lanea06d0c32013-03-25 08:52:03 -0700204
205 for klass in test_klasses:
206 self.assertIsInstance(ofp.message.parse_message(klass(xid=1).pack()), klass)
207
Rich Lane038c9e62014-01-27 15:33:18 -0800208 def test_parse_unknown_message(self):
209 import loxi
210 import loxi.of10 as ofp
211
212 buf = "\x01\xfe\x00\x08\x12\x34\x56\x78"
213 msg = ofp.message.parse_message(buf)
214 self.assertIsInstance(msg, ofp.message.message)
215
Rich Lanea06d0c32013-03-25 08:52:03 -0700216class TestUtils(unittest.TestCase):
Rich Lanea06d0c32013-03-25 08:52:03 -0700217 def test_pretty_wildcards(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700218 self.assertEquals("OFPFW_ALL", ofp.util.pretty_wildcards(ofp.OFPFW_ALL))
219 self.assertEquals("0", ofp.util.pretty_wildcards(0))
220 self.assertEquals("OFPFW_DL_SRC|OFPFW_DL_DST",
221 ofp.util.pretty_wildcards(ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST))
222 self.assertEquals("OFPFW_NW_SRC_MASK&0x2000",
223 ofp.util.pretty_wildcards(ofp.OFPFW_NW_SRC_ALL))
224 self.assertEquals("OFPFW_NW_SRC_MASK&0x1a00",
225 ofp.util.pretty_wildcards(0x00001a00))
226 self.assertEquals("OFPFW_IN_PORT|0x80000000",
227 ofp.util.pretty_wildcards(ofp.OFPFW_IN_PORT|0x80000000))
228
229class TestAll(unittest.TestCase):
230 """
231 Round-trips every class through serialization/deserialization.
232 Not a replacement for handcoded tests because it only uses the
233 default member values.
234 """
235
236 def setUp(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700237 mods = [ofp.action,ofp.message,ofp.common]
238 self.klasses = [klass for mod in mods
239 for klass in mod.__dict__.values()
Rich Lane9d98adf2013-11-29 18:37:24 -0800240 if isinstance(klass, type) and
241 issubclass(klass, loxi.OFObject) and
Rich Lane038c9e62014-01-27 15:33:18 -0800242 not hasattr(klass, 'subtypes')]
Rich Lanea06d0c32013-03-25 08:52:03 -0700243 self.klasses.sort(key=lambda x: str(x))
244
245 def test_serialization(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700246 expected_failures = []
247 for klass in self.klasses:
248 def fn():
249 obj = klass()
250 if hasattr(obj, "xid"): obj.xid = 42
251 buf = obj.pack()
Rich Lane5b2745c2013-11-30 00:36:24 -0800252 obj2 = klass.unpack(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700253 self.assertEquals(obj, obj2)
254 if klass in expected_failures:
255 self.assertRaises(Exception, fn)
256 else:
257 fn()
258
Rich Lane65790d22013-06-14 00:08:52 -0700259 def test_parse_message(self):
260 expected_failures = []
261 for klass in self.klasses:
Rich Lane9d98adf2013-11-29 18:37:24 -0800262 if not issubclass(klass, ofp.message.message):
Rich Lane65790d22013-06-14 00:08:52 -0700263 continue
264 def fn():
265 obj = klass(xid=42)
266 buf = obj.pack()
267 obj2 = ofp.message.parse_message(buf)
268 self.assertEquals(obj, obj2)
269 if klass in expected_failures:
270 self.assertRaises(Exception, fn)
271 else:
272 fn()
273
Rich Lanea06d0c32013-03-25 08:52:03 -0700274 def test_show(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700275 expected_failures = []
276 for klass in self.klasses:
277 def fn():
278 obj = klass()
279 if hasattr(obj, "xid"): obj.xid = 42
280 obj.show()
281 if klass in expected_failures:
282 self.assertRaises(Exception, fn)
283 else:
284 fn()
285
286if __name__ == '__main__':
287 unittest.main()