You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

112 lines
4.0 KiB

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Client.Options;
  3. using MQTTnet.LowLevelClient;
  4. using MQTTnet.Packets;
  5. using MQTTnet.Protocol;
  6. using MQTTnet.Tests.Mockups;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MQTTnet.Tests
  12. {
  13. [TestClass]
  14. public class LowLevelMqttClient_Tests
  15. {
  16. public TestContext TestContext { get; set; }
  17. [TestMethod]
  18. public async Task Connect_And_Disconnect()
  19. {
  20. using (var testEnvironment = new TestEnvironment(TestContext))
  21. {
  22. var server = await testEnvironment.StartServerAsync();
  23. var factory = new MqttFactory();
  24. var lowLevelClient = factory.CreateLowLevelMqttClient();
  25. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  26. await lowLevelClient.DisconnectAsync(CancellationToken.None);
  27. }
  28. }
  29. [TestMethod]
  30. public async Task Authenticate()
  31. {
  32. using (var testEnvironment = new TestEnvironment(TestContext))
  33. {
  34. var server = await testEnvironment.StartServerAsync();
  35. var factory = new MqttFactory();
  36. var lowLevelClient = factory.CreateLowLevelMqttClient();
  37. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  38. var receivedPacket = await Authenticate(lowLevelClient).ConfigureAwait(false);
  39. await lowLevelClient.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
  40. Assert.IsNotNull(receivedPacket);
  41. Assert.AreEqual(MqttConnectReturnCode.ConnectionAccepted, receivedPacket.ReturnCode);
  42. }
  43. }
  44. [TestMethod]
  45. public async Task Subscribe()
  46. {
  47. using (var testEnvironment = new TestEnvironment(TestContext))
  48. {
  49. var server = await testEnvironment.StartServerAsync();
  50. var factory = new MqttFactory();
  51. var lowLevelClient = factory.CreateLowLevelMqttClient();
  52. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  53. await Authenticate(lowLevelClient).ConfigureAwait(false);
  54. var receivedPacket = await Subscribe(lowLevelClient, "a").ConfigureAwait(false);
  55. await lowLevelClient.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
  56. Assert.IsNotNull(receivedPacket);
  57. Assert.AreEqual(MqttSubscribeReturnCode.SuccessMaximumQoS0, receivedPacket.ReturnCodes[0]);
  58. }
  59. }
  60. async Task<MqttConnAckPacket> Authenticate(ILowLevelMqttClient client)
  61. {
  62. await client.SendAsync(new MqttConnectPacket()
  63. {
  64. CleanSession = true,
  65. ClientId = TestContext.TestName,
  66. Username = "user",
  67. Password = Encoding.UTF8.GetBytes("pass")
  68. },
  69. CancellationToken.None).ConfigureAwait(false);
  70. return await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false) as MqttConnAckPacket;
  71. }
  72. async Task<MqttSubAckPacket> Subscribe(ILowLevelMqttClient client, string topic)
  73. {
  74. await client.SendAsync(new MqttSubscribePacket
  75. {
  76. PacketIdentifier = 1,
  77. TopicFilters = new List<MqttTopicFilter>
  78. {
  79. new MqttTopicFilter
  80. {
  81. Topic = topic
  82. }
  83. }
  84. },
  85. CancellationToken.None).ConfigureAwait(false);
  86. return await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false) as MqttSubAckPacket;
  87. }
  88. }
  89. }