-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from libp2p/0.5.4
0.5.4 release - Throw exception when multistream messages do not end with line ending (… - Retrieve the value from seenMessages once to avoid NullPointerExcepti… - Reduce log level from INFO to DEBUG on inbound invalid gossip message (… - More flexible pubsub message fields handling (#132)
- Loading branch information
Showing
8 changed files
with
193 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
src/main/kotlin/io/libp2p/etc/util/netty/StringSuffixCodec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package io.libp2p.etc.util.netty | ||
|
||
import io.netty.channel.ChannelHandlerContext | ||
import io.netty.handler.codec.DecoderException | ||
import io.netty.handler.codec.MessageToMessageCodec | ||
|
||
/** | ||
* Adds/removes trailing character from messages | ||
*/ | ||
class StringSuffixCodec(val trainlingChar: Char) : MessageToMessageCodec<String, String>() { | ||
class StringSuffixCodec(val trailingChar: Char) : MessageToMessageCodec<String, String>() { | ||
|
||
override fun encode(ctx: ChannelHandlerContext?, msg: String, out: MutableList<Any>) { | ||
out += (msg + trainlingChar) | ||
out += (msg + trailingChar) | ||
} | ||
|
||
override fun decode(ctx: ChannelHandlerContext?, msg: String, out: MutableList<Any>) { | ||
out += msg.trimEnd(trainlingChar) | ||
if (!msg.endsWith(trailingChar)) throw DecoderException("Missing message end character") | ||
out += msg.substring(0, msg.length - 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/kotlin/io/libp2p/etc/util/netty/StringSuffixCodecTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.libp2p.etc.util.netty | ||
|
||
import io.netty.channel.embedded.EmbeddedChannel | ||
import io.netty.handler.codec.DecoderException | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class StringSuffixCodecTest { | ||
val channel = EmbeddedChannel(StringSuffixCodec('\n')) | ||
|
||
@Test | ||
fun encodeAppendTrailingChar() { | ||
channel.writeOutbound("theMessage") | ||
val result = channel.readOutbound<String>() | ||
assertEquals(result, "theMessage\n") | ||
} | ||
|
||
@Test | ||
fun decodeStripsTrailingChar() { | ||
channel.writeInbound("theMessage\n") | ||
val result = channel.readInbound<String>() | ||
assertEquals(result, "theMessage") | ||
} | ||
|
||
@Test | ||
fun decodeOnlyStripsSingleTrailingChar() { | ||
channel.writeInbound("theMessage\n\n") | ||
val result = channel.readInbound<String>() | ||
assertEquals(result, "theMessage\n") | ||
} | ||
|
||
@Test | ||
fun decodeThrowsWhenTrailingCharMissing() { | ||
assertThrows<DecoderException> { channel.writeInbound("theMessage") } | ||
} | ||
} |
Oops, something went wrong.