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