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.

MqttServerTests.cs 15 KiB

пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 6 година
пре 7 година
пре 6 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 Task.Delay(200);
  198. await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtMostOnce));
  199. await Task.Delay(500);
  200. }
  201. finally
  202. {
  203. await s.StopAsync();
  204. }
  205. Assert.AreEqual(0, receivedMessagesCount);
  206. }
  207. [TestMethod]
  208. public async Task MqttServer_PersistRetainedMessage()
  209. {
  210. var storage = new TestStorage();
  211. var serverAdapter = new TestMqttServerAdapter();
  212. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  213. try
  214. {
  215. var options = new MqttServerOptions { Storage = storage };
  216. await s.StartAsync(options);
  217. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  218. await c1.PublishAndWaitForAsync(s, new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
  219. await c1.DisconnectAsync();
  220. }
  221. finally
  222. {
  223. await s.StopAsync();
  224. }
  225. Assert.AreEqual(1, storage.Messages.Count);
  226. s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  227. var receivedMessagesCount = 0;
  228. try
  229. {
  230. var options = new MqttServerOptions { Storage = storage };
  231. await s.StartAsync(options);
  232. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  233. c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  234. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
  235. await Task.Delay(500);
  236. }
  237. finally
  238. {
  239. await s.StopAsync();
  240. }
  241. Assert.AreEqual(1, receivedMessagesCount);
  242. }
  243. [TestMethod]
  244. public async Task MqttServer_InterceptMessage()
  245. {
  246. void Interceptor(MqttApplicationMessageInterceptorContext context)
  247. {
  248. context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
  249. }
  250. var serverAdapter = new TestMqttServerAdapter();
  251. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  252. try
  253. {
  254. var options = new MqttServerOptions { ApplicationMessageInterceptor = Interceptor };
  255. await s.StartAsync(options);
  256. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  257. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  258. await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
  259. var isIntercepted = false;
  260. c2.ApplicationMessageReceived += (sender, args) =>
  261. {
  262. isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
  263. };
  264. var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
  265. await c1.PublishAsync(m);
  266. await c1.DisconnectAsync();
  267. await Task.Delay(500);
  268. Assert.IsTrue(isIntercepted);
  269. }
  270. finally
  271. {
  272. await s.StopAsync();
  273. }
  274. }
  275. [TestMethod]
  276. public async Task MqttServer_Body()
  277. {
  278. var serverAdapter = new TestMqttServerAdapter();
  279. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  280. var bodyIsMatching = false;
  281. try
  282. {
  283. await s.StartAsync(new MqttServerOptions());
  284. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  285. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  286. c1.ApplicationMessageReceived += (_, e) =>
  287. {
  288. if (Encoding.UTF8.GetString(e.ApplicationMessage.Payload) == "The body")
  289. {
  290. bodyIsMatching = true;
  291. }
  292. };
  293. await c1.SubscribeAsync("A", MqttQualityOfServiceLevel.AtMostOnce);
  294. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("A").WithPayload(Encoding.UTF8.GetBytes("The body")).Build());
  295. await Task.Delay(500);
  296. }
  297. finally
  298. {
  299. await s.StopAsync();
  300. }
  301. Assert.IsTrue(bodyIsMatching);
  302. }
  303. private class TestStorage : IMqttServerStorage
  304. {
  305. public IList<MqttApplicationMessage> Messages = new List<MqttApplicationMessage>();
  306. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  307. {
  308. Messages = messages;
  309. return Task.CompletedTask;
  310. }
  311. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  312. {
  313. return Task.FromResult(Messages);
  314. }
  315. }
  316. private static async Task TestPublishAsync(
  317. string topic,
  318. MqttQualityOfServiceLevel qualityOfServiceLevel,
  319. string topicFilter,
  320. MqttQualityOfServiceLevel filterQualityOfServiceLevel,
  321. int expectedReceivedMessagesCount)
  322. {
  323. var serverAdapter = new TestMqttServerAdapter();
  324. var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger());
  325. var receivedMessagesCount = 0;
  326. try
  327. {
  328. await s.StartAsync(new MqttServerOptions());
  329. var c1 = await serverAdapter.ConnectTestClient(s, "c1");
  330. var c2 = await serverAdapter.ConnectTestClient(s, "c2");
  331. c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
  332. await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
  333. await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
  334. await Task.Delay(500);
  335. await c1.UnsubscribeAsync(topicFilter);
  336. await Task.Delay(500);
  337. }
  338. finally
  339. {
  340. await s.StopAsync();
  341. }
  342. Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
  343. }
  344. }
  345. }