From 5736dd8d057ef4d8ba4d0f73776ef0a5a0321375 Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 22 Jan 2020 09:30:09 +0100 Subject: [PATCH] MessagePackerTest: cover FormatException case --- Assets/Mirror/Tests/MessagePackerTest.cs | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Assets/Mirror/Tests/MessagePackerTest.cs b/Assets/Mirror/Tests/MessagePackerTest.cs index 264e9cbd5..9f2f5a2e7 100644 --- a/Assets/Mirror/Tests/MessagePackerTest.cs +++ b/Assets/Mirror/Tests/MessagePackerTest.cs @@ -1,3 +1,4 @@ +using System; using NUnit.Framework; namespace Mirror.Tests { @@ -20,5 +21,35 @@ public void TestPacking() Assert.That(unpacked.sceneName, Is.EqualTo("Hello world")); Assert.That(unpacked.sceneOperation, Is.EqualTo(SceneOperation.LoadAdditive)); } + + [Test] + public void TestUnpackIdMismatch() + { + // Unpack 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(data); + // BAD: IF WE GET HERE THEN NO EXCEPTION WAS THROWN + Assert.Fail(); + } + catch (FormatException) + { + // GOOD + } + } } }