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.
 
 
 
 

161 lines
5.7 KiB

  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MQTTnet.Client.Options;
  4. using MQTTnet.LowLevelClient;
  5. using MQTTnet.Packets;
  6. using MQTTnet.Protocol;
  7. using MQTTnet.Tests.Mockups;
  8. using System.Collections.Generic;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MQTTnet.Exceptions;
  14. namespace MQTTnet.Tests
  15. {
  16. [TestClass]
  17. public class LowLevelMqttClient_Tests
  18. {
  19. public TestContext TestContext { get; set; }
  20. [TestMethod]
  21. [ExpectedException(typeof(MqttCommunicationException))]
  22. public async Task Connect_To_Not_Existing_Server()
  23. {
  24. var client = new MqttFactory().CreateLowLevelMqttClient();
  25. var options = new MqttClientOptionsBuilder()
  26. .WithTcpServer("localhost")
  27. .Build();
  28. await client.ConnectAsync(options, CancellationToken.None).ConfigureAwait(false);
  29. }
  30. [TestMethod]
  31. public async Task Connect_And_Disconnect()
  32. {
  33. using (var testEnvironment = new TestEnvironment(TestContext))
  34. {
  35. await testEnvironment.StartServerAsync();
  36. var factory = new MqttFactory();
  37. var lowLevelClient = factory.CreateLowLevelMqttClient();
  38. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  39. await lowLevelClient.DisconnectAsync(CancellationToken.None);
  40. }
  41. }
  42. [TestMethod]
  43. public async Task Authenticate()
  44. {
  45. using (var testEnvironment = new TestEnvironment(TestContext))
  46. {
  47. await testEnvironment.StartServerAsync();
  48. var factory = new MqttFactory();
  49. var lowLevelClient = factory.CreateLowLevelMqttClient();
  50. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  51. var receivedPacket = await Authenticate(lowLevelClient).ConfigureAwait(false);
  52. await lowLevelClient.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
  53. Assert.IsNotNull(receivedPacket);
  54. Assert.AreEqual(MqttConnectReturnCode.ConnectionAccepted, receivedPacket.ReturnCode);
  55. }
  56. }
  57. [TestMethod]
  58. public async Task Subscribe()
  59. {
  60. using (var testEnvironment = new TestEnvironment(TestContext))
  61. {
  62. await testEnvironment.StartServerAsync();
  63. var factory = new MqttFactory();
  64. var lowLevelClient = factory.CreateLowLevelMqttClient();
  65. await lowLevelClient.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build(), CancellationToken.None);
  66. await Authenticate(lowLevelClient).ConfigureAwait(false);
  67. var receivedPacket = await Subscribe(lowLevelClient, "a").ConfigureAwait(false);
  68. await lowLevelClient.DisconnectAsync(CancellationToken.None).ConfigureAwait(false);
  69. Assert.IsNotNull(receivedPacket);
  70. Assert.AreEqual(MqttSubscribeReturnCode.SuccessMaximumQoS0, receivedPacket.ReturnCodes[0]);
  71. }
  72. }
  73. [TestMethod]
  74. public async Task Loose_Connection()
  75. {
  76. using (var testEnvironment = new TestEnvironment(TestContext))
  77. {
  78. testEnvironment.ServerPort = 8364;
  79. var server = await testEnvironment.StartServerAsync();
  80. var client = await testEnvironment.ConnectLowLevelClientAsync(o => o.WithCommunicationTimeout(TimeSpan.Zero));
  81. await Authenticate(client).ConfigureAwait(false);
  82. await server.StopAsync();
  83. await Task.Delay(1000);
  84. try
  85. {
  86. await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None).ConfigureAwait(false);
  87. await client.SendAsync(MqttPingReqPacket.Instance, CancellationToken.None).ConfigureAwait(false);
  88. }
  89. catch (MqttCommunicationException exception)
  90. {
  91. Assert.IsTrue(exception.InnerException is SocketException);
  92. return;
  93. }
  94. catch
  95. {
  96. Assert.Fail("Wrong exception type thrown.");
  97. }
  98. Assert.Fail("This MUST fail");
  99. }
  100. }
  101. async Task<MqttConnAckPacket> Authenticate(ILowLevelMqttClient client)
  102. {
  103. await client.SendAsync(new MqttConnectPacket
  104. {
  105. CleanSession = true,
  106. ClientId = TestContext.TestName,
  107. Username = "user",
  108. Password = Encoding.UTF8.GetBytes("pass")
  109. },
  110. CancellationToken.None).ConfigureAwait(false);
  111. return await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false) as MqttConnAckPacket;
  112. }
  113. async Task<MqttSubAckPacket> Subscribe(ILowLevelMqttClient client, string topic)
  114. {
  115. await client.SendAsync(new MqttSubscribePacket
  116. {
  117. PacketIdentifier = 1,
  118. TopicFilters = new List<MqttTopicFilter>
  119. {
  120. new MqttTopicFilter
  121. {
  122. Topic = topic
  123. }
  124. }
  125. },
  126. CancellationToken.None).ConfigureAwait(false);
  127. return await client.ReceiveAsync(CancellationToken.None).ConfigureAwait(false) as MqttSubAckPacket;
  128. }
  129. }
  130. }