Skip to content

Commit

Permalink
fix: Added WriteBytesAndSize tests, and fixed the function to be peda…
Browse files Browse the repository at this point in the history
…ntic. (#773)
  • Loading branch information
atlv24 authored and paulpach committed Apr 8, 2019
1 parent 99c8f5c commit 72e4e55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
17 changes: 5 additions & 12 deletions Assets/Mirror/Runtime/NetworkWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,16 @@ public void Write(byte[] buffer, int offset, int count)
// (like an inventory with different items etc.)
public void WriteBytesAndSize(byte[] buffer, int offset, int count)
{
uint length = checked((uint)count);
// null is supported because [SyncVar]s might be structs with null byte[] arrays
// (writing a size=0 empty array is not the same, the server and client would be out of sync)
// (using size=-1 for null would limit max size to 32kb instead of 64kb)
if (buffer == null)
writer.Write(buffer != null); // notNull?
if (buffer != null)
{
writer.Write(false); // notNull?
return;
}
if (count < 0)
{
Debug.LogError("NetworkWriter WriteBytesAndSize: size " + count + " cannot be negative");
return;
WritePackedUInt32(length);
writer.Write(buffer, offset, count);
}

writer.Write(true); // notNull?
WritePackedUInt32((uint)count);
writer.Write(buffer, offset, count);
}

// Weaver needs a write function with just one byte[] parameter
Expand Down
10 changes: 10 additions & 0 deletions Assets/Mirror/Tests/NetworkWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public void TestReading0LengthBytes()
Assert.That(reader.ReadBytes(0).Length, Is.EqualTo(0));
}

[Test]
public void TestWritingNegativeBytesAndSizeFailure()
{
NetworkWriter writer = new NetworkWriter();
Assert.Throws<OverflowException>(() => writer.WriteBytesAndSize(new byte[0], 0, -1));
Assert.That(writer.Position, Is.EqualTo(0));
Assert.Throws<OverflowException>(() => writer.WriteBytesAndSize(null, 0, -1));
Assert.That(writer.Position, Is.EqualTo(0));
}

[Test]
public void TestReadingTooMuch()
{
Expand Down

0 comments on commit 72e4e55

Please sign in to comment.