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.
 
 
 
 

49 lines
1.8 KiB

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