From 9cab2ab36e7981c847e3e9ae8c3fbbb36531ba6d Mon Sep 17 00:00:00 2001 From: Christian Date: Thu, 16 Jan 2014 18:07:25 -0500 Subject: [PATCH] Add in some tests and examples for the "simple" network stuff --- .../java/cpw/mods/fml/test/TestNetStuff.java | 123 ++++++++++++++++++ .../fml/test/simplenet/SimpleNetHandler1.java | 14 ++ .../fml/test/simplenet/SimpleNetHandler2.java | 14 ++ .../test/simplenet/SimpleNetTestMessage1.java | 17 +++ .../test/simplenet/SimpleNetTestMessage2.java | 18 +++ .../fml/test/simplenet/SimpleNetTester.java | 20 +++ 6 files changed, 206 insertions(+) create mode 100644 src/test/java/cpw/mods/fml/test/TestNetStuff.java create mode 100644 src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler1.java create mode 100644 src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler2.java create mode 100644 src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage1.java create mode 100644 src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage2.java create mode 100644 src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTester.java diff --git a/src/test/java/cpw/mods/fml/test/TestNetStuff.java b/src/test/java/cpw/mods/fml/test/TestNetStuff.java new file mode 100644 index 0000000000..4098aba0a4 --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/TestNetStuff.java @@ -0,0 +1,123 @@ +package cpw.mods.fml.test; + +import static org.junit.Assert.*; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; +import com.google.common.base.Strings; +import cpw.mods.fml.common.network.ByteBufUtils; + +public class TestNetStuff { + + @Test + public void testByteBufUtilsByteCount() + { + assertEquals("length of 1 is 1", 1, ByteBufUtils.varIntByteCount(1)); + assertEquals("length of 127 is 1", 1, ByteBufUtils.varIntByteCount(127)); + assertEquals("length of 128 is 2", 2, ByteBufUtils.varIntByteCount(128)); + assertEquals("length of 16383 is 2", 2, ByteBufUtils.varIntByteCount(16383)); + assertEquals("length of 16384 is 3", 3, ByteBufUtils.varIntByteCount(16384)); + assertEquals("length of 2097151 is 3", 3, ByteBufUtils.varIntByteCount(2097151)); + assertEquals("length of 2097152 is 4", 4, ByteBufUtils.varIntByteCount(2097152)); + assertEquals("length of 268435455 is 4", 4, ByteBufUtils.varIntByteCount(268435455)); + assertEquals("length of 268435456 is 5", 5, ByteBufUtils.varIntByteCount(268435456)); + assertEquals("length of MIN_VAL is 5", 5, ByteBufUtils.varIntByteCount(Integer.MIN_VALUE)); + assertEquals("length of MAX_VAL is 5", 5, ByteBufUtils.varIntByteCount(Integer.MAX_VALUE)); + assertEquals("length of -1 is 5", 5, ByteBufUtils.varIntByteCount(-1)); + } + + @Test + public void testByteBufUtilsByteArrays() + { + ByteBuf buf = Unpooled.buffer(5, 5); + ByteBufUtils.writeVarInt(buf, 1, 1); + assertArrayEquals("1 as byte[] is [1]", new byte[] { 1, 0, 0, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 127, 1); + assertArrayEquals("127 as byte[] is [127]", new byte[] { 127, 0, 0, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 128, 2); + assertArrayEquals("128 as byte[] is [-128, 1]", new byte[] { -128, 1, 0, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 16383, 2); + assertArrayEquals("16383 as byte[] is [-1, 127]", new byte[] { -1, 127, 0, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 16384, 3); + assertArrayEquals("16384 as byte[] is [-1, -128, 1]", new byte[] { -128, -128, 1, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 2097151, 3); + assertArrayEquals("2097151 as byte[] is [-1, -1, 127]", new byte[] { -1, -1, 127, 0, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 2097152, 4); + assertArrayEquals("16384 as byte[] is [-128, -128, 1]", new byte[] { -128, -128, -128, 1, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 268435455, 4); + assertArrayEquals("268435455 as byte[] is [-1, -1, -1, 127]", new byte[] { -1, -1, -1, 127, 0 }, buf.array()); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 268435456, 5); + assertArrayEquals("268435456 as byte[] is [-1, -128, 1]", new byte[] { -128, -128, -128, -128, 1 }, buf.array()); + } + + @Test + public void testByteBufUtilsByteReversals() + { + ByteBuf buf = Unpooled.buffer(5, 5); + ByteBufUtils.writeVarInt(buf, 1, 1); + assertEquals("1 is 1", 1, ByteBufUtils.readVarInt(buf, 1)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 127, 1); + assertEquals("127 is 127", 127, ByteBufUtils.readVarInt(buf, 1)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 128, 2); + assertEquals("128 is 128", 128, ByteBufUtils.readVarInt(buf, 2)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 16383, 2); + assertEquals("16383 is 16383", 16383, ByteBufUtils.readVarInt(buf, 2)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 16384, 3); + assertEquals("16384 is 16384", 16384, ByteBufUtils.readVarInt(buf, 3)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 2097151, 3); + assertEquals("2097151 is 2097151", 2097151, ByteBufUtils.readVarInt(buf, 3)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 2097152, 4); + assertEquals("2097152 is 2097152", 2097152, ByteBufUtils.readVarInt(buf, 4)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 268435455, 4); + assertEquals("268435455 is 268435455", 268435455, ByteBufUtils.readVarInt(buf, 4)); + + buf.clear(); + ByteBufUtils.writeVarInt(buf, 268435456, 5); + assertEquals("268435456 is 268435456", 268435456, ByteBufUtils.readVarInt(buf, 5)); + } + + @Test + public void testByteBufUtilsStrings() + { + String test = new String("test"); + ByteBuf buf = Unpooled.buffer(20, 20); + ByteBufUtils.writeUTF8String(buf, test); + assertArrayEquals("String bytes", new byte[] { 4, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, buf.array()); + + String repeat = Strings.repeat("test", 100); + buf = Unpooled.buffer(420, 420); + ByteBufUtils.writeUTF8String(buf, repeat); + assertArrayEquals("String repeat bytes", new byte[] {-112, 3, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, buf.array()); + } + +} diff --git a/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler1.java b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler1.java new file mode 100644 index 0000000000..a124edc54c --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler1.java @@ -0,0 +1,14 @@ +package cpw.mods.fml.test.simplenet; + +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +public class SimpleNetHandler1 implements IMessageHandler +{ + @Override + public SimpleNetTestMessage2 onMessage(SimpleNetTestMessage1 message, MessageContext context) + { + return null; + } + +} diff --git a/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler2.java b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler2.java new file mode 100644 index 0000000000..fd4c71fd84 --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetHandler2.java @@ -0,0 +1,14 @@ +package cpw.mods.fml.test.simplenet; + +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +public class SimpleNetHandler2 implements IMessageHandler +{ + @Override + public SimpleNetTestMessage1 onMessage(SimpleNetTestMessage2 message, MessageContext context) + { + return null; + } + +} diff --git a/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage1.java b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage1.java new file mode 100644 index 0000000000..74e05155f9 --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage1.java @@ -0,0 +1,17 @@ +package cpw.mods.fml.test.simplenet; + +import io.netty.buffer.ByteBuf; +import cpw.mods.fml.common.network.simpleimpl.IMessage; + +public class SimpleNetTestMessage1 implements IMessage { + @Override + public void fromBytes(ByteBuf buf) + { + } + + @Override + public void toBytes(ByteBuf buf) + { + } + +} diff --git a/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage2.java b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage2.java new file mode 100644 index 0000000000..7b030540b3 --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTestMessage2.java @@ -0,0 +1,18 @@ +package cpw.mods.fml.test.simplenet; + +import io.netty.buffer.ByteBuf; +import cpw.mods.fml.common.network.simpleimpl.IMessage; + +public class SimpleNetTestMessage2 implements IMessage { + + @Override + public void fromBytes(ByteBuf buf) + { + } + + @Override + public void toBytes(ByteBuf buf) + { + } + +} diff --git a/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTester.java b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTester.java new file mode 100644 index 0000000000..6be28fec98 --- /dev/null +++ b/src/test/java/cpw/mods/fml/test/simplenet/SimpleNetTester.java @@ -0,0 +1,20 @@ +package cpw.mods.fml.test.simplenet; + +import static org.junit.Assert.*; +import org.junit.Test; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; + +public class SimpleNetTester { + + @Test + public void test() + { + SimpleNetworkWrapper simpleChannel = NetworkRegistry.INSTANCE.newSimpleChannel("TEST"); + simpleChannel.registerMessage(SimpleNetHandler1.class, SimpleNetTestMessage1.class, 1, Side.SERVER); + simpleChannel.registerMessage(SimpleNetHandler2.class, SimpleNetTestMessage2.class, 2, Side.CLIENT); + assertTrue("Hello", true); + } + +}