blob: ba06d738a04799d09fdd1feef4312fe53631f486 [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
37class TestUnpackArray(unittest.TestCase):
38 def test_simple(self):
39 a = loxi.generic_util.unpack_array(str, 3, "abcdefghi")
40 self.assertEquals(['abc', 'def', 'ghi'], a)
41
42 with self.assertRaisesRegexp(loxi.ProtocolError, "invalid array length"):
43 loxi.generic_util.unpack_array(str, 3, "abcdefgh")
44
45class TestUnpackList(unittest.TestCase):
46 def test_simple(self):
47 a = loxi.generic_util.unpack_list(str, '!B', "\x04abc\x03de\x02f\x01")
48 self.assertEquals(['\x04abc', '\x03de', '\x02f', '\x01'], a)
49
Rich Lane1de06ab2013-04-26 16:58:37 -070050class TestOFReader(unittest.TestCase):
51 def test_empty(self):
52 reader = OFReader("")
53 self.assertEquals(str(reader.read('')), "")
54 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
55 reader.read_buf(1)
56
57 def test_simple(self):
58 reader = OFReader("abcdefg")
59 self.assertEquals(reader.read('2s')[0], "ab")
60 self.assertEquals(reader.read('2s')[0], "cd")
61 self.assertEquals(reader.read('3s')[0], "efg")
62 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
63 reader.read('s')
64
65 def test_skip(self):
66 reader = OFReader("abcdefg")
67 reader.skip(4)
68 self.assertEquals(reader.read('s')[0], "e")
69 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
70 reader.skip(3)
71
72 def test_empty(self):
73 reader = OFReader("abcdefg")
74 self.assertEquals(reader.is_empty(), False)
75 reader.skip(6)
76 self.assertEquals(reader.is_empty(), False)
77 reader.skip(1)
78 self.assertEquals(reader.is_empty(), True)
79 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
80 reader.skip(1)
81
82 def test_exception_effect(self):
83 reader = OFReader("abcdefg")
84 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
85 reader.skip(8)
86 self.assertEquals(reader.is_empty(), False)
87 reader.skip(7)
88 self.assertEquals(reader.is_empty(), True)
89
90 def test_peek(self):
91 reader = OFReader("abcdefg")
92 self.assertEquals(reader.peek('2s')[0], "ab")
93 self.assertEquals(reader.peek('2s')[0], "ab")
94 self.assertEquals(reader.read('2s')[0], "ab")
95 self.assertEquals(reader.peek('2s')[0], "cd")
96 reader.skip(2)
97 self.assertEquals(reader.read('3s')[0], "efg")
98 with self.assertRaisesRegexp(loxi.ProtocolError, "Buffer too short"):
99 reader.peek('s')
100
101 def test_read_all(self):
102 reader = OFReader("abcdefg")
103 reader.skip(2)
104 self.assertEquals(reader.read_all(), "cdefg")
105 self.assertEquals(reader.read_all(), "")
106
107 def test_slice(self):
108 reader = OFReader("abcdefg")
109 reader.skip(2)
110 self.assertEquals(reader.slice(3).read_all(), "cde")
111 self.assertEquals(reader.slice(2).read_all(), "fg")
112 self.assertEquals(reader.is_empty(), True)
113
Rich Lane15cbe842013-04-26 16:04:11 -0700114if __name__ == '__main__':
115 unittest.main()