No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MqttPacketSerializerTests.cs 13 KiB

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