Skip to content

Commit

Permalink
Overrides for strictRead and strictWrite in BinaryProtocol (#548)
Browse files Browse the repository at this point in the history
- Adding overrides for the strictRead and strictWrite in the BinaryProtocol constructor. Before these were defaulted to false and immutable.
- Tests for beginReadMessage and beginWriteMessage, which utilize the strictRead and strictWrite values.

Fixes #547

---------

Co-authored-by: Michael Selsky <Michael@Selsky.me>
  • Loading branch information
rhappe and MichaelSelsky committed Feb 2, 2024
1 parent afff746 commit 3c6d808
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ import kotlin.jvm.JvmOverloads
class BinaryProtocol @JvmOverloads constructor(
transport: Transport,
private val stringLengthLimit: Long = -1,
private val containerLengthLimit: Long = -1
private val containerLengthLimit: Long = -1,
private val strictRead: Boolean = false,
private val strictWrite: Boolean = false,
) : BaseProtocol(transport) {
/**
* A shared buffer for writing.
*/
private val buffer = ByteArray(8)
private val strictRead = false
private val strictWrite = false

@Throws(IOException::class)
override fun writeMessageBegin(name: String, typeId: Byte, seqId: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,77 @@ class BinaryProtocolTest {
}
}

@Test
fun readMessage() {
val name = "foo"
val buffer = Buffer()
buffer.writeInt(name.encodeToByteArray().size)
buffer.writeUtf8(name)
buffer.writeByte(1)
buffer.writeInt(1)
val proto = BinaryProtocol(transport = BufferTransport(buffer))

val messageData = proto.readMessageBegin()
messageData.name shouldBe "foo"
messageData.type shouldBe 1
messageData.seqId shouldBe 1
}

@Test
fun readMessageStrict() {
val name = "foo"
val buffer = Buffer()
buffer.writeInt(-0x7FFEFFFF)
buffer.writeInt(name.encodeToByteArray().size)
buffer.writeUtf8(name)
buffer.writeInt(1)
val proto = BinaryProtocol(
transport = BufferTransport(buffer),
strictRead = true,
)
val messageData = proto.readMessageBegin()
messageData.name shouldBe "foo"
messageData.type shouldBe 1
messageData.seqId shouldBe 1
}

@Test
fun readMessageStrictMissingVersion() {
val name = "foo"
val buffer = Buffer()
buffer.writeInt(name.encodeToByteArray().size)
buffer.writeUtf8(name)
buffer.writeInt(1)
val proto = BinaryProtocol(
transport = BufferTransport(buffer),
strictRead = true,
)

val error = runCatching {
proto.readMessageBegin()
}.exceptionOrNull() as ProtocolException
error.message shouldBe "Missing version in readMessageBegin"
}

@Test
fun readMessageStrictInvalidVersion() {
val name = "foo"
val buffer = Buffer()
buffer.writeInt(-0xFF)
buffer.writeInt(name.encodeToByteArray().size)
buffer.writeUtf8(name)
buffer.writeInt(1)
val proto = BinaryProtocol(
transport = BufferTransport(buffer),
strictRead = true,
)

val error = runCatching {
proto.readMessageBegin()
}.exceptionOrNull() as ProtocolException
error.message shouldBe "Bad version in readMessageBegin"
}

@Test
fun writeByte() {
val buffer = Buffer()
Expand Down Expand Up @@ -141,6 +212,40 @@ class BinaryProtocolTest {
buffer.readUtf8() shouldBe "here is a string"
}

@Test
fun writeMessage() {
val buffer = Buffer()
val proto = BinaryProtocol(BufferTransport(buffer))
proto.writeMessageBegin(
name = "foo",
typeId = 1,
seqId = 1,
)

buffer.readInt() shouldBe 3
buffer.readUtf8(3) shouldBe "foo"
buffer.readByte() shouldBe 1
buffer.readInt() shouldBe 1
}

@Test
fun writeMessageStrict() {
val buffer = Buffer()
val proto = BinaryProtocol(
transport = BufferTransport(buffer),
strictWrite = true,
)
proto.writeMessageBegin(
name = "foo",
typeId = 1,
seqId = 1,
)
buffer.readInt() shouldBe -0x7FFEFFFF
buffer.readInt() shouldBe 3
buffer.readUtf8(3) shouldBe "foo"
buffer.readInt() shouldBe 1
}

@Test
fun adapterTest() {
// This test case comes from actual data, and is intended
Expand Down

0 comments on commit 3c6d808

Please sign in to comment.