You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

298 lines
11 KiB

  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Options;
  6. using MQTTnet.Client.Publishing;
  7. using MQTTnet.Client.Receiving;
  8. using MQTTnet.Client.Subscribing;
  9. using MQTTnet.Client.Unsubscribing;
  10. using MQTTnet.Formatter;
  11. using MQTTnet.Protocol;
  12. using MQTTnet.Server;
  13. using MQTTnet.Tests.Mockups;
  14. namespace MQTTnet.Tests.MQTTv5
  15. {
  16. [TestClass]
  17. public class Client_Tests
  18. {
  19. [TestMethod]
  20. public async Task Connect_With_New_Mqtt_Features()
  21. {
  22. using (var testEnvironment = new TestEnvironment())
  23. {
  24. await testEnvironment.StartServerAsync();
  25. // This test can be also executed against "broker.hivemq.com" to validate package format.
  26. var client = await testEnvironment.ConnectClientAsync(
  27. new MqttClientOptionsBuilder()
  28. //.WithTcpServer("broker.hivemq.com")
  29. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
  30. .WithProtocolVersion(MqttProtocolVersion.V500)
  31. .WithTopicAliasMaximum(20)
  32. .WithReceiveMaximum(20)
  33. .WithWillMessage(new MqttApplicationMessageBuilder().WithTopic("abc").Build())
  34. .WithWillDelayInterval(20)
  35. .Build());
  36. MqttApplicationMessage receivedMessage = null;
  37. await client.SubscribeAsync("a");
  38. client.UseApplicationMessageReceivedHandler(context => { receivedMessage = context.ApplicationMessage; });
  39. await client.PublishAsync(new MqttApplicationMessageBuilder()
  40. .WithTopic("a")
  41. .WithPayload("x")
  42. .WithUserProperty("a", "1")
  43. .WithUserProperty("b", "2")
  44. .WithPayloadFormatIndicator(MqttPayloadFormatIndicator.CharacterData)
  45. .WithAtLeastOnceQoS()
  46. .Build());
  47. await Task.Delay(500);
  48. Assert.IsNotNull(receivedMessage);
  49. Assert.AreEqual(2, receivedMessage.UserProperties.Count);
  50. }
  51. }
  52. [TestMethod]
  53. public async Task Connect()
  54. {
  55. var server = new MqttFactory().CreateMqttServer();
  56. var client = new MqttFactory().CreateMqttClient();
  57. try
  58. {
  59. await server.StartAsync(new MqttServerOptions());
  60. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  61. }
  62. finally
  63. {
  64. await server.StopAsync();
  65. }
  66. }
  67. [TestMethod]
  68. public async Task Connect_And_Disconnect()
  69. {
  70. var server = new MqttFactory().CreateMqttServer();
  71. var client = new MqttFactory().CreateMqttClient();
  72. try
  73. {
  74. await server.StartAsync(new MqttServerOptions());
  75. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  76. await client.DisconnectAsync();
  77. }
  78. finally
  79. {
  80. await server.StopAsync();
  81. }
  82. }
  83. [TestMethod]
  84. public async Task Subscribe()
  85. {
  86. var server = new MqttFactory().CreateMqttServer();
  87. var client = new MqttFactory().CreateMqttClient();
  88. try
  89. {
  90. await server.StartAsync(new MqttServerOptions());
  91. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  92. var result = await client.SubscribeAsync(new MqttClientSubscribeOptions()
  93. {
  94. SubscriptionIdentifier = 1,
  95. TopicFilters = new List<TopicFilter>
  96. {
  97. new TopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce}
  98. }
  99. });
  100. await client.DisconnectAsync();
  101. Assert.AreEqual(1, result.Items.Count);
  102. Assert.AreEqual(MqttClientSubscribeResultCode.GrantedQoS1, result.Items[0].ResultCode);
  103. }
  104. finally
  105. {
  106. await server.StopAsync();
  107. }
  108. }
  109. [TestMethod]
  110. public async Task Unsubscribe()
  111. {
  112. var server = new MqttFactory().CreateMqttServer();
  113. var client = new MqttFactory().CreateMqttClient();
  114. try
  115. {
  116. await server.StartAsync(new MqttServerOptions());
  117. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  118. await client.SubscribeAsync("a");
  119. var result = await client.UnsubscribeAsync("a");
  120. await client.DisconnectAsync();
  121. Assert.AreEqual(1, result.Items.Count);
  122. Assert.AreEqual(MqttClientUnsubscribeResultCode.Success, result.Items[0].ReasonCode);
  123. }
  124. finally
  125. {
  126. await server.StopAsync();
  127. }
  128. }
  129. [TestMethod]
  130. public async Task Publish_QoS_0()
  131. {
  132. var server = new MqttFactory().CreateMqttServer();
  133. var client = new MqttFactory().CreateMqttClient();
  134. try
  135. {
  136. await server.StartAsync(new MqttServerOptions());
  137. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  138. var result = await client.PublishAsync("a", "b");
  139. await client.DisconnectAsync();
  140. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  141. }
  142. finally
  143. {
  144. await server.StopAsync();
  145. }
  146. }
  147. [TestMethod]
  148. public async Task Publish_QoS_1()
  149. {
  150. var server = new MqttFactory().CreateMqttServer();
  151. var client = new MqttFactory().CreateMqttClient();
  152. try
  153. {
  154. await server.StartAsync(new MqttServerOptions());
  155. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  156. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.AtLeastOnce);
  157. await client.DisconnectAsync();
  158. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  159. }
  160. finally
  161. {
  162. await server.StopAsync();
  163. }
  164. }
  165. [TestMethod]
  166. public async Task Publish_QoS_2()
  167. {
  168. var server = new MqttFactory().CreateMqttServer();
  169. var client = new MqttFactory().CreateMqttClient();
  170. try
  171. {
  172. await server.StartAsync(new MqttServerOptions());
  173. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  174. var result = await client.PublishAsync("a", "b", MqttQualityOfServiceLevel.ExactlyOnce);
  175. await client.DisconnectAsync();
  176. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  177. }
  178. finally
  179. {
  180. await server.StopAsync();
  181. }
  182. }
  183. [TestMethod]
  184. public async Task Publish_With_Properties()
  185. {
  186. var server = new MqttFactory().CreateMqttServer();
  187. var client = new MqttFactory().CreateMqttClient();
  188. try
  189. {
  190. await server.StartAsync(new MqttServerOptions());
  191. var applicationMessage = new MqttApplicationMessageBuilder()
  192. .WithTopic("Hello")
  193. .WithPayload("World")
  194. .WithAtMostOnceQoS()
  195. .WithUserProperty("x", "1")
  196. .WithUserProperty("y", "2")
  197. .WithResponseTopic("response")
  198. .WithContentType("text")
  199. .WithMessageExpiryInterval(50)
  200. .WithCorrelationData(new byte[12])
  201. .WithTopicAlias(2)
  202. .Build();
  203. await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  204. var result = await client.PublishAsync(applicationMessage);
  205. await client.DisconnectAsync();
  206. Assert.AreEqual(MqttClientPublishReasonCode.Success, result.ReasonCode);
  207. }
  208. finally
  209. {
  210. await server.StopAsync();
  211. }
  212. }
  213. [TestMethod]
  214. public async Task Subscribe_And_Publish()
  215. {
  216. var server = new MqttFactory().CreateMqttServer();
  217. var client1 = new MqttFactory().CreateMqttClient();
  218. var client2 = new MqttFactory().CreateMqttClient();
  219. try
  220. {
  221. await server.StartAsync(new MqttServerOptions());
  222. var receivedMessages = new List<MqttApplicationMessageReceivedEventArgs>();
  223. await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client1").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  224. client1.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
  225. {
  226. lock (receivedMessages)
  227. {
  228. receivedMessages.Add(e);
  229. }
  230. });
  231. await client1.SubscribeAsync("a");
  232. await client2.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1").WithClientId("client2").WithProtocolVersion(MqttProtocolVersion.V500).Build());
  233. await client2.PublishAsync("a", "b");
  234. await Task.Delay(500);
  235. await client2.DisconnectAsync();
  236. await client1.DisconnectAsync();
  237. Assert.AreEqual(1, receivedMessages.Count);
  238. Assert.AreEqual("client1", receivedMessages[0].ClientId);
  239. Assert.AreEqual("a", receivedMessages[0].ApplicationMessage.Topic);
  240. Assert.AreEqual("b", receivedMessages[0].ApplicationMessage.ConvertPayloadToString());
  241. }
  242. finally
  243. {
  244. await server.StopAsync();
  245. }
  246. }
  247. }
  248. }