From cca60c270a8cee4c1df9459bab31a2f56d6f0eaa Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Fri, 31 May 2024 12:34:23 -0700 Subject: [PATCH] Fix tests so they pass (post-SDK migration) --- .../NativeGraphicsManagerTest.cs | 8 +- EOLib.IO.Test/Map/MapFilePropertiesTest.cs | 4 +- .../Serializers/PubFileSerializerTest.cs | 24 ++- .../FileTransfer/FileRequestServiceTest.cs | 160 ++++++++++++------ .../TestHelpers/PacketSendServiceHelpers.cs | 24 ++- 5 files changed, 136 insertions(+), 84 deletions(-) diff --git a/EOLib.Graphics.Test/NativeGraphicsManagerTest.cs b/EOLib.Graphics.Test/NativeGraphicsManagerTest.cs index 14c2ffeb6..69d24a3e7 100644 --- a/EOLib.Graphics.Test/NativeGraphicsManagerTest.cs +++ b/EOLib.Graphics.Test/NativeGraphicsManagerTest.cs @@ -155,7 +155,7 @@ public void WhenLoadTexture_MaleHat_Transparent_SetsSpecialColorToTransparent() Texture2D resultTexture; var bmp = LoadGFXReturnsBitmap(GFXTypes.MaleHat, requestedResource); - FillBitmapWithColor(bmp, new Color(0x08, 0x00, 0x00, 0xff)); + FillBitmapWithColor(bmp, new Color(0xff000008)); resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.MaleHat, requestedResource, true); var data = new Microsoft.Xna.Framework.Color[resultTexture.Width * resultTexture.Height]; @@ -171,7 +171,7 @@ public void WhenLoadTexture_FemaleHat_Transparent_SetsSpecialColorToTransparent( Texture2D resultTexture; var bmp = LoadGFXReturnsBitmap(GFXTypes.FemaleHat, requestedResource); - FillBitmapWithColor(bmp, new Color(0x08, 0x00, 0x00, 0xff)); + FillBitmapWithColor(bmp, new Color(0xff000008)); resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.FemaleHat, requestedResource, true); var data = new Color[resultTexture.Width * resultTexture.Height]; @@ -221,9 +221,9 @@ private static void FillBitmapWithColor(Memory image, Color color) { for (int i = 54; i < image.Length; i+=4) { - image.Span[i] = color.R; + image.Span[i] = color.B; image.Span[i + 1] = color.G; - image.Span[i + 2] = color.B; + image.Span[i + 2] = color.R; image.Span[i + 3] = color.A; } } diff --git a/EOLib.IO.Test/Map/MapFilePropertiesTest.cs b/EOLib.IO.Test/Map/MapFilePropertiesTest.cs index c961c0c7b..49cfbe460 100644 --- a/EOLib.IO.Test/Map/MapFilePropertiesTest.cs +++ b/EOLib.IO.Test/Map/MapFilePropertiesTest.cs @@ -93,7 +93,7 @@ public void MapFileProperties_DeserializeFromByteArray_ThrowsExceptionWhenNotEMF private static IMapFileProperties CreateMapPropertiesWithSomeTestData(IMapFileProperties props) { - return props.WithChecksum(new List {1, 2, 3, 4}) + return props.WithChecksum(new List {1, 2}) .WithName("Some test name") .WithWidth(200) .WithHeight(100) @@ -117,7 +117,7 @@ private static byte[] CreateExpectedBytes(IMapFileProperties props) var ret = new List(); ret.AddRange(Encoding.ASCII.GetBytes(props.FileType)); - ret.AddRange(props.Checksum.Cast()); + ret.AddRange(props.Checksum.SelectMany(x => numberEncoderService.EncodeNumber(x, 2))); var fullName = Enumerable.Repeat((byte)0xFF, 24).ToArray(); var encodedName = mapStringEncoderService.EncodeMapString(props.Name, props.Name.Length); diff --git a/EOLib.IO.Test/Services/Serializers/PubFileSerializerTest.cs b/EOLib.IO.Test/Services/Serializers/PubFileSerializerTest.cs index 0f917146c..9119c8a7f 100644 --- a/EOLib.IO.Test/Services/Serializers/PubFileSerializerTest.cs +++ b/EOLib.IO.Test/Services/Serializers/PubFileSerializerTest.cs @@ -30,9 +30,12 @@ public abstract class PubFileSerializerTest [Test] public void DeserializeFromByteArray_WrongLength_Throws() { - const int ExpectedChecksum = 1234567890; + const int ExpectedChecksum1 = 12345; + const int ExpectedChecksum2 = 6789; const int ExpectedLength = 4; + var expectedChecksum = new List { ExpectedChecksum1, ExpectedChecksum2 }; + var records = new[] { new U().WithID(1).WithName("Rec_1"), @@ -41,16 +44,19 @@ public void DeserializeFromByteArray_WrongLength_Throws() new U().WithID(4).WithName("Rec_4"), }; - var pubBytesShort = MakePubFileBytes(ExpectedChecksum, ExpectedLength - 1, records); + var pubBytesShort = MakePubFileBytes(expectedChecksum, ExpectedLength - 1, records); Assert.That(() => CreateSerializer().DeserializeFromByteArray(1, pubBytesShort, () => new T()), Throws.InstanceOf()); } [Test] public void DeserializeFromByteArray_HasExpectedHeader() { - const int ExpectedChecksum = 1234567890; + const int ExpectedChecksum1 = 12345; + const int ExpectedChecksum2 = 6789; const int ExpectedLength = 4; + var expectedChecksum = new List { ExpectedChecksum1, ExpectedChecksum2 }; + var records = new[] { new U().WithID(1).WithName("Rec_1"), @@ -59,17 +65,17 @@ public void DeserializeFromByteArray_HasExpectedHeader() new U().WithID(4).WithName("Rec_4"), }; - var pubBytes = MakePubFileBytes(ExpectedChecksum, ExpectedLength, records); + var pubBytes = MakePubFileBytes(expectedChecksum, ExpectedLength, records); var file = CreateSerializer().DeserializeFromByteArray(1, pubBytes, () => new T()); - Assert.That(file.CheckSum, Is.EqualTo(ExpectedChecksum)); + Assert.That(file.CheckSum, Is.EqualTo(expectedChecksum)); Assert.That(file.Length, Is.EqualTo(ExpectedLength)); } [Test] public void SerializeToByteArray_ReturnsExpectedBytes() { - var expectedBytes = MakePubFileBytes(55565554, + var expectedBytes = MakePubFileBytes(new List { 5556, 5554 }, 9, new U().WithID(1).WithName("TestFixture"), new U().WithID(2).WithName("Test2"), @@ -104,7 +110,7 @@ public void DeserializeFromByteArray_HasExpectedIDAndNames() new U().WithID(8).WithName("Test8"), new U().WithID(9).WithName("eof") }; - var bytes = MakePubFileBytes(55565554, 9, records); + var bytes = MakePubFileBytes(new List { 5556, 5554 }, 9, records); var serializer = CreateSerializer(); var file = serializer.DeserializeFromByteArray(1, bytes, () => new T()); @@ -113,14 +119,14 @@ public void DeserializeFromByteArray_HasExpectedIDAndNames() file.Select(x => new { x.ID, x.Name }).ToList()); } - private byte[] MakePubFileBytes(int checksum, int length, params IPubRecord[] records) + private byte[] MakePubFileBytes(List checksum, int length, params IPubRecord[] records) { var numberEncoderService = new NumberEncoderService(); var recordSerializer = new PubRecordSerializer(numberEncoderService); var bytes = new List(); bytes.AddRange(Encoding.ASCII.GetBytes(new T().FileType)); - bytes.AddRange(numberEncoderService.EncodeNumber(checksum, 4)); + bytes.AddRange(checksum.SelectMany(x => numberEncoderService.EncodeNumber(x, 2))); bytes.AddRange(numberEncoderService.EncodeNumber(length, 2)); bytes.Add(numberEncoderService.EncodeNumber(1, 1)[0]); foreach (var record in records) diff --git a/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs b/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs index 3ddd8bfec..5a2ce8a44 100644 --- a/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs +++ b/EOLib.Test/Net/FileTransfer/FileRequestServiceTest.cs @@ -13,6 +13,11 @@ using NUnit.Framework; using Moq; using System.Collections.Generic; +using Moffat.EndlessOnline.SDK.Protocol.Net; +using Moffat.EndlessOnline.SDK.Protocol.Net.Client; +using Moffat.EndlessOnline.SDK.Protocol.Net.Server; +using System.Text; +using Moffat.EndlessOnline.SDK.Data; namespace EOLib.Test.Net.FileTransfer { @@ -44,21 +49,21 @@ public void SetUp() [Test] public void RequestFile_ResponsePacketHasInvalidHeader_ThrowsEmptyPacketReceivedException() { - Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(PacketFamily.Account, PacketAction.Accept); - Assert.ThrowsAsync(async () => await _fileRequestService.RequestFile(InitFileType.Item, 1)); + Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(); + Assert.ThrowsAsync(async () => await _fileRequestService.RequestFile(FileType.Eif, 1)); } [Test] public void RequestFile_ResponsePacketInvalidExtraByte_ThrowsMalformedPacketException() { - Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(PacketFamily.Init, PacketAction.Init, (byte)InitReply.ItemFile, 33); - Assert.ThrowsAsync(async () => await _fileRequestService.RequestFile(InitFileType.Item, 1)); + Mock.Get(_packetSendService).SetupReceivedPacketHasHeader((byte)InitReply.FileEif, 33); + Assert.ThrowsAsync(async () => await _fileRequestService.RequestFile(FileType.Eif, 1)); } [Test] public void RequestFile_SendsPacket_BasedOnSpecifiedType() { - var types = new[] { InitFileType.Item, InitFileType.Npc, InitFileType.Spell, InitFileType.Class }; + var types = new[] { FileType.Eif, FileType.Enf, FileType.Esf, FileType.Ecf }; foreach (var type in types) { var packetIsCorrect = false; @@ -75,33 +80,33 @@ public void RequestFile_SendsPacket_BasedOnSpecifiedType() [Test] public void RequestFile_CorrectResponse_ExecutesWithoutFault() { - var types = new[] { InitFileType.Item, InitFileType.Npc, InitFileType.Spell, InitFileType.Class }; + var types = new[] { FileType.Eif, FileType.Enf, FileType.Esf, FileType.Ecf }; foreach (var type in types) { - Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(PacketFamily.Init, PacketAction.Init, CreateFilePacket(type)); + Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(CreateFilePacket(type)); AggregateException aggEx = null; switch (type) { - case InitFileType.Item: + case FileType.Eif: Mock.Get(_pubFileDeserializer) .Setup(x => x.DeserializeFromByteArray(1, It.IsAny(), It.IsAny>>())) .Returns(new EIFFile()); aggEx = _fileRequestService.RequestFile(type, 1).Exception; break; - case InitFileType.Npc: + case FileType.Enf: Mock.Get(_pubFileDeserializer) .Setup(x => x.DeserializeFromByteArray(1, It.IsAny(), It.IsAny>>())) .Returns(new ENFFile()); aggEx = _fileRequestService.RequestFile(type, 1).Exception; break; - case InitFileType.Spell: + case FileType.Esf: Mock.Get(_pubFileDeserializer) .Setup(x => x.DeserializeFromByteArray(1, It.IsAny(), It.IsAny>>())) .Returns(new ESFFile()); aggEx = _fileRequestService.RequestFile(type, 1).Exception; break; - case InitFileType.Class: + case FileType.Ecf: Mock.Get(_pubFileDeserializer) .Setup(x => x.DeserializeFromByteArray(1, It.IsAny(), It.IsAny>>())) .Returns(new ECFFile()); @@ -121,22 +126,22 @@ public void RequestFile_CorrectResponse_ExecutesWithoutFault() [Test] public void RequestMapFile_ResponsePacketHasInvalidHeader_ThrowsEmptyPacketReceivedException() { - Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(PacketFamily.Account, PacketAction.Accept); + Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(); Assert.ThrowsAsync(async () => await _fileRequestService.RequestMapFile(1, 1)); } [Test] public void RequestMapFile_ResponsePacketHasIncorrectFileType_ThrowsMalformedPacketException() { - Mock.Get(_packetSendService).SetupReceivedPacketHasHeader(PacketFamily.Init, PacketAction.Init, (byte) InitReply.SpellFile, 33); - Assert.ThrowsAsync(async () => await _fileRequestService.RequestMapFile(1, 1)); + Mock.Get(_packetSendService).SetupReceivedPacketHasHeader((byte) InitReply.FileEsf, 33); + Assert.ThrowsAsync(async () => await _fileRequestService.RequestMapFile(1, 1)); } [Test] public void RequestMapFile_SendsPacket_BasedOnSpecifiedMap() { var packetIsCorrect = false; - Mock.Get(_packetSendService).Setup(x => x.SendEncodedPacketAndWaitAsync(It.IsAny())).Callback((IPacket packet) => packetIsCorrect = IsCorrectFileRequestPacket(packet, InitFileType.Map)); + Mock.Get(_packetSendService).Setup(x => x.SendEncodedPacketAndWaitAsync(It.IsAny())).Callback((IPacket packet) => packetIsCorrect = IsCorrectFileRequestPacket(packet, FileType.Emf)); _fileRequestService.RequestMapFile(1, 1); @@ -152,7 +157,7 @@ public void RequestMapFile_HasPlayerAndMapID() var packetIsCorrect = false; Mock.Get(_packetSendService) .Setup(x => x.SendEncodedPacketAndWaitAsync(It.IsAny())) - .Callback((IPacket p) => packetIsCorrect = IsCorrectFileRequestPacket(p, InitFileType.Map, PlayerID, MapID)); + .Callback((IPacket p) => packetIsCorrect = IsCorrectFileRequestPacket(p, FileType.Emf, PlayerID, MapID)); _fileRequestService.RequestMapFile(MapID, PlayerID); @@ -163,67 +168,112 @@ public void RequestMapFile_HasPlayerAndMapID() #region Helper Methods - private static bool IsCorrectFileRequestPacket(IPacket packet, InitFileType type, short playerId = 0, short mapId = 0) + private static bool IsCorrectFileRequestPacket(IPacket packet, FileType type, short sessionId = 0, short mapId = 0) { - var correctTyping = packet.Family == PacketFamily.Welcome && packet.Action == PacketAction.Agree && packet.ReadChar() == (byte) type; + var waPacket = packet as WelcomeAgreeClientPacket; + var correctTyping = waPacket.FileType == type; var correctData = true; - if (mapId > 0 && playerId > 0) + if (mapId > 0 && sessionId > 0) { - correctData = packet.ReadShort() == playerId && packet.ReadShort() == mapId; + var emfData = waPacket.FileTypeData as WelcomeAgreeClientPacket.FileTypeDataEmf; + correctData = emfData.FileId == mapId && waPacket.SessionId == sessionId; } return correctTyping && correctData; } - private static byte[] CreateFilePacket(InitFileType type) + private static byte[] CreateFilePacket(FileType type) { - IPacketBuilder packetBuilder = new PacketBuilder(); + var ret = new InitInitServerPacket(); var nes = new NumberEncoderService(); var rs = new PubRecordSerializer(nes); + var eoWriter = new EoWriter(); switch (type) { - case InitFileType.Item: - packetBuilder = packetBuilder - .AddByte((byte) InitReply.ItemFile).AddChar(1) //spacer - .AddString("EIF").AddInt(1) //RID - .AddShort(2) //Len - .AddByte(1) //filler byte - .AddBytes(rs.SerializeToByteArray(new EIFRecord().WithID(1).WithName("Test1"))) - .AddBytes(rs.SerializeToByteArray(new EIFRecord().WithID(2).WithName("eof"))); + case FileType.Eif: + ret.ReplyCode = InitReply.FileEif; + + eoWriter.AddString("EIF"); + eoWriter.AddInt(1); // RID + eoWriter.AddShort(2); // length + eoWriter.AddByte(1); // version + eoWriter.AddBytes(rs.SerializeToByteArray(new EIFRecord().WithID(1).WithName("Test1"))); + eoWriter.AddBytes(rs.SerializeToByteArray(new EIFRecord().WithID(2).WithName("eof"))); + + ret.ReplyCodeData = new InitInitServerPacket.ReplyCodeDataFileEif + { + PubFile = new PubFile + { + FileId = 1, + Content = eoWriter.ToByteArray() + } + }; break; - case InitFileType.Npc: - packetBuilder = packetBuilder - .AddByte((byte) InitReply.NpcFile).AddChar(1) //spacer - .AddString("ENF").AddInt(1) //RID - .AddShort(2) //Len - .AddByte(1) //filler byte - .AddBytes(rs.SerializeToByteArray(new ENFRecord().WithID(1).WithName("Test1"))) - .AddBytes(rs.SerializeToByteArray(new ENFRecord().WithID(2).WithName("eof"))); + case FileType.Enf: + ret.ReplyCode = InitReply.FileEnf; + + eoWriter.AddString("ENF"); + eoWriter.AddInt(1); // RID + eoWriter.AddShort(2); // length + eoWriter.AddByte(1); // version + eoWriter.AddBytes(rs.SerializeToByteArray(new ENFRecord().WithID(1).WithName("Test1"))); + eoWriter.AddBytes(rs.SerializeToByteArray(new ENFRecord().WithID(2).WithName("eof"))); + + ret.ReplyCodeData = new InitInitServerPacket.ReplyCodeDataFileEnf + { + PubFile = new PubFile + { + FileId = 1, + Content = eoWriter.ToByteArray() + } + }; break; - case InitFileType.Spell: - packetBuilder = packetBuilder - .AddByte((byte) InitReply.SpellFile).AddChar(1) //spacer - .AddString("ESF").AddInt(1) //RID - .AddShort(2) //Len - .AddByte(1) //filler byte - .AddBytes(rs.SerializeToByteArray(new ESFRecord().WithID(1).WithNames(new List { "Test1", "" }))) - .AddBytes(rs.SerializeToByteArray(new ESFRecord().WithID(2).WithNames(new List { "eof", "" }))); + case FileType.Esf: + ret.ReplyCode = InitReply.FileEsf; + + eoWriter.AddString("ESF"); + eoWriter.AddInt(1); // RID + eoWriter.AddShort(2); // length + eoWriter.AddByte(1); // version + eoWriter.AddBytes(rs.SerializeToByteArray(new ESFRecord().WithID(1).WithName("Test1"))); + eoWriter.AddBytes(rs.SerializeToByteArray(new ESFRecord().WithID(2).WithName("eof"))); + + ret.ReplyCodeData = new InitInitServerPacket.ReplyCodeDataFileEsf + { + PubFile = new PubFile + { + FileId = 1, + Content = eoWriter.ToByteArray() + } + }; break; - case InitFileType.Class: - packetBuilder = packetBuilder - .AddByte((byte) InitReply.ClassFile).AddChar(1) //spacer - .AddString("ECF").AddInt(1) //RID - .AddShort(2) //Len - .AddByte(1) //filler byte - .AddBytes(rs.SerializeToByteArray(new ECFRecord().WithID(1).WithName("Test1"))) - .AddBytes(rs.SerializeToByteArray(new ECFRecord().WithID(2).WithName("eof"))); + case FileType.Ecf: + ret.ReplyCode = InitReply.FileEcf; + + eoWriter.AddString("ECF"); + eoWriter.AddInt(1); // RID + eoWriter.AddShort(2); // length + eoWriter.AddByte(1); // version + eoWriter.AddBytes(rs.SerializeToByteArray(new ECFRecord().WithID(1).WithName("Test1"))); + eoWriter.AddBytes(rs.SerializeToByteArray(new ECFRecord().WithID(2).WithName("eof"))); + + ret.ReplyCodeData = new InitInitServerPacket.ReplyCodeDataFileEcf + { + PubFile = new PubFile + { + FileId = 1, + Content = eoWriter.ToByteArray() + } + }; break; } - return packetBuilder.Build().RawData.ToArray(); + eoWriter = new EoWriter(); + ret.Serialize(eoWriter); + return eoWriter.ToByteArray(); } #endregion diff --git a/EOLib.Test/TestHelpers/PacketSendServiceHelpers.cs b/EOLib.Test/TestHelpers/PacketSendServiceHelpers.cs index cb5a5978e..4a0629c98 100644 --- a/EOLib.Test/TestHelpers/PacketSendServiceHelpers.cs +++ b/EOLib.Test/TestHelpers/PacketSendServiceHelpers.cs @@ -1,8 +1,10 @@ -using System.Diagnostics.CodeAnalysis; -using System.Threading.Tasks; -using EOLib.Net; -using EOLib.Net.Communication; +using EOLib.Net.Communication; +using Moffat.EndlessOnline.SDK.Data; +using Moffat.EndlessOnline.SDK.Protocol.Net; using Moq; +using System; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; namespace EOLib.Test.TestHelpers { @@ -13,18 +15,12 @@ internal static class PacketSendServiceHelpers /// Setup the PacketSendService mock to return a packet with the specified family/action/data from SendEncodedPacketAndWaitAsync /// /// The mocked packet send service - /// Packet family for the "received" packet - /// Packet action for the "received" packet /// Packet data payload (any additional data that should be in the packet) - internal static void SetupReceivedPacketHasHeader(this Mock packetSendServiceMock, - PacketFamily family, - PacketAction action, - params byte[] data) + internal static void SetupReceivedPacketHasHeader(this Mock packetSendServiceMock, params byte[] data) + where T : IPacket { - var receivedPacket = new PacketBuilder(family, action) - .AddBytes(data) - .Build(); - + IPacket receivedPacket = (IPacket)Activator.CreateInstance(typeof(T)); + receivedPacket.Deserialize(new EoReader(data)); packetSendServiceMock.Setup(x => x.SendEncodedPacketAndWaitAsync(It.IsAny())) .Returns(Task.FromResult(receivedPacket)); }