From d034bada1b62d5695b49a458606e6320214348d7 Mon Sep 17 00:00:00 2001 From: ashu-pietech <150220413+ashu-pietech@users.noreply.github.com> Date: Sat, 10 Feb 2024 17:02:03 +0530 Subject: [PATCH] issue77: Fixed the invalid range error issue with the small matchee raw topic when compared with base raw topic length (#78) * issue77: Fixed the invalid range error issue with the small matchee raw topic when compared with base raw topic length * Add test case for range error fix & fix typo in comments * Fix unit test failing in wildcard case --- lib/src/mqtt_subscription_topic.dart | 10 +++++++++- test/mqtt_client_base_test.dart | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/src/mqtt_subscription_topic.dart b/lib/src/mqtt_subscription_topic.dart index b6c6312..d24f5e1 100644 --- a/lib/src/mqtt_subscription_topic.dart +++ b/lib/src/mqtt_subscription_topic.dart @@ -95,8 +95,16 @@ class MqttSubscriptionTopic extends MqttTopic { return false; } } + // If we're at the last fragment of the matchee rawTopic but there are + // more fragments in the lhs rawTopic then the matchee rawTopic + // is too specific to be a match. + if (i + 1 == matcheeTopic.topicFragments.length && + topicFragments.length > matcheeTopic.topicFragments.length && + lhsFragment != matcheeTopic.topicFragments[i]) { + return false; + } // If we're at the last fragment of the lhs rawTopic but there are - // more fragments in the in the matchee then the matchee rawTopic + // more fragments in the matchee then the matchee rawTopic // is too specific to be a match. if (i + 1 == topicFragments.length && matcheeTopic.topicFragments.length > topicFragments.length) { diff --git a/test/mqtt_client_base_test.dart b/test/mqtt_client_base_test.dart index fd4c1a6..1149d53 100644 --- a/test/mqtt_client_base_test.dart +++ b/test/mqtt_client_base_test.dart @@ -382,6 +382,15 @@ void main() { expect( subTopic.matches(MqttPublicationTopic('some/random/topic')), isFalse); }); + test('different length topics do not match and do not throw range error', + () { + const topic = 'finance/ibm/closingprice/+/topic/sub'; + final subTopic = MqttSubscriptionTopic(topic); + expect( + subTopic + .matches(MqttPublicationTopic('finance/ibm/closingprice/sub')), + isFalse); + }); test( 'MultiWildcard does not match topic with difference before Wildcard level', () {