blob: a3b18c6704598291471cf187fd1e06da1c409624 [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 Lane57026dc2013-05-01 10:13:16 -070046class TestUnpackListLV16(unittest.TestCase):
47 def test_simple(self):
48 def deserializer(reader):
49 reader.skip(2)
50 return reader.read_all()
51 reader = loxi.generic_util.OFReader("\x00\x05abc\x00\x04de\x00\x03f\x00\x02")
52 a = loxi.generic_util.unpack_list_lv16(reader, deserializer)
53 self.assertEquals(['abc', 'de', 'f', ''], a)
54
Rich Lane1de06ab2013-04-26 16:58:37 -070055class TestOFReader(unittest.TestCase):
56 def test_empty(self):
57 reader = OFReader("")
58 self.assertEquals(str(reader.read('')), "")
59 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
60 reader.read_buf(1)
61
62 def test_simple(self):
63 reader = OFReader("abcdefg")
64 self.assertEquals(reader.read('2s')[0], "ab")
65 self.assertEquals(reader.read('2s')[0], "cd")
66 self.assertEquals(reader.read('3s')[0], "efg")
67 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
68 reader.read('s')
69
70 def test_skip(self):
71 reader = OFReader("abcdefg")
72 reader.skip(4)
73 self.assertEquals(reader.read('s')[0], "e")
74 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
75 reader.skip(3)
76
77 def test_empty(self):
78 reader = OFReader("abcdefg")
79 self.assertEquals(reader.is_empty(), False)
80 reader.skip(6)
81 self.assertEquals(reader.is_empty(), False)
82 reader.skip(1)
83 self.assertEquals(reader.is_empty(), True)
84 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
85 reader.skip(1)
86
87 def test_exception_effect(self):
88 reader = OFReader("abcdefg")
89 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
90 reader.skip(8)
91 self.assertEquals(reader.is_empty(), False)
92 reader.skip(7)
93 self.assertEquals(reader.is_empty(), True)
94
95 def test_peek(self):
96 reader = OFReader("abcdefg")
97 self.assertEquals(reader.peek('2s')[0], "ab")
98 self.assertEquals(reader.peek('2s')[0], "ab")
99 self.assertEquals(reader.read('2s')[0], "ab")
100 self.assertEquals(reader.peek('2s')[0], "cd")
101 reader.skip(2)
102 self.assertEquals(reader.read('3s')[0], "efg")
103 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
104 reader.peek('s')
105
106 def test_read_all(self):
107 reader = OFReader("abcdefg")
108 reader.skip(2)
109 self.assertEquals(reader.read_all(), "cdefg")
110 self.assertEquals(reader.read_all(), "")
111
112 def test_slice(self):
113 reader = OFReader("abcdefg")
114 reader.skip(2)
115 self.assertEquals(reader.slice(3).read_all(), "cde")
116 self.assertEquals(reader.slice(2).read_all(), "fg")
117 self.assertEquals(reader.is_empty(), True)
118
Rich Lane5df3fd82013-12-01 14:53:06 -0800119 def test_skip_align(self):
120 reader = OFReader("abcd" + "efgh" + "ijkl" + "mnop" + "qr")
121 reader.skip_align()
122 self.assertEquals(reader.peek('2s')[0], 'ab')
123 self.assertEquals(reader.read('2s')[0], "ab")
124 reader.skip_align()
125 self.assertEquals(reader.peek('2s')[0], 'ij')
126 self.assertEquals(reader.read('2s')[0], 'ij')
127 child = reader.slice(8)
128 self.assertEquals(child.peek('2s')[0], 'kl')
129 child.skip_align()
130 self.assertEquals(child.peek('2s')[0], 'qr')
131
Rich Lane15cbe842013-04-26 16:04:11 -0700132if __name__ == '__main__':
133 unittest.main()