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.
 
 
 
 

453 lines
13 KiB

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