Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

446 řádky
14 KiB

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