Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion BinarySerializer.Test/Issues/Issue124/Issue124Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
64 changes: 64 additions & 0 deletions BinarySerializer.Test/Issues/Issue180/Issue180Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BinarySerialization.Test.Issues.Issue180
Expand Down Expand Up @@ -71,5 +72,68 @@ public void TestOutOfMemory()
var _ = Deserialize<BitFieldsPrecededByByte[]>(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<InvalidOperationException>(() => ser.Deserialize<ObservationWrapper>(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<InvalidOperationException>(() => ser.DeserializeAsync<ObservationWrapper>(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<ObservationData[]>(msg);

Assert.IsNotNull(message);
Assert.AreEqual(67, message[0].OneByte[0]);
Assert.AreEqual(0, message[0].OneBit);
Assert.AreEqual(75, message[0].SevenBits);
}
}
}
15 changes: 7 additions & 8 deletions BinarySerializer.Test/Issues/Issue55/Issue55Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChunkContainer>(inputStream);
//Assert.That(roundtrip.Chunk, Is.InstanceOf<TestChunk>());

//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<TestChunk>(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);
}
}
}
10 changes: 10 additions & 0 deletions BinarySerializer/Graph/ValueGraph/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down