Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MqttServerTests.cs 13 KiB

il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.Packets;
  9. using MQTTnet.Core.Protocol;
  10. using MQTTnet.Core.Server;
  11. using Microsoft.Extensions.DependencyInjection;
  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 services = new ServiceCollection()
  52. .AddMqttServer()
  53. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  54. .BuildServiceProvider();
  55. var s = services.GetRequiredService<IMqttServer>();
  56. await s.StartAsync();
  57. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  58. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  59. var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
  60. var receivedMessagesCount = 0;
  61. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  62. await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
  63. await c2.DisconnectAsync();
  64. await Task.Delay(1000);
  65. await s.StopAsync();
  66. Assert.AreEqual(1, receivedMessagesCount);
  67. }
  68. [TestMethod]
  69. public async Task MqttServer_Unsubscribe()
  70. {
  71. var serverAdapter = new TestMqttServerAdapter();
  72. var services = new ServiceCollection()
  73. .AddMqttServer()
  74. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  75. .BuildServiceProvider();
  76. var s = services.GetRequiredService<IMqttServer>();
  77. await s.StartAsync();
  78. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  79. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  80. var receivedMessagesCount = 0;
  81. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  82. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  83. await c2.PublishAsync(message);
  84. Assert.AreEqual(0, receivedMessagesCount);
  85. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  86. await c2.PublishAsync(message);
  87. await Task.Delay(500);
  88. Assert.AreEqual(1, receivedMessagesCount);
  89. await c1.UnsubscribeAsync("a");
  90. await c2.PublishAsync(message);
  91. await Task.Delay(500);
  92. Assert.AreEqual(1, receivedMessagesCount);
  93. await s.StopAsync();
  94. await Task.Delay(500);
  95. Assert.AreEqual(1, receivedMessagesCount);
  96. }
  97. [TestMethod]
  98. public async Task MqttServer_Publish()
  99. {
  100. var serverAdapter = new TestMqttServerAdapter();
  101. var services = new ServiceCollection()
  102. .AddMqttServer()
  103. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  104. .BuildServiceProvider();
  105. var s = services.GetRequiredService<IMqttServer>();
  106. await s.StartAsync();
  107. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  108. var receivedMessagesCount = 0;
  109. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  110. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  111. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  112. s.Publish(message);
  113. await Task.Delay(500);
  114. await s.StopAsync();
  115. Assert.AreEqual(1, receivedMessagesCount);
  116. }
  117. [TestMethod]
  118. public async Task MqttServer_NoRetainedMessage()
  119. {
  120. var serverAdapter = new TestMqttServerAdapter();
  121. var services = new ServiceCollection()
  122. .AddMqttServer()
  123. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  124. .BuildServiceProvider();
  125. var s = services.GetRequiredService<IMqttServer>();
  126. await s.StartAsync();
  127. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  128. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, false));
  129. await c1.DisconnectAsync();
  130. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  131. var receivedMessagesCount = 0;
  132. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  133. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  134. await Task.Delay(500);
  135. await s.StopAsync();
  136. Assert.AreEqual(0, receivedMessagesCount);
  137. }
  138. [TestMethod]
  139. public async Task MqttServer_RetainedMessage()
  140. {
  141. var serverAdapter = new TestMqttServerAdapter();
  142. var services = new ServiceCollection()
  143. .AddMqttServer()
  144. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  145. .BuildServiceProvider();
  146. var s = services.GetRequiredService<IMqttServer>();
  147. await s.StartAsync();
  148. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  149. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  150. await c1.DisconnectAsync();
  151. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  152. var receivedMessagesCount = 0;
  153. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  154. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  155. await Task.Delay(500);
  156. await s.StopAsync();
  157. Assert.AreEqual(1, receivedMessagesCount);
  158. }
  159. [TestMethod]
  160. public async Task MqttServer_ClearRetainedMessage()
  161. {
  162. var serverAdapter = new TestMqttServerAdapter();
  163. var services = new ServiceCollection()
  164. .AddMqttServer()
  165. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  166. .BuildServiceProvider();
  167. var s = services.GetRequiredService<IMqttServer>();
  168. await s.StartAsync();
  169. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  170. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  171. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, true));
  172. await c1.DisconnectAsync();
  173. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  174. var receivedMessagesCount = 0;
  175. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  176. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  177. await Task.Delay(500);
  178. await s.StopAsync();
  179. Assert.AreEqual(0, receivedMessagesCount);
  180. }
  181. [TestMethod]
  182. public async Task MqttServer_PersistRetainedMessage()
  183. {
  184. var storage = new TestStorage();
  185. var serverAdapter = new TestMqttServerAdapter();
  186. var services = new ServiceCollection()
  187. .AddMqttServer()
  188. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  189. .BuildServiceProvider();
  190. var s = services.GetRequiredService<IMqttServer>();
  191. await s.StartAsync();
  192. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  193. await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
  194. await c1.DisconnectAsync();
  195. await s.StopAsync();
  196. s = services.GetRequiredService<IMqttServer>();
  197. await s.StartAsync();
  198. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  199. var receivedMessagesCount = 0;
  200. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  201. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
  202. await Task.Delay(500);
  203. await s.StopAsync();
  204. Assert.AreEqual(1, receivedMessagesCount);
  205. }
  206. [TestMethod]
  207. public async Task MqttServer_InterceptMessage()
  208. {
  209. var options = new MqttServerOptions
  210. {
  211. ApplicationMessageInterceptor = message =>
  212. {
  213. message.Payload = Encoding.ASCII.GetBytes("extended");
  214. return message;
  215. }
  216. };
  217. var serverAdapter = new TestMqttServerAdapter();
  218. var services = new ServiceCollection()
  219. .AddMqttServer()
  220. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  221. .BuildServiceProvider();
  222. var s = services.GetRequiredService<IMqttServer>();
  223. await s.StartAsync();
  224. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  225. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  226. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
  227. var isIntercepted = false;
  228. c2.ApplicationMessageReceived += (sender, args) =>
  229. {
  230. isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
  231. };
  232. var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
  233. await c1.PublishAsync(m);
  234. await c1.DisconnectAsync();
  235. await Task.Delay(500);
  236. Assert.IsTrue(isIntercepted);
  237. }
  238. private class TestStorage : IMqttServerStorage
  239. {
  240. private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
  241. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  242. {
  243. _messages = messages;
  244. return Task.CompletedTask;
  245. }
  246. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  247. {
  248. return Task.FromResult(_messages);
  249. }
  250. }
  251. private static async Task TestPublishAsync(
  252. string topic,
  253. MqttQualityOfServiceLevel qualityOfServiceLevel,
  254. string topicFilter,
  255. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  256. int expectedReceivedMessagesCount)
  257. {
  258. var serverAdapter = new TestMqttServerAdapter();
  259. var services = new ServiceCollection()
  260. .AddMqttServer()
  261. .AddSingleton<IMqttServerAdapter>(serverAdapter)
  262. .BuildServiceProvider();
  263. var s = services.GetRequiredService<IMqttServer>();
  264. await s.StartAsync();
  265. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  266. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  267. var receivedMessagesCount = 0;
  268. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  269. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
  270. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  271. await Task.Delay(500);
  272. await c1.UnsubscribeAsync(topicFilter);
  273. await Task.Delay(500);
  274. await s.StopAsync();
  275. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  276. }
  277. }
  278. }