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.
 
 
 
 

433 lines
13 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using MQTTnet.Adapter;
  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, "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. {
  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.ConnectionAccepted
  111. };
  112. SerializeAndCompare(p, "IAIAAA==", 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 SerializeV311_MqttDisconnectPacket()
  126. {
  127. SerializeAndCompare(new MqttDisconnectPacket(), "4AA=");
  128. }
  129. [TestMethod]
  130. public void SerializeV311_MqttPingReqPacket()
  131. {
  132. SerializeAndCompare(new MqttPingReqPacket(), "wAA=");
  133. }
  134. [TestMethod]
  135. public void SerializeV311_MqttPingRespPacket()
  136. {
  137. SerializeAndCompare(new MqttPingRespPacket(), "0AA=");
  138. }
  139. [TestMethod]
  140. public void SerializeV311_MqttPublishPacket()
  141. {
  142. var p = new MqttPublishPacket
  143. {
  144. PacketIdentifier = 123,
  145. Dup = true,
  146. Retain = true,
  147. Payload = Encoding.ASCII.GetBytes("HELLO"),
  148. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  149. Topic = "A/B/C"
  150. };
  151. SerializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  152. }
  153. [TestMethod]
  154. public void DeserializeV311_MqttPublishPacket()
  155. {
  156. var p = new MqttPublishPacket
  157. {
  158. PacketIdentifier = 123,
  159. Dup = true,
  160. Retain = true,
  161. Payload = Encoding.ASCII.GetBytes("HELLO"),
  162. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  163. Topic = "A/B/C"
  164. };
  165. DeserializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  166. }
  167. [TestMethod]
  168. public void SerializeV311_MqttPubAckPacket()
  169. {
  170. var p = new MqttPubAckPacket
  171. {
  172. PacketIdentifier = 123
  173. };
  174. SerializeAndCompare(p, "QAIAew==");
  175. }
  176. [TestMethod]
  177. public void DeserializeV311_MqttPubAckPacket()
  178. {
  179. var p = new MqttPubAckPacket
  180. {
  181. PacketIdentifier = 123
  182. };
  183. DeserializeAndCompare(p, "QAIAew==");
  184. }
  185. [TestMethod]
  186. public void SerializeV311_MqttPubRecPacket()
  187. {
  188. var p = new MqttPubRecPacket
  189. {
  190. PacketIdentifier = 123
  191. };
  192. SerializeAndCompare(p, "UAIAew==");
  193. }
  194. [TestMethod]
  195. public void DeserializeV311_MqttPubRecPacket()
  196. {
  197. var p = new MqttPubRecPacket
  198. {
  199. PacketIdentifier = 123
  200. };
  201. DeserializeAndCompare(p, "UAIAew==");
  202. }
  203. [TestMethod]
  204. public void SerializeV311_MqttPubRelPacket()
  205. {
  206. var p = new MqttPubRelPacket
  207. {
  208. PacketIdentifier = 123
  209. };
  210. SerializeAndCompare(p, "YgIAew==");
  211. }
  212. [TestMethod]
  213. public void DeserializeV311_MqttPubRelPacket()
  214. {
  215. var p = new MqttPubRelPacket
  216. {
  217. PacketIdentifier = 123
  218. };
  219. DeserializeAndCompare(p, "YgIAew==");
  220. }
  221. [TestMethod]
  222. public void SerializeV311_MqttPubCompPacket()
  223. {
  224. var p = new MqttPubCompPacket
  225. {
  226. PacketIdentifier = 123
  227. };
  228. SerializeAndCompare(p, "cAIAew==");
  229. }
  230. [TestMethod]
  231. public void DeserializeV311_MqttPubCompPacket()
  232. {
  233. var p = new MqttPubCompPacket
  234. {
  235. PacketIdentifier = 123
  236. };
  237. DeserializeAndCompare(p, "cAIAew==");
  238. }
  239. [TestMethod]
  240. public void SerializeV311_MqttSubscribePacket()
  241. {
  242. var p = new MqttSubscribePacket
  243. {
  244. PacketIdentifier = 123
  245. };
  246. p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
  247. p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
  248. p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
  249. SerializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  250. }
  251. [TestMethod]
  252. public void DeserializeV311_MqttSubscribePacket()
  253. {
  254. var p = new MqttSubscribePacket
  255. {
  256. PacketIdentifier = 123
  257. };
  258. p.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.ExactlyOnce));
  259. p.TopicFilters.Add(new TopicFilter("1/2/3", MqttQualityOfServiceLevel.AtLeastOnce));
  260. p.TopicFilters.Add(new TopicFilter("x/y/z", MqttQualityOfServiceLevel.AtMostOnce));
  261. DeserializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  262. }
  263. [TestMethod]
  264. public void SerializeV311_MqttSubAckPacket()
  265. {
  266. var p = new MqttSubAckPacket
  267. {
  268. PacketIdentifier = 123
  269. };
  270. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  271. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  272. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  273. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  274. SerializeAndCompare(p, "kAYAewABAoA=");
  275. }
  276. [TestMethod]
  277. public void DeserializeV311_MqttSubAckPacket()
  278. {
  279. var p = new MqttSubAckPacket
  280. {
  281. PacketIdentifier = 123
  282. };
  283. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  284. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  285. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  286. p.SubscribeReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  287. DeserializeAndCompare(p, "kAYAewABAoA=");
  288. }
  289. [TestMethod]
  290. public void SerializeV311_MqttUnsubscribePacket()
  291. {
  292. var p = new MqttUnsubscribePacket
  293. {
  294. PacketIdentifier = 123
  295. };
  296. p.TopicFilters.Add("A/B/C");
  297. p.TopicFilters.Add("1/2/3");
  298. p.TopicFilters.Add("x/y/z");
  299. SerializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  300. }
  301. [TestMethod]
  302. public void DeserializeV311_MqttUnsubscribePacket()
  303. {
  304. var p = new MqttUnsubscribePacket
  305. {
  306. PacketIdentifier = 123
  307. };
  308. p.TopicFilters.Add("A/B/C");
  309. p.TopicFilters.Add("1/2/3");
  310. p.TopicFilters.Add("x/y/z");
  311. DeserializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  312. }
  313. [TestMethod]
  314. public void SerializeV311_MqttUnsubAckPacket()
  315. {
  316. var p = new MqttUnsubAckPacket
  317. {
  318. PacketIdentifier = 123
  319. };
  320. SerializeAndCompare(p, "sAIAew==");
  321. }
  322. [TestMethod]
  323. public void DeserializeV311_MqttUnsubAckPacket()
  324. {
  325. var p = new MqttUnsubAckPacket
  326. {
  327. PacketIdentifier = 123
  328. };
  329. DeserializeAndCompare(p, "sAIAew==");
  330. }
  331. private static void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
  332. {
  333. var serializer = new MqttPacketSerializer { ProtocolVersion = protocolVersion };
  334. var chunks = serializer.Serialize(packet);
  335. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(chunks)));
  336. }
  337. private static void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value)
  338. {
  339. var serializer = new MqttPacketSerializer();
  340. var buffer1 = serializer.Serialize(packet);
  341. using (var headerStream = new MemoryStream(Join(buffer1)))
  342. {
  343. var header = MqttPacketReader.ReadHeaderFromSource(headerStream, CancellationToken.None);
  344. using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.BodyLength))
  345. {
  346. var deserializedPacket = serializer.Deserialize(new ReceivedMqttPacket(header, bodyStream));
  347. var buffer2 = serializer.Serialize(deserializedPacket);
  348. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
  349. }
  350. }
  351. }
  352. private static byte[] Join(IEnumerable<ArraySegment<byte>> chunks)
  353. {
  354. var buffer = new MemoryStream();
  355. foreach (var chunk in chunks)
  356. {
  357. buffer.Write(chunk.Array, chunk.Offset, chunk.Count);
  358. }
  359. return buffer.ToArray();
  360. }
  361. }
  362. }