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.
 
 
 
 

435 righe
13 KiB

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using MQTTnet.Core.Channel;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.Packets;
  9. using MQTTnet.Core.Protocol;
  10. using MQTTnet.Core.Serializer;
  11. namespace MQTTnet.Core.Tests
  12. {
  13. [TestClass]
  14. public class DefaultMqttV311PacketSerializerTests
  15. {
  16. [TestMethod]
  17. public void SerializeV311_MqttConnectPacket()
  18. {
  19. var p = new MqttConnectPacket
  20. {
  21. ClientId = "XYZ",
  22. Password = "PASS",
  23. Username = "USER",
  24. KeepAlivePeriod = 123,
  25. CleanSession = true
  26. };
  27. SerializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
  28. }
  29. [TestMethod]
  30. public void SerializeV311_MqttConnectPacketWithWillMessage()
  31. {
  32. var p = new MqttConnectPacket
  33. {
  34. ClientId = "XYZ",
  35. Password = "PASS",
  36. Username = "USER",
  37. KeepAlivePeriod = 123,
  38. CleanSession = true,
  39. WillMessage = new MqttApplicationMessage(
  40. "My/last/will",
  41. Encoding.UTF8.GetBytes("Good byte."),
  42. MqttQualityOfServiceLevel.AtLeastOnce,
  43. true)
  44. };
  45. SerializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
  46. }
  47. [TestMethod]
  48. public void DeserializeV311_MqttConnectPacket()
  49. {
  50. var p = new MqttConnectPacket
  51. {
  52. ClientId = "XYZ",
  53. Password = "PASS",
  54. Username = "USER",
  55. KeepAlivePeriod = 123,
  56. CleanSession = true
  57. };
  58. DeserializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
  59. }
  60. [TestMethod]
  61. public void DeserializeV311_MqttConnectPacketWithWillMessage()
  62. {
  63. var p = new MqttConnectPacket
  64. {
  65. ClientId = "XYZ",
  66. Password = "PASS",
  67. Username = "USER",
  68. KeepAlivePeriod = 123,
  69. CleanSession = true,
  70. WillMessage = new MqttApplicationMessage(
  71. "My/last/will",
  72. Encoding.UTF8.GetBytes("Good byte."),
  73. MqttQualityOfServiceLevel.AtLeastOnce,
  74. true)
  75. };
  76. DeserializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
  77. }
  78. [TestMethod]
  79. public void SerializeV311_MqttConnAckPacket()
  80. {
  81. var p = new MqttConnAckPacket
  82. {
  83. IsSessionPresent = true,
  84. ConnectReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  85. };
  86. SerializeAndCompare(p, "IAIBBQ==");
  87. }
  88. [TestMethod]
  89. public void DeserializeV311_MqttConnAckPacket()
  90. {
  91. var p = new MqttConnAckPacket
  92. {
  93. IsSessionPresent = true,
  94. ConnectReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  95. };
  96. DeserializeAndCompare(p, "IAIBBQ==");
  97. }
  98. [TestMethod]
  99. public void SerializeV311_MqttDisconnectPacket()
  100. {
  101. SerializeAndCompare(new MqttDisconnectPacket(), "4AA=");
  102. }
  103. [TestMethod]
  104. public void SerializeV311_MqttPingReqPacket()
  105. {
  106. SerializeAndCompare(new MqttPingReqPacket(), "wAA=");
  107. }
  108. [TestMethod]
  109. public void SerializeV311_MqttPingRespPacket()
  110. {
  111. SerializeAndCompare(new MqttPingRespPacket(), "0AA=");
  112. }
  113. [TestMethod]
  114. public void SerializeV311_MqttPublishPacket()
  115. {
  116. var p = new MqttPublishPacket
  117. {
  118. PacketIdentifier = 123,
  119. Dup = true,
  120. Retain = true,
  121. Payload = Encoding.ASCII.GetBytes("HELLO"),
  122. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  123. Topic = "A/B/C"
  124. };
  125. SerializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  126. }
  127. [TestMethod]
  128. public void DeserializeV311_MqttPublishPacket()
  129. {
  130. var p = new MqttPublishPacket
  131. {
  132. PacketIdentifier = 123,
  133. Dup = true,
  134. Retain = true,
  135. Payload = Encoding.ASCII.GetBytes("HELLO"),
  136. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  137. Topic = "A/B/C"
  138. };
  139. DeserializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  140. }
  141. [TestMethod]
  142. public void SerializeV311_MqttPubAckPacket()
  143. {
  144. var p = new MqttPubAckPacket
  145. {
  146. PacketIdentifier = 123
  147. };
  148. SerializeAndCompare(p, "QAIAew==");
  149. }
  150. [TestMethod]
  151. public void DeserializeV311_MqttPubAckPacket()
  152. {
  153. var p = new MqttPubAckPacket
  154. {
  155. PacketIdentifier = 123
  156. };
  157. DeserializeAndCompare(p, "QAIAew==");
  158. }
  159. [TestMethod]
  160. public void SerializeV311_MqttPubRecPacket()
  161. {
  162. var p = new MqttPubRecPacket
  163. {
  164. PacketIdentifier = 123
  165. };
  166. SerializeAndCompare(p, "UAIAew==");
  167. }
  168. [TestMethod]
  169. public void DeserializeV311_MqttPubRecPacket()
  170. {
  171. var p = new MqttPubRecPacket
  172. {
  173. PacketIdentifier = 123
  174. };
  175. DeserializeAndCompare(p, "UAIAew==");
  176. }
  177. [TestMethod]
  178. public void SerializeV311_MqttPubRelPacket()
  179. {
  180. var p = new MqttPubRelPacket
  181. {
  182. PacketIdentifier = 123
  183. };
  184. SerializeAndCompare(p, "YgIAew==");
  185. }
  186. [TestMethod]
  187. public void DeserializeV311_MqttPubRelPacket()
  188. {
  189. var p = new MqttPubRelPacket
  190. {
  191. PacketIdentifier = 123
  192. };
  193. DeserializeAndCompare(p, "YgIAew==");
  194. }
  195. [TestMethod]
  196. public void SerializeV311_MqttPubCompPacket()
  197. {
  198. var p = new MqttPubCompPacket
  199. {
  200. PacketIdentifier = 123
  201. };
  202. SerializeAndCompare(p, "cAIAew==");
  203. }
  204. [TestMethod]
  205. public void DeserializeV311_MqttPubCompPacket()
  206. {
  207. var p = new MqttPubCompPacket
  208. {
  209. PacketIdentifier = 123
  210. };
  211. DeserializeAndCompare(p, "cAIAew==");
  212. }
  213. [TestMethod]
  214. public void SerializeV311_MqttSubscribePacket()
  215. {
  216. var p = new MqttSubscribePacket
  217. {
  218. PacketIdentifier = 123
  219. };
  220. p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
  221. p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
  222. p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
  223. SerializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  224. }
  225. [TestMethod]
  226. public void DeserializeV311_MqttSubscribePacket()
  227. {
  228. var p = new MqttSubscribePacket
  229. {
  230. PacketIdentifier = 123
  231. };
  232. p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
  233. p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
  234. p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
  235. DeserializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  236. }
  237. [TestMethod]
  238. public void SerializeV311_MqttSubAckPacket()
  239. {
  240. var p = new MqttSubAckPacket
  241. {
  242. PacketIdentifier = 123
  243. };
  244. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  245. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  246. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  247. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  248. SerializeAndCompare(p, "kAYAewABAoA=");
  249. }
  250. [TestMethod]
  251. public void DeserializeV311_MqttSubAckPacket()
  252. {
  253. var p = new MqttSubAckPacket
  254. {
  255. PacketIdentifier = 123
  256. };
  257. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  258. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  259. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  260. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  261. DeserializeAndCompare(p, "kAYAewABAoA=");
  262. }
  263. [TestMethod]
  264. public void SerializeV311_MqttUnsubscribePacket()
  265. {
  266. var p = new MqttUnsubscribePacket
  267. {
  268. PacketIdentifier = 123
  269. };
  270. p.TopicFilters.Add("A/B/C");
  271. p.TopicFilters.Add("1/2/3");
  272. p.TopicFilters.Add("x/y/z");
  273. SerializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  274. }
  275. [TestMethod]
  276. public void DeserializeV311_MqttUnsubscribePacket()
  277. {
  278. var p = new MqttUnsubscribePacket
  279. {
  280. PacketIdentifier = 123
  281. };
  282. p.TopicFilters.Add("A/B/C");
  283. p.TopicFilters.Add("1/2/3");
  284. p.TopicFilters.Add("x/y/z");
  285. DeserializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  286. }
  287. [TestMethod]
  288. public void SerializeV311_MqttUnsubAckPacket()
  289. {
  290. var p = new MqttUnsubAckPacket
  291. {
  292. PacketIdentifier = 123
  293. };
  294. SerializeAndCompare(p, "sAIAew==");
  295. }
  296. [TestMethod]
  297. public void DeserializeV311_MqttUnsubAckPacket()
  298. {
  299. var p = new MqttUnsubAckPacket
  300. {
  301. PacketIdentifier = 123
  302. };
  303. DeserializeAndCompare(p, "sAIAew==");
  304. }
  305. public class TestChannel : IMqttCommunicationChannel
  306. {
  307. private readonly MemoryStream _stream = new MemoryStream();
  308. public bool IsConnected { get; } = true;
  309. public TestChannel()
  310. {
  311. }
  312. public TestChannel(byte[] initialData)
  313. {
  314. _stream.Write(initialData, 0, initialData.Length);
  315. _stream.Position = 0;
  316. }
  317. public async Task ConnectAsync(MqttClientOptions options)
  318. {
  319. await Task.FromResult(0);
  320. }
  321. public async Task DisconnectAsync()
  322. {
  323. await Task.FromResult(0);
  324. }
  325. public async Task WriteAsync(byte[] buffer)
  326. {
  327. await _stream.WriteAsync(buffer, 0, buffer.Length);
  328. }
  329. public async Task ReadAsync(byte[] buffer)
  330. {
  331. await _stream.ReadAsync(buffer, 0, buffer.Length);
  332. }
  333. public byte[] ToArray()
  334. {
  335. return _stream.ToArray();
  336. }
  337. }
  338. private void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
  339. {
  340. var serializer = new DefaultMqttV311PacketSerializer();
  341. var channel = new TestChannel();
  342. serializer.SerializeAsync(packet, channel).Wait();
  343. var buffer = channel.ToArray();
  344. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(buffer));
  345. }
  346. private void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
  347. {
  348. var serializer = new DefaultMqttV311PacketSerializer();
  349. var channel1 = new TestChannel();
  350. serializer.SerializeAsync(packet, channel1).Wait();
  351. var buffer1 = channel1.ToArray();
  352. var channel2 = new TestChannel(buffer1);
  353. var deserializedPacket = serializer.DeserializeAsync(channel2).Result;
  354. var buffer2 = channel2.ToArray();
  355. var channel3 = new TestChannel(buffer2);
  356. serializer.SerializeAsync(deserializedPacket, channel3).Wait();
  357. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(channel3.ToArray()));
  358. }
  359. }
  360. }