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.
 
 
 
 

442 regels
13 KiB

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