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.

MqttPacketSerializerTests.cs 13 KiB

пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 6 година
пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.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. using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength))
  355. {
  356. var deserializedPacket = serializer.Deserialize(new ReceivedMqttPacket(header.Flags, new MqttPacketBodyReader(bodyStream.ToArray())));
  357. var buffer2 = serializer.Serialize(deserializedPacket);
  358. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
  359. }
  360. }
  361. }
  362. private static byte[] Join(params ArraySegment<byte>[] chunks)
  363. {
  364. var buffer = new MemoryStream();
  365. foreach (var chunk in chunks)
  366. {
  367. buffer.Write(chunk.Array, chunk.Offset, chunk.Count);
  368. }
  369. return buffer.ToArray();
  370. }
  371. }
  372. }