Search in sources :

Example 31 with ByteBufferAccessor

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());
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 32 with ByteBufferAccessor

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);
    }
}
Also used : ObjectSerializationCache(org.apache.kafka.common.protocol.ObjectSerializationCache) TopicRecord(org.apache.kafka.common.metadata.TopicRecord) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 33 with ByteBufferAccessor

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());
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 34 with ByteBufferAccessor

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());
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 35 with ByteBufferAccessor

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());
}
Also used : ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufferAccessor (org.apache.kafka.common.protocol.ByteBufferAccessor)39 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.jupiter.api.Test)23 ObjectSerializationCache (org.apache.kafka.common.protocol.ObjectSerializationCache)13 TopicPartition (org.apache.kafka.common.TopicPartition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Uuid (org.apache.kafka.common.Uuid)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 Collections (java.util.Collections)3 List (java.util.List)3 Map (java.util.Map)3 LeaderChangeMessage (org.apache.kafka.common.message.LeaderChangeMessage)3 UpdateMetadataEndpoint (org.apache.kafka.common.message.UpdateMetadataRequestData.UpdateMetadataEndpoint)3 Send (org.apache.kafka.common.network.Send)3 BufferUnderflowException (java.nio.BufferUnderflowException)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2