From 8856a7aa344ddbc05e1916ee520df8a130789c95 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 26 Jan 2018 23:56:22 +0100 Subject: [PATCH] Fix broken package reader --- .../Adapter/MqttChannelAdapter.cs | 2 +- Tests/MQTTnet.Core.Tests/MqttClientTests.cs | 3 +- Tests/MQTTnet.Core.Tests/MqttServerTests.cs | 37 ++++++++++++++++++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs index 4b8bafd..b94cf61 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs @@ -137,7 +137,7 @@ namespace MQTTnet.Adapter return new ReceivedMqttPacket(header, new MemoryStream(new byte[0], false)); } - var body = header.BodyLength <= ReadBufferSize ? new MemoryStream(new byte[header.BodyLength]) : new MemoryStream(); + var body = header.BodyLength <= ReadBufferSize ? new MemoryStream(header.BodyLength) : new MemoryStream(); var buffer = new byte[ReadBufferSize]; while (body.Length < header.BodyLength) diff --git a/Tests/MQTTnet.Core.Tests/MqttClientTests.cs b/Tests/MQTTnet.Core.Tests/MqttClientTests.cs index d434325..1334a80 100644 --- a/Tests/MQTTnet.Core.Tests/MqttClientTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttClientTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Net.Sockets; +using System.Net.Sockets; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Client; diff --git a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs index 5081dfc..26c07df 100644 --- a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs @@ -299,7 +299,7 @@ namespace MQTTnet.Core.Tests var serverAdapter = new TestMqttServerAdapter(); var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger()); - + try { var options = new MqttServerOptions { ApplicationMessageInterceptor = Interceptor }; @@ -330,6 +330,41 @@ namespace MQTTnet.Core.Tests } } + [TestMethod] + public async Task MqttServer_Body() + { + var serverAdapter = new TestMqttServerAdapter(); + var s = new MqttFactory().CreateMqttServer(new[] { serverAdapter }, new MqttNetLogger()); + + var bodyIsMatching = false; + try + { + await s.StartAsync(new MqttServerOptions()); + + var c1 = await serverAdapter.ConnectTestClient(s, "c1"); + var c2 = await serverAdapter.ConnectTestClient(s, "c2"); + + c1.ApplicationMessageReceived += (_, e) => + { + if (Encoding.UTF8.GetString(e.ApplicationMessage.Payload) == "The body") + { + bodyIsMatching = true; + } + }; + + await c1.SubscribeAsync("A", MqttQualityOfServiceLevel.AtMostOnce); + await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("A").WithPayload(Encoding.UTF8.GetBytes("The body")).Build()); + + await Task.Delay(500); + } + finally + { + await s.StopAsync(); + } + + Assert.IsTrue(bodyIsMatching); + } + private class TestStorage : IMqttServerStorage { private IList _messages = new List();