Add tests for reading complete un/compressed stream

This commit is contained in:
themode 2022-01-27 14:58:30 +01:00
parent dbcb21d1aa
commit 735184b909
2 changed files with 73 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.utils.binary.BinaryBuffer;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.PooledBuffers;
import net.minestom.server.utils.binary.Writeable;
import net.minestom.server.utils.cache.LocalCache;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ -216,12 +217,20 @@ public final class PacketUtils {
public static void writeFramedPacket(@NotNull ByteBuffer buffer,
@NotNull ServerPacket packet,
boolean compression) {
writeFramedPacket(buffer, packet.getId(), packet,
compression ? MinecraftServer.getCompressionThreshold() : 0);
}
public static void writeFramedPacket(@NotNull ByteBuffer buffer,
int id,
@NotNull Writeable writeable,
int compressionThreshold) {
BinaryWriter writerView = BinaryWriter.view(buffer); // ensure that the buffer is not resized
if (!compression) {
if (compressionThreshold <= 0) {
// Uncompressed format https://wiki.vg/Protocol#Without_compression
final int lengthIndex = Utils.writeEmptyVarIntHeader(buffer);
Utils.writeVarInt(buffer, packet.getId());
packet.write(writerView);
Utils.writeVarInt(buffer, id);
writeable.write(writerView);
final int finalSize = buffer.position() - (lengthIndex + 3);
Utils.writeVarIntHeader(buffer, lengthIndex, finalSize);
return;
@ -231,10 +240,10 @@ public final class PacketUtils {
final int uncompressedIndex = Utils.writeEmptyVarIntHeader(buffer);
final int contentStart = buffer.position();
Utils.writeVarInt(buffer, packet.getId());
packet.write(writerView);
Utils.writeVarInt(buffer, id);
writeable.write(writerView);
final int packetSize = buffer.position() - contentStart;
final boolean compressed = packetSize >= MinecraftServer.getCompressionThreshold();
final boolean compressed = packetSize >= compressionThreshold;
if (compressed) {
// Packet large enough, compress it
final ByteBuffer input = PooledBuffers.tempBuffer().put(0, buffer, contentStart, packetSize);

View File

@ -0,0 +1,58 @@
package net.minestom.server.network;
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
import net.minestom.server.network.packet.client.play.ClientPluginMessagePacket;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.binary.BinaryBuffer;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.PooledBuffers;
import org.junit.jupiter.api.Test;
import java.util.zip.DataFormatException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class SocketReadTest {
@Test
public void completeUncompressed() throws DataFormatException {
var packet = new ClientChatMessagePacket("Hello World!");
var buffer = PooledBuffers.packetBuffer();
PacketUtils.writeFramedPacket(buffer, 3, packet, 0);
var wrapper = BinaryBuffer.wrap(buffer);
wrapper.reset(0, buffer.position());
var result = PacketUtils.readPackets(wrapper, false);
assertNull(result.remaining());
var packets = result.packets();
assertEquals(1, packets.size());
var rawPacket = packets.get(0);
assertEquals(3, rawPacket.id());
var readPacket = new ClientChatMessagePacket(new BinaryReader(rawPacket.payload()));
assertEquals("Hello World!", readPacket.message());
}
@Test
public void completeCompressed() throws DataFormatException {
var packet = new ClientPluginMessagePacket("channel", new byte[2000]);
var buffer = PooledBuffers.packetBuffer();
PacketUtils.writeFramedPacket(buffer, 0x0A, packet, 256);
var wrapper = BinaryBuffer.wrap(buffer);
wrapper.reset(0, buffer.position());
var result = PacketUtils.readPackets(wrapper, true);
assertNull(result.remaining());
var packets = result.packets();
assertEquals(1, packets.size());
var rawPacket = packets.get(0);
assertEquals(0x0A, rawPacket.id());
var readPacket = new ClientPluginMessagePacket(new BinaryReader(rawPacket.payload()));
assertEquals("channel", readPacket.channel());
assertEquals(2000, readPacket.data().length);
}
}