MessagePackerTest: cover FormatException case

This commit is contained in:
vis2k 2020-01-22 09:30:09 +01:00
parent 17af872f99
commit 5736dd8d05

View File

@ -1,3 +1,4 @@
using System;
using NUnit.Framework; using NUnit.Framework;
namespace Mirror.Tests namespace Mirror.Tests
{ {
@ -20,5 +21,35 @@ public void TestPacking()
Assert.That(unpacked.sceneName, Is.EqualTo("Hello world")); Assert.That(unpacked.sceneName, Is.EqualTo("Hello world"));
Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive)); Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive));
} }
[Test]
public void TestUnpackIdMismatch()
{
// Unpack<T> has a id != msgType case that throws a FormatException.
// let's try to trigger it.
SceneMessage message = new SceneMessage()
{
sceneName = "Hello world",
sceneOperation = SceneOperation.LoadAdditive
};
byte[] data = MessagePacker.Pack(message);
// overwrite the id
data[0] = 0x01;
data[1] = 0x02;
try
{
SceneMessage unpacked = MessagePacker.Unpack<SceneMessage>(data);
// BAD: IF WE GET HERE THEN NO EXCEPTION WAS THROWN
Assert.Fail();
}
catch (FormatException)
{
// GOOD
}
}
} }
} }