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.
 
 
 
 

436 lignes
15 KiB

  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.Diagnostics;
  7. using MQTTnet.Protocol;
  8. using MQTTnet.Server;
  9. using MQTTnet.Client;
  10. namespace MQTTnet.Core.Tests
  11. {
  12. [TestClass]
  13. public class MqttServerTests
  14. {
  15. [TestMethod]
  16. public void MqttServer_PublishSimple_AtMostOnce()
  17. {
  18. TestPublishAsync(
  19. "A/B/C",
  20. MqttQualityOfServiceLevel.AtMostOnce,
  21. "A/B/C",
  22. MqttQualityOfServiceLevel.AtMostOnce,
  23. 1).Wait();
  24. }
  25. [TestMethod]
  26. public void MqttServer_PublishSimple_AtLeastOnce()
  27. {
  28. TestPublishAsync(
  29. "A/B/C",
  30. MqttQualityOfServiceLevel.AtLeastOnce,
  31. "A/B/C",
  32. MqttQualityOfServiceLevel.AtLeastOnce,
  33. 1).Wait();
  34. }
  35. [TestMethod]
  36. public void MqttServer_PublishSimple_ExactlyOnce()
  37. {
  38. TestPublishAsync(
  39. "A/B/C",
  40. MqttQualityOfServiceLevel.ExactlyOnce,
  41. "A/B/C",
  42. MqttQualityOfServiceLevel.ExactlyOnce,
  43. 1).Wait();
  44. }
  45. [TestMethod]
  46. public async Task MqttServer_WillMessage()
  47. {
  48. var serverAdapter = new TestMqttServerAdapter();
  49. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  50. var receivedMessagesCount = 0;
  51. try
  52. {
  53. await s.StartAsync(new MqttServerOptions());
  54. var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
  55. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  56. var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
  57. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  58. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
  59. await c2.DisconnectAsync();
  60. await Task.Delay(1000);
  61. await c1.DisconnectAsync();
  62. }
  63. finally
  64. {
  65. await s.StopAsync();
  66. }
  67. Assert.AreEqual(0, receivedMessagesCount);
  68. }
  69. [TestMethod]
  70. public async Task MqttServer_SubscribeUnsubscribe()
  71. {
  72. var serverAdapter = new TestMqttServerAdapter();
  73. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  74. var receivedMessagesCount = 0;
  75. try
  76. {
  77. await s.StartAsync(new MqttServerOptions());
  78. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  79. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  80. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  81. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  82. await c2.PublishAsync(message);
  83. Assert.AreEqual(0, receivedMessagesCount);
  84. var subscribeEventCalled = false;
  85. s.ClientSubscribedTopic += (_, e) =>
  86. {
  87. subscribeEventCalled = e.TopicFilter.Topic == "a" && e.ClientId == "c1";
  88. };
  89. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  90. await Task.Delay(100);
  91. Assert.IsTrue(subscribeEventCalled, "Subscribe event not called.");
  92. await c2.PublishAsync(message);
  93. await Task.Delay(500);
  94. Assert.AreEqual(1, receivedMessagesCount);
  95. var unsubscribeEventCalled = false;
  96. s.ClientUnsubscribedTopic += (_, e) =>
  97. {
  98. unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == "c1";
  99. };
  100. await c1.UnsubscribeAsync("a");
  101. await Task.Delay(100);
  102. Assert.IsTrue(unsubscribeEventCalled, "Unsubscribe event not called.");
  103. await c2.PublishAsync(message);
  104. await Task.Delay(500);
  105. Assert.AreEqual(1, receivedMessagesCount);
  106. }
  107. finally
  108. {
  109. await s.StopAsync();
  110. }
  111. await Task.Delay(500);
  112. Assert.AreEqual(1, receivedMessagesCount);
  113. }
  114. [TestMethod]
  115. public async Task MqttServer_Publish()
  116. {
  117. var serverAdapter = new TestMqttServerAdapter();
  118. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  119. var receivedMessagesCount = 0;
  120. try
  121. {
  122. await s.StartAsync(new MqttServerOptions());
  123. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  124. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  125. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  126. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  127. await s.PublishAsync(message);
  128. await Task.Delay(500);
  129. }
  130. finally
  131. {
  132. await s.StopAsync();
  133. }
  134. Assert.AreEqual(1, receivedMessagesCount);
  135. }
  136. [TestMethod]
  137. public async Task MqttServer_NoRetainedMessage()
  138. {
  139. var serverAdapter = new TestMqttServerAdapter();
  140. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  141. var receivedMessagesCount = 0;
  142. try
  143. {
  144. await s.StartAsync(new MqttServerOptions());
  145. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  146. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build());
  147. await c1.DisconnectAsync();
  148. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  149. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  150. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
  151. await Task.Delay(500);
  152. }
  153. finally
  154. {
  155. await s.StopAsync();
  156. }
  157. Assert.AreEqual(0, receivedMessagesCount);
  158. }
  159. [TestMethod]
  160. public async Task MqttServer_RetainedMessage()
  161. {
  162. var serverAdapter = new TestMqttServerAdapter();
  163. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  164. var receivedMessagesCount = 0;
  165. try
  166. {
  167. await s.StartAsync(new MqttServerOptions());
  168. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  169. await c1.PublishAndWaitForAsync(s, new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  170. await c1.DisconnectAsync();
  171. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  172. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  173. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
  174. await Task.Delay(500);
  175. }
  176. finally
  177. {
  178. await s.StopAsync();
  179. }
  180. Assert.AreEqual(1, receivedMessagesCount);
  181. }
  182. [TestMethod]
  183. public async Task MqttServer_ClearRetainedMessage()
  184. {
  185. var serverAdapter = new TestMqttServerAdapter();
  186. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  187. var receivedMessagesCount = 0;
  188. try
  189. {
  190. await s.StartAsync(new MqttServerOptions());
  191. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  192. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  193. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build());
  194. await c1.DisconnectAsync();
  195. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  196. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  197. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));
  198. await Task.Delay(500);
  199. }
  200. finally
  201. {
  202. await s.StopAsync();
  203. }
  204. Assert.AreEqual(0, receivedMessagesCount);
  205. }
  206. [TestMethod]
  207. public async Task MqttServer_PersistRetainedMessage()
  208. {
  209. var storage = new TestStorage();
  210. var serverAdapter = new TestMqttServerAdapter();
  211. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  212. try
  213. {
  214. var options = new MqttServerOptions { Storage = storage };
  215. await s.StartAsync(options);
  216. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  217. await c1.PublishAndWaitForAsync(s, new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  218. await c1.DisconnectAsync();
  219. }
  220. finally
  221. {
  222. await s.StopAsync();
  223. }
  224. Assert.AreEqual(1, storage.Messages.Count);
  225. s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  226. var receivedMessagesCount = 0;
  227. try
  228. {
  229. var options = new MqttServerOptions { Storage = storage };
  230. await s.StartAsync(options);
  231. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  232. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  233. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
  234. await Task.Delay(500);
  235. }
  236. finally
  237. {
  238. await s.StopAsync();
  239. }
  240. Assert.AreEqual(1, receivedMessagesCount);
  241. }
  242. [TestMethod]
  243. public async Task MqttServer_InterceptMessage()
  244. {
  245. void Interceptor(MqttApplicationMessageInterceptorContext context)
  246. {
  247. context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
  248. }
  249. var serverAdapter = new TestMqttServerAdapter();
  250. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  251. try
  252. {
  253. var options = new MqttServerOptions { ApplicationMessageInterceptor = Interceptor };
  254. await s.StartAsync(options);
  255. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  256. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  257. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
  258. var isIntercepted = false;
  259. c2.ApplicationMessageReceived += (sender, args) =>
  260. {
  261. isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
  262. };
  263. var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
  264. await c1.PublishAsync(m);
  265. await c1.DisconnectAsync();
  266. await Task.Delay(500);
  267. Assert.IsTrue(isIntercepted);
  268. }
  269. finally
  270. {
  271. await s.StopAsync();
  272. }
  273. }
  274. [TestMethod]
  275. public async Task MqttServer_Body()
  276. {
  277. var serverAdapter = new TestMqttServerAdapter();
  278. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  279. var bodyIsMatching = false;
  280. try
  281. {
  282. await s.StartAsync(new MqttServerOptions());
  283. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  284. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  285. c1.ApplicationMessageReceived += (_, e) =>
  286. {
  287. if (Encoding.UTF8.GetString(e.ApplicationMessage.Payload) == "The body")
  288. {
  289. bodyIsMatching = true;
  290. }
  291. };
  292. await c1.SubscribeAsync("A", MqttQualityOfServiceLevel.AtMostOnce);
  293. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("A").WithPayload(Encoding.UTF8.GetBytes("The body")).Build());
  294. await Task.Delay(500);
  295. }
  296. finally
  297. {
  298. await s.StopAsync();
  299. }
  300. Assert.IsTrue(bodyIsMatching);
  301. }
  302. private class TestStorage : IMqttServerStorage
  303. {
  304. public IList<MqttApplicationMessage> Messages = new List<MqttApplicationMessage>();
  305. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  306. {
  307. Messages = messages;
  308. return Task.CompletedTask;
  309. }
  310. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  311. {
  312. return Task.FromResult(Messages);
  313. }
  314. }
  315. private static async Task TestPublishAsync(
  316. string topic,
  317. MqttQualityOfServiceLevel qualityOfServiceLevel,
  318. string topicFilter,
  319. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  320. int expectedReceivedMessagesCount)
  321. {
  322. var serverAdapter = new TestMqttServerAdapter();
  323. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  324. var receivedMessagesCount = 0;
  325. try
  326. {
  327. await s.StartAsync(new MqttServerOptions());
  328. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  329. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  330. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  331. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
  332. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  333. await Task.Delay(500);
  334. await c1.UnsubscribeAsync(topicFilter);
  335. await Task.Delay(500);
  336. }
  337. finally
  338. {
  339. await s.StopAsync();
  340. }
  341. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  342. }
  343. }
  344. }