From 74a552296510747f662aa08b92f2a76da03b0ea1 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 25 Apr 2018 20:22:25 +0200 Subject: [PATCH] Add topic filter tests from MQTT spec --- .../Adapter/MqttChannelAdapter.cs | 2 +- .../TopicFilterComparerTests.cs | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs index 894b8d9..c531add 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs @@ -129,7 +129,7 @@ namespace MQTTnet.Adapter { return null; } - + if (header.BodyLength == 0) { return new ReceivedMqttPacket(header, new MemoryStream(new byte[0], false)); diff --git a/Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs b/Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs index be16921..e91e988 100644 --- a/Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs +++ b/Tests/MQTTnet.Core.Tests/TopicFilterComparerTests.cs @@ -60,7 +60,33 @@ namespace MQTTnet.Core.Tests CompareAndAssert("A/B/C/D", "#", true); } - private void CompareAndAssert(string topic, string filter, bool expectedResult) + [TestMethod] + public void TopicFilterComparer_MultiLevel_Sport() + { + // Tests from official MQTT spec (4.7.1.2 Multi-level wildcard) + CompareAndAssert("sport/tennis/player1", "sport/tennis/player1/#", true); + CompareAndAssert("sport/tennis/player1/ranking", "sport/tennis/player1/#", true); + CompareAndAssert("sport/tennis/player1/score/wimbledon", "sport/tennis/player1/#", true); + + CompareAndAssert("sport/tennis/player1", "sport/tennis/+", true); + CompareAndAssert("sport/tennis/player2", "sport/tennis/+", true); + CompareAndAssert("sport/tennis/player1/ranking", "sport/tennis/+", false); + + CompareAndAssert("sport", "sport/#", true); + CompareAndAssert("sport", "sport/+", false); + CompareAndAssert("sport/", "sport/+", true); + } + + [TestMethod] + public void TopicFilterComparer_SingleLevel_Finance() + { + // Tests from official MQTT spec (4.7.1.3 Single level wildcard) + CompareAndAssert("/finance", "+/+", true); + CompareAndAssert("/finance", "/+", true); + CompareAndAssert("/finance", "+", false); + } + + private static void CompareAndAssert(string topic, string filter, bool expectedResult) { Assert.AreEqual(expectedResult, MqttTopicFilterComparer.IsMatch(topic, filter)); }