From 26c42ee72cc085b2195a5045c11038f508d01c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20M=C3=B6ller?= Date: Thu, 12 Jan 2023 13:22:59 +0100 Subject: [PATCH] Fix decoding of PointAlongLine messages with no positive offset byte --- .../Binary/Codecs/PointAlongLineLocationCodec.cs | 2 +- .../Binary/PointAlongLineLocationTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/OpenLR/Codecs/Binary/Codecs/PointAlongLineLocationCodec.cs b/src/OpenLR/Codecs/Binary/Codecs/PointAlongLineLocationCodec.cs index 5747036..878ba25 100644 --- a/src/OpenLR/Codecs/Binary/Codecs/PointAlongLineLocationCodec.cs +++ b/src/OpenLR/Codecs/Binary/Codecs/PointAlongLineLocationCodec.cs @@ -57,7 +57,7 @@ public static PointAlongLineLocation Decode(byte[] data) pointAlongLine.First = first; pointAlongLine.Orientation = OrientationConverter.Decode(data, 7, 0); pointAlongLine.SideOfRoad = SideOfRoadConverter.Decode(data, 14, 0); - pointAlongLine.PositiveOffsetPercentage = OffsetConvertor.Decode(data, 16); + pointAlongLine.PositiveOffsetPercentage = data.Length > 16 ? OffsetConvertor.Decode(data, 16) : null; pointAlongLine.Last = last; return pointAlongLine; diff --git a/test/OpenLR.Test/Binary/PointAlongLineLocationTests.cs b/test/OpenLR.Test/Binary/PointAlongLineLocationTests.cs index 6efe69e..b20a3cb 100644 --- a/test/OpenLR.Test/Binary/PointAlongLineLocationTests.cs +++ b/test/OpenLR.Test/Binary/PointAlongLineLocationTests.cs @@ -6,6 +6,8 @@ namespace OpenLR.Test.Binary { + using NuGet.Frameworks; + /// /// Contains tests for decoding/encoding a point along location to/from OpenLR binary representation. /// @@ -53,6 +55,19 @@ public void DecodeBase64Test() Assert.AreEqual(SideOfRoad.Left, pointAlongLineLocation.SideOfRoad); Assert.AreEqual(30.19, pointAlongLineLocation.PositiveOffsetPercentage, 0.5); // binary encode loses accuracy. } + + /// + /// Decode a message with only 16 bytes, i.e. no positive offset byte + /// + [Test] + public void DecodeBase64WithNoOffset() + { + // Message with 16 bytes. + var stringData = Convert.FromBase64String("Kwhv2ikWNjPZAP/XAAUzCA=="); + + Assert.IsTrue(PointAlongLineLocationCodec.CanDecode(stringData)); + Assert.DoesNotThrow(() => PointAlongLineLocationCodec.Decode(stringData)); + } /// /// A simple test encoding from a base64 string.