From 1ffcd293e43b0ca5e18784521b04376a65119690 Mon Sep 17 00:00:00 2001 From: fkrzywka Date: Wed, 4 Jul 2018 14:46:57 +0200 Subject: Add byte-buffer handling assertions when decoding ByteBuffer reader index should be reset in case of failed decoding of wire frame, as parsing might be retried when more bytes arrive Change-Id: I6dcb5c94c8cffba969f572fca8bbb728ea9500bd Signed-off-by: fkrzywka Issue-ID: DCAEGEN2-601 --- .../dcae/collectors/veshv/domain/WireFrameCodecsTest.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'hv-collector-domain/src/test/kotlin') diff --git a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt index a5242e0f..a1395266 100644 --- a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt +++ b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt @@ -20,6 +20,7 @@ package org.onap.dcae.collectors.veshv.domain import arrow.core.Either +import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled import io.netty.buffer.UnpooledByteBufAllocator import org.assertj.core.api.Assertions.assertThat @@ -149,6 +150,7 @@ object WireFrameCodecsTest : Spek({ val buff = Unpooled.buffer() decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(EmptyWireFrame::class.java) } + assertBufferIntact(buff) } it("should return end-of-transmission message when given end-of-transmission marker byte") { @@ -163,6 +165,7 @@ object WireFrameCodecsTest : Spek({ .writeByte(0xEE) decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFrameHeaderBytes::class.java) } + assertBufferIntact(buff) } it("should return error when payload message header does not fit") { @@ -171,6 +174,7 @@ object WireFrameCodecsTest : Spek({ .writeBytes("MOMOM".toByteArray()) decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFrameHeaderBytes::class.java) } + assertBufferIntact(buff) } it("should return error when length looks ok but first byte is not 0xFF or 0xAA") { @@ -179,6 +183,7 @@ object WireFrameCodecsTest : Spek({ .writeBytes("some garbage".toByteArray()) decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(InvalidWireFrameMarker::class.java) } + assertBufferIntact(buff) } it("should return end-of-transmission message when length looks ok and first byte is 0xAA") { @@ -195,6 +200,7 @@ object WireFrameCodecsTest : Spek({ buff.writerIndex(buff.writerIndex() - 2) decoder.decodeFirst(buff).assertFailedWithError { it.isInstanceOf(MissingWireFramePayloadBytes::class.java) } + assertBufferIntact(buff) } it("should decode payload message leaving rest unread") { @@ -231,10 +237,11 @@ object WireFrameCodecsTest : Spek({ version = 1, payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, payloadSize = payload.size) + val buff = encoder.encode(input) - - decoder.decodeFirst(encoder.encode(input)) + decoder.decodeFirst(buff) .assertFailedWithError { it.isInstanceOf(PayloadSizeExceeded::class.java) } + assertBufferIntact(buff) } it("should validate only first message") { @@ -253,6 +260,11 @@ object WireFrameCodecsTest : Spek({ } }) +private fun assertBufferIntact(buff: ByteBuf) { + assertThat(buff.refCnt()).describedAs("buffer should not be released").isEqualTo(1) + assertThat(buff.readerIndex()).describedAs("buffer reader index should be intact").isEqualTo(0) +} + private fun Either.assertFailedWithError(assertj: (ObjectAssert) -> Unit) { fold({ assertj(assertThat(it)) }, { fail("Error expected") }) } -- cgit 1.2.3-korg