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.
 
 
 
 

46 regels
1.6 KiB

  1. #if NETCOREAPP
  2. using System.Buffers;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Formatter;
  5. using MQTTnet.Packets;
  6. namespace MQTTnet.AspNetCore.Tests
  7. {
  8. [TestClass]
  9. public class ReaderExtensionsTest
  10. {
  11. [TestMethod]
  12. public void TestTryDeserialize()
  13. {
  14. var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311);
  15. var buffer = serializer.Encode(new MqttPublishPacket() {Topic = "a", Payload = new byte[5]});
  16. var sequence = new ReadOnlySequence<byte>(buffer.Array, buffer.Offset, buffer.Count);
  17. var part = sequence;
  18. MqttBasePacket packet;
  19. var consumed = part.Start;
  20. var observed = part.Start;
  21. var result = false;
  22. part = sequence.Slice(sequence.Start, 0); // empty message should fail
  23. result = serializer.TryDecode(part, out packet, out consumed, out observed);
  24. Assert.IsFalse(result);
  25. part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
  26. result = serializer.TryDecode(part, out packet, out consumed, out observed);
  27. Assert.IsFalse(result);
  28. part = sequence.Slice(sequence.Start, 4); // partial body should fail
  29. result = serializer.TryDecode(part, out packet, out consumed, out observed);
  30. Assert.IsFalse(result);
  31. part = sequence; // complete msg should work
  32. result = serializer.TryDecode(part, out packet, out consumed, out observed);
  33. Assert.IsTrue(result);
  34. }
  35. }
  36. }
  37. #endif