blob: 03885a215240cdaaca114482d8da4faf7949e087 [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):
Rich Lane1de06ab2013-04-26 16:58:37 -070047 def test_simple(self):
48 reader = OFReader("abcdefg")
49 self.assertEquals(reader.read('2s')[0], "ab")
50 self.assertEquals(reader.read('2s')[0], "cd")
51 self.assertEquals(reader.read('3s')[0], "efg")
52 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
53 reader.read('s')
54
55 def test_skip(self):
56 reader = OFReader("abcdefg")
57 reader.skip(4)
58 self.assertEquals(reader.read('s')[0], "e")
59 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
60 reader.skip(3)
61
62 def test_empty(self):
63 reader = OFReader("abcdefg")
64 self.assertEquals(reader.is_empty(), False)
65 reader.skip(6)
66 self.assertEquals(reader.is_empty(), False)
67 reader.skip(1)
68 self.assertEquals(reader.is_empty(), True)
69 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
70 reader.skip(1)
71
72 def test_exception_effect(self):
73 reader = OFReader("abcdefg")
74 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
75 reader.skip(8)
76 self.assertEquals(reader.is_empty(), False)
77 reader.skip(7)
78 self.assertEquals(reader.is_empty(), True)
79
80 def test_peek(self):
81 reader = OFReader("abcdefg")
82 self.assertEquals(reader.peek('2s')[0], "ab")
83 self.assertEquals(reader.peek('2s')[0], "ab")
84 self.assertEquals(reader.read('2s')[0], "ab")
85 self.assertEquals(reader.peek('2s')[0], "cd")
86 reader.skip(2)
87 self.assertEquals(reader.read('3s')[0], "efg")
88 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
89 reader.peek('s')
90
91 def test_read_all(self):
92 reader = OFReader("abcdefg")
93 reader.skip(2)
94 self.assertEquals(reader.read_all(), "cdefg")
95 self.assertEquals(reader.read_all(), "")
96
97 def test_slice(self):
98 reader = OFReader("abcdefg")
99 reader.skip(2)
100 self.assertEquals(reader.slice(3).read_all(), "cde")
101 self.assertEquals(reader.slice(2).read_all(), "fg")
102 self.assertEquals(reader.is_empty(), True)
103
Rich Lane5df3fd82013-12-01 14:53:06 -0800104 def test_skip_align(self):
105 reader = OFReader("abcd" + "efgh" + "ijkl" + "mnop" + "qr")
106 reader.skip_align()
107 self.assertEquals(reader.peek('2s')[0], 'ab')
108 self.assertEquals(reader.read('2s')[0], "ab")
109 reader.skip_align()
110 self.assertEquals(reader.peek('2s')[0], 'ij')
111 self.assertEquals(reader.read('2s')[0], 'ij')
112 child = reader.slice(8)
113 self.assertEquals(child.peek('2s')[0], 'kl')
114 child.skip_align()
115 self.assertEquals(child.peek('2s')[0], 'qr')
116
Rich Lane15cbe842013-04-26 16:04:11 -0700117if __name__ == '__main__':
118 unittest.main()