|
| 1 | +namespace HiveMQtt.Test.HiveMQClient; |
| 2 | + |
| 3 | +using Xunit; |
| 4 | +using HiveMQtt.Client; |
| 5 | + |
| 6 | +public class UtilTest |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public void SingleLevelWildcardMatch() |
| 10 | + { |
| 11 | + // The plus sign (‘+’ U+002B) is a wildcard character that matches only one topic level. |
| 12 | + // |
| 13 | + // The single-level wildcard can be used at any level in the Topic Filter, including first and last levels. Where it is used, it MUST occupy an entire level of the filter [MQTT-4.7.1-2]. It can be used at more than one level in the Topic Filter and can be used in conjunction with the multi-level wildcard. |
| 14 | + // |
| 15 | + // For example, “sport/tennis/+” matches “sport/tennis/player1” and “sport/tennis/player2”, but not “sport/tennis/player1/ranking”. Also, because the single-level wildcard matches only a single level, “sport/+” does not match “sport” but it does match “sport/”. |
| 16 | + // |
| 17 | + // · “+” is valid |
| 18 | + // · “+/tennis/#” is valid |
| 19 | + // · “sport+” is not valid |
| 20 | + // · “sport/+/player1” is valid |
| 21 | + // · “/finance” matches “+/+” and “/+”, but not “+” |
| 22 | + bool result; |
| 23 | + |
| 24 | + // “sport/tennis/+” matches “sport/tennis/player1” |
| 25 | + result = HiveMQClient.MatchTopic("sport/tennis/+", "sport/tennis/player1"); |
| 26 | + Assert.True(result); |
| 27 | + |
| 28 | + // “sport/tennis/+” doesn't match sport/tennis/player1/ranking” |
| 29 | + result = HiveMQClient.MatchTopic("sport/tennis/+", "sport/tennis/player1/ranking"); |
| 30 | + Assert.False(result); |
| 31 | + |
| 32 | + // “sport/+” does not match “sport” |
| 33 | + result = HiveMQClient.MatchTopic("sport/+", "sport"); |
| 34 | + Assert.False(result); |
| 35 | + |
| 36 | + // “sport/+” does match “sport/” |
| 37 | + result = HiveMQClient.MatchTopic("sport/+", "sport/"); |
| 38 | + Assert.True(result); |
| 39 | + |
| 40 | + // "sport/+/player1" matches "sport/tennis/player1" |
| 41 | + result = HiveMQClient.MatchTopic("sport/+/player1", "sport/tennis/player1"); |
| 42 | + Assert.True(result); |
| 43 | + |
| 44 | + // "/finance" matches “/+” |
| 45 | + result = HiveMQClient.MatchTopic("/+", "/finance"); |
| 46 | + Assert.True(result); |
| 47 | + |
| 48 | + // "/finance" doesn't match “+” |
| 49 | + result = HiveMQClient.MatchTopic("+", "/finance"); |
| 50 | + Assert.False(result); |
| 51 | + |
| 52 | + // A subscription to “+/monitor/Clients” will not receive any messages published to “$SYS/monitor/Clients” |
| 53 | + result = HiveMQClient.MatchTopic("+/monitor/Clients", "$SYS/monitor/Clients"); |
| 54 | + Assert.False(result); |
| 55 | + |
| 56 | + // A subscription to “$SYS/monitor/+” will receive messages published to “$SYS/monitor/Clients” |
| 57 | + result = HiveMQClient.MatchTopic("$SYS/monitor/+", "$SYS/monitor/Clients"); |
| 58 | + Assert.True(result); |
| 59 | + |
| 60 | + // https://github.com/hivemq/hivemq-mqtt-client-dotnet/issues/126 |
| 61 | + result = HiveMQClient.MatchTopic("hivemqtt/sendmessageonloop/+/test", "hivemqtt/sendmessageonloop/2938472/test"); |
| 62 | + Assert.True(result); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void MultiLevelWildcardMatch() |
| 67 | + { |
| 68 | + // From: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901244 |
| 69 | + // # The number sign (‘#’ U+0023) is a wildcard character that matches any number of levels within a topic. The multi-level wildcard represents the parent and any number of child levels. The multi-level wildcard character MUST be specified either on its own or following a topic level separator. In either case it MUST be the last character specified in the Topic Filter [MQTT-4.7.1-1]. |
| 70 | + // |
| 71 | + // For example, if a Client subscribes to “sport/tennis/player1/#”, it would receive messages published using these Topic Names: |
| 72 | + // · “sport/tennis/player1” |
| 73 | + // · “sport/tennis/player1/ranking |
| 74 | + // · “sport/tennis/player1/score/wimbledon” |
| 75 | + // |
| 76 | + // · “sport/#” also matches the singular “sport”, since # includes the parent level. |
| 77 | + // · “#” is valid and will receive every Application Message |
| 78 | + // · “sport/tennis/#” is valid |
| 79 | + // · “sport/tennis#” is not valid |
| 80 | + // · “sport/tennis/#/ranking” is not valid |
| 81 | + bool result; |
| 82 | + |
| 83 | + // “sport/tennis/#” matches “sport/tennis/player1” |
| 84 | + result = HiveMQClient.MatchTopic("sport/tennis/player1/#", "sport/tennis/player1"); |
| 85 | + Assert.True(result); |
| 86 | + |
| 87 | + // “sport/tennis/#” matches “sport/tennis/player1/ranking” |
| 88 | + result = HiveMQClient.MatchTopic("sport/tennis/player1/#", "sport/tennis/player1/ranking"); |
| 89 | + Assert.True(result); |
| 90 | + |
| 91 | + // “sport/tennis/+” matches “sport/tennis/player1/ranking” |
| 92 | + result = HiveMQClient.MatchTopic("sport/tennis/player1/#", "sport/tennis/player1/score/wimbledon"); |
| 93 | + Assert.True(result); |
| 94 | + |
| 95 | + // “sport/#” also matches the singular “sport”, since # includes the parent level. |
| 96 | + result = HiveMQClient.MatchTopic("sport/#", "sport"); |
| 97 | + Assert.True(result); |
| 98 | + |
| 99 | + // “#” is valid and will receive every Application Message |
| 100 | + result = HiveMQClient.MatchTopic("#", "any/and/all/topics"); |
| 101 | + Assert.True(result); |
| 102 | + |
| 103 | + // Invalid multi-level wildcards |
| 104 | + Assert.Throws<ArgumentException>(() => HiveMQClient.MatchTopic("invalid/mlwc#", "sport/tennis/player1/ranking")); |
| 105 | + |
| 106 | + // “sport/tennis/#/ranking” is not valid |
| 107 | + Assert.Throws<ArgumentException>(() => HiveMQClient.MatchTopic("sport/tennis/#/ranking", "sport/tennis/player1/ranking")); |
| 108 | + Assert.Throws<ArgumentException>(() => HiveMQClient.MatchTopic("/#/", "sport/tennis/player1/ranking")); |
| 109 | + |
| 110 | + // “sport/tennis#” is not valid |
| 111 | + Assert.Throws<ArgumentException>(() => HiveMQClient.MatchTopic("sports/tennis#", "sport/tennis/player1/ranking")); |
| 112 | + |
| 113 | + // A subscription to “#” will not receive any messages published to a topic beginning with a $ |
| 114 | + result = HiveMQClient.MatchTopic("#", "$SYS/broker/clients/total"); |
| 115 | + Assert.False(result); |
| 116 | + |
| 117 | + // A subscription to “$SYS/#” will receive messages published to topics beginning with “$SYS/” |
| 118 | + result = HiveMQClient.MatchTopic("$SYS/#", "$SYS/broker/clients/total"); |
| 119 | + Assert.True(result); |
| 120 | + |
| 121 | + } |
| 122 | +} |
0 commit comments