mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Use latest language features (#2176)
* 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
This commit is contained in:
@@ -9,303 +9,303 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Tests.Formatter
|
||||
namespace MQTTnet.Tests.Formatter;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class MqttBufferReader_Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class MqttBufferReader_Tests
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException), "Expected at least 4 bytes but there are only 3 bytes")]
|
||||
public void Fire_Exception_If_Not_Enough_Data()
|
||||
{
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException), "Expected at least 4 bytes but there are only 3 bytes")]
|
||||
public void Fire_Exception_If_Not_Enough_Data()
|
||||
var buffer = new byte[] { 0, 1, 2 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
reader.SetBuffer(buffer, 0, 3);
|
||||
|
||||
// 1 byte is missing.
|
||||
reader.ReadFourByteInteger();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException), "Expected at least 4 bytes but there are only 3 bytes")]
|
||||
public void Fire_Exception_If_Not_Enough_Data_With_Longer_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
reader.SetBuffer(buffer, 0, 3);
|
||||
|
||||
// 1 byte is missing.
|
||||
reader.ReadFourByteInteger();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Is_EndOfStream_Without_Buffer()
|
||||
{
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
Assert.IsTrue(reader.EndOfStream);
|
||||
Assert.AreEqual(0, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Read_Remaining_Data_From_Larger_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
|
||||
// This should only read 5 bytes even if more data is in the buffer
|
||||
// due to custom bounds.
|
||||
var remainingData = reader.ReadRemainingData();
|
||||
|
||||
Assert.IsTrue(reader.EndOfStream);
|
||||
Assert.AreEqual(0, reader.BytesLeft);
|
||||
Assert.AreEqual(5, remainingData.Length);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Read_Various_Positions_and_Offsets()
|
||||
{
|
||||
const int numBufferElements = 1000;
|
||||
const int numTestSequences = 10000;
|
||||
|
||||
const string testString = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
var rndSeed = Environment.TickCount;
|
||||
// rndSeed = ...; // assign random seed here if there is an error so that it can be repeated
|
||||
var rnd = new Random(rndSeed);
|
||||
// rndSeed is printed when an assert fails
|
||||
|
||||
var bufferElements = new List<ElementReference>();
|
||||
|
||||
byte[] elementBuffer;
|
||||
|
||||
// Fill a buffer with strings or numbers in big endian format
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
reader.SetBuffer(buffer, 0, 3);
|
||||
|
||||
// 1 byte is missing.
|
||||
reader.ReadFourByteInteger();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException), "Expected at least 4 bytes but there are only 3 bytes")]
|
||||
public void Fire_Exception_If_Not_Enough_Data_With_Longer_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
reader.SetBuffer(buffer, 0, 3);
|
||||
|
||||
// 1 byte is missing.
|
||||
reader.ReadFourByteInteger();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Is_EndOfStream_Without_Buffer()
|
||||
{
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
Assert.IsTrue(reader.EndOfStream);
|
||||
Assert.AreEqual(0, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Read_Remaining_Data_From_Larger_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
|
||||
// This should only read 5 bytes even if more data is in the buffer
|
||||
// due to custom bounds.
|
||||
var remainingData = reader.ReadRemainingData();
|
||||
|
||||
Assert.IsTrue(reader.EndOfStream);
|
||||
Assert.AreEqual(0, reader.BytesLeft);
|
||||
Assert.AreEqual(5, remainingData.Length);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Read_Various_Positions_and_Offsets()
|
||||
{
|
||||
const int NumBufferElements = 1000;
|
||||
const int NumTestSequences = 10000;
|
||||
|
||||
const string TestString = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
var rndSeed = Environment.TickCount;
|
||||
// rndSeed = ...; // assign random seed here if there is an error so that it can be repeated
|
||||
var rnd = new Random(rndSeed);
|
||||
// rndSeed is printed when an assert fails
|
||||
|
||||
var bufferElements = new List<ElementReference>();
|
||||
|
||||
byte[] elementBuffer;
|
||||
|
||||
// Fill a buffer with strings or numbers in big endian format
|
||||
using (var ms = new MemoryStream())
|
||||
using (var bw = new BinaryWriter(ms))
|
||||
{
|
||||
using (var bw = new BinaryWriter(ms))
|
||||
var bufferOffset = 0;
|
||||
|
||||
for (var i = 0; i < numBufferElements; i++)
|
||||
{
|
||||
var bufferOffset = 0;
|
||||
var nextElementType = (ElementReference.BufferElementType)rnd.Next(ElementReference.NumBufferElementTypes);
|
||||
|
||||
for (var i = 0; i < NumBufferElements; i++)
|
||||
{
|
||||
var nextElementType = (ElementReference.BufferElementType)rnd.Next(ElementReference.NumBufferElementTypes);
|
||||
|
||||
byte[] elementBytes = null;
|
||||
uint elementNumberValue = 0; // all element number values fit into uint
|
||||
string elementStringValue = null;
|
||||
var elementSize = 0;
|
||||
|
||||
var alreadyBigEndian = false;
|
||||
switch (nextElementType)
|
||||
{
|
||||
case ElementReference.BufferElementType.Byte:
|
||||
var byteValue = (byte)i;
|
||||
elementNumberValue = byteValue;
|
||||
elementSize = sizeof(byte);
|
||||
elementBytes = new byte[1] { byteValue };
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
break;
|
||||
case ElementReference.BufferElementType.TwoByteInt:
|
||||
var ushortValue = (ushort)i;
|
||||
elementNumberValue = ushortValue;
|
||||
elementSize = sizeof(ushort);
|
||||
elementBytes = BitConverter.GetBytes(ushortValue);
|
||||
break;
|
||||
case ElementReference.BufferElementType.FourByteInt:
|
||||
var uintValue = (uint)i;
|
||||
elementNumberValue = uintValue;
|
||||
elementSize = sizeof(uint);
|
||||
elementBytes = BitConverter.GetBytes(uintValue);
|
||||
break;
|
||||
case ElementReference.BufferElementType.VariableSizeInt:
|
||||
{
|
||||
elementNumberValue = (uint)i;
|
||||
var writer = new MqttBufferWriter(4, 4);
|
||||
writer.WriteVariableByteInteger(elementNumberValue);
|
||||
elementSize = writer.Length;
|
||||
elementBytes = new byte[elementSize];
|
||||
var buffer = writer.GetBuffer();
|
||||
Array.Copy(buffer, elementBytes, elementSize);
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.String:
|
||||
{
|
||||
var stringLen = rnd.Next(TestString.Length);
|
||||
elementStringValue = TestString.Substring(0, stringLen); // could be empty
|
||||
var writer = new MqttBufferWriter(stringLen + 1, stringLen + 1);
|
||||
writer.WriteString(elementStringValue);
|
||||
elementSize = writer.Length;
|
||||
elementBytes = new byte[elementSize];
|
||||
var buffer = writer.GetBuffer();
|
||||
Array.Copy(buffer, elementBytes, elementSize);
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (BitConverter.IsLittleEndian && !alreadyBigEndian)
|
||||
{
|
||||
Array.Reverse(elementBytes);
|
||||
}
|
||||
|
||||
bw.Write(elementBytes);
|
||||
bufferElements.Add(new ElementReference(nextElementType, elementSize, elementNumberValue, elementStringValue, bufferOffset));
|
||||
bufferOffset += elementSize;
|
||||
}
|
||||
|
||||
elementBuffer = ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < NumTestSequences; i++)
|
||||
{
|
||||
var firstElementIndex = rnd.Next(NumBufferElements - 1);
|
||||
// segmentElementIndex will be < NumBufferElements - 1, which means there is at least one byte left for segment element count
|
||||
var elementCount = rnd.Next(NumBufferElements - firstElementIndex) + 1;
|
||||
var lastElementIndex = firstElementIndex + elementCount - 1;
|
||||
// Use element index to get buffer offsets
|
||||
var lastElement = bufferElements[lastElementIndex];
|
||||
var segmentStartPosition = bufferElements[firstElementIndex].BufferOffset;
|
||||
var segmentEndPosition = lastElement.BufferOffset + lastElement.Size;
|
||||
var segmentLength = segmentEndPosition - segmentStartPosition;
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(elementBuffer, segmentStartPosition, segmentLength);
|
||||
|
||||
// read all elements in the buffer segment; values should be as expected
|
||||
for (var n = 0; n < elementCount; n++)
|
||||
{
|
||||
var element = bufferElements[firstElementIndex + n];
|
||||
uint elementNumberValue = 0;
|
||||
byte[] elementBytes = null;
|
||||
uint elementNumberValue = 0; // all element number values fit into uint
|
||||
string elementStringValue = null;
|
||||
var elementSize = 0;
|
||||
|
||||
var expectedPosition = element.BufferOffset - segmentStartPosition;
|
||||
Assert.AreEqual(expectedPosition, reader.Position);
|
||||
|
||||
switch (element.Type)
|
||||
var alreadyBigEndian = false;
|
||||
switch (nextElementType)
|
||||
{
|
||||
case ElementReference.BufferElementType.Byte:
|
||||
{
|
||||
elementNumberValue = reader.ReadByte();
|
||||
}
|
||||
var byteValue = (byte)i;
|
||||
elementNumberValue = byteValue;
|
||||
elementSize = sizeof(byte);
|
||||
elementBytes = [byteValue];
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
break;
|
||||
case ElementReference.BufferElementType.TwoByteInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadTwoByteInteger();
|
||||
}
|
||||
var ushortValue = (ushort)i;
|
||||
elementNumberValue = ushortValue;
|
||||
elementSize = sizeof(ushort);
|
||||
elementBytes = BitConverter.GetBytes(ushortValue);
|
||||
break;
|
||||
case ElementReference.BufferElementType.FourByteInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadFourByteInteger();
|
||||
}
|
||||
var uintValue = (uint)i;
|
||||
elementNumberValue = uintValue;
|
||||
elementSize = sizeof(uint);
|
||||
elementBytes = BitConverter.GetBytes(uintValue);
|
||||
break;
|
||||
case ElementReference.BufferElementType.VariableSizeInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadVariableByteInteger();
|
||||
elementNumberValue = (uint)i;
|
||||
var writer = new MqttBufferWriter(4, 4);
|
||||
writer.WriteVariableByteInteger(elementNumberValue);
|
||||
elementSize = writer.Length;
|
||||
elementBytes = new byte[elementSize];
|
||||
var buffer = writer.GetBuffer();
|
||||
Array.Copy(buffer, elementBytes, elementSize);
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.String:
|
||||
{
|
||||
elementStringValue = reader.ReadString();
|
||||
var stringLen = rnd.Next(testString.Length);
|
||||
elementStringValue = testString[..stringLen]; // could be empty
|
||||
var writer = new MqttBufferWriter(stringLen + 1, stringLen + 1);
|
||||
writer.WriteString(elementStringValue);
|
||||
elementSize = writer.Length;
|
||||
elementBytes = new byte[elementSize];
|
||||
var buffer = writer.GetBuffer();
|
||||
Array.Copy(buffer, elementBytes, elementSize);
|
||||
alreadyBigEndian = true; // nothing to swap
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (elementStringValue != null)
|
||||
if (BitConverter.IsLittleEndian && !alreadyBigEndian)
|
||||
{
|
||||
Assert.AreEqual(elementStringValue, element.StringValue, $"'{elementStringValue}' not equal '{element.StringValue}' with random seed {rndSeed}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.AreEqual(elementNumberValue, element.NumberValue, $"{elementNumberValue} not equal {element.NumberValue} with random seed {rndSeed}.");
|
||||
Array.Reverse(elementBytes);
|
||||
}
|
||||
|
||||
bw.Write(elementBytes);
|
||||
bufferElements.Add(new ElementReference(nextElementType, elementSize, elementNumberValue, elementStringValue, bufferOffset));
|
||||
bufferOffset += elementSize;
|
||||
}
|
||||
|
||||
// confirm end of stream
|
||||
Assert.AreEqual(0, reader.BytesLeft, "BytesLeft not zero as expected with random seed " + rndSeed);
|
||||
Assert.IsTrue(reader.EndOfStream, "Not end of stream as expected with random seed " + rndSeed);
|
||||
elementBuffer = ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Full_Buffer()
|
||||
for (var i = 0; i < numTestSequences; i++)
|
||||
{
|
||||
var buffer = new byte[] { 5, 6, 7, 8, 9 };
|
||||
var firstElementIndex = rnd.Next(numBufferElements - 1);
|
||||
// segmentElementIndex will be < NumBufferElements - 1, which means there is at least one byte left for segment element count
|
||||
var elementCount = rnd.Next(numBufferElements - firstElementIndex) + 1;
|
||||
var lastElementIndex = firstElementIndex + elementCount - 1;
|
||||
// Use element index to get buffer offsets
|
||||
var lastElement = bufferElements[lastElementIndex];
|
||||
var segmentStartPosition = bufferElements[firstElementIndex].BufferOffset;
|
||||
var segmentEndPosition = lastElement.BufferOffset + lastElement.Size;
|
||||
var segmentLength = segmentEndPosition - segmentStartPosition;
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
reader.SetBuffer(elementBuffer, segmentStartPosition, segmentLength);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Partial_End_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 5, 5);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Partial_Start_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
|
||||
// Helper class to build up a reference to elements of various types in a buffer
|
||||
class ElementReference
|
||||
{
|
||||
public enum BufferElementType
|
||||
// read all elements in the buffer segment; values should be as expected
|
||||
for (var n = 0; n < elementCount; n++)
|
||||
{
|
||||
Byte,
|
||||
TwoByteInt,
|
||||
FourByteInt,
|
||||
VariableSizeInt,
|
||||
String
|
||||
var element = bufferElements[firstElementIndex + n];
|
||||
uint elementNumberValue = 0;
|
||||
string elementStringValue = null;
|
||||
|
||||
var expectedPosition = element.BufferOffset - segmentStartPosition;
|
||||
Assert.AreEqual(expectedPosition, reader.Position);
|
||||
|
||||
switch (element.Type)
|
||||
{
|
||||
case ElementReference.BufferElementType.Byte:
|
||||
{
|
||||
elementNumberValue = reader.ReadByte();
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.TwoByteInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadTwoByteInteger();
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.FourByteInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadFourByteInteger();
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.VariableSizeInt:
|
||||
{
|
||||
elementNumberValue = reader.ReadVariableByteInteger();
|
||||
}
|
||||
break;
|
||||
case ElementReference.BufferElementType.String:
|
||||
{
|
||||
elementStringValue = reader.ReadString();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (elementStringValue != null)
|
||||
{
|
||||
Assert.AreEqual(elementStringValue, element.StringValue, $"'{elementStringValue}' not equal '{element.StringValue}' with random seed {rndSeed}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.AreEqual(elementNumberValue, element.NumberValue, $"{elementNumberValue} not equal {element.NumberValue} with random seed {rndSeed}.");
|
||||
}
|
||||
}
|
||||
|
||||
public const int NumBufferElementTypes = 5;
|
||||
|
||||
public ElementReference(BufferElementType type, int size, uint numberValue, string stringValue, int bufferOffset)
|
||||
{
|
||||
Type = type;
|
||||
Size = size;
|
||||
NumberValue = numberValue;
|
||||
StringValue = stringValue;
|
||||
BufferOffset = bufferOffset;
|
||||
}
|
||||
|
||||
public int BufferOffset { get; }
|
||||
public uint NumberValue { get; }
|
||||
public int Size { get; }
|
||||
public string StringValue { get; }
|
||||
public BufferElementType Type { get; }
|
||||
// confirm end of stream
|
||||
Assert.AreEqual(0, reader.BytesLeft, "BytesLeft not zero as expected with random seed " + rndSeed);
|
||||
Assert.IsTrue(reader.EndOfStream, "Not end of stream as expected with random seed " + rndSeed);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Full_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Partial_End_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 5, 5);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Report_Correct_Length_For_Partial_Start_Buffer()
|
||||
{
|
||||
var buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
|
||||
// The used buffer contains more data than used!
|
||||
reader.SetBuffer(buffer, 0, 5);
|
||||
|
||||
Assert.IsFalse(reader.EndOfStream);
|
||||
Assert.AreEqual(5, reader.BytesLeft);
|
||||
}
|
||||
|
||||
|
||||
// Helper class to build up a reference to elements of various types in a buffer
|
||||
class ElementReference
|
||||
{
|
||||
public enum BufferElementType
|
||||
{
|
||||
Byte,
|
||||
TwoByteInt,
|
||||
FourByteInt,
|
||||
VariableSizeInt,
|
||||
String
|
||||
}
|
||||
|
||||
public const int NumBufferElementTypes = 5;
|
||||
|
||||
public ElementReference(BufferElementType type, int size, uint numberValue, string stringValue, int bufferOffset)
|
||||
{
|
||||
Type = type;
|
||||
Size = size;
|
||||
NumberValue = numberValue;
|
||||
StringValue = stringValue;
|
||||
BufferOffset = bufferOffset;
|
||||
}
|
||||
|
||||
public int BufferOffset { get; }
|
||||
public uint NumberValue { get; }
|
||||
public int Size { get; }
|
||||
public string StringValue { get; }
|
||||
public BufferElementType Type { get; }
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,494 +4,437 @@
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Tests.Formatter
|
||||
namespace MQTTnet.Tests.Formatter;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class MqttPacketSerialization_V3_Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class MqttPacketSerialization_V3_Tests
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttAuthPacket_V311()
|
||||
{
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttAuthPacket_V311()
|
||||
{
|
||||
var authPacket = new MqttAuthPacket();
|
||||
Assert.ThrowsException<MqttProtocolViolationException>(() => MqttPacketSerializationHelper.EncodeAndDecodePacket(authPacket, MqttProtocolVersion.V311));
|
||||
}
|
||||
var authPacket = new MqttAuthPacket();
|
||||
Assert.ThrowsException<MqttProtocolViolationException>(() => MqttPacketSerializationHelper.EncodeAndDecodePacket(authPacket, MqttProtocolVersion.V311));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnAckPacket_V311()
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnAckPacket_V311()
|
||||
{
|
||||
var connAckPacket = new MqttConnAckPacket
|
||||
{
|
||||
var connAckPacket = new MqttConnAckPacket
|
||||
{
|
||||
AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
ReasonCode = MqttConnectReasonCode.ServerUnavailable,
|
||||
ReasonString = "ReasonString",
|
||||
ReceiveMaximum = 123,
|
||||
ResponseInformation = "ResponseInformation",
|
||||
RetainAvailable = true,
|
||||
ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized,
|
||||
ServerReference = "ServerReference",
|
||||
AssignedClientIdentifier = "AssignedClientIdentifier",
|
||||
IsSessionPresent = true,
|
||||
MaximumPacketSize = 456,
|
||||
MaximumQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
ServerKeepAlive = 789,
|
||||
SessionExpiryInterval = 852,
|
||||
SharedSubscriptionAvailable = true,
|
||||
SubscriptionIdentifiersAvailable = true,
|
||||
TopicAliasMaximum = 963,
|
||||
WildcardSubscriptionAvailable = true,
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
AuthenticationData = "AuthenticationData"u8.ToArray(),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
ReasonCode = MqttConnectReasonCode.ServerUnavailable,
|
||||
ReasonString = "ReasonString",
|
||||
ReceiveMaximum = 123,
|
||||
ResponseInformation = "ResponseInformation",
|
||||
RetainAvailable = true,
|
||||
ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized,
|
||||
ServerReference = "ServerReference",
|
||||
AssignedClientIdentifier = "AssignedClientIdentifier",
|
||||
IsSessionPresent = true,
|
||||
MaximumPacketSize = 456,
|
||||
MaximumQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
ServerKeepAlive = 789,
|
||||
SessionExpiryInterval = 852,
|
||||
SharedSubscriptionAvailable = true,
|
||||
SubscriptionIdentifiersAvailable = true,
|
||||
TopicAliasMaximum = 963,
|
||||
WildcardSubscriptionAvailable = true,
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
//Assert.AreEqual(connAckPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ResponseInformation); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.RetainAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttConnectReturnCode.ConnectionRefusedNotAuthorized, deserialized.ReturnCode);
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AssignedClientIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connAckPacket.IsSessionPresent, deserialized.IsSessionPresent);
|
||||
Assert.AreEqual(0U, deserialized.MaximumPacketSize); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, deserialized.MaximumQoS); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ServerKeepAlive); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SharedSubscriptionAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SubscriptionIdentifiersAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.TopicAliasMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.WildcardSubscriptionAvailable);
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnAckPacket_V310()
|
||||
{
|
||||
var connAckPacket = new MqttConnAckPacket
|
||||
{
|
||||
AuthenticationData = "AuthenticationData"u8.ToArray(),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
ReasonCode = MqttConnectReasonCode.ServerUnavailable,
|
||||
ReasonString = "ReasonString",
|
||||
ReceiveMaximum = 123,
|
||||
ResponseInformation = "ResponseInformation",
|
||||
RetainAvailable = true,
|
||||
ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized,
|
||||
ServerReference = "ServerReference",
|
||||
AssignedClientIdentifier = "AssignedClientIdentifier",
|
||||
IsSessionPresent = true,
|
||||
MaximumPacketSize = 456,
|
||||
MaximumQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
ServerKeepAlive = 789,
|
||||
SessionExpiryInterval = 852,
|
||||
SharedSubscriptionAvailable = true,
|
||||
SubscriptionIdentifiersAvailable = true,
|
||||
TopicAliasMaximum = 963,
|
||||
WildcardSubscriptionAvailable = true,
|
||||
UserProperties = [new MqttUserProperty("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connAckPacket, MqttProtocolVersion.V310);
|
||||
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
//Assert.AreEqual(connAckPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ResponseInformation); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.RetainAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttConnectReturnCode.ConnectionRefusedNotAuthorized, deserialized.ReturnCode);
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AssignedClientIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.IsSessionPresent); // Not supported in v3.1.0 <- !
|
||||
Assert.AreEqual(0U, deserialized.MaximumPacketSize); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, deserialized.MaximumQoS); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ServerKeepAlive); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SharedSubscriptionAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SubscriptionIdentifiersAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.TopicAliasMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.WildcardSubscriptionAvailable);
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnectPacket_V311()
|
||||
{
|
||||
var connectPacket = new MqttConnectPacket
|
||||
{
|
||||
Username = "Username",
|
||||
Password = "Password"u8.ToArray(),
|
||||
ClientId = "ClientId",
|
||||
AuthenticationData = "AuthenticationData"u8.ToArray(),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
CleanSession = true,
|
||||
ReceiveMaximum = 123,
|
||||
WillFlag = true,
|
||||
WillTopic = "WillTopic",
|
||||
WillMessage = "WillMessage"u8.ToArray(),
|
||||
WillRetain = true,
|
||||
KeepAlivePeriod = 456,
|
||||
MaximumPacketSize = 789,
|
||||
RequestProblemInformation = true,
|
||||
RequestResponseInformation = true,
|
||||
SessionExpiryInterval = 27,
|
||||
TopicAliasMaximum = 67,
|
||||
WillContentType = "WillContentType",
|
||||
WillCorrelationData = "WillCorrelationData"u8.ToArray(),
|
||||
WillDelayInterval = 782,
|
||||
WillQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
WillResponseTopic = "WillResponseTopic",
|
||||
WillMessageExpiryInterval = 542,
|
||||
WillPayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
|
||||
UserProperties = [new("Foo", "Bar")],
|
||||
WillUserProperties = [new("WillFoo", "WillBar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connectPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(connectPacket.Username, deserialized.Username);
|
||||
CollectionAssert.AreEqual(connectPacket.Password, deserialized.Password);
|
||||
Assert.AreEqual(connectPacket.ClientId, deserialized.ClientId);
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connectPacket.CleanSession, deserialized.CleanSession);
|
||||
Assert.AreEqual(0L, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connectPacket.WillFlag, deserialized.WillFlag);
|
||||
Assert.AreEqual(connectPacket.WillTopic, deserialized.WillTopic);
|
||||
CollectionAssert.AreEqual(connectPacket.WillMessage, deserialized.WillMessage);
|
||||
Assert.AreEqual(connectPacket.WillRetain, deserialized.WillRetain);
|
||||
Assert.AreEqual(connectPacket.KeepAlivePeriod, deserialized.KeepAlivePeriod);
|
||||
// MaximumPacketSize not available in MQTTv3.
|
||||
// RequestProblemInformation not available in MQTTv3.
|
||||
// RequestResponseInformation not available in MQTTv3.
|
||||
// SessionExpiryInterval not available in MQTTv3.
|
||||
// TopicAliasMaximum not available in MQTTv3.
|
||||
// WillContentType not available in MQTTv3.
|
||||
// WillCorrelationData not available in MQTTv3.
|
||||
// WillDelayInterval not available in MQTTv3.
|
||||
Assert.AreEqual(connectPacket.WillQoS, deserialized.WillQoS);
|
||||
// WillResponseTopic not available in MQTTv3.
|
||||
// WillMessageExpiryInterval not available in MQTTv3.
|
||||
// WillPayloadFormatIndicator not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
Assert.IsNull(deserialized.WillUserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttDisconnectPacket_V311()
|
||||
{
|
||||
var disconnectPacket = new MqttDisconnectPacket
|
||||
{
|
||||
ReasonCode = MqttDisconnectReasonCode.NormalDisconnection, // MQTTv3 has no other values than this.
|
||||
ReasonString = "ReasonString",
|
||||
ServerReference = "ServerReference",
|
||||
SessionExpiryInterval = 234,
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(disconnectPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(disconnectPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPingReqPacket_V311()
|
||||
{
|
||||
var pingReqPacket = new MqttPingReqPacket();
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingReqPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.IsNotNull(deserialized);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPingRespPacket_V311()
|
||||
{
|
||||
var pingRespPacket = new MqttPingRespPacket();
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingRespPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.IsNotNull(deserialized);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubAckPacket_V311()
|
||||
{
|
||||
var pubAckPacket = new MqttPubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubAckReasonCode.NoMatchingSubscribers,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(MqttPubAckReasonCode.Success, deserialized.ReasonCode); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubCompPacket_V311()
|
||||
{
|
||||
var pubCompPacket = new MqttPubCompPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubCompReasonCode.PacketIdentifierNotFound,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubCompPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubCompPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPublishPacket_V311()
|
||||
{
|
||||
var publishPacket = new MqttPublishPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
Dup = true,
|
||||
Retain = true,
|
||||
PayloadSegment = new ArraySegment<byte>("Payload"u8.ToArray()),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Topic = "Topic",
|
||||
ResponseTopic = "/Response",
|
||||
ContentType = "Content-Type",
|
||||
CorrelationData = "CorrelationData"u8.ToArray(),
|
||||
TopicAlias = 27,
|
||||
SubscriptionIdentifiers = [123],
|
||||
MessageExpiryInterval = 38,
|
||||
PayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(publishPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(publishPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(publishPacket.Dup, deserialized.Dup);
|
||||
Assert.AreEqual(publishPacket.Retain, deserialized.Retain);
|
||||
CollectionAssert.AreEqual(publishPacket.Payload.ToArray(), deserialized.Payload.ToArray());
|
||||
Assert.AreEqual(publishPacket.QualityOfServiceLevel, deserialized.QualityOfServiceLevel);
|
||||
Assert.AreEqual(publishPacket.Topic, deserialized.Topic);
|
||||
Assert.AreEqual(null, deserialized.ResponseTopic); // Not supported in v3.1.1.
|
||||
Assert.AreEqual(null, deserialized.ContentType); // Not supported in v3.1.1.
|
||||
CollectionAssert.AreEqual(null, deserialized.CorrelationData); // Not supported in v3.1.1.
|
||||
Assert.AreEqual(0U, deserialized.TopicAlias); // Not supported in v3.1.1.
|
||||
CollectionAssert.AreEqual(null, deserialized.SubscriptionIdentifiers); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.MessageExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttPayloadFormatIndicator.Unspecified, deserialized.PayloadFormatIndicator); // Not supported in v3.1.1
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubRecPacket_V311()
|
||||
{
|
||||
var pubRecPacket = new MqttPubRecPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubRecReasonCode.UnspecifiedError,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRecPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubRecPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubRelPacket_V311()
|
||||
{
|
||||
var pubRelPacket = new MqttPubRelPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubRelReasonCode.PacketIdentifierNotFound,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRelPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubRelPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttSubAckPacket_V311()
|
||||
{
|
||||
var subAckPacket = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonString = "ReasonString",
|
||||
ReasonCodes = [MqttSubscribeReasonCode.GrantedQoS1],
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(subAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(subAckPacket.ReasonCodes.Count, deserialized.ReasonCodes.Count);
|
||||
Assert.AreEqual(subAckPacket.ReasonCodes[0], deserialized.ReasonCodes[0]);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttSubscribePacket_V311()
|
||||
{
|
||||
var subscribePacket = new MqttSubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
SubscriptionIdentifier = 456,
|
||||
TopicFilters =
|
||||
[
|
||||
new()
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
Topic = "Topic",
|
||||
NoLocal = true,
|
||||
RetainHandling = MqttRetainHandling.SendAtSubscribeIfNewSubscriptionOnly,
|
||||
RetainAsPublished = true,
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
}
|
||||
};
|
||||
],
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connAckPacket, MqttProtocolVersion.V311);
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subscribePacket, MqttProtocolVersion.V311);
|
||||
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
//Assert.AreEqual(connAckPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ResponseInformation); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.RetainAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttConnectReturnCode.ConnectionRefusedNotAuthorized, deserialized.ReturnCode);
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AssignedClientIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connAckPacket.IsSessionPresent, deserialized.IsSessionPresent);
|
||||
Assert.AreEqual(0U, deserialized.MaximumPacketSize); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, deserialized.MaximumQoS); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ServerKeepAlive); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SharedSubscriptionAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SubscriptionIdentifiersAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.TopicAliasMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.WildcardSubscriptionAvailable);
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnAckPacket_V310()
|
||||
Assert.AreEqual(subscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(0U, deserialized.SubscriptionIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(1, deserialized.TopicFilters.Count);
|
||||
Assert.AreEqual(subscribePacket.TopicFilters[0].Topic, deserialized.TopicFilters[0].Topic);
|
||||
Assert.AreEqual(false, deserialized.TopicFilters[0].NoLocal); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttRetainHandling.SendAtSubscribe, deserialized.TopicFilters[0].RetainHandling); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.TopicFilters[0].RetainAsPublished); // Not supported in v3.1.1
|
||||
Assert.AreEqual(subscribePacket.TopicFilters[0].QualityOfServiceLevel, deserialized.TopicFilters[0].QualityOfServiceLevel);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttUnsubAckPacket_V311()
|
||||
{
|
||||
var unsubAckPacket = new MqttUnsubAckPacket
|
||||
{
|
||||
var connAckPacket = new MqttConnAckPacket
|
||||
{
|
||||
AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
ReasonCode = MqttConnectReasonCode.ServerUnavailable,
|
||||
ReasonString = "ReasonString",
|
||||
ReceiveMaximum = 123,
|
||||
ResponseInformation = "ResponseInformation",
|
||||
RetainAvailable = true,
|
||||
ReturnCode = MqttConnectReturnCode.ConnectionRefusedNotAuthorized,
|
||||
ServerReference = "ServerReference",
|
||||
AssignedClientIdentifier = "AssignedClientIdentifier",
|
||||
IsSessionPresent = true,
|
||||
MaximumPacketSize = 456,
|
||||
MaximumQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
ServerKeepAlive = 789,
|
||||
SessionExpiryInterval = 852,
|
||||
SharedSubscriptionAvailable = true,
|
||||
SubscriptionIdentifiersAvailable = true,
|
||||
TopicAliasMaximum = 963,
|
||||
WildcardSubscriptionAvailable = true,
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
PacketIdentifier = 123,
|
||||
ReasonCodes = [MqttUnsubscribeReasonCode.ImplementationSpecificError],
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connAckPacket, MqttProtocolVersion.V310);
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
//Assert.AreEqual(connAckPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ResponseInformation); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.RetainAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttConnectReturnCode.ConnectionRefusedNotAuthorized, deserialized.ReturnCode);
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AssignedClientIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.IsSessionPresent); // Not supported in v3.1.0 <- !
|
||||
Assert.AreEqual(0U, deserialized.MaximumPacketSize); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, deserialized.MaximumQoS); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.ServerKeepAlive); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SharedSubscriptionAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.SubscriptionIdentifiersAvailable); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.TopicAliasMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.WildcardSubscriptionAvailable);
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
Assert.AreEqual(unsubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.ReasonCodes); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttConnectPacket_V311()
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttUnsubscribePacket_V311()
|
||||
{
|
||||
var unsubscribePacket = new MqttUnsubscribePacket
|
||||
{
|
||||
var connectPacket = new MqttConnectPacket
|
||||
{
|
||||
Username = "Username",
|
||||
Password = Encoding.UTF8.GetBytes("Password"),
|
||||
ClientId = "ClientId",
|
||||
AuthenticationData = Encoding.UTF8.GetBytes("AuthenticationData"),
|
||||
AuthenticationMethod = "AuthenticationMethod",
|
||||
CleanSession = true,
|
||||
ReceiveMaximum = 123,
|
||||
WillFlag = true,
|
||||
WillTopic = "WillTopic",
|
||||
WillMessage = Encoding.UTF8.GetBytes("WillMessage"),
|
||||
WillRetain = true,
|
||||
KeepAlivePeriod = 456,
|
||||
MaximumPacketSize = 789,
|
||||
RequestProblemInformation = true,
|
||||
RequestResponseInformation = true,
|
||||
SessionExpiryInterval = 27,
|
||||
TopicAliasMaximum = 67,
|
||||
WillContentType = "WillContentType",
|
||||
WillCorrelationData = Encoding.UTF8.GetBytes("WillCorrelationData"),
|
||||
WillDelayInterval = 782,
|
||||
WillQoS = MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
WillResponseTopic = "WillResponseTopic",
|
||||
WillMessageExpiryInterval = 542,
|
||||
WillPayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
},
|
||||
WillUserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("WillFoo", "WillBar")
|
||||
}
|
||||
};
|
||||
PacketIdentifier = 123,
|
||||
TopicFilters = ["TopicFilter1"],
|
||||
UserProperties = [new("Foo", "Bar")]
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(connectPacket, MqttProtocolVersion.V311);
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubscribePacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(connectPacket.Username, deserialized.Username);
|
||||
CollectionAssert.AreEqual(connectPacket.Password, deserialized.Password);
|
||||
Assert.AreEqual(connectPacket.ClientId, deserialized.ClientId);
|
||||
CollectionAssert.AreEqual(null, deserialized.AuthenticationData); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.AuthenticationMethod); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connectPacket.CleanSession, deserialized.CleanSession);
|
||||
Assert.AreEqual(0L, deserialized.ReceiveMaximum); // Not supported in v3.1.1
|
||||
Assert.AreEqual(connectPacket.WillFlag, deserialized.WillFlag);
|
||||
Assert.AreEqual(connectPacket.WillTopic, deserialized.WillTopic);
|
||||
CollectionAssert.AreEqual(connectPacket.WillMessage, deserialized.WillMessage);
|
||||
Assert.AreEqual(connectPacket.WillRetain, deserialized.WillRetain);
|
||||
Assert.AreEqual(connectPacket.KeepAlivePeriod, deserialized.KeepAlivePeriod);
|
||||
// MaximumPacketSize not available in MQTTv3.
|
||||
// RequestProblemInformation not available in MQTTv3.
|
||||
// RequestResponseInformation not available in MQTTv3.
|
||||
// SessionExpiryInterval not available in MQTTv3.
|
||||
// TopicAliasMaximum not available in MQTTv3.
|
||||
// WillContentType not available in MQTTv3.
|
||||
// WillCorrelationData not available in MQTTv3.
|
||||
// WillDelayInterval not available in MQTTv3.
|
||||
Assert.AreEqual(connectPacket.WillQoS, deserialized.WillQoS);
|
||||
// WillResponseTopic not available in MQTTv3.
|
||||
// WillMessageExpiryInterval not available in MQTTv3.
|
||||
// WillPayloadFormatIndicator not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
Assert.IsNull(deserialized.WillUserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttDisconnectPacket_V311()
|
||||
{
|
||||
var disconnectPacket = new MqttDisconnectPacket
|
||||
{
|
||||
ReasonCode = MqttDisconnectReasonCode.NormalDisconnection, // MQTTv3 has no other values than this.
|
||||
ReasonString = "ReasonString",
|
||||
ServerReference = "ServerReference",
|
||||
SessionExpiryInterval = 234,
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(disconnectPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(disconnectPacket.ReasonCode, deserialized.ReasonCode);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ServerReference); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.SessionExpiryInterval); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPingReqPacket_V311()
|
||||
{
|
||||
var pingReqPacket = new MqttPingReqPacket();
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingReqPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.IsNotNull(deserialized);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPingRespPacket_V311()
|
||||
{
|
||||
var pingRespPacket = new MqttPingRespPacket();
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pingRespPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.IsNotNull(deserialized);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubAckPacket_V311()
|
||||
{
|
||||
var pubAckPacket = new MqttPubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubAckReasonCode.NoMatchingSubscribers,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(MqttPubAckReasonCode.Success, deserialized.ReasonCode); // Not supported in v3.1.1
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubCompPacket_V311()
|
||||
{
|
||||
var pubCompPacket = new MqttPubCompPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubCompReasonCode.PacketIdentifierNotFound,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubCompPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubCompPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPublishPacket_V311()
|
||||
{
|
||||
var publishPacket = new MqttPublishPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
Dup = true,
|
||||
Retain = true,
|
||||
PayloadSegment = new ArraySegment<byte>(Encoding.ASCII.GetBytes("Payload")),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Topic = "Topic",
|
||||
ResponseTopic = "/Response",
|
||||
ContentType = "Content-Type",
|
||||
CorrelationData = Encoding.UTF8.GetBytes("CorrelationData"),
|
||||
TopicAlias = 27,
|
||||
SubscriptionIdentifiers = new List<uint>
|
||||
{
|
||||
123
|
||||
},
|
||||
MessageExpiryInterval = 38,
|
||||
PayloadFormatIndicator = MqttPayloadFormatIndicator.CharacterData,
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(publishPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(publishPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(publishPacket.Dup, deserialized.Dup);
|
||||
Assert.AreEqual(publishPacket.Retain, deserialized.Retain);
|
||||
CollectionAssert.AreEqual(publishPacket.Payload.ToArray(), deserialized.Payload.ToArray());
|
||||
Assert.AreEqual(publishPacket.QualityOfServiceLevel, deserialized.QualityOfServiceLevel);
|
||||
Assert.AreEqual(publishPacket.Topic, deserialized.Topic);
|
||||
Assert.AreEqual(null, deserialized.ResponseTopic); // Not supported in v3.1.1.
|
||||
Assert.AreEqual(null, deserialized.ContentType); // Not supported in v3.1.1.
|
||||
CollectionAssert.AreEqual(null, deserialized.CorrelationData); // Not supported in v3.1.1.
|
||||
Assert.AreEqual(0U, deserialized.TopicAlias); // Not supported in v3.1.1.
|
||||
CollectionAssert.AreEqual(null, deserialized.SubscriptionIdentifiers); // Not supported in v3.1.1
|
||||
Assert.AreEqual(0U, deserialized.MessageExpiryInterval); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttPayloadFormatIndicator.Unspecified, deserialized.PayloadFormatIndicator); // Not supported in v3.1.1
|
||||
Assert.IsNull(deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubRecPacket_V311()
|
||||
{
|
||||
var pubRecPacket = new MqttPubRecPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubRecReasonCode.UnspecifiedError,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRecPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubRecPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttPubRelPacket_V311()
|
||||
{
|
||||
var pubRelPacket = new MqttPubRelPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCode = MqttPubRelReasonCode.PacketIdentifierNotFound,
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(pubRelPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(pubRelPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
// ReasonCode not available in MQTTv3.
|
||||
// ReasonString not available in MQTTv3.
|
||||
// UserProperties not available in MQTTv3.
|
||||
Assert.IsNull(deserialized.UserProperties);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttSubAckPacket_V311()
|
||||
{
|
||||
var subAckPacket = new MqttSubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonString = "ReasonString",
|
||||
ReasonCodes = new List<MqttSubscribeReasonCode>
|
||||
{
|
||||
MqttSubscribeReasonCode.GrantedQoS1
|
||||
},
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(subAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
Assert.AreEqual(subAckPacket.ReasonCodes.Count, deserialized.ReasonCodes.Count);
|
||||
Assert.AreEqual(subAckPacket.ReasonCodes[0], deserialized.ReasonCodes[0]);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttSubscribePacket_V311()
|
||||
{
|
||||
var subscribePacket = new MqttSubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
SubscriptionIdentifier = 456,
|
||||
TopicFilters = new List<MqttTopicFilter>
|
||||
{
|
||||
new MqttTopicFilter
|
||||
{
|
||||
Topic = "Topic",
|
||||
NoLocal = true,
|
||||
RetainHandling = MqttRetainHandling.SendAtSubscribeIfNewSubscriptionOnly,
|
||||
RetainAsPublished = true,
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
}
|
||||
},
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(subscribePacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(subscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(0U, deserialized.SubscriptionIdentifier); // Not supported in v3.1.1
|
||||
Assert.AreEqual(1, deserialized.TopicFilters.Count);
|
||||
Assert.AreEqual(subscribePacket.TopicFilters[0].Topic, deserialized.TopicFilters[0].Topic);
|
||||
Assert.AreEqual(false, deserialized.TopicFilters[0].NoLocal); // Not supported in v3.1.1
|
||||
Assert.AreEqual(MqttRetainHandling.SendAtSubscribe, deserialized.TopicFilters[0].RetainHandling); // Not supported in v3.1.1
|
||||
Assert.AreEqual(false, deserialized.TopicFilters[0].RetainAsPublished); // Not supported in v3.1.1
|
||||
Assert.AreEqual(subscribePacket.TopicFilters[0].QualityOfServiceLevel, deserialized.TopicFilters[0].QualityOfServiceLevel);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttUnsubAckPacket_V311()
|
||||
{
|
||||
var unsubAckPacket = new MqttUnsubAckPacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
ReasonCodes = new List<MqttUnsubscribeReasonCode>
|
||||
{
|
||||
MqttUnsubscribeReasonCode.ImplementationSpecificError
|
||||
},
|
||||
ReasonString = "ReasonString",
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubAckPacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(unsubAckPacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(null, deserialized.ReasonString); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.ReasonCodes); // Not supported in v3.1.1
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties); // Not supported in v3.1.1
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Serialize_Full_MqttUnsubscribePacket_V311()
|
||||
{
|
||||
var unsubscribePacket = new MqttUnsubscribePacket
|
||||
{
|
||||
PacketIdentifier = 123,
|
||||
TopicFilters = new List<string>
|
||||
{
|
||||
"TopicFilter1"
|
||||
},
|
||||
UserProperties = new List<MqttUserProperty>
|
||||
{
|
||||
new MqttUserProperty("Foo", "Bar")
|
||||
}
|
||||
};
|
||||
|
||||
var deserialized = MqttPacketSerializationHelper.EncodeAndDecodePacket(unsubscribePacket, MqttProtocolVersion.V311);
|
||||
|
||||
Assert.AreEqual(unsubscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(unsubscribePacket.TopicFilters.Count, deserialized.TopicFilters.Count);
|
||||
Assert.AreEqual(unsubscribePacket.TopicFilters[0], deserialized.TopicFilters[0]);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
Assert.AreEqual(unsubscribePacket.PacketIdentifier, deserialized.PacketIdentifier);
|
||||
Assert.AreEqual(unsubscribePacket.TopicFilters.Count, deserialized.TopicFilters.Count);
|
||||
Assert.AreEqual(unsubscribePacket.TopicFilters[0], deserialized.TopicFilters[0]);
|
||||
CollectionAssert.AreEqual(null, deserialized.UserProperties);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Tests.Formatter;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class MqttPacketSerialization_V5_Tests
|
||||
{
|
||||
|
||||
@@ -1,102 +1,101 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
namespace MQTTnet.Tests.Formatter
|
||||
namespace MQTTnet.Tests.Formatter;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class MqttPacketWriter_Tests
|
||||
{
|
||||
[TestClass]
|
||||
public sealed class MqttPacketWriter_Tests
|
||||
[TestMethod]
|
||||
public void Reset_After_Usage()
|
||||
{
|
||||
[TestMethod]
|
||||
public void Reset_After_Usage()
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("AString");
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteByte(0x0);
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteVariableByteInteger(1234U);
|
||||
writer.WriteVariableByteInteger(9876U);
|
||||
|
||||
writer.Reset(0);
|
||||
|
||||
Assert.AreEqual(0, writer.Length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Use_All_Data_Types()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("AString");
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteByte(0x0);
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteVariableByteInteger(1234U);
|
||||
writer.WriteVariableByteInteger(9876U);
|
||||
|
||||
var buffer = writer.GetBuffer();
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(buffer, 0, writer.Length);
|
||||
|
||||
Assert.AreEqual("AString", reader.ReadString());
|
||||
Assert.IsTrue(reader.ReadByte() == 1);
|
||||
Assert.IsTrue(reader.ReadByte() == 0);
|
||||
Assert.IsTrue(reader.ReadByte() == 1);
|
||||
Assert.AreEqual(1234U, reader.ReadVariableByteInteger());
|
||||
Assert.AreEqual(9876U, reader.ReadVariableByteInteger());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException))]
|
||||
public void Throw_If_String_Too_Long()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
|
||||
writer.WriteString(string.Empty.PadLeft(65536));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_And_Read_Multiple_Times()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("A relative short string.");
|
||||
writer.WriteBinary(new byte[1234]);
|
||||
writer.WriteByte(0x01);
|
||||
writer.WriteByte(0x02);
|
||||
writer.WriteVariableByteInteger(5647382);
|
||||
writer.WriteString("A relative short string.");
|
||||
writer.WriteVariableByteInteger(8574489);
|
||||
writer.WriteBinary(new byte[48]);
|
||||
writer.WriteByte(2);
|
||||
writer.WriteByte(0x02);
|
||||
writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug");
|
||||
writer.WriteBinary(new byte[3]);
|
||||
|
||||
var readPayload = new ArraySegment<byte>(writer.GetBuffer(), 0, writer.Length).ToArray();
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(readPayload, 0, readPayload.Length);
|
||||
|
||||
for (var i = 0; i < 100000; i++)
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("AString");
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteByte(0x0);
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteVariableByteInteger(1234U);
|
||||
writer.WriteVariableByteInteger(9876U);
|
||||
reader.Seek(0);
|
||||
|
||||
writer.Reset(0);
|
||||
|
||||
Assert.AreEqual(0, writer.Length);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Use_All_Data_Types()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("AString");
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteByte(0x0);
|
||||
writer.WriteByte(0x1);
|
||||
writer.WriteVariableByteInteger(1234U);
|
||||
writer.WriteVariableByteInteger(9876U);
|
||||
|
||||
var buffer = writer.GetBuffer();
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(buffer, 0, writer.Length);
|
||||
|
||||
Assert.AreEqual("AString", reader.ReadString());
|
||||
Assert.IsTrue(reader.ReadByte() == 1);
|
||||
Assert.IsTrue(reader.ReadByte() == 0);
|
||||
Assert.IsTrue(reader.ReadByte() == 1);
|
||||
Assert.AreEqual(1234U, reader.ReadVariableByteInteger());
|
||||
Assert.AreEqual(9876U, reader.ReadVariableByteInteger());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(MqttProtocolViolationException))]
|
||||
public void Throw_If_String_Too_Long()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
|
||||
writer.WriteString(string.Empty.PadLeft(65536));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Write_And_Read_Multiple_Times()
|
||||
{
|
||||
var writer = new MqttBufferWriter(4096, 65535);
|
||||
writer.WriteString("A relative short string.");
|
||||
writer.WriteBinary(new byte[1234]);
|
||||
writer.WriteByte(0x01);
|
||||
writer.WriteByte(0x02);
|
||||
writer.WriteVariableByteInteger(5647382);
|
||||
writer.WriteString("A relative short string.");
|
||||
writer.WriteVariableByteInteger(8574489);
|
||||
writer.WriteBinary(new byte[48]);
|
||||
writer.WriteByte(2);
|
||||
writer.WriteByte(0x02);
|
||||
writer.WriteString("fjgffiogfhgfhoihgoireghreghreguhreguireoghreouighreouighreughreguiorehreuiohruiorehreuioghreug");
|
||||
writer.WriteBinary(new byte[3]);
|
||||
|
||||
var readPayload = new ArraySegment<byte>(writer.GetBuffer(), 0, writer.Length).ToArray();
|
||||
|
||||
var reader = new MqttBufferReader();
|
||||
reader.SetBuffer(readPayload, 0, readPayload.Length);
|
||||
|
||||
for (var i = 0; i < 100000; i++)
|
||||
{
|
||||
reader.Seek(0);
|
||||
|
||||
reader.ReadString();
|
||||
reader.ReadBinaryData();
|
||||
reader.ReadByte();
|
||||
reader.ReadByte();
|
||||
reader.ReadVariableByteInteger();
|
||||
reader.ReadString();
|
||||
reader.ReadVariableByteInteger();
|
||||
reader.ReadBinaryData();
|
||||
reader.ReadByte();
|
||||
reader.ReadByte();
|
||||
reader.ReadString();
|
||||
reader.ReadBinaryData();
|
||||
}
|
||||
reader.ReadString();
|
||||
reader.ReadBinaryData();
|
||||
reader.ReadByte();
|
||||
reader.ReadByte();
|
||||
reader.ReadVariableByteInteger();
|
||||
reader.ReadString();
|
||||
reader.ReadVariableByteInteger();
|
||||
reader.ReadBinaryData();
|
||||
reader.ReadByte();
|
||||
reader.ReadByte();
|
||||
reader.ReadString();
|
||||
reader.ReadBinaryData();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user