public void ConnectAsyncCallsConnectCompleteEventWithEventData() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; MqttProtocolInformation.Settings.KeepAliveTime = 5*60; client.ConnectComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect)); are.Set(); }; client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!are.WaitOne(5000)) { Assert.Fail("ConnectComplete event not fired."); } }
public async void ConnectAsyncCallsSocketConnectWithDefaultParams() { var ip = "1.1.1.1"; var socketMock = new Mock<ISocketAdapter>(); var loggerMock = new Mock<ILogger>(); var client = new MqttClient(socketMock.Object, loggerMock.Object, SocketEncryption.None); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest", CleanSession = true, Duplicate = false, KeepAliveTime = 15, UserName = "******", Password = "******", QualityOfService = QualityOfService.ExactlyOnce }; await client.ConnectWithMessageAsync(bldr, ip); socketMock.Verify(socket => socket.ConnectAsync( It.Is<string>(s => s.Equals(ip)), It.Is<int>(i => i == MqttProtocolInformation.Settings.Port), // Called with default port? It.Is<SocketEventArgs>(a => a != null) ), Times.Once()); }
public void ConnectAsync(MqttConnectMessageBuilder bldr, string ipOrHost, int port, SocketEncryption encryption, object eventData) { var args = new SocketEventArgs { EncryptionLevel = encryption, ClientUid = GenerateClientUid(bldr) }; args.OnOperationComplete((eventArgs) => { OnTcpConnectAsyncCompleted(eventArgs, eventData); if (eventArgs.SocketException == null) { SendMessageAsync(bldr, eventData, eventArgs.ClientUid); } else { FireConnectComplete(new MqttNetEventArgs { Message = bldr.GetMessage(), Exception = eventArgs.SocketException, AdditionalErrorInfo = eventArgs.AdditionalErrorInfo, EventData = eventData, ClientUid = args.ClientUid }); } }); Socket.ConnectAsync(ipOrHost, port, args); }
public void ConnectDisconnectConnectSequenceDoesNotThrow() { var areConnect = new AutoResetEvent(false); var areDisconnect = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; var moqSocket = new MoqSocket(); var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; client.ConnectComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Connect)); areConnect.Set(); }; client.SendMessageComplete += (sender, args) => { Assert.AreSame(eventData, args.EventData); Assert.IsTrue(moqSocket.SentMessages.Contains(MessageType.Disconnect)); areDisconnect.Set(); }; client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!areConnect.WaitOne(5000)) { Assert.Fail("First ConnectComplete event did not fire."); } client.DisconnectAsync(eventData, null); if (!areDisconnect.WaitOne(5000)) { Assert.Fail("First DisconnectComplete event did not fire."); } client.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); if (!areConnect.WaitOne(5000)) { Assert.Fail("Second ConnectComplete event did not fire."); } }
public void CanReadProtocolHeader() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, ClientId = "A_Device" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual(msgBuilder.Duplicate, msg.Duplicate); Assert.AreEqual(msgBuilder.QualityOfService, msg.QualityOfService); Assert.AreEqual(msgBuilder.Retain, msg.Retain); Assert.AreEqual(msgBuilder.ClientId, msg.ClientId); }
public void OnMessageCallbackGetsCalledWithWildcardTopics1() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/+", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); subClient.OnMessage(msg => { Assert.IsNotNull(msg); Assert.IsTrue(msg.MessageType == MessageType.Publish); Assert.AreEqual(msg.Payload[0], 0x00); Assert.AreEqual(msg.Payload[1], 0x01); Assert.AreEqual(msg.Payload[2], 0x02); are.Set(); }); moqSocket.ReceiveMessage(new MqttPublishMessageBuilder { TopicName = "a/b/c", Payload = new byte[] { 0x00, 0x01, 0x02 } }); if (!are.WaitOne(5000)) { Assert.Fail("OnMessage callback not called."); } }
public void CanCreateFromConnectMessageBuilder() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, ClientId = "A_Device" }; Assert.AreEqual(MessageType.Connect, msgBuilder.MessageType); var msg = msgBuilder.GetMessage(); Assert.AreEqual(typeof(MqttConnectMessage), msg.GetType()); Assert.AreEqual(MessageType.Connect, msg.MessageType); Assert.AreEqual(false, msg.Duplicate); Assert.AreEqual(QualityOfService.ExactlyOnce, msg.QualityOfService); Assert.AreEqual(true, msg.Retain); }
public async void ConnectAsyncCallsSocketConnetWithGivenParams() { var ip = "1.1.1.1"; var port = 1883; var socketMock = new Mock<ISocketAdapter>(); var loggerMock = new Mock<ILogger>(); var client = new MqttClient(socketMock.Object, loggerMock.Object, SocketEncryption.None); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; await client.ConnectWithMessageAsync(bldr, ip, port); socketMock.Verify(socket => socket.ConnectAsync( It.Is<string>(s => s.Equals(ip)), // Called with passed in IP address? It.Is<int>(i => i == port), // Called with passed in port? It.Is<SocketEventArgs>(a => a != null) ), Times.Once()); // Called once and only once }
public void CanReadConnectFlagsOpt1() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, CleanSession = true, WillQualityOfService = QualityOfService.ExactlyOnce, WillRetainFlag = true, ClientId = "A_Device", UserName = "******" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual(true, msg.CleanSession); Assert.AreEqual(false, msg.WillFlag); Assert.AreEqual(QualityOfService.ExactlyOnce, msg.WillQualityOfService); Assert.AreEqual(true, msg.WillRetain); Assert.AreEqual(false, msg.PasswordFlag); Assert.AreEqual(true, msg.UserNameFlag); }
public void IsClosedCorrectlyReflectsStateOfObject() { var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/+", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); Assert.IsTrue(subClient.IsClosed); subClient.OnMessage(msg => { Assert.IsNotNull(msg); Assert.IsTrue(msg.MessageType == MessageType.Publish); }); Assert.IsFalse(subClient.IsClosed); subClient.Close(); Assert.IsTrue(subClient.IsClosed); }
public void CanReadDefaultKeepAliveTime() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, ClientId = "A_Device", Password = "******" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual(MqttProtocolInformation.Settings.KeepAliveTime, msg.KeepAliveTime); }
public void ConnectToFailedBrokerCallsNetworkErrorEventAfterTimeout() { var are = new AutoResetEvent(false); var eventData = "Test data"; var moqSocket = new MoqSocket { DoNotRespond = true }; MqttProtocolInformation.Settings.NetworkTimeout = 5; // 5 seconds var client = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "Unit-test" }; client.SendMessageComplete += (sender, args) => Assert.Fail(); client.NetworkError += (sender, args) => are.Set(); client.SendMessageAsync(bldr, eventData, null); if (!are.WaitOne(MqttProtocolInformation.Settings.NetworkTimeout * 1000 + 5000)) { Assert.Fail("NetworkError event not fired for Connect."); } }
public void KeepAliveTimeValidateCatchesOutOfRangeValue() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, ClientId = "A_Device", Password = "******" }; try { msgBuilder.KeepAliveTime = 0x1FFFF; } catch (ArgumentException) { return; } catch (Exception) { Assert.Fail("Incorrect exception type thrown."); } Assert.Fail("No exception thrown for out of range KeepAliveTime"); }
public void CanReadZeroLengthWillMessage() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = false, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, ClientId = "A_Device", WillTopic = "a/b/c/d", WillMessage = "" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual("a/b/c/d", msg.WillTopic); Assert.AreEqual("", msg.WillMessage); Assert.AreEqual(true, msg.WillFlag); }
protected string GenerateClientUid(MqttConnectMessageBuilder msg) { return GenerateClientUid(msg.ClientId, msg.UserName); }
/// <summary> /// Connect to a broker using a message builder instance. /// </summary> /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param> /// <param name="ipOrHost">An IP address or host name</param> /// <param name="port">The port number to use. Overrides default port in settings</param> /// <returns></returns> public void ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port) { _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, MessageType.Connect); }
/// <summary> /// Connect to a broker. /// </summary> /// <param name="clientId">The MQTT client ID</param> /// <param name="ipOrHost">An IP address or host name</param> /// <param name="port">The port number to use. Overrides default port in settings</param> /// <returns></returns> public void ConnectAsync(string clientId, string ipOrHost, int port) { var bldr = new MqttConnectMessageBuilder { ClientId = clientId }; ConnectWithMessageAsync(bldr, ipOrHost, port); }
public void CanReadClientId() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, ClientId = "A_Device", Password = "******" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual("A_Device", msg.ClientId); }
public void CanReadUserNameAndPassword() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = false, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, ClientId = "A_Device", UserName = "******", Password = "******" }; var msg = msgBuilder.GetMessage() as MqttConnectMessage; Assert.AreEqual("Slartibartfast", msg.UserName); Assert.AreEqual("Magrathean", msg.Password); }
public void OnMessageCallbackDoesNotGetCalledAfterClose() { var are = new AutoResetEvent(false); var ip = "1.1.1.1"; var port = 1883; var eventData = "Test data"; string connectionKey = "123"; var moqSocket = new MoqSocket(); var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket); var bldr = new MqttConnectMessageBuilder { ClientId = "UnitTest" }; protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData); var subscriptionItem = new SubscriptionItem { TopicName = "a/b/c", QualityOfService = QualityOfService.AtMostOnce }; var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey); subClient.OnMessage(msg => { Assert.Fail("OnMessage callback was called after Close call."); are.Set(); }); subClient.Close(); moqSocket.ReceiveMessage(new MqttPublishMessageBuilder { TopicName = "a/b/c", Payload = new byte[] { 0x00, 0x01, 0x02 } }); are.WaitOne(3000); }
/// <summary> /// Connect to a broker using a message builder instance. /// </summary> /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param> /// <param name="ipOrHost">An IP address or host name</param> /// <param name="port">The port number to use. Overrides default port in settings</param> /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns> public IAsyncOperation<MqttConnectAckMessage> ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port) { var tcs = new TaskCompletionSource<MqttConnectAckMessage>(); _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, tcs); return tcs.Task.AsAsyncOperation(); }
/// <summary> /// Connect to a broker using a message builder instance. /// </summary> /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param> /// <param name="ipOrHost">An IP address or host name</param> /// <param name="port">The port number to use. Overrides default port in settings</param> /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns> public Task<MqttConnectAckMessage> ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost, int port) { var tcs = new TaskCompletionSource<MqttConnectAckMessage>(); if (_mqtt.IsConnected(_clientUid)) { _mqtt.CloseConnection(_clientUid); } _mqtt.ConnectAsync(msgBuilder, ipOrHost, port, _encryptionLevel, tcs); return tcs.Task; }
public void ClientIdValidationCatchesTooLongValue() { var msgBuilder = new MqttConnectMessageBuilder { Duplicate = false, QualityOfService = QualityOfService.ExactlyOnce, Retain = true, CleanSession = false, WillQualityOfService = QualityOfService.AtLeastOnce, WillRetainFlag = false, }; try { msgBuilder.ClientId = "123456789012345678901234"; } catch (ArgumentException) { return; } catch (Exception) { Assert.Fail("Incorrect exception type thrown."); } Assert.Fail("No exception thrown for invalid ClientId"); }
/// <summary> /// Connect to a broker using a message builder instance. /// </summary> /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param> /// <param name="ipOrHost">An IP address or host name</param> /// <returns></returns> public void ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost) { int port = IsEncrypted ? MqttProtocolInformation.Settings.SecurePort : MqttProtocolInformation.Settings.Port; ConnectWithMessageAsync(msgBuilder, ipOrHost, port); }
/// <summary> /// Connect to a broker. /// </summary> /// <param name="clientId">The MQTT client ID</param> /// <param name="ipOrHost">An IP address or host name</param> /// <param name="port">The port number to use. Overrides default port in settings</param> /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns> public IAsyncOperation<MqttConnectAckMessage> ConnectAsync(string clientId, string ipOrHost, int port) { var bldr = new MqttConnectMessageBuilder { ClientId = clientId }; return ConnectWithMessageAsync(bldr, ipOrHost, port); }
/// <summary> /// Connect to a broker. /// </summary> /// <param name="clientId">The MQTT client ID</param> /// <param name="ipOrHost">An IP address or host name</param> /// <returns></returns> public void ConnectAsync(string clientId, string ipOrHost) { int port = IsEncrypted ? MqttProtocolInformation.Settings.SecurePort : MqttProtocolInformation.Settings.Port; var bldr = new MqttConnectMessageBuilder { ClientId = clientId }; ConnectWithMessageAsync(bldr, ipOrHost, port); }
/// <summary> /// Connect to a broker using a message builder instance. /// </summary> /// <param name="msgBuilder">A message builder instance with advanced connection parameters</param> /// <param name="ipOrHost">An IP address or host name</param> /// <returns>MqttConnectAckMessage representing the message returned from the broker</returns> public IAsyncOperation<MqttConnectAckMessage> ConnectWithMessageAsync(MqttConnectMessageBuilder msgBuilder, string ipOrHost) { int port = IsEncrypted ? MqttProtocolInformation.Settings.SecurePort : MqttProtocolInformation.Settings.Port; return ConnectWithMessageAsync(msgBuilder, ipOrHost, port); }