mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-23 13:23:05 +08:00
Add Directory.Build.props. (#2120)
* Add Directory.Build.props. * Adjust Analysis related properties and add conditional enablement for ContinuousIntegrationBuild. * Adopt new changes in master --------- Co-authored-by: christian <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
@@ -350,7 +350,7 @@ public class General_Tests : BaseTestClass
|
||||
var isIntercepted = false;
|
||||
c2.ApplicationMessageReceivedAsync += e =>
|
||||
{
|
||||
isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(e.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
|
||||
isIntercepted = string.Equals("extended", Encoding.UTF8.GetString(e.ApplicationMessage.Payload), StringComparison.Ordinal);
|
||||
return CompletedTask.Instance;
|
||||
};
|
||||
|
||||
|
||||
@@ -295,10 +295,10 @@ public sealed class HotSwapCerts_Tests
|
||||
}
|
||||
}
|
||||
|
||||
class HotSwappableClientCertProvider : IMqttClientCertificatesProvider, IDisposable
|
||||
sealed class HotSwappableClientCertProvider : IMqttClientCertificatesProvider, IDisposable
|
||||
{
|
||||
X509Certificate2Collection _certificates;
|
||||
ConcurrentBag<X509Certificate2> _serverCerts = new ConcurrentBag<X509Certificate2>();
|
||||
ConcurrentBag<X509Certificate2> _serverCerts = new();
|
||||
|
||||
public HotSwappableClientCertProvider()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// 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;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -16,10 +17,15 @@ namespace MQTTnet.Tests.Server;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
[TestClass]
|
||||
public sealed class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
public sealed class MqttSubscriptionsManager_Tests : BaseTestClass, IDisposable
|
||||
{
|
||||
MqttClientSubscriptionsManager _subscriptionsManager;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_subscriptionsManager?.Dispose();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
|
||||
{
|
||||
@@ -46,7 +52,7 @@ public sealed class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
{
|
||||
TopicFilters =
|
||||
[
|
||||
new() { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce }
|
||||
new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce }
|
||||
]
|
||||
};
|
||||
|
||||
@@ -178,6 +184,48 @@ public sealed class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
CheckIsNotSubscribed("house/2/room/bed");
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
var logger = new TestLogger();
|
||||
var options = new MqttServerOptions();
|
||||
var retainedMessagesManager = new MqttRetainedMessagesManager(new MqttServerEventContainer(), logger);
|
||||
var eventContainer = new MqttServerEventContainer();
|
||||
var clientSessionManager = new MqttClientSessionsManager(options, retainedMessagesManager, eventContainer, logger);
|
||||
|
||||
var session = new MqttSession(
|
||||
new MqttConnectPacket
|
||||
{
|
||||
ClientId = ""
|
||||
},
|
||||
new ConcurrentDictionary<object, object>(),
|
||||
options,
|
||||
eventContainer,
|
||||
retainedMessagesManager,
|
||||
clientSessionManager);
|
||||
|
||||
_subscriptionsManager = new MqttClientSubscriptionsManager(session, new MqttServerEventContainer(), retainedMessagesManager, clientSessionManager);
|
||||
}
|
||||
|
||||
void CheckIsNotSubscribed(string topic)
|
||||
{
|
||||
var result = CheckSubscriptions(topic, MqttQualityOfServiceLevel.AtMostOnce, "");
|
||||
Assert.IsFalse(result.IsSubscribed);
|
||||
}
|
||||
|
||||
void CheckIsSubscribed(string topic)
|
||||
{
|
||||
var result = CheckSubscriptions(topic, MqttQualityOfServiceLevel.AtMostOnce, "");
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, result.QualityOfServiceLevel);
|
||||
}
|
||||
|
||||
CheckSubscriptionsResult CheckSubscriptions(string topic, MqttQualityOfServiceLevel applicationMessageQoSLevel, string senderClientId)
|
||||
{
|
||||
MqttTopicHash.Calculate(topic, out var topicHash, out _, out _);
|
||||
return _subscriptionsManager.CheckSubscriptions(topic, topicHash, applicationMessageQoSLevel, senderClientId);
|
||||
}
|
||||
|
||||
async Task SubscribeToTopic(string topic)
|
||||
{
|
||||
var sp = new MqttSubscribePacket
|
||||
@@ -190,40 +238,4 @@ public sealed class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
|
||||
await _subscriptionsManager.Subscribe(sp, CancellationToken.None);
|
||||
}
|
||||
|
||||
void CheckIsSubscribed(string topic)
|
||||
{
|
||||
var result = CheckSubscriptions(topic, MqttQualityOfServiceLevel.AtMostOnce, "");
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(MqttQualityOfServiceLevel.AtMostOnce, result.QualityOfServiceLevel);
|
||||
}
|
||||
|
||||
void CheckIsNotSubscribed(string topic)
|
||||
{
|
||||
var result = CheckSubscriptions(topic, MqttQualityOfServiceLevel.AtMostOnce, "");
|
||||
Assert.IsFalse(result.IsSubscribed);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void TestInitialize()
|
||||
{
|
||||
var logger = new TestLogger();
|
||||
var options = new MqttServerOptions();
|
||||
var retainedMessagesManager = new MqttRetainedMessagesManager(new MqttServerEventContainer(), logger);
|
||||
var eventContainer = new MqttServerEventContainer();
|
||||
var clientSessionManager = new MqttClientSessionsManager(options, retainedMessagesManager, eventContainer, logger);
|
||||
|
||||
var session = new MqttSession(new MqttConnectPacket
|
||||
{
|
||||
ClientId = ""
|
||||
}, new ConcurrentDictionary<object, object>(), options, eventContainer, retainedMessagesManager, clientSessionManager);
|
||||
|
||||
_subscriptionsManager = new MqttClientSubscriptionsManager(session, new MqttServerEventContainer(), retainedMessagesManager, clientSessionManager);
|
||||
}
|
||||
|
||||
CheckSubscriptionsResult CheckSubscriptions(string topic, MqttQualityOfServiceLevel applicationMessageQoSLevel, string senderClientId)
|
||||
{
|
||||
MqttTopicHash.Calculate(topic, out var topicHash, out _, out _);
|
||||
return _subscriptionsManager.CheckSubscriptions(topic, topicHash, applicationMessageQoSLevel, senderClientId);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class Retained_Messages_Tests : BaseTestClass
|
||||
var c1 = await testEnvironment.ConnectClient();
|
||||
|
||||
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[3]).WithRetainFlag().Build());
|
||||
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload(new byte[0]).WithRetainFlag().Build());
|
||||
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("retained").WithPayload([]).WithRetainFlag().Build());
|
||||
|
||||
await c1.DisconnectAsync();
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ public class Security_Tests : BaseTestClass
|
||||
var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).WithCredentials(username, password).Build();
|
||||
|
||||
var ex = await Assert.ThrowsExactlyAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
|
||||
Assert.IsInstanceOfType(ex.InnerException, typeof(MqttProtocolViolationException));
|
||||
Assert.IsInstanceOfType<MqttProtocolViolationException>(ex.InnerException);
|
||||
Assert.AreEqual("Error while authenticating. If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].", ex.Message, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -222,7 +223,7 @@ public sealed class Subscribe_Tests : BaseTestClass
|
||||
|
||||
for (var i = 0; i < 500; i++)
|
||||
{
|
||||
var so = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(i.ToString()).Build();
|
||||
var so = new MqttClientSubscribeOptionsBuilder().WithTopicFilter(i.ToString(CultureInfo.InvariantCulture)).Build();
|
||||
|
||||
await c1.SubscribeAsync(so);
|
||||
|
||||
@@ -234,7 +235,7 @@ public sealed class Subscribe_Tests : BaseTestClass
|
||||
var messageBuilder = new MqttApplicationMessageBuilder();
|
||||
for (var i = 0; i < 500; i++)
|
||||
{
|
||||
messageBuilder.WithTopic(i.ToString());
|
||||
messageBuilder.WithTopic(i.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
await c2.PublishAsync(messageBuilder.Build());
|
||||
|
||||
@@ -264,7 +265,7 @@ public sealed class Subscribe_Tests : BaseTestClass
|
||||
var optionsBuilder = new MqttClientSubscribeOptionsBuilder();
|
||||
for (var i = 0; i < 500; i++)
|
||||
{
|
||||
optionsBuilder.WithTopicFilter(i.ToString());
|
||||
optionsBuilder.WithTopicFilter(i.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
await c1.SubscribeAsync(optionsBuilder.Build());
|
||||
@@ -274,7 +275,7 @@ public sealed class Subscribe_Tests : BaseTestClass
|
||||
var messageBuilder = new MqttApplicationMessageBuilder();
|
||||
for (var i = 0; i < 500; i++)
|
||||
{
|
||||
messageBuilder.WithTopic(i.ToString());
|
||||
messageBuilder.WithTopic(i.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
await c2.PublishAsync(messageBuilder.Build());
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ using MQTTnet.Tests.Mockups;
|
||||
namespace MQTTnet.Tests.Server;
|
||||
|
||||
[TestClass]
|
||||
public sealed class SubscriptionTopicHashTests
|
||||
public sealed class SubscriptionTopicHashTests : IDisposable
|
||||
{
|
||||
MqttSession _clientSession;
|
||||
|
||||
@@ -84,6 +84,11 @@ public sealed class SubscriptionTopicHashTests
|
||||
CheckTopicHash("client1/+/level1/#", 0x65004A0000000000, 0xFF00FF0000000000);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_clientSession?.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test long topic name with last level being #
|
||||
/// </summary>
|
||||
@@ -414,7 +419,7 @@ public sealed class SubscriptionTopicHashTests
|
||||
foreach (var t in topics)
|
||||
{
|
||||
// replace last letter with x
|
||||
topicsToFind.Add(t.Substring(0, t.Length - 1) + "x");
|
||||
topicsToFind.Add(string.Concat(t.AsSpan(0, t.Length - 1), "x"));
|
||||
}
|
||||
|
||||
matchCount = CheckTopicSubscriptions(_clientSession, topicsToFind, "No Wildcard No Match (Replace X)");
|
||||
@@ -468,7 +473,7 @@ public sealed class SubscriptionTopicHashTests
|
||||
Assert.AreEqual(0xff, hashMaskBytes[7], "mask 7 mismatch");
|
||||
}
|
||||
|
||||
void CheckTopicHash(string topic, ulong expectedHash, ulong expectedHashMask)
|
||||
static void CheckTopicHash(string topic, ulong expectedHash, ulong expectedHashMask)
|
||||
{
|
||||
MqttTopicHash.Calculate(topic, out var topicHash, out var hashMask, out _);
|
||||
|
||||
@@ -481,7 +486,7 @@ public sealed class SubscriptionTopicHashTests
|
||||
Assert.AreEqual(expectedHashMask, hashMask, "Topic hash mask not as expected");
|
||||
}
|
||||
|
||||
int CheckTopicSubscriptions(MqttSession clientSession, List<string> topicsToFind, string subject)
|
||||
static int CheckTopicSubscriptions(MqttSession clientSession, List<string> topicsToFind, string subject)
|
||||
{
|
||||
var matchCount = 0;
|
||||
|
||||
@@ -517,7 +522,7 @@ public sealed class SubscriptionTopicHashTests
|
||||
return matchCount;
|
||||
}
|
||||
|
||||
byte[] GetBytes(ulong value)
|
||||
static byte[] GetBytes(ulong value)
|
||||
{
|
||||
var bytes = BitConverter.GetBytes(value);
|
||||
// Ensure that highest byte comes first for comparison left to right
|
||||
|
||||
@@ -71,6 +71,6 @@ public sealed class Topic_Alias_Tests : BaseTestClass
|
||||
|
||||
Assert.HasCount(3, receivedTopics);
|
||||
CollectionAssert.AllItemsAreNotNull(receivedTopics);
|
||||
Assert.IsTrue(receivedTopics.All(t => t.Equals("this_is_the_topic")));
|
||||
Assert.IsTrue(receivedTopics.All(t => t.Equals("this_is_the_topic", System.StringComparison.Ordinal)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user