Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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