Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Core.Adapter;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.Diagnostics;
  9. using MQTTnet.Core.Packets;
  10. using MQTTnet.Core.Protocol;
  11. using MQTTnet.Core.Server;
  12. namespace MQTTnet.Core.Tests
  13. {
  14. [TestClass]
  15. public class MqttServerTests
  16. {
  17. [TestMethod]
  18. public void MqttServer_PublishSimple_AtMostOnce()
  19. {
  20. TestPublishAsync(
  21. "A/B/C",
  22. MqttQualityOfServiceLevel.AtMostOnce,
  23. "A/B/C",
  24. MqttQualityOfServiceLevel.AtMostOnce,
  25. 1).Wait();
  26. }
  27. [TestMethod]
  28. public void MqttServer_PublishSimple_AtLeastOnce()
  29. {
  30. TestPublishAsync(
  31. "A/B/C",
  32. MqttQualityOfServiceLevel.AtLeastOnce,
  33. "A/B/C",
  34. MqttQualityOfServiceLevel.AtLeastOnce,
  35. 1).Wait();
  36. }
  37. [TestMethod]
  38. public void MqttServer_PublishSimple_ExactlyOnce()
  39. {
  40. TestPublishAsync(
  41. "A/B/C",
  42. MqttQualityOfServiceLevel.ExactlyOnce,
  43. "A/B/C",
  44. MqttQualityOfServiceLevel.ExactlyOnce,
  45. 1).Wait();
  46. }
  47. [TestMethod]
  48. public async Task MqttServer_WillMessage()
  49. {
  50. var serverAdapter = new TestMqttServerAdapter();
  51. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  52. await s.StartAsync();
  53. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  54. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  55. var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
  56. var receivedMessagesCount = 0;
  57. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  58. await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  59. await c2.DisconnectAsync();
  60. await Task.Delay(1000);
  61. await s.StopAsync();
  62. Assert.AreEqual(1, receivedMessagesCount);
  63. }
  64. [TestMethod]
  65. public async Task MqttServer_Unsubscribe()
  66. {
  67. var serverAdapter = new TestMqttServerAdapter();
  68. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  69. await s.StartAsync();
  70. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  71. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  72. var receivedMessagesCount = 0;
  73. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  74. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  75. await c2.PublishAsync(message);
  76. Assert.AreEqual(0, receivedMessagesCount);
  77. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  78. await c2.PublishAsync(message);
  79. await Task.Delay(500);
  80. Assert.AreEqual(1, receivedMessagesCount);
  81. await c1.UnsubscribeAsync("a");
  82. await c2.PublishAsync(message);
  83. await Task.Delay(500);
  84. Assert.AreEqual(1, receivedMessagesCount);
  85. await s.StopAsync();
  86. await Task.Delay(500);
  87. Assert.AreEqual(1, receivedMessagesCount);
  88. }
  89. [TestMethod]
  90. public async Task MqttServer_Publish()
  91. {
  92. var serverAdapter = new TestMqttServerAdapter();
  93. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  94. await s.StartAsync();
  95. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  96. var receivedMessagesCount = 0;
  97. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  98. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  99. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  100. s.Publish(message);
  101. await Task.Delay(500);
  102. await s.StopAsync();
  103. Assert.AreEqual(1, receivedMessagesCount);
  104. }
  105. [TestMethod]
  106. public async Task MqttServer_NoRetainedMessage()
  107. {
  108. var serverAdapter = new TestMqttServerAdapter();
  109. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  110. await s.StartAsync();
  111. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  112. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, false));
  113. await c1.DisconnectAsync();
  114. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  115. var receivedMessagesCount = 0;
  116. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  117. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  118. await Task.Delay(500);
  119. await s.StopAsync();
  120. Assert.AreEqual(0, receivedMessagesCount);
  121. }
  122. [TestMethod]
  123. public async Task MqttServer_RetainedMessage()
  124. {
  125. var serverAdapter = new TestMqttServerAdapter();
  126. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  127. await s.StartAsync();
  128. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  129. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  130. await c1.DisconnectAsync();
  131. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  132. var receivedMessagesCount = 0;
  133. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  134. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  135. await Task.Delay(500);
  136. await s.StopAsync();
  137. Assert.AreEqual(1, receivedMessagesCount);
  138. }
  139. [TestMethod]
  140. public async Task MqttServer_ClearRetainedMessage()
  141. {
  142. var serverAdapter = new TestMqttServerAdapter();
  143. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  144. await s.StartAsync();
  145. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  146. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  147. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, true));
  148. await c1.DisconnectAsync();
  149. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  150. var receivedMessagesCount = 0;
  151. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  152. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  153. await Task.Delay(500);
  154. await s.StopAsync();
  155. Assert.AreEqual(0, receivedMessagesCount);
  156. }
  157. [TestMethod]
  158. public async Task MqttServer_PersistRetainedMessage()
  159. {
  160. var storage = new TestStorage();
  161. var serverAdapter = new TestMqttServerAdapter();
  162. var s = new MqttServer(new MqttServerOptions { Storage = storage }, new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  163. await s.StartAsync();
  164. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  165. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  166. await c1.DisconnectAsync();
  167. await s.StopAsync();
  168. s = new MqttServer(new MqttServerOptions { Storage = storage }, new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  169. await s.StartAsync();
  170. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  171. var receivedMessagesCount = 0;
  172. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  173. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  174. await Task.Delay(500);
  175. await s.StopAsync();
  176. Assert.AreEqual(1, receivedMessagesCount);
  177. }
  178. [TestMethod]
  179. public async Task MqttServer_InterceptMessage()
  180. {
  181. var options = new MqttServerOptions
  182. {
  183. ApplicationMessageInterceptor = message =>
  184. {
  185. message.Payload = Encoding.ASCII.GetBytes("extended");
  186. return message;
  187. }
  188. };
  189. var serverAdapter = new TestMqttServerAdapter();
  190. var s = new MqttServer(options, new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  191. await s.StartAsync();
  192. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  193. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  194. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
  195. var isIntercepted = false;
  196. c2.ApplicationMessageReceived += (sender, args) =>
  197. {
  198. isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
  199. };
  200. var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
  201. await c1.PublishAsync(m);
  202. await c1.DisconnectAsync();
  203. await Task.Delay(500);
  204. Assert.IsTrue(isIntercepted);
  205. }
  206. private class TestStorage : IMqttServerStorage
  207. {
  208. private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
  209. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  210. {
  211. _messages = messages;
  212. return Task.CompletedTask;
  213. }
  214. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  215. {
  216. return Task.FromResult(_messages);
  217. }
  218. }
  219. private static async Task TestPublishAsync(
  220. string topic,
  221. MqttQualityOfServiceLevel qualityOfServiceLevel,
  222. string topicFilter,
  223. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  224. int expectedReceivedMessagesCount)
  225. {
  226. var serverAdapter = new TestMqttServerAdapter();
  227. var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { serverAdapter }, new MqttNetTrace());
  228. await s.StartAsync();
  229. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  230. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  231. var receivedMessagesCount = 0;
  232. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  233. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
  234. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  235. await Task.Delay(500);
  236. await c1.UnsubscribeAsync(topicFilter);
  237. await Task.Delay(500);
  238. await s.StopAsync();
  239. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  240. }
  241. }
  242. }