Files
CentrED/tests/UTestPackets.pas
T
aksdb 99749a5168 ✅ Add FPCUnit test suites for shared code and server
Introduces two headless FPCUnit runners under tests/ with 119 tests
covering the shared stream, cache, and packet layers, the MUL file
library, and the server config, region, account, and landscape
bounds logic. Each runner uses its own object output directory so
client and server PPUs with colliding unit names cannot pollute
each other's builds. Drone now builds and runs both suites before
packaging. Found on the way: two product defects fixed in the
previous commits.
2026-11-15 22:08:32 +01:00

254 lines
6.2 KiB
ObjectPascal

unit UTestPackets;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry,
UEnhancedMemoryStream, UPacket, UStatics, UPackets;
type
TTestClientPackets = class(TTestCase)
published
procedure LoginRequestLayout;
procedure RequestBlocksWithoutCoords;
procedure RequestBlocksWithCoords;
procedure ElevateStaticPacketSize;
procedure ElevateStaticSignedZ;
procedure DrawMapPacketSize;
procedure MoveStaticPacketSize;
procedure DeleteStaticPacketSize;
procedure HueStaticPacketSize;
procedure InsertStaticPacketSize;
procedure CompressedPacketWireFormat;
procedure FreeBlockPacketSize;
end;
implementation
procedure TTestClientPackets.LoginRequestLayout;
var
packet: TLoginRequestPacket;
stream: TEnhancedMemoryStream;
begin
packet := TLoginRequestPacket.Create('alice', 'secret');
try
stream := packet.Stream;
AssertEquals('id + length + payload', Int64(19), stream.Size);
stream.Position := 0;
AssertEquals('admin id', Byte($02), stream.ReadByte);
AssertEquals('packet length', Cardinal(19), stream.ReadCardinal);
AssertEquals('connection sub id', Byte($03), stream.ReadByte);
AssertEquals('username', 'alice', stream.ReadStringNull);
AssertEquals('password', 'secret', stream.ReadStringNull);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.RequestBlocksWithoutCoords;
var
packet: TRequestBlocksPacket;
stream: TEnhancedMemoryStream;
coords: TBlockCoordsArray;
begin
SetLength(coords, 0);
packet := TRequestBlocksPacket.Create(coords);
try
stream := packet.Stream;
AssertEquals('header only', Int64(5), stream.Size);
stream.Position := 0;
stream.ReadByte;
AssertEquals('declared length', Cardinal(5), stream.ReadCardinal);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.RequestBlocksWithCoords;
var
packet: TRequestBlocksPacket;
stream: TEnhancedMemoryStream;
coords: TBlockCoordsArray;
begin
SetLength(coords, 2);
coords[0].X := 1;
coords[0].Y := 2;
coords[1].X := 3;
coords[1].Y := 4;
packet := TRequestBlocksPacket.Create(coords);
try
stream := packet.Stream;
AssertEquals('header + two coords', Int64(5 + 2 * 4), stream.Size);
stream.Position := 0;
stream.ReadByte;
AssertEquals('declared length matches', Cardinal(13),
stream.ReadCardinal);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.ElevateStaticPacketSize;
var
packet: TElevateStaticPacket;
stream: TEnhancedMemoryStream;
begin
packet := TElevateStaticPacket.Create(1, 2, 3, 4, 5, 6);
try
stream := packet.Stream;
AssertEquals('declared length equals stream size',
Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.ElevateStaticSignedZ;
var
packet: TElevateStaticPacket;
stream: TEnhancedMemoryStream;
z: ShortInt;
begin
packet := TElevateStaticPacket.Create(1, 2, 3, 4, 5, -6);
try
stream := packet.Stream;
stream.Position := stream.Size - 1;
z := stream.ReadShortInt;
AssertEquals('negative z must survive the writer', ShortInt(-6), z);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.DrawMapPacketSize;
var
packet: TDrawMapPacket;
stream: TEnhancedMemoryStream;
begin
packet := TDrawMapPacket.Create(1, 2, 3, 4);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.MoveStaticPacketSize;
var
packet: TMoveStaticPacket;
stream: TEnhancedMemoryStream;
begin
packet := TMoveStaticPacket.Create(1, 2, 3, 4, 5, 6, 7);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.DeleteStaticPacketSize;
var
item: TStaticItem;
packet: TDeleteStaticPacket;
stream: TEnhancedMemoryStream;
begin
item := TStaticItem.Create(nil, nil, 1, 2);
try
packet := TDeleteStaticPacket.Create(item);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
finally
item.Free;
end;
end;
procedure TTestClientPackets.HueStaticPacketSize;
var
packet: THueStaticPacket;
stream: TEnhancedMemoryStream;
begin
packet := THueStaticPacket.Create(1, 2, 3, 4, 5, 6);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.InsertStaticPacketSize;
var
packet: TInsertStaticPacket;
stream: TEnhancedMemoryStream;
begin
packet := TInsertStaticPacket.Create(1, 2, 3, 4, 5);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
procedure TTestClientPackets.CompressedPacketWireFormat;
var
source: TPacket;
compressed: TCompressedPacket;
stream: TEnhancedMemoryStream;
innerSize: Cardinal;
i: Integer;
begin
source := TPacket.Create($06, 8);
try
stream := source.Stream;
stream.Position := stream.Size;
for i := 1 to 8 do
stream.WriteByte(Byte(i * 11));
compressed := TCompressedPacket.Create(source); //takes ownership
try
AssertEquals('wrapper id', Byte($01), compressed.PacketID);
stream := compressed.Stream;
stream.Position := 0;
AssertEquals('wrapper id byte', Byte($01), stream.ReadByte);
AssertEquals('wrapper length covers everything', Int64(stream.Size),
Int64(stream.ReadCardinal));
stream.Position := 5;
innerSize := stream.ReadCardinal;
AssertEquals('announced inner stream size', Cardinal(9), innerSize);
AssertTrue('deflate payload present', stream.Size > 9);
finally
compressed.Free;
end;
except
source.Free; //only reached if compression failed before taking ownership
raise;
end;
end;
procedure TTestClientPackets.FreeBlockPacketSize;
var
packet: TFreeBlockPacket;
stream: TEnhancedMemoryStream;
begin
packet := TFreeBlockPacket.Create(3, 4);
try
stream := packet.Stream;
AssertEquals(Int64(packet.PacketLength), stream.Size);
finally
packet.Free;
end;
end;
initialization
RegisterTests([TTestClientPackets]);
end.