mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 01:03:01 +08:00
* Make use of language features line AsyncDispose, init etc. * Fix build errors * Improve Unit Tests * Improve Unit Tests * Improve Unit Tests * Update samples * Remove obsolete benchmarks * Code cleanup * Improve unit tests * Improve Unit Tests * Fix Unit Tests * Improve Unit Tests * Improve Unit Tests * Restore unit tests * Exclude load tests from unit tests
41 lines
1.5 KiB
C#
41 lines
1.5 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Buffers;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using MQTTnet.AspNetCore;
|
|
using MQTTnet.Formatter;
|
|
using MQTTnet.Packets;
|
|
|
|
namespace MQTTnet.Tests.ASP;
|
|
|
|
[TestClass]
|
|
public sealed class ReaderExtensionsTest
|
|
{
|
|
[TestMethod]
|
|
public void TestTryDeserialize()
|
|
{
|
|
var serializer = new MqttPacketFormatterAdapter(MqttProtocolVersion.V311, new MqttBufferWriter(4096, 65535));
|
|
|
|
var buffer = serializer.Encode(new MqttPublishPacket { Topic = "a", PayloadSegment = new byte[5] }).Join();
|
|
|
|
var sequence = new ReadOnlySequence<byte>(buffer.Array, buffer.Offset, buffer.Count);
|
|
|
|
var part = sequence.Slice(sequence.Start, 0); // empty message should fail
|
|
var result = serializer.TryDecode(part, out _, out _, out _, out _);
|
|
Assert.IsFalse(result);
|
|
|
|
part = sequence.Slice(sequence.Start, 1); // partial fixed header should fail
|
|
result = serializer.TryDecode(part, out _, out _, out _, out _);
|
|
Assert.IsFalse(result);
|
|
|
|
part = sequence.Slice(sequence.Start, 4); // partial body should fail
|
|
result = serializer.TryDecode(part, out _, out _, out _, out _);
|
|
Assert.IsFalse(result);
|
|
|
|
part = sequence; // complete msg should work
|
|
result = serializer.TryDecode(part, out _, out _, out _, out _);
|
|
Assert.IsTrue(result);
|
|
}
|
|
} |