Browse Source

fixed dup flag serialization and added roundtrip tests

release/3.x.x
Jan Eggers 6 years ago
parent
commit
7dfb6fdc52
3 changed files with 79 additions and 2 deletions
  1. +1
    -1
      Source/MQTTnet/Packets/MqttPubAckPacket.cs
  2. +1
    -1
      Source/MQTTnet/Serializer/MqttPacketSerializer.cs
  3. +77
    -0
      Tests/MQTTnet.Core.Tests/MqttPacketSerializerTests.cs

+ 1
- 1
Source/MQTTnet/Packets/MqttPubAckPacket.cs View File

@@ -4,7 +4,7 @@
{
public override string ToString()
{
return "PubAck";
return $"PubAck [PacketIdentifier={PacketIdentifier}]";
}
}
}

+ 1
- 1
Source/MQTTnet/Serializer/MqttPacketSerializer.cs View File

@@ -190,7 +190,7 @@ namespace MQTTnet.Serializer

var retain = (receivedMqttPacket.FixedHeader & 0x1) > 0;
var qualityOfServiceLevel = (MqttQualityOfServiceLevel)(receivedMqttPacket.FixedHeader >> 1 & 0x3);
var dup = (receivedMqttPacket.FixedHeader & 0x3) > 0;
var dup = (receivedMqttPacket.FixedHeader & 0x8) > 0;

var topic = receivedMqttPacket.Body.ReadStringWithLengthPrefix();



+ 77
- 0
Tests/MQTTnet.Core.Tests/MqttPacketSerializerTests.cs View File

@@ -199,6 +199,64 @@ namespace MQTTnet.Core.Tests
DeserializeAndCompare(p, "Ow4ABUEvQi9DAHtIRUxMTw==");
}

[TestMethod]
public void DeserializeV311_MqttPublishPacket_Qos1()
{
var p = new MqttPublishPacket
{
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
};

var p2 = Roundtrip(p);

Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
Assert.AreEqual(p.Dup, p2.Dup);
}

[TestMethod]
public void DeserializeV311_MqttPublishPacket_Qos2()
{
var p = new MqttPublishPacket
{
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
PacketIdentifier = 1
};

var p2 = Roundtrip(p);

Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
Assert.AreEqual(p.Dup, p2.Dup);
}

[TestMethod]
public void DeserializeV311_MqttPublishPacket_Qos3()
{
var p = new MqttPublishPacket
{
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce,
PacketIdentifier = 1
};

var p2 = Roundtrip(p);

Assert.AreEqual(p.QualityOfServiceLevel, p2.QualityOfServiceLevel);
Assert.AreEqual(p.Dup, p2.Dup);
}


[TestMethod]
public void DeserializeV311_MqttPublishPacket_DupFalse()
{
var p = new MqttPublishPacket
{
Dup = false,
};

var p2 = Roundtrip(p);

Assert.AreEqual(p.Dup, p2.Dup);
}

[TestMethod]
public void SerializeV311_MqttPubAckPacket()
{
@@ -430,6 +488,25 @@ namespace MQTTnet.Core.Tests
}
}

private static T Roundtrip<T>(T packet, MqttProtocolVersion protocolVersion = MqttProtocolVersion.V311)
where T : MqttBasePacket
{
var serializer = new MqttPacketSerializer { ProtocolVersion = protocolVersion };

var buffer1 = serializer.Serialize(packet);

using (var headerStream = new MemoryStream(Join(buffer1)))
{
var channel = new TestMqttChannel(headerStream);
var header = MqttPacketReader.ReadFixedHeaderAsync(channel, CancellationToken.None).GetAwaiter().GetResult();

using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.RemainingLength))
{
return (T)serializer.Deserialize(new ReceivedMqttPacket(header.Flags, new MqttPacketBodyReader(bodyStream.ToArray(), 0)));
}
}
}

private static byte[] Join(params ArraySegment<byte>[] chunks)
{
var buffer = new MemoryStream();


Loading…
Cancel
Save