diff --git a/BinarySerializer.Test/Issues/Issue124/Issue124Tests.cs b/BinarySerializer.Test/Issues/Issue124/Issue124Tests.cs index 852541d3..ae142b1e 100644 --- a/BinarySerializer.Test/Issues/Issue124/Issue124Tests.cs +++ b/BinarySerializer.Test/Issues/Issue124/Issue124Tests.cs @@ -25,7 +25,9 @@ public void Test() }; var actual = Roundtrip(expected); - + Assert.AreEqual(expected.Packets.Count, actual.Packets.Count); + Assert.IsInstanceOfType(actual.Packets[0].PacketBody, typeof(Packet1)); + Assert.IsInstanceOfType(actual.Packets[1].PacketBody, typeof(Packet1)); } } } diff --git a/BinarySerializer.Test/Issues/Issue180/Issue180Tests.cs b/BinarySerializer.Test/Issues/Issue180/Issue180Tests.cs index 68a751f6..d577f15e 100644 --- a/BinarySerializer.Test/Issues/Issue180/Issue180Tests.cs +++ b/BinarySerializer.Test/Issues/Issue180/Issue180Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace BinarySerialization.Test.Issues.Issue180 @@ -71,5 +72,68 @@ public void TestOutOfMemory() var _ = Deserialize(serialized); } } + + public class ObservationWrapper + { + [FieldOrder(1)] + public ObservationData[] Observations; + } + public class ObservationData + { + [FieldOrder(0)] + [FieldCount(1)] + public byte[] OneByte; // 1 + + [FieldOrder(1)] + [FieldBitLength(7)] + public byte SevenBits; // 67 => 0b1000011 + + [FieldOrder(2)] + [FieldBitLength(1)] + public byte OneBit; // 0 => Eight bit of 67 which is 0 + } + + [TestMethod] + public void TestInfinityLoop_ShouldNotBeInfinityLoop_ShouldCrash() + { + var msg = new byte[] + { + 1, // 1 Byte which does not fit into the provided type + 67, // OneByteArray + 75, // 7 bits + 1 bit + }; + var ser = new BinarySerializer(); + Assert.ThrowsException(() => ser.Deserialize(msg)); + } + + [TestMethod] + public void TestInfinityLoop_ShouldNotBeInfinityLoop_ShouldCrashOnAsync() + { + var msg = new byte[] + { + 1, // 1 Byte which does not fit into the provided type + 67, // OneByteArray + 75, // 7 bits + 1 bit + }; + var ser = new BinarySerializer(); + Assert.ThrowsExceptionAsync(() => ser.DeserializeAsync(msg)); + } + + [TestMethod] + public void TestInfinityLoop_DoesNotEndUpInInfinityLoop() + { + var msg = new byte[] + { + 67, // OneByte + 75, // 7 bits + 1 bit + }; + var ser = new BinarySerializer(); + var message = ser.Deserialize(msg); + + Assert.IsNotNull(message); + Assert.AreEqual(67, message[0].OneByte[0]); + Assert.AreEqual(0, message[0].OneBit); + Assert.AreEqual(75, message[0].SevenBits); + } } } diff --git a/BinarySerializer.Test/Issues/Issue55/Issue55Tests.cs b/BinarySerializer.Test/Issues/Issue55/Issue55Tests.cs index 53c1bef4..a8ed67d2 100644 --- a/BinarySerializer.Test/Issues/Issue55/Issue55Tests.cs +++ b/BinarySerializer.Test/Issues/Issue55/Issue55Tests.cs @@ -37,16 +37,15 @@ public void Test() outputStream.Seek(0, SeekOrigin.Begin); var inputStream = new LooksLikeANetworkStream(outputStream); //non-seekable stream - //var inputStream = outputStream; //successful var roundtrip = serializer.Deserialize(inputStream); - //Assert.That(roundtrip.Chunk, Is.InstanceOf()); - - //var sourceChunk = (TestChunk)source.Chunk; - //var testChunk = (TestChunk)roundtrip.Chunk; - //Assert.That(testChunk.Customs.Length, Is.EqualTo(sourceChunk.Customs.Length)); - //Assert.That(testChunk.Customs.ElementAt(0).Value, Is.EqualTo(sourceChunk.Customs.ElementAt(0).Value)); - //Assert.That(testChunk.Customs.ElementAt(1).Value, Is.EqualTo(sourceChunk.Customs.ElementAt(1).Value)); + Assert.IsInstanceOfType(roundtrip.Chunk); + var sourceChunk = (TestChunk)source.Chunk; + var testChunk = (TestChunk)roundtrip.Chunk; + Assert.AreEqual(testChunk.Customs.Length, sourceChunk.Customs.Length); + + Assert.AreEqual(testChunk.Customs[0].Value, sourceChunk.Customs[0].Value); + Assert.AreEqual(testChunk.Customs[1].Value, sourceChunk.Customs[1].Value); } } } \ No newline at end of file diff --git a/BinarySerializer/Graph/ValueGraph/ValueNode.cs b/BinarySerializer/Graph/ValueGraph/ValueNode.cs index 67d71f01..5fd94b2d 100644 --- a/BinarySerializer/Graph/ValueGraph/ValueNode.cs +++ b/BinarySerializer/Graph/ValueGraph/ValueNode.cs @@ -315,6 +315,11 @@ internal void Deserialize(BoundedStream stream, SerializationOptions options, Ev return; } + if (stream.AvailableForReading < -1) // Checking less than -1 as some cases returns -1 on unreadable stream see `ShouldThrowIOExceptionNotInvalidOperationExceptionTest` + { + throw new IndexOutOfRangeException("Read passed end of stream, binary message does not fit the provided type."); + } + AlignLeft(stream); var offset = GetFieldOffset(); @@ -366,6 +371,11 @@ internal async Task DeserializeAsync(BoundedStream stream, SerializationOptions return; } + if (stream.AvailableForReading < -1) // Checking less than -1 as some cases returns -1 on unreadable stream see `ShouldThrowIOExceptionNotInvalidOperationExceptionTest` + { + throw new IndexOutOfRangeException("Read passed end of stream, binary message does not fit the provided type."); + } + AlignLeft(stream); var offset = GetFieldOffset();