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.
 
 
 
 

91 lines
3.3 KiB

  1. using System.Threading.Tasks;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.Client;
  5. using MQTTnet.Client.Connecting;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Exceptions;
  8. using MQTTnet.Protocol;
  9. namespace MQTTnet.Tests.Server
  10. {
  11. [TestClass]
  12. public sealed class Security_Tests : BaseTestClass
  13. {
  14. [TestMethod]
  15. public async Task Use_Username_Null_Password_Empty()
  16. {
  17. string username = null;
  18. var password = string.Empty;
  19. using (var testEnvironment = CreateTestEnvironment())
  20. {
  21. testEnvironment.IgnoreClientLogErrors = true;
  22. await testEnvironment.StartServer();
  23. var client = testEnvironment.CreateClient();
  24. var clientOptions = new MqttClientOptionsBuilder()
  25. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
  26. .WithCredentials(username, password)
  27. .Build();
  28. var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
  29. Assert.IsInstanceOfType(ex.InnerException, typeof(MqttProtocolViolationException));
  30. 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);
  31. Assert.AreEqual(MqttClientConnectResultCode.UnspecifiedError, ex.ResultCode);
  32. }
  33. }
  34. [TestMethod]
  35. public Task Handle_Wrong_UserName()
  36. {
  37. return TestCredentials("x", "Password1");
  38. }
  39. [TestMethod]
  40. public Task Handle_Wrong_Password()
  41. {
  42. return TestCredentials("UserName", "x");
  43. }
  44. [TestMethod]
  45. public Task Handle_Wrong_UserName_And_Password()
  46. {
  47. return TestCredentials("x", "x");
  48. }
  49. private async Task TestCredentials(string userName, string password)
  50. {
  51. using (var testEnvironment = CreateTestEnvironment())
  52. {
  53. testEnvironment.IgnoreClientLogErrors = true;
  54. await testEnvironment.StartServer(testEnvironment.Factory.CreateServerOptionsBuilder()
  55. .WithConnectionValidator(c =>
  56. {
  57. if (c.Username != "UserName1")
  58. {
  59. c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
  60. }
  61. if (c.Password != "Password1")
  62. {
  63. c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
  64. }
  65. }));
  66. var client = testEnvironment.CreateClient();
  67. var clientOptions = new MqttClientOptionsBuilder()
  68. .WithTcpServer("127.0.0.1", testEnvironment.ServerPort)
  69. .WithCredentials(userName, password)
  70. .Build();
  71. var ex = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(async () => await client.ConnectAsync(clientOptions));
  72. Assert.AreEqual(MqttClientConnectResultCode.BadUserNameOrPassword, ex.Result.ResultCode);
  73. }
  74. }
  75. }
  76. }