|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MQTTnet.Core.Adapter;
- using MQTTnet.Core.Client;
- using MQTTnet.Core.Packets;
- using MQTTnet.Core.Protocol;
- using MQTTnet.Core.Server;
- using Microsoft.Extensions.DependencyInjection;
-
- namespace MQTTnet.Core.Tests
- {
- [TestClass]
- public class MqttServerTests
- {
- [TestMethod]
- public void MqttServer_PublishSimple_AtMostOnce()
- {
- TestPublishAsync(
- "A/B/C",
- MqttQualityOfServiceLevel.AtMostOnce,
- "A/B/C",
- MqttQualityOfServiceLevel.AtMostOnce,
- 1).Wait();
- }
-
- [TestMethod]
- public void MqttServer_PublishSimple_AtLeastOnce()
- {
- TestPublishAsync(
- "A/B/C",
- MqttQualityOfServiceLevel.AtLeastOnce,
- "A/B/C",
- MqttQualityOfServiceLevel.AtLeastOnce,
- 1).Wait();
- }
-
- [TestMethod]
- public void MqttServer_PublishSimple_ExactlyOnce()
- {
- TestPublishAsync(
- "A/B/C",
- MqttQualityOfServiceLevel.ExactlyOnce,
- "A/B/C",
- MqttQualityOfServiceLevel.ExactlyOnce,
- 1).Wait();
- }
-
- [TestMethod]
- public async Task MqttServer_WillMessage()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var willMessage = new MqttApplicationMessageBuilder().WithTopic("My/last/will").WithAtMostOnceQoS().Build();
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
-
- var receivedMessagesCount = 0;
- c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
- await c1.SubscribeAsync(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
-
- await c2.DisconnectAsync();
-
- await Task.Delay(1000);
-
- await s.StopAsync();
-
- Assert.AreEqual(1, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_Unsubscribe()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
-
- var receivedMessagesCount = 0;
- c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
-
- var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
-
- await c2.PublishAsync(message);
- Assert.AreEqual(0, receivedMessagesCount);
-
- await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
- await c2.PublishAsync(message);
-
- await Task.Delay(500);
- Assert.AreEqual(1, receivedMessagesCount);
-
- await c1.UnsubscribeAsync("a");
- await c2.PublishAsync(message);
-
- await Task.Delay(500);
- Assert.AreEqual(1, receivedMessagesCount);
-
- await s.StopAsync();
- await Task.Delay(500);
-
- Assert.AreEqual(1, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_Publish()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
-
- var receivedMessagesCount = 0;
- c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
-
- var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
- await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
-
- s.Publish(message);
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(1, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_NoRetainedMessage()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, false));
- await c1.DisconnectAsync();
-
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
- var receivedMessagesCount = 0;
- c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
- await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
-
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(0, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_RetainedMessage()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
- await c1.DisconnectAsync();
-
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
- var receivedMessagesCount = 0;
- c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
- await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
-
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(1, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_ClearRetainedMessage()
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
- await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, true));
- await c1.DisconnectAsync();
-
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
- var receivedMessagesCount = 0;
- c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
- await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
-
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(0, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_PersistRetainedMessage()
- {
- var storage = new TestStorage();
-
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- await c1.PublishAsync(new MqttApplicationMessage("retained", new byte[3], MqttQualityOfServiceLevel.AtLeastOnce, true));
- await c1.DisconnectAsync();
-
- await s.StopAsync();
-
- s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
- var receivedMessagesCount = 0;
- c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
- await c2.SubscribeAsync(new TopicFilter("retained", MqttQualityOfServiceLevel.AtLeastOnce));
-
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(1, receivedMessagesCount);
- }
-
- [TestMethod]
- public async Task MqttServer_InterceptMessage()
- {
- var options = new MqttServerOptions
- {
- ApplicationMessageInterceptor = message =>
- {
- message.Payload = Encoding.ASCII.GetBytes("extended");
- return message;
- }
- };
-
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
- await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("test").Build());
-
- var isIntercepted = false;
- c2.ApplicationMessageReceived += (sender, args) =>
- {
- isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(args.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
- };
-
- var m = new MqttApplicationMessageBuilder().WithTopic("test").Build();
- await c1.PublishAsync(m);
- await c1.DisconnectAsync();
-
- await Task.Delay(500);
-
- Assert.IsTrue(isIntercepted);
- }
-
- private class TestStorage : IMqttServerStorage
- {
- private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
-
- public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
- {
- _messages = messages;
- return Task.CompletedTask;
- }
-
- public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
- {
- return Task.FromResult(_messages);
- }
- }
-
- private static async Task TestPublishAsync(
- string topic,
- MqttQualityOfServiceLevel qualityOfServiceLevel,
- string topicFilter,
- MqttQualityOfServiceLevel filterQualityOfServiceLevel,
- int expectedReceivedMessagesCount)
- {
- var serverAdapter = new TestMqttServerAdapter();
- var services = new ServiceCollection()
- .AddMqttServer()
- .AddSingleton<IMqttServerAdapter>(serverAdapter)
- .BuildServiceProvider();
-
- var s = services.GetRequiredService<IMqttServer>();
- await s.StartAsync();
-
- var c1 = await serverAdapter.ConnectTestClient(s, "c1");
- var c2 = await serverAdapter.ConnectTestClient(s, "c2");
-
- var receivedMessagesCount = 0;
- c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
-
- await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic(topicFilter).WithQualityOfServiceLevel(filterQualityOfServiceLevel).Build());
- await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
-
- await Task.Delay(500);
- await c1.UnsubscribeAsync(topicFilter);
-
- await Task.Delay(500);
-
- await s.StopAsync();
-
- Assert.AreEqual(expectedReceivedMessagesCount, receivedMessagesCount);
- }
- }
- }
|