Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

386 righe
13 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.Core.Client;
  7. using MQTTnet.Core.Diagnostics;
  8. using MQTTnet.Core.Protocol;
  9. using MQTTnet.Core.Server;
  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 TopicFilter("#"));
  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(1, receivedMessagesCount);
  68. }
  69. [TestMethod]
  70. public async Task MqttServer_Unsubscribe()
  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. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  85. await c2.PublishAsync(message);
  86. await Task.Delay(500);
  87. Assert.AreEqual(1, receivedMessagesCount);
  88. await c1.UnsubscribeAsync("a");
  89. await c2.PublishAsync(message);
  90. await Task.Delay(500);
  91. Assert.AreEqual(1, receivedMessagesCount);
  92. }
  93. finally
  94. {
  95. await s.StopAsync();
  96. }
  97. await Task.Delay(500);
  98. Assert.AreEqual(1, receivedMessagesCount);
  99. }
  100. [TestMethod]
  101. public async Task MqttServer_Publish()
  102. {
  103. var serverAdapter = new TestMqttServerAdapter();
  104. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  105. var receivedMessagesCount = 0;
  106. try
  107. {
  108. await s.StartAsync(new MqttServerOptions());
  109. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  110. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  111. var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
  112. await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
  113. s.PublishAsync(message).Wait();
  114. await Task.Delay(500);
  115. }
  116. finally
  117. {
  118. await s.StopAsync();
  119. }
  120. Assert.AreEqual(1, receivedMessagesCount);
  121. }
  122. [TestMethod]
  123. public async Task MqttServer_NoRetainedMessage()
  124. {
  125. var serverAdapter = new TestMqttServerAdapter();
  126. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  127. var receivedMessagesCount = 0;
  128. try
  129. {
  130. await s.StartAsync(new MqttServerOptions());
  131. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  132. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).Build());
  133. await c1.DisconnectAsync();
  134. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  135. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  136. await c2.SubscribeAsync(new TopicFilter("retained"));
  137. await Task.Delay(500);
  138. }
  139. finally
  140. {
  141. await s.StopAsync();
  142. }
  143. Assert.AreEqual(0, receivedMessagesCount);
  144. }
  145. [TestMethod]
  146. public async Task MqttServer_RetainedMessage()
  147. {
  148. var serverAdapter = new TestMqttServerAdapter();
  149. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  150. var receivedMessagesCount = 0;
  151. try
  152. {
  153. await s.StartAsync(new MqttServerOptions());
  154. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  155. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  156. await c1.DisconnectAsync();
  157. await Task.Delay(TimeSpan.FromSeconds(2));
  158. // TODO: Find another way to wait for the retained components.
  159. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  160. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  161. await c2.SubscribeAsync(new TopicFilter("retained"));
  162. await Task.Delay(500);
  163. }
  164. finally
  165. {
  166. await s.StopAsync();
  167. }
  168. Assert.AreEqual(1, receivedMessagesCount);
  169. }
  170. [TestMethod]
  171. public async Task MqttServer_ClearRetainedMessage()
  172. {
  173. var serverAdapter = new TestMqttServerAdapter();
  174. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  175. var receivedMessagesCount = 0;
  176. try
  177. {
  178. await s.StartAsync(new MqttServerOptions());
  179. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  180. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  181. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build());
  182. await c1.DisconnectAsync();
  183. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  184. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  185. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));
  186. await Task.Delay(500);
  187. }
  188. finally
  189. {
  190. await s.StopAsync();
  191. }
  192. Assert.AreEqual(0, receivedMessagesCount);
  193. }
  194. [TestMethod]
  195. public async Task MqttServer_PersistRetainedMessage()
  196. {
  197. var storage = new TestStorage();
  198. var serverAdapter = new TestMqttServerAdapter();
  199. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  200. try
  201. {
  202. var options = new MqttServerOptions { Storage = storage };
  203. await s.StartAsync(options);
  204. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  205. await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  206. await c1.DisconnectAsync();
  207. }
  208. finally
  209. {
  210. await s.StopAsync();
  211. }
  212. await Task.Delay(TimeSpan.FromSeconds(2));
  213. // TODO: Find another way to wait for the retained components.
  214. s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  215. var receivedMessagesCount = 0;
  216. try
  217. {
  218. var options = new MqttServerOptions { Storage = storage };
  219. await s.StartAsync(options);
  220. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  221. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  222. await c2.SubscribeAsync(new TopicFilter("retained"));
  223. await Task.Delay(500);
  224. }
  225. finally
  226. {
  227. await s.StopAsync();
  228. }
  229. Assert.AreEqual(1, receivedMessagesCount);
  230. }
  231. [TestMethod]
  232. public async Task MqttServer_InterceptMessage()
  233. {
  234. void Interceptor(MqttApplicationMessageInterceptorContext context)
  235. {
  236. context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
  237. }
  238. var serverAdapter = new TestMqttServerAdapter();
  239. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  240. try
  241. {
  242. var options = new MqttServerOptions { ApplicationMessageInterceptor = Interceptor };
  243. await s.StartAsync(options);
  244. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  245. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  246. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
  247. var isIntercepted = false;
  248. c2.ApplicationMessageReceived += (sender, args) =>
  249. {
  250. isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
  251. };
  252. var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
  253. await c1.PublishAsync(m);
  254. await c1.DisconnectAsync();
  255. await Task.Delay(500);
  256. Assert.IsTrue(isIntercepted);
  257. }
  258. finally
  259. {
  260. await s.StopAsync();
  261. }
  262. }
  263. private class TestStorage : IMqttServerStorage
  264. {
  265. private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
  266. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  267. {
  268. _messages = messages;
  269. return Task.CompletedTask;
  270. }
  271. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  272. {
  273. return Task.FromResult(_messages);
  274. }
  275. }
  276. private static async Task TestPublishAsync(
  277. string topic,
  278. MqttQualityOfServiceLevel qualityOfServiceLevel,
  279. string topicFilter,
  280. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  281. int expectedReceivedMessagesCount)
  282. {
  283. var serverAdapter = new TestMqttServerAdapter();
  284. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  285. var receivedMessagesCount = 0;
  286. try
  287. {
  288. await s.StartAsync(new MqttServerOptions());
  289. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  290. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  291. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  292. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
  293. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  294. await Task.Delay(500);
  295. await c1.UnsubscribeAsync(topicFilter);
  296. await Task.Delay(500);
  297. }
  298. finally
  299. {
  300. await s.StopAsync();
  301. }
  302. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  303. }
  304. }
  305. }