blob: 8b2f59f5238fdeb46651541f0cd10d3c5575542c [file] [log] [blame]
Rich Lane15cbe842013-04-26 16:04:11 -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
32 import loxi.generic_util
Rich Lane1de06ab2013-04-26 16:58:37 -070033 from loxi.generic_util import OFReader
Rich Lane15cbe842013-04-26 16:04:11 -070034except ImportError:
35 exit("loxi package not found. Try setting PYTHONPATH.")
36
Rich Lane15cbe842013-04-26 16:04:11 -070037class TestUnpackList(unittest.TestCase):
38 def test_simple(self):
Rich Lane57026dc2013-05-01 10:13:16 -070039 def deserializer(reader):
40 length, = reader.peek("!B")
41 return reader.read('!%ds' % length)[0]
42 reader = loxi.generic_util.OFReader("\x04abc\x03de\x02f\x01")
43 a = loxi.generic_util.unpack_list(reader, deserializer)
Rich Lane15cbe842013-04-26 16:04:11 -070044 self.assertEquals(['\x04abc', '\x03de', '\x02f', '\x01'], a)
45
Rich Lane1de06ab2013-04-26 16:58:37 -070046class TestOFReader(unittest.TestCase):
47 def test_empty(self):
48 reader = OFReader("")
49 self.assertEquals(str(reader.read('')), "")
50 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
51 reader.read_buf(1)
52
53 def test_simple(self):
54 reader = OFReader("abcdefg")
55 self.assertEquals(reader.read('2s')[0], "ab")
56 self.assertEquals(reader.read('2s')[0], "cd")
57 self.assertEquals(reader.read('3s')[0], "efg")
58 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
59 reader.read('s')
60
61 def test_skip(self):
62 reader = OFReader("abcdefg")
63 reader.skip(4)
64 self.assertEquals(reader.read('s')[0], "e")
65 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
66 reader.skip(3)
67
68 def test_empty(self):
69 reader = OFReader("abcdefg")
70 self.assertEquals(reader.is_empty(), False)
71 reader.skip(6)
72 self.assertEquals(reader.is_empty(), False)
73 reader.skip(1)
74 self.assertEquals(reader.is_empty(), True)
75 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
76 reader.skip(1)
77
78 def test_exception_effect(self):
79 reader = OFReader("abcdefg")
80 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
81 reader.skip(8)
82 self.assertEquals(reader.is_empty(), False)
83 reader.skip(7)
84 self.assertEquals(reader.is_empty(), True)
85
86 def test_peek(self):
87 reader = OFReader("abcdefg")
88 self.assertEquals(reader.peek('2s')[0], "ab")
89 self.assertEquals(reader.peek('2s')[0], "ab")
90 self.assertEquals(reader.read('2s')[0], "ab")
91 self.assertEquals(reader.peek('2s')[0], "cd")
92 reader.skip(2)
93 self.assertEquals(reader.read('3s')[0], "efg")
94 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
95 reader.peek('s')
96
97 def test_read_all(self):
98 reader = OFReader("abcdefg")
99 reader.skip(2)
100 self.assertEquals(reader.read_all(), "cdefg")
101 self.assertEquals(reader.read_all(), "")
102
103 def test_slice(self):
104 reader = OFReader("abcdefg")
105 reader.skip(2)
106 self.assertEquals(reader.slice(3).read_all(), "cde")
107 self.assertEquals(reader.slice(2).read_all(), "fg")
108 self.assertEquals(reader.is_empty(), True)
109
Rich Lane5df3fd82013-12-01 14:53:06 -0800110 def test_skip_align(self):
111 reader = OFReader("abcd" + "efgh" + "ijkl" + "mnop" + "qr")
112 reader.skip_align()
113 self.assertEquals(reader.peek('2s')[0], 'ab')
114 self.assertEquals(reader.read('2s')[0], "ab")
115 reader.skip_align()
116 self.assertEquals(reader.peek('2s')[0], 'ij')
117 self.assertEquals(reader.read('2s')[0], 'ij')
118 child = reader.slice(8)
119 self.assertEquals(child.peek('2s')[0], 'kl')
120 child.skip_align()
121 self.assertEquals(child.peek('2s')[0], 'qr')
122
Rich Lane15cbe842013-04-26 16:04:11 -0700123if __name__ == '__main__':
124 unittest.main()