25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

361 lines
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.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. }