mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
commit 3f2f8d4b0a31b20734eca3fff86c43bd1ac3e3ad
Merge: 34babbf2 d71ff3d6
Author: Christian <6939810+chkr1011@users.noreply.github.com>
Date: Sat Jul 23 11:06:22 2022 +0200
Merge branch 'master' into 487-any-way-to-generate-events-on-receipt-of-puback-or-pubrec-packets
commit 34babbf2714b742bc4f3802804a1a07159d38e22
Author: Christian <6939810+chkr1011@users.noreply.github.com>
Date: Mon Jul 11 22:56:01 2022 +0200
Fix typo.
commit add6277458f528fc03bf4a1ba228a55c5a8d1a80
Author: Christian <6939810+chkr1011@users.noreply.github.com>
Date: Sat Jul 2 12:50:13 2022 +0200
Add new event for client acknowledges.
172 lines
6.6 KiB
C#
172 lines
6.6 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.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using MQTTnet.Adapter;
|
|
using MQTTnet.Client;
|
|
using MQTTnet.Exceptions;
|
|
using MQTTnet.Implementations;
|
|
using MQTTnet.Protocol;
|
|
|
|
namespace MQTTnet.Tests.Server
|
|
{
|
|
[TestClass]
|
|
public sealed class Security_Tests : BaseTestClass
|
|
{
|
|
[TestMethod]
|
|
public async Task Do_Not_Affect_Authorized_Clients()
|
|
{
|
|
using (var testEnvironment = CreateTestEnvironment())
|
|
{
|
|
testEnvironment.IgnoreClientLogErrors = true;
|
|
|
|
await testEnvironment.StartServer();
|
|
|
|
var publishedApplicationMessages = new List<MqttApplicationMessage>();
|
|
testEnvironment.Server.InterceptingPublishAsync += eventArgs =>
|
|
{
|
|
lock (publishedApplicationMessages)
|
|
{
|
|
publishedApplicationMessages.Add(eventArgs.ApplicationMessage);
|
|
}
|
|
|
|
return PlatformAbstractionLayer.CompletedTask;
|
|
};
|
|
|
|
testEnvironment.Server.ValidatingConnectionAsync += eventArgs =>
|
|
{
|
|
if (eventArgs.Username == "SECRET")
|
|
{
|
|
eventArgs.ReasonCode = MqttConnectReasonCode.Success;
|
|
}
|
|
else
|
|
{
|
|
eventArgs.ReasonCode = MqttConnectReasonCode.NotAuthorized;
|
|
}
|
|
|
|
return PlatformAbstractionLayer.CompletedTask;
|
|
};
|
|
|
|
using (var validClient = testEnvironment.CreateClient())
|
|
{
|
|
await validClient.ConnectAsync(
|
|
testEnvironment.Factory.CreateClientOptionsBuilder()
|
|
.WithTcpServer("localhost", testEnvironment.ServerPort)
|
|
.WithCredentials("SECRET")
|
|
.WithClientId("CLIENT")
|
|
.Build());
|
|
|
|
await validClient.PublishStringAsync("HELLO 1");
|
|
|
|
// The following code tries to connect a new client with the same client ID but invalid
|
|
// credentials. This should block the second client but keep the first one up and running.
|
|
try
|
|
{
|
|
using (var invalidClient = testEnvironment.CreateClient())
|
|
{
|
|
await invalidClient.ConnectAsync(
|
|
testEnvironment.Factory.CreateClientOptionsBuilder()
|
|
.WithTcpServer("localhost", testEnvironment.ServerPort)
|
|
.WithCredentials("???")
|
|
.WithClientId("CLIENT")
|
|
.Build());
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Ignore errors from the second client.
|
|
}
|
|
|
|
await LongTestDelay();
|
|
|
|
await validClient.PublishStringAsync("HELLO 2");
|
|
|
|
await LongTestDelay();
|
|
|
|
await validClient.PublishStringAsync("HELLO 3");
|
|
|
|
await LongTestDelay();
|
|
|
|
Assert.AreEqual(3, publishedApplicationMessages.Count);
|
|
Assert.AreEqual(1, testEnvironment.Server.GetClientsAsync().GetAwaiter().GetResult().Count);
|
|
}
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_Password()
|
|
{
|
|
return TestCredentials("UserName", "x");
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_UserName()
|
|
{
|
|
return TestCredentials("x", "Password1");
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_UserName_And_Password()
|
|
{
|
|
return TestCredentials("x", "x");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task Use_Username_Null_Password_Empty()
|
|
{
|
|
string username = null;
|
|
var password = string.Empty;
|
|
|
|
using (var testEnvironment = CreateTestEnvironment())
|
|
{
|
|
testEnvironment.IgnoreClientLogErrors = true;
|
|
|
|
await testEnvironment.StartServer();
|
|
|
|
var client = testEnvironment.CreateClient();
|
|
|
|
var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).WithCredentials(username, password).Build();
|
|
|
|
var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
|
|
Assert.IsInstanceOfType(ex.InnerException, typeof(MqttProtocolViolationException));
|
|
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);
|
|
Assert.AreEqual(MqttClientConnectResultCode.UnspecifiedError, ex.ResultCode);
|
|
}
|
|
}
|
|
|
|
async Task TestCredentials(string userName, string password)
|
|
{
|
|
using (var testEnvironment = CreateTestEnvironment())
|
|
{
|
|
testEnvironment.IgnoreClientLogErrors = true;
|
|
|
|
var server = await testEnvironment.StartServer();
|
|
|
|
server.ValidatingConnectionAsync += e =>
|
|
{
|
|
if (e.Username != "UserName1")
|
|
{
|
|
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
|
}
|
|
|
|
if (e.Password != "Password1")
|
|
{
|
|
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
|
}
|
|
|
|
return PlatformAbstractionLayer.CompletedTask;
|
|
};
|
|
|
|
var client = testEnvironment.CreateClient();
|
|
|
|
var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).WithCredentials(userName, password).Build();
|
|
|
|
var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
|
|
Assert.AreEqual(MqttClientConnectResultCode.BadUserNameOrPassword, ex.Result.ResultCode);
|
|
}
|
|
}
|
|
}
|
|
} |