use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MessageFormatRecordTest method testBlobRecordV2.
/**
* Tests Blob Record Version 2
* Creates test data and creates a blob record version 2 for the specified blob type with the test data
* Verifies that the stream from blob output is same as the passed in data
* Corrupts data and verifies that de-serialization fails
* @param blobSize
* @param blobType
* @throws IOException
* @throws MessageFormatException
*/
private void testBlobRecordV2(int blobSize, BlobType blobType) throws IOException, MessageFormatException {
ByteBuffer blobContent = ByteBuffer.allocate(blobSize);
new Random().nextBytes(blobContent.array());
int size = (int) MessageFormatRecord.Blob_Format_V2.getBlobRecordSize(blobSize);
ByteBuffer entireBlob = ByteBuffer.allocate(size);
BlobData blobData = getBlobRecordV2(blobSize, blobType, blobContent, entireBlob);
Assert.assertEquals("Blob size mismatch", blobSize, blobData.getSize());
byte[] verify = new byte[blobSize];
blobData.content().readBytes(verify);
Assert.assertArrayEquals("BlobContent mismatch", blobContent.array(), verify);
blobData.release();
// corrupt blob record V2
entireBlob.flip();
byte savedByte = entireBlob.get(blobSize / 2);
entireBlob.put(blobSize / 2, (byte) (savedByte + 1));
try {
MessageFormatRecord.deserializeBlob(new ByteBufferInputStream(entireBlob));
fail("Failed to detect corruption of blob record");
} catch (MessageFormatException e) {
Assert.assertEquals("Error code mismatch", MessageFormatErrorCodes.Data_Corrupt, e.getErrorCode());
}
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MessageFormatRecordTest method testUpdateRecordV1.
/**
* Tests UpdateRecord V1 for serialization and deserialization
* @throws IOException
* @throws MessageFormatException
*/
@Test
public void testUpdateRecordV1() throws IOException, MessageFormatException {
// Test update V1 record
// irrespective of what values are set for acccountId, containerId and updateTimeMs, legacy values will be returned
// with Update_Format_V1
short accountId = Utils.getRandomShort(TestUtils.RANDOM);
short containerId = Utils.getRandomShort(TestUtils.RANDOM);
long updateTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt();
ByteBuffer updateRecord = ByteBuffer.allocate(Update_Format_V1.getRecordSize());
Update_Format_V1.serialize(updateRecord, new UpdateRecord(accountId, containerId, updateTimeMs, new DeleteSubRecord()));
updateRecord.flip();
UpdateRecord deserializeUpdateRecord = MessageFormatRecord.deserializeUpdateRecord(new ByteBufferInputStream(updateRecord));
Assert.assertEquals("AccountId mismatch ", UNKNOWN_ACCOUNT_ID, deserializeUpdateRecord.getAccountId());
Assert.assertEquals("ContainerId mismatch ", UNKNOWN_CONTAINER_ID, deserializeUpdateRecord.getContainerId());
Assert.assertEquals("DeletionTime mismatch ", Utils.Infinite_Time, deserializeUpdateRecord.getUpdateTimeInMs());
Assert.assertEquals("Type of update record incorrect", SubRecord.Type.DELETE, deserializeUpdateRecord.getType());
Assert.assertNotNull("DeleteSubRecord is null", deserializeUpdateRecord.getDeleteSubRecord());
// corrupt update V1 record
updateRecord.flip();
byte toCorrupt = updateRecord.get(10);
updateRecord.put(10, (byte) (toCorrupt + 1));
try {
MessageFormatRecord.deserializeUpdateRecord(new ByteBufferInputStream(updateRecord));
fail("Deserialization of a corrupt update record V1 should have failed ");
} catch (MessageFormatException e) {
Assert.assertEquals(e.getErrorCode(), MessageFormatErrorCodes.Data_Corrupt);
}
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MessageFormatRecordTest method deserializeMetadataContentV3.
private CompositeBlobInfo deserializeMetadataContentV3(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) throws IOException {
ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(metadataContent);
DataInputStream inputStream = new DataInputStream(byteBufferInputStream);
short metadataContentVersion = inputStream.readShort();
Assert.assertEquals("Metadata Content Version mismatch ", MessageFormatRecord.Metadata_Content_Version_V3, metadataContentVersion);
return MessageFormatRecord.Metadata_Content_Format_V3.deserializeMetadataContentRecord(inputStream, storeKeyFactory);
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class BoundedNettyByteBufReceiveTest method testBoundedByteBufferReceiveOnLargeRequest.
/**
* Test when the request size is bigger than the maximum size
* @throws Exception
*/
@Test
public void testBoundedByteBufferReceiveOnLargeRequest() throws Exception {
int bufferSize = 2000;
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
buffer.putLong(bufferSize);
byte[] buf = new byte[bufferSize - Long.BYTES];
new Random().nextBytes(buf);
buffer.put(buf);
buffer.flip();
BoundedNettyByteBufReceive set = new BoundedNettyByteBufReceive(100);
// The max request size is 100, but the buffer size is 2000, will result in IOException
try {
set.readFrom(Channels.newChannel(new ByteBufferInputStream(buffer)));
Assert.fail("Should fail with IOException");
} catch (IOException e) {
}
buffer.clear();
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class BoundedNettyByteBufReceiveTest method testBoundedByteBufferReceive.
/**
* Test basic operation of {@link BoundedNettyByteBufReceive}.
* @throws Exception
*/
@Test
public void testBoundedByteBufferReceive() throws Exception {
int bufferSize = 2000;
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
buffer.putLong(bufferSize);
byte[] buf = new byte[bufferSize - Long.BYTES];
new Random().nextBytes(buf);
buffer.put(buf);
buffer.flip();
BoundedNettyByteBufReceive set = new BoundedNettyByteBufReceive(100000);
Assert.assertEquals("Wrong number of bytes read", bufferSize, set.readFrom(Channels.newChannel(new ByteBufferInputStream(buffer))));
buffer.clear();
ByteBuf payload = set.content();
for (int i = 8; i < bufferSize; i++) {
Assert.assertEquals(buffer.array()[i], payload.readByte());
}
payload.release();
}
Aggregations