Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

458 lignes
13 KiB

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