Files
MQTTnet/Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs
T
980a5d0a6d Improve performance and drop support for old frameworks (#2068)
* Remove WebSocket4Net extension library

* Remove UWP support

* Drop support for all old frameworks

* Update CI

* Update CI

* Update CI

* Update CI

* Remove obsolete code

* Set default protocol version to 5.0.0

* Remove obsolete tests

* Remove old dependencies

* Reset release notes

* Move server to dedicated project

* Fix unit tests

* Update and cleanup nuget packages

* Fix unit tests
Refactor code

* No longer throw exception when receiving non success CONNACK

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable nuget signing

* Enable code signing

* Enable code signing

* Enable code signing

* Fix client endpoint handling

* Improve CI

* Fix CI

* Fix CI

* Fix CI

* Fix CI

* Import changes from version 4

* Cleanup target frameworks and remove obsolete code

* Drop support for dotnet7.0

* Remove managed client

* Version5 auditmode (#2016)

Added NuGet audit mode and some other settings to get informed about security issues.

* Address warnings from analyzers

* Support for ReadOnlySequence as payload (#2046)

* use ReadOnlySequence as payload

* fix build

* fix build

* skip sign in forks

* remove unnecessary owner

* tests and extensions

* remove test

* simplify WritePacketBuffer, remove unused helper

* Adjust namespaces

* Fix build errors

* Import latest fixes from main branch

* Adjust namespaces

---------

Co-authored-by: SeppPenner <HansM2013@gmx.de>
Co-authored-by: Martin Regen <mregen@microsoft.com>
2024-08-24 11:47:21 +02:00

232 lines
8.7 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 Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.Formatter;
using MQTTnet.Tests.Mockups;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Internal;
using MQTTnet.Protocol;
using MQTTnet.Server;
namespace MQTTnet.Tests.MQTTv5
{
[TestClass]
public sealed class Server_Tests : BaseTestClass
{
[TestMethod]
public async Task Will_Message_Send()
{
using (var testEnvironment = CreateTestEnvironment())
{
await testEnvironment.StartServer();
var clientOptions = new MqttClientOptionsBuilder().WithWillTopic("My/last/will").WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce).WithProtocolVersion(MqttProtocolVersion.V500);
var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
var receivedMessagesCount = 0;
c1.ApplicationMessageReceivedAsync += e =>
{
Interlocked.Increment(ref receivedMessagesCount);
return CompletedTask.Instance;
};
await c1.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("#").Build());
var c2 = await testEnvironment.ConnectClient(clientOptions);
c2.Dispose(); // Dispose will not send a DISCONNECT packet first so the will message must be sent.
await LongTestDelay();
Assert.AreEqual(1, receivedMessagesCount);
}
}
[TestMethod]
public async Task Validate_IsSessionPresent()
{
using (var testEnvironment = CreateTestEnvironment())
{
// Create server with persistent sessions enabled
await testEnvironment.StartServer(o => o.WithPersistentSessions());
const string ClientId = "Client1";
// Create client without clean session and long session expiry interval
var options1 = CreateClientOptions(testEnvironment, ClientId, false, 9999);
var client1 = await testEnvironment.ConnectClient(options1);
// Disconnect; empty session should remain on server
await client1.DisconnectAsync();
// Simulate some time delay between connections
await Task.Delay(1000);
// Reconnect the same client ID to existing session
var client2 = testEnvironment.CreateClient();
var options2 = CreateClientOptions(testEnvironment, ClientId, false, 9999);
var result = await client2.ConnectAsync(options2).ConfigureAwait(false);
await client2.DisconnectAsync();
// Session should be present
Assert.IsTrue(result.IsSessionPresent, "Session not present");
}
}
[TestMethod]
public async Task Connect_with_Undefined_SessionExpiryInterval()
{
using (var testEnvironment = CreateTestEnvironment())
{
// Create server with persistent sessions enabled
await testEnvironment.StartServer(o => o.WithPersistentSessions());
const string ClientId = "Client1";
// Create client without clean session and NO session expiry interval,
// that means, the session should not persist
var options1 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var client1 = await testEnvironment.ConnectClient(options1);
// Disconnect; no session should remain on server because the session expiry interval was undefined
await client1.DisconnectAsync();
// Simulate some time delay between connections
await Task.Delay(1000);
// Reconnect the same client ID to existing session
var client2 = testEnvironment.CreateClient();
var options2 = CreateClientOptions(testEnvironment, ClientId, false, 9999);
var result = await client2.ConnectAsync(options2).ConfigureAwait(false);
await client2.DisconnectAsync();
// Session should not be present
Assert.IsTrue(!result.IsSessionPresent, "Session is present when it should not");
}
}
[TestMethod]
public async Task Reconnect_with_different_SessionExpiryInterval()
{
using (var testEnvironment = CreateTestEnvironment())
{
// Create server with persistent sessions enabled
await testEnvironment.StartServer(o => o.WithPersistentSessions());
const string ClientId = "Client1";
// Create client with clean session and session expiry interval > 0
var options = CreateClientOptions(testEnvironment, ClientId, true, 9999);
var client1 = await testEnvironment.ConnectClient(options);
// Disconnect; session should remain on server
await client1.DisconnectAsync();
await Task.Delay(1000);
// Reconnect the same client ID to the existing session but leave session expiry interval undefined this time.
// Session should be present because the client1 connection had SessionExpiryInterval > 0
var client2 = testEnvironment.CreateClient();
var options2 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var result2 = await client2.ConnectAsync(options2).ConfigureAwait(false);
await client2.DisconnectAsync();
Assert.IsTrue(result2.IsSessionPresent, "Session is not present when it should");
// Simulate some time delay between connections
await Task.Delay(1000);
// Reconnect the same client ID.
// No session should be present because the previous session expiry interval was undefined for the client2 connection
var client3 = testEnvironment.CreateClient();
var options3 = CreateClientOptions(testEnvironment, ClientId, false, 0);
var result3 = await client2.ConnectAsync(options3).ConfigureAwait(false);
await client3.DisconnectAsync();
// Session should be present
Assert.IsTrue(!result3.IsSessionPresent, "Session is present when it should not");
}
}
[TestMethod]
public async Task Disconnect_with_Reason()
{
using (var testEnvironment = CreateTestEnvironment())
{
var disconnectReason = MqttClientDisconnectReason.UnspecifiedError;
string testClientId = null;
await testEnvironment.StartServer();
testEnvironment.Server.ClientConnectedAsync += e =>
{
testClientId = e.ClientId;
return CompletedTask.Instance;
};
var client = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500));
client.DisconnectedAsync += e =>
{
disconnectReason = e.Reason;
return CompletedTask.Instance;
};
await LongTestDelay();
// Test client should be connected now
Assert.IsTrue(testClientId != null);
// Have the server disconnect the client with AdministrativeAction reason
await testEnvironment.Server.DisconnectClientAsync(testClientId, MqttDisconnectReasonCode.AdministrativeAction);
await LongTestDelay();
// The reason should be returned to the client in the DISCONNECT packet
Assert.AreEqual(MqttClientDisconnectReason.AdministrativeAction, disconnectReason);
}
}
static MqttClientOptions CreateClientOptions(TestEnvironment testEnvironment, string clientId, bool cleanSession, uint sessionExpiryInterval)
{
return testEnvironment.ClientFactory.CreateClientOptionsBuilder()
.WithProtocolVersion(MqttProtocolVersion.V500)
.WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
.WithSessionExpiryInterval(sessionExpiryInterval)
.WithCleanSession(cleanSession)
.WithClientId(clientId)
.Build();
}
}
}