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 | 1de06ab | 2013-04-26 16:58:37 -0700 | [diff] [blame] | 46 | class 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 Lane | 5df3fd8 | 2013-12-01 14:53:06 -0800 | [diff] [blame] | 110 | 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 Lane | 15cbe84 | 2013-04-26 16:04:11 -0700 | [diff] [blame] | 123 | if __name__ == '__main__': |
| 124 | unittest.main() |