Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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