diff options
author | fkrzywka <filip.krzywka@nokia.com> | 2018-07-04 14:46:57 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-02 13:53:11 +0200 |
commit | 1ffcd293e43b0ca5e18784521b04376a65119690 (patch) | |
tree | adb231d2c3163350b383646ac077ee3d9982a0b5 | |
parent | f8a9a10a75bf139203fe9ea48a01708c7bda0781 (diff) |
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 <filip.krzywka@nokia.com>
Issue-ID: DCAEGEN2-601
-rw-r--r-- | hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt | 16 |
1 files changed, 14 insertions, 2 deletions
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 <A, B> Either<A, B>.assertFailedWithError(assertj: (ObjectAssert<A>) -> Unit) { fold({ assertj(assertThat(it)) }, { fail("Error expected") }) } |