use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingMalformedMessage.
/**
* Test attempting to parse an event which has a malformed message body.
*/
@Test
public void testParsingMalformedMessage() {
MetadataRecordSerde serde = new MetadataRecordSerde();
ByteBuffer buffer = ByteBuffer.allocate(4);
// frame version
buffer.put((byte) 0x01);
// apiKey
buffer.put((byte) 0x00);
// apiVersion
buffer.put((byte) 0x00);
// malformed data
buffer.put((byte) 0x80);
buffer.position(0);
buffer.limit(4);
assertStartsWith("Failed to deserialize record with type", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), buffer.remaining())).getMessage());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testSerde.
@Test
public void testSerde() {
TopicRecord topicRecord = new TopicRecord().setName("foo").setTopicId(Uuid.randomUuid());
MetadataRecordSerde serde = new MetadataRecordSerde();
for (short version = TopicRecord.LOWEST_SUPPORTED_VERSION; version <= TopicRecord.HIGHEST_SUPPORTED_VERSION; version++) {
ApiMessageAndVersion messageAndVersion = new ApiMessageAndVersion(topicRecord, version);
ObjectSerializationCache cache = new ObjectSerializationCache();
int size = serde.recordSize(messageAndVersion, cache);
ByteBuffer buffer = ByteBuffer.allocate(size);
ByteBufferAccessor bufferAccessor = new ByteBufferAccessor(buffer);
serde.write(messageAndVersion, cache, bufferAccessor);
buffer.flip();
assertEquals(size, buffer.remaining());
ApiMessageAndVersion readMessageAndVersion = serde.read(bufferAccessor, size);
assertEquals(messageAndVersion, readMessageAndVersion);
}
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingVersionTooLarge.
/**
* Test attempting to parse an event which has a version > Short.MAX_VALUE
*/
@Test
public void testParsingVersionTooLarge() {
MetadataRecordSerde serde = new MetadataRecordSerde();
ByteBuffer buffer = ByteBuffer.allocate(64);
buffer.clear();
// frame version
buffer.put((byte) 0x01);
// apiKey
buffer.put((byte) 0x08);
// api version
buffer.put((byte) 0xff);
// api version
buffer.put((byte) 0xff);
// api version
buffer.put((byte) 0xff);
// api version end
buffer.put((byte) 0x7f);
buffer.put((byte) 0x80);
buffer.position(0);
buffer.limit(64);
assertStartsWith("Value for version was too large", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), buffer.remaining())).getMessage());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingMalformedMessageVersionVarint.
/**
* Test attempting to parse an event which has a malformed message version varint.
*/
@Test
public void testParsingMalformedMessageVersionVarint() {
MetadataRecordSerde serde = new MetadataRecordSerde();
ByteBuffer buffer = ByteBuffer.allocate(64);
buffer.clear();
buffer.put((byte) 0x01);
buffer.put((byte) 0x08);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.position(0);
buffer.limit(64);
assertStartsWith("Error while reading version", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), buffer.remaining())).getMessage());
}
use of org.apache.kafka.common.protocol.ByteBufferAccessor in project kafka by apache.
the class MetadataRecordSerdeTest method testParsingMalformedFrameVersionVarint.
/**
* Test attempting to parse an event which has a malformed frame version type varint.
*/
@Test
public void testParsingMalformedFrameVersionVarint() {
MetadataRecordSerde serde = new MetadataRecordSerde();
ByteBuffer buffer = ByteBuffer.allocate(64);
buffer.clear();
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.put((byte) 0x80);
buffer.position(0);
buffer.limit(64);
assertStartsWith("Error while reading frame version", assertThrows(MetadataParseException.class, () -> serde.read(new ByteBufferAccessor(buffer), buffer.remaining())).getMessage());
}
Aggregations