public void TryParseTest(string file, string expectedPackageTypesFile, string dateFormat, string token) { var packets = ReadCapturedPackets(file, null); var expectedPackageTypes = !string.IsNullOrEmpty(expectedPackageTypesFile) ? GetPackageTypeList <PackageType>(expectedPackageTypesFile) : new List <PackageType>(); var packetManager = new PokerKingPacketManager(); var expectedCommandsIndex = 0; using (ShimsContext.Create()) { foreach (var packet in packets) { ShimDateTime.NowGet = () => packet.CreatedTimeStamp; if (packetManager.TryParse(packet, out IList <PokerKingPackage> packages)) { foreach (var package in packages) { Console.WriteLine(package.PackageType); if (expectedPackageTypes.Count > 0) { Assert.That(package.PackageType, Is.EqualTo(expectedPackageTypes[expectedCommandsIndex++])); if (!string.IsNullOrEmpty(token)) { var key = Encoding.ASCII.GetBytes(token); var cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5Padding"); cipher.Init(false, new KeyParameter(key)); var bytes = cipher.ProcessBytes(package.Body); var final = cipher.DoFinal(); var decryptedData = bytes == null ? final : (final == null ? bytes : bytes.Concat(final).ToArray()); package.Body = decryptedData; } AssertPackage(package, packet); } } } } } }
public void DeserializationTest(string file, string jsonFile) { var bytes = ReadPacketFile(file); var packetManager = new PokerKingPacketManager(); var capturedPacket = new CapturedPacket { Bytes = bytes, CreatedTimeStamp = DateTime.Parse("08/02/2018 12:28:28"), Destination = new IPEndPoint(IPAddress.Parse("192.168.0.105"), 27633), Source = new IPEndPoint(IPAddress.Parse("47.52.92.161"), 9188), SequenceNumber = 1962805251 }; var result = packetManager.TryParse(capturedPacket, out IList <PokerKingPackage> packages); foreach (var package in packages) { object actual = null; switch (package.PackageType) { case PackageType.NoticeGameRoundEnd: actual = SerializationHelper.Deserialize <NoticeGameRoundEnd>(package.Body); break; case PackageType.NoticePlayerActionTurn: actual = SerializationHelper.Deserialize <NoticePlayerActionTurn>(package.Body); break; case PackageType.NoticeGameSnapShot: actual = SerializationHelper.Deserialize <NoticeGameSnapShot>(package.Body); break; case PackageType.NoticePlayerAction: actual = SerializationHelper.Deserialize <NoticePlayerAction>(package.Body); break; case PackageType.RequestJoinRoom: actual = SerializationHelper.Deserialize <RequestJoinRoom>(package.Body); break; } var jsonExpected = File.ReadAllText(Path.Combine(TestDataFolder, jsonFile)); var jsonActual = JsonConvert.SerializeObject(actual, Formatting.Indented, new StringEnumConverter()); Assert.That(jsonActual, Is.EqualTo(jsonExpected)); } }
public void TryParsePacketTest(string file, int expected) { var bytes = ReadPacketFile(file); var packetManager = new PokerKingPacketManager(); var capturedPacket = new CapturedPacket { Bytes = bytes, CreatedTimeStamp = DateTime.Parse("08/02/2018 12:28:28"), Destination = new IPEndPoint(IPAddress.Parse("192.168.0.105"), 27633), Source = new IPEndPoint(IPAddress.Parse("47.52.92.161"), 9188), SequenceNumber = 1962805251 }; var result = packetManager.TryParse(capturedPacket, out IList <PokerKingPackage> actualPackages); Assert.IsTrue(result); Assert.IsNotNull(actualPackages); Assert.That(actualPackages.Count, Is.EqualTo(expected)); }
public void MultipleTryBuildTest(string folder, int expectedTotalHands) { var testFolder = Path.Combine(TestDataFolder, folder); DirectoryAssert.Exists(testFolder); var logFiles = Directory.GetFiles(testFolder, "*.txt."); var capturedPackets = new List <CapturedPacket>(); foreach (var logFile in logFiles) { capturedPackets.AddRange(TcpImporterTestUtils.ReadCapturedPackets(logFile, null)); } capturedPackets = capturedPackets.OrderBy(x => x.CreatedTimeStamp).ToList(); var handHistories = new List <HandHistory>(); var packetManager = new PokerKingPacketManager(); var handBuilder = new PKHandBuilder(); var debugPKImporter = new DebugPKImporter(); foreach (var capturedPacket in capturedPackets) { if (!packetManager.TryParse(capturedPacket, out IList <PokerKingPackage> packages)) { continue; } var isFastFold = PKImporterHelper.IsFastFoldPort(capturedPacket.Destination.Port) || PKImporterHelper.IsFastFoldPort(capturedPacket.Source.Port); foreach (var package in packages) { package.IsFastFold = isFastFold; if (!PKImporterStub.IsAllowedPackage(package)) { continue; } package.Timestamp = capturedPacket.CreatedTimeStamp; debugPKImporter.LogPackage(capturedPacket, package); if (package.PackageType == PackageType.RequestJoinRoom) { debugPKImporter.ParsePackage <RequestJoinRoom>(package, body => LogProvider.Log.Info($"User {package.UserId} entered room {body.RoomId}."), () => LogProvider.Log.Info($"User {package.UserId} entered room.")); continue; } var port = destinationPorts.Contains(capturedPacket.Destination.Port) ? capturedPacket.Destination.Port : capturedPacket.Source.Port; if (package.PackageType == PackageType.RequestLeaveRoom) { debugPKImporter.ParsePackage <RequestLeaveRoom>(package, body => { LogProvider.Log.Info($"User {package.UserId} left room {body.RoomId}."); handBuilder.CleanRoom(port, body.RoomId); }, () => LogProvider.Log.Info($"User {package.UserId} left room {package.RoomId}.")); continue; } if (isFastFold && package.PackageType == PackageType.NoticeQuickLeave) { debugPKImporter.ParsePackage <NoticeQuickLeave>(package, body => { handBuilder.CleanFastFoldRooms(package, port, out List <HandHistory> fastFoldHandHistories); foreach (var fastFoldHandHistory in fastFoldHandHistories) { LogProvider.Log.Info($"Hand #{fastFoldHandHistory.HandId} user #{package.UserId} room #{package.RoomId}"); handHistories.Add(fastFoldHandHistory); } LogProvider.Log.Info($"User {package.UserId} left room {body.RoomId} (Fast Fold)."); }, () => LogProvider.Log.Info($"User {package.UserId} left room {package.RoomId} (Fast Fold).")); continue; } if (handBuilder.TryBuild(package, port, out HandHistory handHistory)) { handHistories.Add(handHistory); LogProvider.Log.Info($"Hand #{handHistory.HandId} has been sent. [{package.UserId}]"); } } } WriteHandHistoriesToFile(handHistories); Assert.That(handHistories.Count, Is.EqualTo(expectedTotalHands)); }