blob: 250000fcd9be968b397790a2256539764e0bbde8 [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
115 def test_invalid_action_type(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700116 buf = '\xff\xfe\x00\x08\x00\x00\x00\x00'
Rich Lane78a3bc42013-11-29 19:22:08 -0800117 with self.assertRaisesRegexp(ofp.ProtocolError, 'unknown action subtype'):
Rich Lane5bcc7c72013-12-01 15:49:49 -0800118 loxi.generic_util.unpack_list(OFReader(buf), ofp.action.action.unpack)
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)
203 and hasattr(x, 'pack')]
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
208class TestUtils(unittest.TestCase):
Rich Lanea06d0c32013-03-25 08:52:03 -0700209 def test_pretty_wildcards(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700210 self.assertEquals("OFPFW_ALL", ofp.util.pretty_wildcards(ofp.OFPFW_ALL))
211 self.assertEquals("0", ofp.util.pretty_wildcards(0))
212 self.assertEquals("OFPFW_DL_SRC|OFPFW_DL_DST",
213 ofp.util.pretty_wildcards(ofp.OFPFW_DL_SRC|ofp.OFPFW_DL_DST))
214 self.assertEquals("OFPFW_NW_SRC_MASK&0x2000",
215 ofp.util.pretty_wildcards(ofp.OFPFW_NW_SRC_ALL))
216 self.assertEquals("OFPFW_NW_SRC_MASK&0x1a00",
217 ofp.util.pretty_wildcards(0x00001a00))
218 self.assertEquals("OFPFW_IN_PORT|0x80000000",
219 ofp.util.pretty_wildcards(ofp.OFPFW_IN_PORT|0x80000000))
220
221class TestAll(unittest.TestCase):
222 """
223 Round-trips every class through serialization/deserialization.
224 Not a replacement for handcoded tests because it only uses the
225 default member values.
226 """
227
228 def setUp(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700229 mods = [ofp.action,ofp.message,ofp.common]
230 self.klasses = [klass for mod in mods
231 for klass in mod.__dict__.values()
Rich Lane9d98adf2013-11-29 18:37:24 -0800232 if isinstance(klass, type) and
233 issubclass(klass, loxi.OFObject) and
234 hasattr(klass, 'pack')]
Rich Lanea06d0c32013-03-25 08:52:03 -0700235 self.klasses.sort(key=lambda x: str(x))
236
237 def test_serialization(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700238 expected_failures = []
239 for klass in self.klasses:
240 def fn():
241 obj = klass()
242 if hasattr(obj, "xid"): obj.xid = 42
243 buf = obj.pack()
Rich Lane5b2745c2013-11-30 00:36:24 -0800244 obj2 = klass.unpack(OFReader(buf))
Rich Lanea06d0c32013-03-25 08:52:03 -0700245 self.assertEquals(obj, obj2)
246 if klass in expected_failures:
247 self.assertRaises(Exception, fn)
248 else:
249 fn()
250
Rich Lane65790d22013-06-14 00:08:52 -0700251 def test_parse_message(self):
252 expected_failures = []
253 for klass in self.klasses:
Rich Lane9d98adf2013-11-29 18:37:24 -0800254 if not issubclass(klass, ofp.message.message):
Rich Lane65790d22013-06-14 00:08:52 -0700255 continue
256 def fn():
257 obj = klass(xid=42)
258 buf = obj.pack()
259 obj2 = ofp.message.parse_message(buf)
260 self.assertEquals(obj, obj2)
261 if klass in expected_failures:
262 self.assertRaises(Exception, fn)
263 else:
264 fn()
265
Rich Lanea06d0c32013-03-25 08:52:03 -0700266 def test_show(self):
Rich Lanea06d0c32013-03-25 08:52:03 -0700267 expected_failures = []
268 for klass in self.klasses:
269 def fn():
270 obj = klass()
271 if hasattr(obj, "xid"): obj.xid = 42
272 obj.show()
273 if klass in expected_failures:
274 self.assertRaises(Exception, fn)
275 else:
276 fn()
277
278if __name__ == '__main__':
279 unittest.main()