示例#1
0
 def test_error(self):
     obj = binstruct.Binstruct(b"\x05test")
     with self.assertRaises(binstruct.error):
         obj.unpack_block_uleb128()
     obj = binstruct.Binstruct(b"")
     with self.assertRaises(binstruct.error):
         obj.unpack_block_uleb128()
     obj = binstruct.Binstruct(b"test")
     with self.assertRaises(TypeError):
         obj.unpack_block_uleb128(None)
示例#2
0
 def test_block(self):
     obj = binstruct.Binstruct(b"\x03test")
     value = obj.unpack_block_uleb128()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "tes")
     self.assertEqual(obj.pos, 4)
     obj = binstruct.Binstruct(b"\x84\x80\x80\x00test")
     value = obj.unpack_block_uleb128()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "test")
     self.assertEqual(obj.pos, 8)
示例#3
0
 def test_error(self):
     obj = binstruct.Binstruct(b"\x00\x00\x00\x00\x00\x00\x00\x05test")
     with self.assertRaises(binstruct.error):
         obj.unpack_block_be64()
     obj = binstruct.Binstruct(b"\x05\x00\x00\x00\x00\x00\x00\x00test")
     with self.assertRaises(binstruct.error):
         obj.unpack_block_le64()
     obj = binstruct.Binstruct(b"")
     with self.assertRaises(binstruct.error):
         obj.unpack_block_be64()
     with self.assertRaises(binstruct.error):
         obj.unpack_block_le64()
     obj = binstruct.Binstruct(b"test")
     with self.assertRaises(TypeError):
         obj.unpack_block_be64(None)
     with self.assertRaises(TypeError):
         obj.unpack_block_le64(None)
示例#4
0
 def test_string(self):
     obj = binstruct.Binstruct(b"test\x00next\x00")
     value = obj.unpack_string()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "test")
     self.assertEqual(obj.pos, 5)
     value = obj.unpack_string()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "next")
     self.assertEqual(obj.pos, 10)
示例#5
0
    def test_u8(self):
        obj = binstruct.Binstruct(b"\x81\x82")
        with self.assertRaises(TypeError):
            obj.unpack_u8(None)

        value = obj.unpack_u8()
        self.assertEqual(value, 0x81)
        self.assertEqual(obj.pos, 1)
        self.assertIsInstance(value, int)
        self.assertEqual(obj.unpack_u8(), 0x82)
        self.assertEqual(obj.pos, 2)
示例#6
0
 def test_bool(self):
     obj = binstruct.Binstruct(b"\x00\x01")
     value = obj.unpack_bool()
     self.assertIsInstance(value, bool)
     self.assertEqual(value, False)
     self.assertEqual(obj.pos, 1)
     value = obj.unpack_bool()
     self.assertIsInstance(value, bool)
     self.assertEqual(value, True)
     self.assertEqual(obj.pos, 2)
     with self.assertRaises(binstruct.error):
         obj.unpack_bool()
示例#7
0
 def test_block(self):
     obj = binstruct.Binstruct(b"\x00\x00\x00\x00\x00\x00\x00\x03test")
     value = obj.unpack_block_be64()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "tes")
     self.assertEqual(obj.pos, 11)
     obj = binstruct.Binstruct(b"\x03\x00\x00\x00\x00\x00\x00\x00test")
     value = obj.unpack_block_le64()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "tes")
     self.assertEqual(obj.pos, 11)
     obj = binstruct.Binstruct(b"\x00\x00\x00\x00\x00\x00\x00\x00test")
     value = obj.unpack_block_be64()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "")
     self.assertEqual(obj.pos, 8)
     obj = binstruct.Binstruct(b"\x00\x00\x00\x00\x00\x00\x00\x00test")
     value = obj.unpack_block_le64()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "")
     self.assertEqual(obj.pos, 8)
示例#8
0
 def test_block(self):
     obj = binstruct.Binstruct(b"test\x11")
     value = obj.unpack_block(4)
     self.assertIsInstance(value, str)
     self.assertEqual(value, b"test")
     self.assertEqual(obj.pos, 4)
     value = obj.unpack_block(1)
     self.assertIsInstance(value, str)
     self.assertEqual(value, b"\x11")
     self.assertEqual(obj.pos, 5)
     value = obj.unpack_block(0)
     self.assertIsInstance(value, str)
     self.assertEqual(value, b"")
     self.assertEqual(obj.pos, 5)
示例#9
0
 def test_error(self):
     data = "test\x11"
     obj = binstruct.Binstruct(data)
     with self.assertRaises(TypeError):
         obj.unpack_block()
     with self.assertRaises(TypeError):
         obj.unpack_block(None)
     with self.assertRaises(binstruct.error):
         obj.unpack_block(-1)
     with self.assertRaises(binstruct.error):
         obj.unpack_block(6)
     obj.pos = 5
     with self.assertRaises(binstruct.error):
         obj.unpack_block(1)
     self.assertEqual(obj.pos, 5)
示例#10
0
 def test_empty(self):
     obj = binstruct.Binstruct(b"")
     with self.assertRaises(binstruct.error):
         obj.unpack_string()
示例#11
0
 def test_range_u8(self):
     obj = binstruct.Binstruct(b"")
     with self.assertRaises(binstruct.error):
         obj.unpack_u8()
     with self.assertRaises(binstruct.error):
         obj.pos += 1
示例#12
0
 def setUp(self):
     self.data = b"\x11\x22\x00\x44\x55"
     self.obj = binstruct.Binstruct(self.data)
示例#13
0
 def test_value(self):
     obj = binstruct.Binstruct(b"\xE5\x8E\x26\x00\xFF")
     value = obj.unpack_uleb128()
     self.assertIsInstance(value, int)
     self.assertEqual(value, 624485)
     self.assertEqual(obj.pos, 3)
示例#14
0
 def test_long(self):
     obj = binstruct.Binstruct(b"\xFF\xFF\xFF\xFF\xFF\xE5\x8E\x26\x00\xFF")
     value = obj.unpack_uleb128()
     self.assertIsInstance(value, long)
     self.assertEqual(value, 18446744073709551615L)
     self.assertEqual(obj.pos, 8)
示例#15
0
 def test_arg(self):
     obj = binstruct.Binstruct(b"\01")
     with self.assertRaises(TypeError):
         obj.unpack_string(None)
示例#16
0
 def setUp(self):
     self.obj = binstruct.Binstruct(b"\x81\x82\x83\x84\x85\x86\x87\x88")
示例#17
0
 def test_unterminated(self):
     obj = binstruct.Binstruct(b"test")
     value = obj.unpack_string()
     self.assertIsInstance(value, str)
     self.assertEqual(value, "test")
示例#18
0
 def test_value(self):
     obj = binstruct.Binstruct(b"\x9b\xf1\x59\x00\xFF")
     value = obj.unpack_sleb128()
     self.assertIsInstance(value, int)
     self.assertEqual(value, -624485)
     self.assertEqual(obj.pos, 3)
示例#19
0
 def test_messy(self):
     obj = binstruct.Binstruct(b"\x80\x01")
     self.assertEqual(obj.unpack_sleb128(), 0x80)
     self.assertEqual(obj.pos, 2)
示例#20
0
 def test_corrupt(self):
     obj = binstruct.Binstruct(b"\x80")
     with self.assertRaises(binstruct.error):
         obj.unpack_sleb128()
示例#21
0
 def test_zero(self):
     obj = binstruct.Binstruct(b"\x00")
     self.assertEqual(obj.unpack_sleb128(), 0)
     self.assertEqual(obj.pos, 1)
示例#22
0
 def test_long(self):
     obj = binstruct.Binstruct(b"\xFF\xFF\xFF\xE5\x8E\x76\x00\xFF")
     value = obj.unpack_sleb128()
     self.assertIsInstance(value, int)
     self.assertEqual(value, -322961409)
     self.assertEqual(obj.pos, 6)