Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DefaultMqttV311PacketSerializerTests.cs 13 KiB

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