Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 1 | #!/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. |
| 28 | import unittest |
| 29 | |
| 30 | try: |
| 31 | import loxi |
| 32 | import loxi.generic_util |
Rich Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 33 | from loxi.generic_util import OFReader |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 34 | except ImportError: |
| 35 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 36 | |
Rich Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 37 | class TestUnpackList(unittest.TestCase): |
| 38 | def test_simple(self): |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 39 | 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 Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 44 | self.assertEquals(['\x04abc', '\x03de', '\x02f', '\x01'], a) |
| 45 | |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 46 | class 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 Lane | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 55 | class 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 Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 119 | if __name__ == '__main__': |
| 120 | unittest.main() |