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 lines
1.6 KiB

  1. using System.Buffers;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MQTTnet.AspNetCore;
  4. using MQTTnet.Packets;
  5. using MQTTnet.Serializer;
  6. namespace MQTTnet.AspNetCore.Tests
  7. {
  8. [TestClass]
  9. public class ReaderExtensionsTest
  10. {
  11. [TestMethod]
  12. public void TestTryDeserialize()
  13. {
  14. var serializer = new MqttPacketSerializer();
  15. var buffer = serializer.Serialize(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.TryDeserialize(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.TryDeserialize(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.TryDeserialize(part, out packet, out consumed, out observed);
  30. Assert.IsFalse(result);
  31. part = sequence; // complete msg should work
  32. result = serializer.TryDeserialize(part, out packet, out consumed, out observed);
  33. Assert.IsTrue(result);
  34. }
  35. }
  36. }