|
| 1 | +using LiteNetLib.Layers; |
| 2 | +using LiteNetLib.Utils; |
| 3 | +using NUnit.Framework; |
| 4 | +using System; |
| 5 | +using System.Net; |
| 6 | + |
| 7 | +namespace LiteNetLib.Tests |
| 8 | +{ |
| 9 | + [TestFixture] |
| 10 | + public class CRC32LayerTest |
| 11 | + { |
| 12 | + private Crc32cLayer _crc32Layer; |
| 13 | + private IPEndPoint _dummyEndpoint; |
| 14 | + |
| 15 | + [SetUp] |
| 16 | + public void Setup() |
| 17 | + { |
| 18 | + NetDebug.Logger = null; |
| 19 | + _crc32Layer = new Crc32cLayer(); |
| 20 | + _dummyEndpoint = new IPEndPoint(IPAddress.Loopback, 23456); |
| 21 | + } |
| 22 | + |
| 23 | + [Test] |
| 24 | + public void ReturnsDataWithoutChecksum() |
| 25 | + { |
| 26 | + byte[] packet = GetTestPacketWithCrc(); |
| 27 | + |
| 28 | + int offset = 0; |
| 29 | + int length = packet.Length; |
| 30 | + _crc32Layer.ProcessInboundPacket(_dummyEndpoint, ref packet, ref offset, ref length); |
| 31 | + |
| 32 | + Assert.AreEqual(packet.Length - CRC32C.ChecksumSize, length); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void ReturnsNilCountForBadChecksum() |
| 37 | + { |
| 38 | + byte[] packet = GetTestPacketWithCrc(); |
| 39 | + |
| 40 | + //Fake a change to the data to cause data/crc missmatch |
| 41 | + packet[4] = 0; |
| 42 | + |
| 43 | + int offset = 0; |
| 44 | + int length = packet.Length; |
| 45 | + _crc32Layer.ProcessInboundPacket(_dummyEndpoint, ref packet, ref offset, ref length); |
| 46 | + |
| 47 | + Assert.AreEqual(0, length); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public void ReturnsNilCountForTooShortMessage() |
| 52 | + { |
| 53 | + byte[] packet = new byte[2]; |
| 54 | + |
| 55 | + int offset = 0; |
| 56 | + int length = packet.Length; |
| 57 | + |
| 58 | + _crc32Layer.ProcessInboundPacket(_dummyEndpoint, ref packet, ref offset, ref length); |
| 59 | + |
| 60 | + Assert.AreEqual(0, length); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void CanSendAndReceiveSameMessage() |
| 65 | + { |
| 66 | + byte[] message = GetTestMessageBytes(); |
| 67 | + //Process outbound adds bytes, so we need a larger array |
| 68 | + byte[] package = new byte[message.Length + CRC32C.ChecksumSize]; |
| 69 | + |
| 70 | + Buffer.BlockCopy(message, 0, package, 0, message.Length); |
| 71 | + |
| 72 | + int offset = 0; |
| 73 | + int length = message.Length; |
| 74 | + _crc32Layer.ProcessOutBoundPacket(_dummyEndpoint, ref package, ref offset, ref length); |
| 75 | + _crc32Layer.ProcessInboundPacket(_dummyEndpoint, ref package, ref offset, ref length); |
| 76 | + } |
| 77 | + |
| 78 | + private static byte[] GetTestPacketWithCrc() |
| 79 | + { |
| 80 | + byte[] testMsg = GetTestMessageBytes(); |
| 81 | + uint crc32 = CRC32C.Compute(testMsg, 0, testMsg.Length); |
| 82 | + |
| 83 | + byte[] packet = new byte[testMsg.Length + CRC32C.ChecksumSize]; |
| 84 | + Buffer.BlockCopy(testMsg, 0, packet, 0, testMsg.Length); |
| 85 | + FastBitConverter.GetBytes(packet, testMsg.Length, crc32); |
| 86 | + return packet; |
| 87 | + } |
| 88 | + |
| 89 | + private static byte[] GetTestMessageBytes() |
| 90 | + => System.Text.Encoding.ASCII.GetBytes("This is a test string with some length"); |
| 91 | + } |
| 92 | +} |
0 commit comments