You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

685 lines
23 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using MQTTnet.Adapter;
  9. using MQTTnet.Exceptions;
  10. using MQTTnet.Formatter;
  11. using MQTTnet.Formatter.V3;
  12. using MQTTnet.Formatter.V5;
  13. using MQTTnet.Internal;
  14. using MQTTnet.Packets;
  15. using MQTTnet.Protocol;
  16. namespace MQTTnet.Tests
  17. {
  18. [TestClass]
  19. public class MqttPacketSerializer_Tests
  20. {
  21. [TestMethod]
  22. public void DetectVersionFromMqttConnectPacket()
  23. {
  24. var p = new MqttConnectPacket
  25. {
  26. ClientId = "XYZ",
  27. Password = Encoding.UTF8.GetBytes("PASS"),
  28. Username = "USER",
  29. KeepAlivePeriod = 123,
  30. CleanSession = true
  31. };
  32. var adapter = new MqttPacketFormatterAdapter(WriterFactory());
  33. Assert.AreEqual(MqttProtocolVersion.V310, DeserializeAndDetectVersion(adapter, Serialize(p, MqttProtocolVersion.V310)));
  34. Assert.AreEqual(MqttProtocolVersion.V311, DeserializeAndDetectVersion(adapter, Serialize(p, MqttProtocolVersion.V311)));
  35. Assert.AreEqual(MqttProtocolVersion.V500, DeserializeAndDetectVersion(adapter, Serialize(p, MqttProtocolVersion.V500)));
  36. var ex = Assert.ThrowsException<MqttProtocolViolationException>(() => DeserializeAndDetectVersion(adapter, WriterFactory().AddMqttHeader(MqttControlPacketType.Connect, new byte[0])));
  37. Assert.AreEqual("CONNECT packet must have at least 7 bytes.", ex.Message);
  38. ex = Assert.ThrowsException<MqttProtocolViolationException>(() => DeserializeAndDetectVersion(adapter, WriterFactory().AddMqttHeader(MqttControlPacketType.Connect, new byte[7])));
  39. Assert.AreEqual("Protocol '' not supported.", ex.Message);
  40. ex = Assert.ThrowsException<MqttProtocolViolationException>(() => DeserializeAndDetectVersion(adapter, WriterFactory().AddMqttHeader(MqttControlPacketType.Connect, new byte[] { 255, 255, 0,0,0,0,0 })));
  41. Assert.AreEqual("Expected at least 65537 bytes but there are only 7 bytes", ex.Message);
  42. }
  43. [TestMethod]
  44. public void SerializeV310_MqttConnectPacket()
  45. {
  46. var p = new MqttConnectPacket
  47. {
  48. ClientId = "XYZ",
  49. Password = Encoding.UTF8.GetBytes("PASS"),
  50. Username = "USER",
  51. KeepAlivePeriod = 123,
  52. CleanSession = true
  53. };
  54. SerializeAndCompare(p, "EB0ABk1RSXNkcAPCAHsAA1hZWgAEVVNFUgAEUEFTUw==", MqttProtocolVersion.V310);
  55. }
  56. [TestMethod]
  57. public void SerializeV311_MqttConnectPacket()
  58. {
  59. var p = new MqttConnectPacket
  60. {
  61. ClientId = "XYZ",
  62. Password = Encoding.UTF8.GetBytes("PASS"),
  63. Username = "USER",
  64. KeepAlivePeriod = 123,
  65. CleanSession = true
  66. };
  67. SerializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
  68. }
  69. [TestMethod]
  70. public void SerializeV311_MqttConnectPacketWithWillMessage()
  71. {
  72. var p = new MqttConnectPacket
  73. {
  74. ClientId = "XYZ",
  75. Password = Encoding.UTF8.GetBytes("PASS"),
  76. Username = "USER",
  77. KeepAlivePeriod = 123,
  78. CleanSession = true,
  79. WillMessage = new MqttApplicationMessage
  80. {
  81. Topic = "My/last/will",
  82. Payload = Encoding.UTF8.GetBytes("Good byte."),
  83. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  84. Retain = true
  85. }
  86. };
  87. SerializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
  88. }
  89. [TestMethod]
  90. public void DeserializeV311_MqttConnectPacket()
  91. {
  92. var p = new MqttConnectPacket
  93. {
  94. ClientId = "XYZ",
  95. Password = Encoding.UTF8.GetBytes("PASS"),
  96. Username = "USER",
  97. KeepAlivePeriod = 123,
  98. CleanSession = true
  99. };
  100. DeserializeAndCompare(p, "EBsABE1RVFQEwgB7AANYWVoABFVTRVIABFBBU1M=");
  101. }
  102. [TestMethod]
  103. public void DeserializeV311_MqttConnectPacketWithWillMessage()
  104. {
  105. var p = new MqttConnectPacket
  106. {
  107. ClientId = "XYZ",
  108. Password = Encoding.UTF8.GetBytes("PASS"),
  109. Username = "USER",
  110. KeepAlivePeriod = 123,
  111. CleanSession = true,
  112. WillMessage = new MqttApplicationMessage
  113. {
  114. Topic = "My/last/will",
  115. Payload = Encoding.UTF8.GetBytes("Good byte."),
  116. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  117. Retain = true
  118. }
  119. };
  120. DeserializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
  121. }
  122. [TestMethod]
  123. public void SerializeV311_MqttConnAckPacket()
  124. {
  125. var p = new MqttConnAckPacket
  126. {
  127. IsSessionPresent = true,
  128. ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  129. };
  130. SerializeAndCompare(p, "IAIBBQ==");
  131. }
  132. [TestMethod]
  133. public void SerializeV310_MqttConnAckPacket()
  134. {
  135. var p = new MqttConnAckPacket
  136. {
  137. ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  138. };
  139. SerializeAndCompare(p, "IAIABQ==", MqttProtocolVersion.V310);
  140. }
  141. [TestMethod]
  142. public void DeserializeV311_MqttConnAckPacket()
  143. {
  144. var p = new MqttConnAckPacket
  145. {
  146. IsSessionPresent = true,
  147. ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  148. };
  149. DeserializeAndCompare(p, "IAIBBQ==");
  150. }
  151. [TestMethod]
  152. public void DeserializeV310_MqttConnAckPacket()
  153. {
  154. var p = new MqttConnAckPacket
  155. {
  156. ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized
  157. };
  158. DeserializeAndCompare(p, "IAIABQ==", MqttProtocolVersion.V310);
  159. }
  160. [TestMethod]
  161. public void Serialize_LargePacket()
  162. {
  163. var serializer = new MqttV311PacketFormatter(WriterFactory());
  164. const int payloadLength = 80000;
  165. var payload = new byte[payloadLength];
  166. var value = 0;
  167. for (var i = 0; i < payloadLength; i++)
  168. {
  169. if (value > 255)
  170. {
  171. value = 0;
  172. }
  173. payload[i] = (byte)value;
  174. }
  175. var publishPacket = new MqttPublishPacket
  176. {
  177. Topic = "abcdefghijklmnopqrstuvwxyz0123456789",
  178. Payload = payload
  179. };
  180. var buffer = serializer.Encode(publishPacket);
  181. var testChannel = new TestMqttChannel(new MemoryStream(buffer.Array, buffer.Offset, buffer.Count));
  182. var header = new MqttPacketReader(testChannel).ReadFixedHeaderAsync(
  183. new byte[2],
  184. CancellationToken.None).GetAwaiter().GetResult().FixedHeader;
  185. var eof = buffer.Offset + buffer.Count;
  186. var receivedPacket = new ReceivedMqttPacket(
  187. header.Flags,
  188. new MqttPacketBodyReader(buffer.Array, eof - header.RemainingLength, buffer.Count + buffer.Offset),
  189. 0);
  190. var packet = (MqttPublishPacket)serializer.Decode(receivedPacket);
  191. Assert.AreEqual(publishPacket.Topic, packet.Topic);
  192. Assert.IsTrue(publishPacket.Payload.SequenceEqual(packet.Payload));
  193. }
  194. [TestMethod]
  195. public void SerializeV311_MqttDisconnectPacket()
  196. {
  197. SerializeAndCompare(new MqttDisconnectPacket(), "4AA=");
  198. }
  199. [TestMethod]
  200. public void SerializeV311_MqttPingReqPacket()
  201. {
  202. SerializeAndCompare(new MqttPingReqPacket(), "wAA=");
  203. }
  204. [TestMethod]
  205. public void SerializeV311_MqttPingRespPacket()
  206. {
  207. SerializeAndCompare(new MqttPingRespPacket(), "0AA=");
  208. }
  209. [TestMethod]
  210. public void SerializeV311_MqttPublishPacket()
  211. {
  212. var p = new MqttPublishPacket
  213. {
  214. PacketIdentifier = 123,
  215. Dup = true,
  216. Retain = true,
  217. Payload = Encoding.ASCII.GetBytes("HELLO"),
  218. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  219. Topic = "A/B/C"
  220. };
  221. SerializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  222. }
  223. [TestMethod]
  224. public void SerializeV500_MqttPublishPacket()
  225. {
  226. var prop = new MqttPublishPacketProperties {UserProperties = new List<MqttUserProperty>()};
  227. prop.ResponseTopic = "/Response";
  228. prop.UserProperties.Add(new MqttUserProperty("Foo", "Bar"));
  229. var p = new MqttPublishPacket
  230. {
  231. PacketIdentifier = 123,
  232. Dup = true,
  233. Retain = true,
  234. Payload = Encoding.ASCII.GetBytes("HELLO"),
  235. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  236. Topic = "A/B/C",
  237. Properties = prop
  238. };
  239. var deserialized = Roundtrip(p, MqttProtocolVersion.V500);
  240. Assert.AreEqual(prop.ResponseTopic, deserialized.Properties.ResponseTopic);
  241. Assert.IsTrue(deserialized.Properties.UserProperties.Any(x => x.Name == "Foo"));
  242. }
  243. [TestMethod]
  244. public void SerializeV500_MqttPublishPacket_CorrelationData()
  245. {
  246. var data = "123456789";
  247. var req = new MqttApplicationMessageBuilder()
  248. .WithTopic("Foo")
  249. .WithResponseTopic($"_")
  250. .WithCorrelationData(Guid.NewGuid().ToByteArray())
  251. .WithPayload(data)
  252. .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
  253. .Build();
  254. var p = new MqttV500DataConverter().CreatePublishPacket(req);
  255. var deserialized = Roundtrip(p, MqttProtocolVersion.V500);
  256. Assert.IsTrue(p.Payload.SequenceEqual(deserialized.Payload));
  257. }
  258. [TestMethod]
  259. public void DeserializeV311_MqttPublishPacket()
  260. {
  261. var p = new MqttPublishPacket
  262. {
  263. PacketIdentifier = 123,
  264. Dup = true,
  265. Retain = true,
  266. Payload = Encoding.ASCII.GetBytes("HELLO"),
  267. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  268. Topic = "A/B/C"
  269. };
  270. DeserializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
  271. }
  272. [TestMethod]
  273. public void DeserializeV311_MqttPublishPacket_Qos1()
  274. {
  275. var p = new MqttPublishPacket
  276. {
  277. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
  278. };
  279. var p2 = Roundtrip(p);
  280. Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
  281. Assert.AreEqual(p.Dup, p2.Dup);
  282. }
  283. [TestMethod]
  284. public void DeserializeV311_MqttPublishPacket_Qos2()
  285. {
  286. var p = new MqttPublishPacket
  287. {
  288. QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
  289. PacketIdentifier = 1
  290. };
  291. var p2 = Roundtrip(p);
  292. Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
  293. Assert.AreEqual(p.Dup, p2.Dup);
  294. }
  295. [TestMethod]
  296. public void DeserializeV311_MqttPublishPacket_Qos3()
  297. {
  298. var p = new MqttPublishPacket
  299. {
  300. QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce,
  301. PacketIdentifier = 1
  302. };
  303. var p2 = Roundtrip(p);
  304. Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
  305. Assert.AreEqual(p.Dup, p2.Dup);
  306. }
  307. [TestMethod]
  308. public void DeserializeV311_MqttPublishPacket_DupFalse()
  309. {
  310. var p = new MqttPublishPacket
  311. {
  312. Dup = false,
  313. };
  314. var p2 = Roundtrip(p);
  315. Assert.AreEqual(p.Dup, p2.Dup);
  316. }
  317. [TestMethod]
  318. public void SerializeV311_MqttPubAckPacket()
  319. {
  320. var p = new MqttPubAckPacket
  321. {
  322. PacketIdentifier = 123
  323. };
  324. SerializeAndCompare(p, "QAIAew==");
  325. }
  326. [TestMethod]
  327. public void DeserializeV311_MqttPubAckPacket()
  328. {
  329. var p = new MqttPubAckPacket
  330. {
  331. PacketIdentifier = 123
  332. };
  333. DeserializeAndCompare(p, "QAIAew==");
  334. }
  335. [TestMethod]
  336. public void SerializeV311_MqttPubRecPacket()
  337. {
  338. var p = new MqttPubRecPacket
  339. {
  340. PacketIdentifier = 123
  341. };
  342. SerializeAndCompare(p, "UAIAew==");
  343. }
  344. [TestMethod]
  345. public void DeserializeV311_MqttPubRecPacket()
  346. {
  347. var p = new MqttPubRecPacket
  348. {
  349. PacketIdentifier = 123
  350. };
  351. DeserializeAndCompare(p, "UAIAew==");
  352. }
  353. [TestMethod]
  354. public void SerializeV311_MqttPubRelPacket()
  355. {
  356. var p = new MqttPubRelPacket
  357. {
  358. PacketIdentifier = 123
  359. };
  360. SerializeAndCompare(p, "YgIAew==");
  361. }
  362. [TestMethod]
  363. public void DeserializeV311_MqttPubRelPacket()
  364. {
  365. var p = new MqttPubRelPacket
  366. {
  367. PacketIdentifier = 123
  368. };
  369. DeserializeAndCompare(p, "YgIAew==");
  370. }
  371. [TestMethod]
  372. public void SerializeV311_MqttPubCompPacket()
  373. {
  374. var p = new MqttPubCompPacket
  375. {
  376. PacketIdentifier = 123
  377. };
  378. SerializeAndCompare(p, "cAIAew==");
  379. }
  380. [TestMethod]
  381. public void DeserializeV311_MqttPubCompPacket()
  382. {
  383. var p = new MqttPubCompPacket
  384. {
  385. PacketIdentifier = 123
  386. };
  387. DeserializeAndCompare(p, "cAIAew==");
  388. }
  389. [TestMethod]
  390. public void SerializeV311_MqttSubscribePacket()
  391. {
  392. var p = new MqttSubscribePacket
  393. {
  394. PacketIdentifier = 123
  395. };
  396. p.TopicFilters.Add(new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce });
  397. p.TopicFilters.Add(new MqttTopicFilter { Topic = "1/2/3", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
  398. p.TopicFilters.Add(new MqttTopicFilter { Topic = "x/y/z", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  399. SerializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  400. }
  401. [TestMethod]
  402. public void DeserializeV311_MqttSubscribePacket()
  403. {
  404. var p = new MqttSubscribePacket
  405. {
  406. PacketIdentifier = 123
  407. };
  408. p.TopicFilters.Add(new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce });
  409. p.TopicFilters.Add(new MqttTopicFilter { Topic = "1/2/3", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
  410. p.TopicFilters.Add(new MqttTopicFilter { Topic = "x/y/z", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
  411. DeserializeAndCompare(p, "ghoAewAFQS9CL0MCAAUxLzIvMwEABXgveS96AA==");
  412. }
  413. [TestMethod]
  414. public void SerializeV311_MqttSubAckPacket()
  415. {
  416. var p = new MqttSubAckPacket
  417. {
  418. PacketIdentifier = 123
  419. };
  420. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  421. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  422. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  423. p.ReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  424. SerializeAndCompare(p, "kAYAewABAoA=");
  425. }
  426. [TestMethod]
  427. public void DeserializeV311_MqttSubAckPacket()
  428. {
  429. var p = new MqttSubAckPacket
  430. {
  431. PacketIdentifier = 123
  432. };
  433. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS0);
  434. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS1);
  435. p.ReturnCodes.Add(MqttSubscribeReturnCode.SuccessMaximumQoS2);
  436. p.ReturnCodes.Add(MqttSubscribeReturnCode.Failure);
  437. DeserializeAndCompare(p, "kAYAewABAoA=");
  438. }
  439. [TestMethod]
  440. public void SerializeV311_MqttUnsubscribePacket()
  441. {
  442. var p = new MqttUnsubscribePacket
  443. {
  444. PacketIdentifier = 123
  445. };
  446. p.TopicFilters.Add("A/B/C");
  447. p.TopicFilters.Add("1/2/3");
  448. p.TopicFilters.Add("x/y/z");
  449. SerializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  450. }
  451. [TestMethod]
  452. public void DeserializeV311_MqttUnsubscribePacket()
  453. {
  454. var p = new MqttUnsubscribePacket
  455. {
  456. PacketIdentifier = 123
  457. };
  458. p.TopicFilters.Add("A/B/C");
  459. p.TopicFilters.Add("1/2/3");
  460. p.TopicFilters.Add("x/y/z");
  461. DeserializeAndCompare(p, "ohcAewAFQS9CL0MABTEvMi8zAAV4L3kveg==");
  462. }
  463. [TestMethod]
  464. public void SerializeV311_MqttUnsubAckPacket()
  465. {
  466. var p = new MqttUnsubAckPacket
  467. {
  468. PacketIdentifier = 123
  469. };
  470. SerializeAndCompare(p, "sAIAew==");
  471. }
  472. [TestMethod]
  473. public void DeserializeV311_MqttUnsubAckPacket()
  474. {
  475. var p = new MqttUnsubAckPacket
  476. {
  477. PacketIdentifier = 123
  478. };
  479. DeserializeAndCompare(p, "sAIAew==");
  480. }
  481. private void SerializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
  482. {
  483. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Serialize(packet, protocolVersion)));
  484. }
  485. private byte[] Serialize(MqttBasePacket packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
  486. {
  487. var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, WriterFactory());
  488. return Join(serializer.Encode(packet));
  489. }
  490. protected virtual IMqttPacketWriter WriterFactory()
  491. {
  492. return new MqttPacketWriter();
  493. }
  494. protected virtual IMqttPacketBodyReader ReaderFactory(byte[] data)
  495. {
  496. return new MqttPacketBodyReader(data, 0, data.Length);
  497. }
  498. private void DeserializeAndCompare(MqttBasePacket packet, string expectedBase64Value, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
  499. {
  500. var writer = WriterFactory();
  501. var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer);
  502. var buffer1 = serializer.Encode(packet);
  503. using (var headerStream = new MemoryStream(Join(buffer1)))
  504. {
  505. var channel = new TestMqttChannel(headerStream);
  506. var fixedHeader = new byte[2];
  507. var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader;
  508. using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength))
  509. {
  510. var reader = ReaderFactory(bodyStream.ToArray());
  511. var deserializedPacket = serializer.Decode(new ReceivedMqttPacket(header.Flags, reader, 0));
  512. var buffer2 = serializer.Encode(deserializedPacket);
  513. Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
  514. }
  515. }
  516. }
  517. private T Roundtrip<T>(T packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
  518. where T : MqttBasePacket
  519. {
  520. var writer = WriterFactory();
  521. var serializer = MqttPacketFormatterAdapter.GetMqttPacketFormatter(protocolVersion, writer);
  522. var buffer1 = serializer.Encode(packet);
  523. using (var headerStream = new MemoryStream(Join(buffer1)))
  524. {
  525. var channel = new TestMqttChannel(headerStream);
  526. var fixedHeader = new byte[2];
  527. var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader;
  528. using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, (int)header.RemainingLength))
  529. {
  530. var reader = ReaderFactory(bodyStream.ToArray());
  531. return (T)serializer.Decode(new ReceivedMqttPacket(header.Flags, reader, 0));
  532. }
  533. }
  534. }
  535. private MqttProtocolVersion DeserializeAndDetectVersion(MqttPacketFormatterAdapter adapter, byte[] buffer)
  536. {
  537. using (var headerStream = new MemoryStream(buffer))
  538. {
  539. var channel = new TestMqttChannel(headerStream);
  540. var fixedHeader = new byte[2];
  541. var header = new MqttPacketReader(channel).ReadFixedHeaderAsync(fixedHeader, CancellationToken.None).GetAwaiter().GetResult().FixedHeader;
  542. using (var bodyStream = new MemoryStream(buffer, (int)headerStream.Position, (int)header.RemainingLength))
  543. {
  544. var reader = ReaderFactory(bodyStream.ToArray());
  545. var packet = new ReceivedMqttPacket(header.Flags, reader, 0);
  546. adapter.DetectProtocolVersion(packet);
  547. return adapter.ProtocolVersion;
  548. }
  549. }
  550. }
  551. private static byte[] Join(params ArraySegment<byte>[] chunks)
  552. {
  553. var buffer = new MemoryStream();
  554. foreach (var chunk in chunks)
  555. {
  556. buffer.Write(chunk.Array, chunk.Offset, chunk.Count);
  557. }
  558. return buffer.ToArray();
  559. }
  560. }
  561. }