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.
 
 
 
 

51 rivejä
1.9 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #if NETCOREAPP3_1
  5. using System.Buffers;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using MQTTnet.AspNetCore;
  8. using MQTTnet.Formatter;
  9. using MQTTnet.Packets;
  10. namespace MQTTnet.AspNetCore.Tests
  11. {
  12. [TestClass]
  13. public class ReaderExtensionsTest
  14. {
  15. [TestMethod]
  16. public void TestTryDeserialize()
  17. {
  18. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311, new MqttBufferWriter(4096, 65535));
  19. var buffer = serializer.Encode(new MqttPublishPacket {Topic = "a", Payload = new byte[5]}).Join();
  20. var sequence = new ReadOnlySequence<byte>(buffer.Array, buffer.Offset, buffer.Count);
  21. var part = sequence;
  22. MqttPacket packet;
  23. var consumed = part.Start;
  24. var observed = part.Start;
  25. var result = false;
  26. var read = 0;
  27. part = sequence.Slice(sequence.Start, 0); // empty message should fail
  28. result = serializer.TryDecode(part, out packet, out consumed, out observed, out read);
  29. Assert.IsFalse(result);
  30. part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
  31. result = serializer.TryDecode(part, out packet, out consumed, out observed, out read);
  32. Assert.IsFalse(result);
  33. part = sequence.Slice(sequence.Start, 4); // partial body should fail
  34. result = serializer.TryDecode(part, out packet, out consumed, out observed, out read);
  35. Assert.IsFalse(result);
  36. part = sequence; // complete msg should work
  37. result = serializer.TryDecode(part, out packet, out consumed, out observed, out read);
  38. Assert.IsTrue(result);
  39. }
  40. }
  41. }
  42. #endif