def testBinaryPacker(self): UNKNOWN_TYPE = 100 TEST_FLOAT = 1.23456789 TEST_UINT = 0xff TEST_INT = -1 TEST_VARINT = 78 TEST_STR = 'abc' TEST_BINARY_PACKER_STR = hex_to_binary( 'ffff00ff000000ff00000000000000ffffffffffffffffffffffffffffff4e0361626352069e3fffffffffffff00' ) FS_FOR_3_BYTES = '\xff\xff\xff' bp = BinaryPacker() bp.put(UINT8, TEST_UINT) bp.put(UINT16, TEST_UINT) bp.put(UINT32, TEST_UINT) bp.put(UINT64, TEST_UINT) bp.put(INT8, TEST_INT) bp.put(INT16, TEST_INT) bp.put(INT32, TEST_INT) bp.put(INT64, TEST_INT) bp.put(VAR_INT, TEST_VARINT) bp.put(VAR_STR, TEST_STR) bp.put(FLOAT, TEST_FLOAT) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES, 4) self.assertRaises(PackerError, bp.put, UNKNOWN_TYPE, TEST_INT) self.assertRaises(PackerError, bp.put, BINARY_CHUNK, FS_FOR_3_BYTES, 2) self.assertEqual(bp.getSize(), len(TEST_BINARY_PACKER_STR)) ts = bp.getBinaryString() self.assertEqual(ts, TEST_BINARY_PACKER_STR) bu = BinaryUnpacker(ts) self.assertEqual(bu.get(UINT8), TEST_UINT) self.assertEqual(bu.get(UINT16), TEST_UINT) self.assertEqual(bu.get(UINT32), TEST_UINT) self.assertEqual(bu.get(UINT64), TEST_UINT) self.assertEqual(bu.get(INT8), TEST_INT) self.assertEqual(bu.get(INT16), TEST_INT) self.assertEqual(bu.get(INT32), TEST_INT) self.assertEqual(bu.get(INT64), TEST_INT) self.assertEqual(bu.get(VAR_INT), TEST_VARINT) self.assertEqual(bu.get(VAR_STR), TEST_STR) self.assertAlmostEqual(bu.get(FLOAT), TEST_FLOAT, 2) self.assertEqual(bu.get(BINARY_CHUNK, 3), FS_FOR_3_BYTES) self.assertEqual(bu.get(BINARY_CHUNK, 4), FS_FOR_3_BYTES + "\x00") self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1) self.assertRaises(UnpackerError, bu.get, UNKNOWN_TYPE) self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1) # Running tests with "python <module name>" will NOT work for any Armory tests # You must run tests with "python -m unittest <module name>" or run all tests with "python -m unittest discover" # if __name__ == "__main__": # unittest.main()
def testBinaryPacker(self): UNKNOWN_TYPE = 100 TEST_FLOAT = 1.23456789 TEST_UINT = 0xff TEST_INT = -1 TEST_VARINT = 78 TEST_STR = 'abc' TEST_BINARY_PACKER_STR = hex_to_binary( 'ffff00ff000000ff00000000000000ffffffffffffffffffffffffffffff4e0361626352069e3fffffffffffff00' ) FS_FOR_3_BYTES = '\xff\xff\xff' bp = BinaryPacker() bp.put(UINT8, TEST_UINT) bp.put(UINT16, TEST_UINT) bp.put(UINT32, TEST_UINT) bp.put(UINT64, TEST_UINT) bp.put(INT8, TEST_INT) bp.put(INT16, TEST_INT) bp.put(INT32, TEST_INT) bp.put(INT64, TEST_INT) bp.put(VAR_INT, TEST_VARINT) bp.put(VAR_STR, TEST_STR) bp.put(FLOAT, TEST_FLOAT) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES, 4) self.assertRaises(PackerError, bp.put, UNKNOWN_TYPE, TEST_INT) self.assertRaises(PackerError, bp.put, BINARY_CHUNK, FS_FOR_3_BYTES, 2) self.assertEqual(bp.getSize(), len(TEST_BINARY_PACKER_STR)) ts = bp.getBinaryString() self.assertEqual(ts, TEST_BINARY_PACKER_STR) bu = BinaryUnpacker(ts) self.assertEqual(bu.get(UINT8), TEST_UINT) self.assertEqual(bu.get(UINT16), TEST_UINT) self.assertEqual(bu.get(UINT32), TEST_UINT) self.assertEqual(bu.get(UINT64), TEST_UINT) self.assertEqual(bu.get(INT8), TEST_INT) self.assertEqual(bu.get(INT16), TEST_INT) self.assertEqual(bu.get(INT32), TEST_INT) self.assertEqual(bu.get(INT64), TEST_INT) self.assertEqual(bu.get(VAR_INT), TEST_VARINT) self.assertEqual(bu.get(VAR_STR), TEST_STR) self.assertAlmostEqual(bu.get(FLOAT), TEST_FLOAT, 2) self.assertEqual(bu.get(BINARY_CHUNK, 3), FS_FOR_3_BYTES) self.assertEqual(bu.get(BINARY_CHUNK, 4), FS_FOR_3_BYTES + "\x00") self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1) self.assertRaises(UnpackerError, bu.get, UNKNOWN_TYPE) self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1)
def testBinaryPacker(self): UNKNOWN_TYPE = 100 TEST_FLOAT = 1.23456789 TEST_UINT = 0xFF TEST_INT = -1 TEST_VARINT = 78 TEST_STR = "abc" TEST_BINARY_PACKER_STR = hex_to_binary( "ffff00ff000000ff00000000000000ffffffffffffffffffffffffffffff4e0361626352069e3fffffffffffff00" ) FS_FOR_3_BYTES = "\xff\xff\xff" bp = BinaryPacker() bp.put(UINT8, TEST_UINT) bp.put(UINT16, TEST_UINT) bp.put(UINT32, TEST_UINT) bp.put(UINT64, TEST_UINT) bp.put(INT8, TEST_INT) bp.put(INT16, TEST_INT) bp.put(INT32, TEST_INT) bp.put(INT64, TEST_INT) bp.put(VAR_INT, TEST_VARINT) bp.put(VAR_STR, TEST_STR) bp.put(FLOAT, TEST_FLOAT) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES) bp.put(BINARY_CHUNK, FS_FOR_3_BYTES, 4) self.assertRaises(PackerError, bp.put, UNKNOWN_TYPE, TEST_INT) self.assertRaises(PackerError, bp.put, BINARY_CHUNK, FS_FOR_3_BYTES, 2) self.assertEqual(bp.getSize(), len(TEST_BINARY_PACKER_STR)) ts = bp.getBinaryString() self.assertEqual(ts, TEST_BINARY_PACKER_STR) bu = BinaryUnpacker(ts) self.assertEqual(bu.get(UINT8), TEST_UINT) self.assertEqual(bu.get(UINT16), TEST_UINT) self.assertEqual(bu.get(UINT32), TEST_UINT) self.assertEqual(bu.get(UINT64), TEST_UINT) self.assertEqual(bu.get(INT8), TEST_INT) self.assertEqual(bu.get(INT16), TEST_INT) self.assertEqual(bu.get(INT32), TEST_INT) self.assertEqual(bu.get(INT64), TEST_INT) self.assertEqual(bu.get(VAR_INT), TEST_VARINT) self.assertEqual(bu.get(VAR_STR), TEST_STR) self.assertAlmostEqual(bu.get(FLOAT), TEST_FLOAT, 2) self.assertEqual(bu.get(BINARY_CHUNK, 3), FS_FOR_3_BYTES) self.assertEqual(bu.get(BINARY_CHUNK, 4), FS_FOR_3_BYTES + "\x00") self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1) self.assertRaises(UnpackerError, bu.get, UNKNOWN_TYPE) self.assertRaises(UnpackerError, bu.get, BINARY_CHUNK, 1)