diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2021-02-20 22:38:22 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2021-02-20 22:38:22 (GMT) |
commit | 9a080bc3f8184a5663ce42c3c74ae80e1ae598a0 (patch) | |
tree | fa80229577a84afd58c1dc81ea599cd67036d771 /tests | |
parent | 170dc35d87e0a858c5400937a1ee2d2dde7f653a (diff) |
Pack and unpack LEB128 values on demand.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/common/leb128.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/common/leb128.py b/tests/common/leb128.py new file mode 100644 index 0000000..db3013e --- /dev/null +++ b/tests/common/leb128.py @@ -0,0 +1,108 @@ + +from chrysacase import ChrysalideTestCase +from pychrysalide.common import pack_uleb128, unpack_uleb128, pack_leb128, unpack_leb128 +from pychrysalide.common import PackedBuffer + + +class TestLEB128Values(ChrysalideTestCase): + """TestCase for common LEB128 features*""" + + def testUnsignedLeb128Encoding(self): + """Pack and unpack unsigned LEB128 values.""" + + cases = { + 624485: b'\xe5\x8e\x26', + 127: b'\x7f', + 128: b'\x80\x01', + } + + for value, encoding in cases.items(): + + pbuf = PackedBuffer() + + status = pack_uleb128(value, pbuf) + self.assertTrue(status) + + self.assertEqual(pbuf.payload_length, len(encoding)) + + pbuf.rewind() + + got = pbuf.extract(len(encoding)) + + self.assertEqual(got, encoding) + + self.assertFalse(pbuf.more_data) + + for value, encoding in cases.items(): + + pbuf = PackedBuffer() + pbuf.extend(encoding, False) + + pbuf.rewind() + + got = unpack_uleb128(pbuf) + self.assertIsNotNone(got) + + self.assertEqual(got, value) + + + def testSignedLeb128Encoding(self): + """Pack and unpack signed LEB128 values.""" + + cases = { + -123456: b'\xc0\xbb\x78', + -42: b'\x56', + -9001: b'\xd7\xb9\x7f', + } + + for value, encoding in cases.items(): + + pbuf = PackedBuffer() + + status = pack_leb128(value, pbuf) + self.assertTrue(status) + + self.assertEqual(pbuf.payload_length, len(encoding)) + + pbuf.rewind() + + got = pbuf.extract(len(encoding)) + + self.assertEqual(got, encoding) + + self.assertFalse(pbuf.more_data) + + for value, encoding in cases.items(): + + pbuf = PackedBuffer() + pbuf.extend(encoding, False) + + pbuf.rewind() + + got = unpack_leb128(pbuf) + self.assertIsNotNone(got) + + self.assertEqual(got, value) + + + def testTooBigLeb128Encodings(self): + """Prevent overflow for LEB128 values.""" + + pbuf = PackedBuffer() + pbuf.extend(b'\x80' * 10 + b'\x7f', False) + + pbuf.rewind() + + got = unpack_uleb128(pbuf) + + self.assertIsNone(got) + + pbuf = PackedBuffer() + pbuf.extend(b'\x80' * 10 + b'\x7f', False) + + pbuf.rewind() + + got = unpack_leb128(pbuf) + + self.assertIsNone(got) + |