|
|
|
|
@@ -0,0 +1,59 @@
|
|
|
|
|
package mc.protocol.codec;
|
|
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
|
|
class ProtocolSplitterTest {
|
|
|
|
|
|
|
|
|
|
byte[] expectedPacket1Bytes;
|
|
|
|
|
byte[] expectedPacket2Bytes;
|
|
|
|
|
byte[] bytes;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void split() {
|
|
|
|
|
prepare();
|
|
|
|
|
|
|
|
|
|
ByteBuf inputBuffer = Unpooled.wrappedBuffer(bytes);
|
|
|
|
|
List<Object> outputObject = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
ProtocolSplitter protocolSplitter = new ProtocolSplitter();
|
|
|
|
|
protocolSplitter.decode(null, inputBuffer, outputObject);
|
|
|
|
|
|
|
|
|
|
assertEquals(2, outputObject.size());
|
|
|
|
|
assertTrue(outputObject.get(0) instanceof ByteBuf);
|
|
|
|
|
assertArrayEquals(expectedPacket1Bytes, ((ByteBuf)outputObject.get(0)).array());
|
|
|
|
|
assertTrue(outputObject.get(1) instanceof ByteBuf);
|
|
|
|
|
assertArrayEquals(expectedPacket2Bytes, ((ByteBuf)outputObject.get(1)).array());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void prepare() {
|
|
|
|
|
/*
|
|
|
|
|
+-------------------------------------------------+
|
|
|
|
|
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
|
|
|
|
|
+--------+-------------------------------------------------+----------------+
|
|
|
|
|
|00000000| 10 00 d4 02 09 31 32 37 2e 30 2e 30 2e 31 63 dd |.....127.0.0.1c.|
|
|
|
|
|
|00000010| 01 01 00 |... |
|
|
|
|
|
+--------+-------------------------------------------------+----------------+
|
|
|
|
|
*/
|
|
|
|
|
expectedPacket1Bytes = new byte[]{
|
|
|
|
|
0x00, (byte) 0xd4, 0x02, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x63,
|
|
|
|
|
(byte) 0xdd, 0x01
|
|
|
|
|
};
|
|
|
|
|
expectedPacket2Bytes = new byte[]{
|
|
|
|
|
0x00
|
|
|
|
|
};
|
|
|
|
|
bytes = new byte[2 + expectedPacket1Bytes.length + expectedPacket2Bytes.length];
|
|
|
|
|
bytes[0] = 0x10;
|
|
|
|
|
System.arraycopy(expectedPacket1Bytes, 0,
|
|
|
|
|
bytes, 1, expectedPacket1Bytes.length);
|
|
|
|
|
bytes[1 + expectedPacket1Bytes.length] = 0x01;
|
|
|
|
|
System.arraycopy(expectedPacket2Bytes, 0,
|
|
|
|
|
bytes, 2 + expectedPacket1Bytes.length, expectedPacket2Bytes.length);
|
|
|
|
|
}
|
|
|
|
|
}
|