mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
commit2d94abdde3Author: Christian <christian.kratky@googlemail.com> Date: Sat Nov 13 10:09:59 2021 +0100 Update MQTTnet.nuspec Update docs. commitf0ea13c07fAuthor: logicaloud <47704763+logicaloud@users.noreply.github.com> Date: Sat Nov 13 22:05:54 2021 +1300 Fix MQTT 3.1.1 session persistence when CleanSession=1 (#1294) commit47cd5aa3cdAuthor: Volker Milbrandt <milbrandt@users.noreply.github.com> Date: Sat Nov 13 10:02:55 2021 +0100 test ResultCode for invalid credentials (no userName but password) (#1270) fix order of parameters in exceptions AreEqual(expected, actual) commit258df6bb14Author: HansM <HansM2013@gmx.de> Date: Thu Nov 11 20:10:35 2021 +0100 Removed IDisposable from MqttClientConnection class. (#1288) commit85c3c0b352Author: SeppPenner <HansM2013@gmx.de> Date: Mon Nov 8 08:50:59 2021 +0100 MQTTnet.noUWP.sln: Removed no longer present MQTTnet.Server project.
92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using MQTTnet.Adapter;
|
|
using MQTTnet.Client;
|
|
using MQTTnet.Exceptions;
|
|
using MQTTnet.Protocol;
|
|
|
|
namespace MQTTnet.Tests.Server
|
|
{
|
|
[TestClass]
|
|
public sealed class Security_Tests : BaseTestClass
|
|
{
|
|
[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);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_UserName()
|
|
{
|
|
return TestCredentials("x", "Password1");
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_Password()
|
|
{
|
|
return TestCredentials("UserName", "x");
|
|
}
|
|
|
|
[TestMethod]
|
|
public Task Handle_Wrong_UserName_And_Password()
|
|
{
|
|
return TestCredentials("x", "x");
|
|
}
|
|
|
|
private 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 Task.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);
|
|
}
|
|
}
|
|
}
|
|
} |