您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

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