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.
 
 
 
 

454 líneas
22 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using MQTTnet.Formatter;
  8. using MQTTnet.Packets;
  9. using MQTTnet.Protocol;
  10. namespace MQTTnet.Tests
  11. {
  12. [TestClass]
  13. public sealed class MqttPacketSerialization_V5_Tests
  14. {
  15. [TestMethod]
  16. public void Serialize_Full_MqttAuthPacket_V500()
  17. {
  18. var authPacket = new MqttAuthPacket
  19. {
  20. AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
  21. AuthenticationMethod = "AuthenticationMethod",
  22. ReasonCode = MqttAuthenticateReasonCode.ContinueAuthentication,
  23. ReasonString = "ReasonString",
  24. UserProperties = new List<MqttUserProperty>
  25. {
  26. new MqttUserProperty("Foo", "Bar")
  27. }
  28. };
  29. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(authPacket, MqttProtocolVersion.V500);
  30. CollectionAssert.AreEqual(authPacket.AuthenticationData, deserialized.AuthenticationData);
  31. Assert.AreEqual(authPacket.AuthenticationMethod, deserialized.AuthenticationMethod);
  32. Assert.AreEqual(authPacket.ReasonCode, deserialized.ReasonCode);
  33. Assert.AreEqual(authPacket.ReasonString, deserialized.ReasonString);
  34. CollectionAssert.AreEqual(authPacket.UserProperties, deserialized.UserProperties);
  35. }
  36. [TestMethod]
  37. public void Serialize_Full_MqttConnAckPacket_V500()
  38. {
  39. var connAckPacket = new MqttConnAckPacket
  40. {
  41. AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
  42. AuthenticationMethod = "AuthenticationMethod",
  43. ReasonCode = MqttConnectReasonCode.ServerUnavailable,
  44. ReasonString = "ReasonString",
  45. ReceiveMaximum = 123,
  46. ResponseInformation = "ResponseInformation",
  47. RetainAvailable = true,
  48. ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized,
  49. ServerReference = "ServerReference",
  50. AssignedClientIdentifier = "AssignedClientIdentifier",
  51. IsSessionPresent = true,
  52. MaximumPacketSize = 456,
  53. MaximumQoS = MqttQualityOfServiceLevel.ExactlyOnce,
  54. ServerKeepAlive = 789,
  55. SessionExpiryInterval = 852,
  56. SharedSubscriptionAvailable = true,
  57. SubscriptionIdentifiersAvailable = true,
  58. TopicAliasMaximum = 963,
  59. WildcardSubscriptionAvailable = true,
  60. UserProperties = new List<MqttUserProperty>
  61. {
  62. new MqttUserProperty("Foo", "Bar")
  63. }
  64. };
  65. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connAckPacket, MqttProtocolVersion.V500);
  66. CollectionAssert.AreEqual(connAckPacket.AuthenticationData, deserialized.AuthenticationData);
  67. Assert.AreEqual(connAckPacket.AuthenticationMethod, deserialized.AuthenticationMethod);
  68. Assert.AreEqual(connAckPacket.ReasonCode, deserialized.ReasonCode);
  69. Assert.AreEqual(connAckPacket.ReasonString, deserialized.ReasonString);
  70. Assert.AreEqual(connAckPacket.ReceiveMaximum, deserialized.ReceiveMaximum);
  71. Assert.AreEqual(connAckPacket.ResponseInformation, deserialized.ResponseInformation);
  72. Assert.AreEqual(connAckPacket.RetainAvailable, deserialized.RetainAvailable);
  73. // Return Code only used in MQTTv3
  74. Assert.AreEqual(connAckPacket.ServerReference, deserialized.ServerReference);
  75. Assert.AreEqual(connAckPacket.AssignedClientIdentifier, deserialized.AssignedClientIdentifier);
  76. Assert.AreEqual(connAckPacket.IsSessionPresent, deserialized.IsSessionPresent);
  77. Assert.AreEqual(connAckPacket.MaximumPacketSize, deserialized.MaximumPacketSize);
  78. Assert.AreEqual(connAckPacket.MaximumQoS, deserialized.MaximumQoS);
  79. Assert.AreEqual(connAckPacket.ServerKeepAlive, deserialized.ServerKeepAlive);
  80. Assert.AreEqual(connAckPacket.SessionExpiryInterval, deserialized.SessionExpiryInterval);
  81. Assert.AreEqual(connAckPacket.SharedSubscriptionAvailable, deserialized.SharedSubscriptionAvailable);
  82. Assert.AreEqual(connAckPacket.SubscriptionIdentifiersAvailable, deserialized.SubscriptionIdentifiersAvailable);
  83. Assert.AreEqual(connAckPacket.TopicAliasMaximum, deserialized.TopicAliasMaximum);
  84. Assert.AreEqual(connAckPacket.WildcardSubscriptionAvailable, deserialized.WildcardSubscriptionAvailable);
  85. CollectionAssert.AreEqual(connAckPacket.UserProperties, deserialized.UserProperties);
  86. }
  87. [TestMethod]
  88. public void Serialize_Full_MqttConnectPacket_V500()
  89. {
  90. var connectPacket = new MqttConnectPacket
  91. {
  92. Username = "Username",
  93. Password = Encoding.UTF8.GetBytes("Password"),
  94. ClientId = "ClientId",
  95. AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
  96. AuthenticationMethod = "AuthenticationMethod",
  97. CleanSession = true,
  98. ReceiveMaximum = 123,
  99. WillFlag = true,
  100. WillTopic = "WillTopic",
  101. WillMessage = Encoding.UTF8.GetBytes("WillMessage"),
  102. WillRetain = true,
  103. KeepAlivePeriod = 456,
  104. MaximumPacketSize = 789,
  105. RequestProblemInformation = true,
  106. RequestResponseInformation = true,
  107. SessionExpiryInterval = 27,
  108. TopicAliasMaximum = 67,
  109. WillContentType = "WillContentType",
  110. WillCorrelationData = Encoding.UTF8.GetBytes("WillCorrelationData"),
  111. WillDelayInterval = 782,
  112. WillQoS = MqttQualityOfServiceLevel.ExactlyOnce,
  113. WillResponseTopic = "WillResponseTopic",
  114. WillMessageExpiryInterval = 542,
  115. WillPayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
  116. UserProperties = new List<MqttUserProperty>
  117. {
  118. new MqttUserProperty("Foo", "Bar")
  119. },
  120. WillUserProperties = new List<MqttUserProperty>
  121. {
  122. new MqttUserProperty("WillFoo", "WillBar")
  123. }
  124. };
  125. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connectPacket, MqttProtocolVersion.V500);
  126. Assert.AreEqual(connectPacket.Username, deserialized.Username);
  127. CollectionAssert.AreEqual(connectPacket.Password, deserialized.Password);
  128. Assert.AreEqual(connectPacket.ClientId, deserialized.ClientId);
  129. CollectionAssert.AreEqual(connectPacket.AuthenticationData, deserialized.AuthenticationData);
  130. Assert.AreEqual(connectPacket.AuthenticationMethod, deserialized.AuthenticationMethod);
  131. Assert.AreEqual(connectPacket.CleanSession, deserialized.CleanSession);
  132. Assert.AreEqual(connectPacket.ReceiveMaximum, deserialized.ReceiveMaximum);
  133. Assert.AreEqual(connectPacket.WillFlag, deserialized.WillFlag);
  134. Assert.AreEqual(connectPacket.WillTopic, deserialized.WillTopic);
  135. CollectionAssert.AreEqual(connectPacket.WillMessage, deserialized.WillMessage);
  136. Assert.AreEqual(connectPacket.WillRetain, deserialized.WillRetain);
  137. Assert.AreEqual(connectPacket.KeepAlivePeriod, deserialized.KeepAlivePeriod);
  138. Assert.AreEqual(connectPacket.MaximumPacketSize, deserialized.MaximumPacketSize);
  139. Assert.AreEqual(connectPacket.RequestProblemInformation, deserialized.RequestProblemInformation);
  140. Assert.AreEqual(connectPacket.RequestResponseInformation, deserialized.RequestResponseInformation);
  141. Assert.AreEqual(connectPacket.SessionExpiryInterval, deserialized.SessionExpiryInterval);
  142. Assert.AreEqual(connectPacket.TopicAliasMaximum, deserialized.TopicAliasMaximum);
  143. Assert.AreEqual(connectPacket.WillContentType, deserialized.WillContentType);
  144. CollectionAssert.AreEqual(connectPacket.WillCorrelationData, deserialized.WillCorrelationData);
  145. Assert.AreEqual(connectPacket.WillDelayInterval, deserialized.WillDelayInterval);
  146. Assert.AreEqual(connectPacket.WillQoS, deserialized.WillQoS);
  147. Assert.AreEqual(connectPacket.WillResponseTopic, deserialized.WillResponseTopic);
  148. Assert.AreEqual(connectPacket.WillMessageExpiryInterval, deserialized.WillMessageExpiryInterval);
  149. Assert.AreEqual(connectPacket.WillPayloadFormatIndicator, deserialized.WillPayloadFormatIndicator);
  150. CollectionAssert.AreEqual(connectPacket.UserProperties, deserialized.UserProperties);
  151. CollectionAssert.AreEqual(connectPacket.WillUserProperties, deserialized.WillUserProperties);
  152. }
  153. [TestMethod]
  154. public void Serialize_Full_MqttDisconnectPacket_V500()
  155. {
  156. var disconnectPacket = new MqttDisconnectPacket
  157. {
  158. ReasonCode = MqttDisconnectReasonCode.NormalDisconnection,
  159. ReasonString = "ReasonString",
  160. ServerReference = "ServerReference",
  161. SessionExpiryInterval = 234,
  162. UserProperties = new List<MqttUserProperty>
  163. {
  164. new MqttUserProperty("Foo", "Bar")
  165. }
  166. };
  167. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(disconnectPacket, MqttProtocolVersion.V500);
  168. Assert.AreEqual(disconnectPacket.ReasonCode, deserialized.ReasonCode);
  169. Assert.AreEqual(disconnectPacket.ReasonString, deserialized.ReasonString);
  170. Assert.AreEqual(disconnectPacket.ServerReference, deserialized.ServerReference);
  171. Assert.AreEqual(disconnectPacket.SessionExpiryInterval, deserialized.SessionExpiryInterval);
  172. CollectionAssert.AreEqual(disconnectPacket.UserProperties, deserialized.UserProperties);
  173. }
  174. [TestMethod]
  175. public void Serialize_Full_MqttPingReqPacket_V500()
  176. {
  177. var pingReqPacket = new MqttPingReqPacket();
  178. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingReqPacket, MqttProtocolVersion.V500);
  179. Assert.IsNotNull(deserialized);
  180. }
  181. [TestMethod]
  182. public void Serialize_Full_MqttPingRespPacket_V500()
  183. {
  184. var pingRespPacket = new MqttPingRespPacket();
  185. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingRespPacket, MqttProtocolVersion.V500);
  186. Assert.IsNotNull(deserialized);
  187. }
  188. [TestMethod]
  189. public void Serialize_Full_MqttPubAckPacket_V500()
  190. {
  191. var pubAckPacket = new MqttPubAckPacket
  192. {
  193. PacketIdentifier = 123,
  194. ReasonCode = MqttPubAckReasonCode.NoMatchingSubscribers,
  195. ReasonString = "ReasonString",
  196. UserProperties = new List<MqttUserProperty>
  197. {
  198. new MqttUserProperty("Foo", "Bar")
  199. }
  200. };
  201. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubAckPacket, MqttProtocolVersion.V500);
  202. Assert.AreEqual(pubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
  203. Assert.AreEqual(pubAckPacket.ReasonCode, deserialized.ReasonCode);
  204. Assert.AreEqual(pubAckPacket.ReasonString, deserialized.ReasonString);
  205. CollectionAssert.AreEqual(pubAckPacket.UserProperties, deserialized.UserProperties);
  206. }
  207. [TestMethod]
  208. public void Serialize_Full_MqttPubCompPacket_V500()
  209. {
  210. var pubCompPacket = new MqttPubCompPacket
  211. {
  212. PacketIdentifier = 123,
  213. ReasonCode = MqttPubCompReasonCode.PacketIdentifierNotFound,
  214. ReasonString = "ReasonString",
  215. UserProperties = new List<MqttUserProperty>
  216. {
  217. new MqttUserProperty("Foo", "Bar")
  218. }
  219. };
  220. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubCompPacket, MqttProtocolVersion.V500);
  221. Assert.AreEqual(pubCompPacket.PacketIdentifier, deserialized.PacketIdentifier);
  222. Assert.AreEqual(pubCompPacket.ReasonCode, deserialized.ReasonCode);
  223. Assert.AreEqual(pubCompPacket.ReasonString, deserialized.ReasonString);
  224. CollectionAssert.AreEqual(pubCompPacket.UserProperties, deserialized.UserProperties);
  225. }
  226. [TestMethod]
  227. public void Serialize_Full_MqttPublishPacket_V500()
  228. {
  229. var publishPacket = new MqttPublishPacket
  230. {
  231. PacketIdentifier = 123,
  232. Dup = true,
  233. Retain = true,
  234. Payload = Encoding.ASCII.GetBytes("Payload"),
  235. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  236. Topic = "Topic",
  237. ResponseTopic = "/Response",
  238. ContentType = "Content-Type",
  239. CorrelationData = Encoding.UTF8.GetBytes("CorrelationData"),
  240. TopicAlias = 27,
  241. SubscriptionIdentifiers = new List<uint>
  242. {
  243. 123
  244. },
  245. MessageExpiryInterval = 38,
  246. PayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
  247. UserProperties = new List<MqttUserProperty>
  248. {
  249. new MqttUserProperty("Foo", "Bar")
  250. }
  251. };
  252. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(publishPacket, MqttProtocolVersion.V500);
  253. Assert.AreEqual(publishPacket.PacketIdentifier, deserialized.PacketIdentifier);
  254. Assert.AreEqual(publishPacket.Dup, deserialized.Dup);
  255. Assert.AreEqual(publishPacket.Retain, deserialized.Retain);
  256. CollectionAssert.AreEqual(publishPacket.Payload, deserialized.Payload);
  257. Assert.AreEqual(publishPacket.QualityOfServiceLevel, deserialized.QualityOfServiceLevel);
  258. Assert.AreEqual(publishPacket.Topic, deserialized.Topic);
  259. Assert.AreEqual(publishPacket.ResponseTopic, deserialized.ResponseTopic);
  260. Assert.AreEqual(publishPacket.ContentType, deserialized.ContentType);
  261. CollectionAssert.AreEqual(publishPacket.CorrelationData, deserialized.CorrelationData);
  262. Assert.AreEqual(publishPacket.TopicAlias, deserialized.TopicAlias);
  263. CollectionAssert.AreEqual(publishPacket.SubscriptionIdentifiers, deserialized.SubscriptionIdentifiers);
  264. Assert.AreEqual(publishPacket.MessageExpiryInterval, deserialized.MessageExpiryInterval);
  265. Assert.AreEqual(publishPacket.PayloadFormatIndicator, deserialized.PayloadFormatIndicator);
  266. CollectionAssert.AreEqual(publishPacket.UserProperties, deserialized.UserProperties);
  267. }
  268. [TestMethod]
  269. public void Serialize_Full_MqttPubRecPacket_V500()
  270. {
  271. var pubRecPacket = new MqttPubRecPacket
  272. {
  273. PacketIdentifier = 123,
  274. ReasonCode = MqttPubRecReasonCode.UnspecifiedError,
  275. ReasonString = "ReasonString",
  276. UserProperties = new List<MqttUserProperty>
  277. {
  278. new MqttUserProperty("Foo", "Bar")
  279. }
  280. };
  281. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRecPacket, MqttProtocolVersion.V500);
  282. Assert.AreEqual(pubRecPacket.PacketIdentifier, deserialized.PacketIdentifier);
  283. Assert.AreEqual(pubRecPacket.ReasonCode, deserialized.ReasonCode);
  284. Assert.AreEqual(pubRecPacket.ReasonString, deserialized.ReasonString);
  285. CollectionAssert.AreEqual(pubRecPacket.UserProperties, deserialized.UserProperties);
  286. }
  287. [TestMethod]
  288. public void Serialize_Full_MqttPubRelPacket_V500()
  289. {
  290. var pubRelPacket = new MqttPubRelPacket
  291. {
  292. PacketIdentifier = 123,
  293. ReasonCode = MqttPubRelReasonCode.PacketIdentifierNotFound,
  294. ReasonString = "ReasonString",
  295. UserProperties = new List<MqttUserProperty>
  296. {
  297. new MqttUserProperty("Foo", "Bar")
  298. }
  299. };
  300. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRelPacket, MqttProtocolVersion.V500);
  301. Assert.AreEqual(pubRelPacket.PacketIdentifier, deserialized.PacketIdentifier);
  302. Assert.AreEqual(pubRelPacket.ReasonCode, deserialized.ReasonCode);
  303. Assert.AreEqual(pubRelPacket.ReasonString, deserialized.ReasonString);
  304. CollectionAssert.AreEqual(pubRelPacket.UserProperties, deserialized.UserProperties);
  305. }
  306. [TestMethod]
  307. public void Serialize_Full_MqttSubAckPacket_V500()
  308. {
  309. var subAckPacket = new MqttSubAckPacket
  310. {
  311. PacketIdentifier = 123,
  312. ReasonString = "ReasonString",
  313. ReasonCodes = new List<MqttSubscribeReasonCode>
  314. {
  315. MqttSubscribeReasonCode.GrantedQoS1
  316. },
  317. UserProperties = new List<MqttUserProperty>
  318. {
  319. new MqttUserProperty("Foo", "Bar")
  320. }
  321. };
  322. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subAckPacket, MqttProtocolVersion.V500);
  323. Assert.AreEqual(subAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
  324. Assert.AreEqual(subAckPacket.ReasonString, deserialized.ReasonString);
  325. Assert.AreEqual(subAckPacket.ReasonCodes.Count, deserialized.ReasonCodes.Count);
  326. Assert.AreEqual(subAckPacket.ReasonCodes[0], deserialized.ReasonCodes[0]);
  327. CollectionAssert.AreEqual(subAckPacket.UserProperties, deserialized.UserProperties);
  328. }
  329. [TestMethod]
  330. public void Serialize_Full_MqttSubscribePacket_V500()
  331. {
  332. var subscribePacket = new MqttSubscribePacket
  333. {
  334. PacketIdentifier = 123,
  335. SubscriptionIdentifier = 456,
  336. TopicFilters = new List<MqttTopicFilter>
  337. {
  338. new MqttTopicFilter
  339. {
  340. Topic = "Topic",
  341. NoLocal = true,
  342. RetainHandling = MqttRetainHandling.SendAtSubscribeIfNewSubscriptionOnly,
  343. RetainAsPublished = true,
  344. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
  345. }
  346. },
  347. UserProperties = new List<MqttUserProperty>
  348. {
  349. new MqttUserProperty("Foo", "Bar")
  350. }
  351. };
  352. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subscribePacket, MqttProtocolVersion.V500);
  353. Assert.AreEqual(subscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
  354. Assert.AreEqual(subscribePacket.SubscriptionIdentifier, deserialized.SubscriptionIdentifier);
  355. Assert.AreEqual(1, deserialized.TopicFilters.Count);
  356. Assert.AreEqual(subscribePacket.TopicFilters[0].Topic, deserialized.TopicFilters[0].Topic);
  357. Assert.AreEqual(subscribePacket.TopicFilters[0].NoLocal, deserialized.TopicFilters[0].NoLocal);
  358. Assert.AreEqual(subscribePacket.TopicFilters[0].RetainHandling, deserialized.TopicFilters[0].RetainHandling);
  359. Assert.AreEqual(subscribePacket.TopicFilters[0].RetainAsPublished, deserialized.TopicFilters[0].RetainAsPublished);
  360. Assert.AreEqual(subscribePacket.TopicFilters[0].QualityOfServiceLevel, deserialized.TopicFilters[0].QualityOfServiceLevel);
  361. CollectionAssert.AreEqual(subscribePacket.UserProperties, deserialized.UserProperties);
  362. }
  363. [TestMethod]
  364. public void Serialize_Full_MqttUnsubAckPacket_V500()
  365. {
  366. var unsubAckPacket = new MqttUnsubAckPacket
  367. {
  368. PacketIdentifier = 123,
  369. ReasonCodes = new List<MqttUnsubscribeReasonCode>
  370. {
  371. MqttUnsubscribeReasonCode.ImplementationSpecificError
  372. },
  373. ReasonString = "ReasonString",
  374. UserProperties = new List<MqttUserProperty>
  375. {
  376. new MqttUserProperty("Foo", "Bar")
  377. }
  378. };
  379. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubAckPacket, MqttProtocolVersion.V500);
  380. Assert.AreEqual(unsubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
  381. Assert.AreEqual(unsubAckPacket.ReasonString, deserialized.ReasonString);
  382. Assert.AreEqual(unsubAckPacket.ReasonCodes.Count, deserialized.ReasonCodes.Count);
  383. Assert.AreEqual(unsubAckPacket.ReasonCodes[0], deserialized.ReasonCodes[0]);
  384. CollectionAssert.AreEqual(unsubAckPacket.UserProperties, deserialized.UserProperties);
  385. }
  386. [TestMethod]
  387. public void Serialize_Full_MqttUnsubscribePacket_V500()
  388. {
  389. var unsubscribePacket = new MqttUnsubscribePacket
  390. {
  391. PacketIdentifier = 123,
  392. TopicFilters = new List<string>
  393. {
  394. "TopicFilter1"
  395. },
  396. UserProperties = new List<MqttUserProperty>
  397. {
  398. new MqttUserProperty("Foo", "Bar")
  399. }
  400. };
  401. var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubscribePacket, MqttProtocolVersion.V500);
  402. Assert.AreEqual(unsubscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
  403. Assert.AreEqual(unsubscribePacket.TopicFilters.Count, deserialized.TopicFilters.Count);
  404. Assert.AreEqual(unsubscribePacket.TopicFilters[0], deserialized.TopicFilters[0]);
  405. CollectionAssert.AreEqual(unsubscribePacket.UserProperties, deserialized.UserProperties);
  406. }
  407. }
  408. }