public void ClientIdValidationCatchesNullValue()
        {
            var msgBuilder = new MqttConnectMessageBuilder
            {
                Duplicate            = false,
                QualityOfService     = QualityOfService.ExactlyOnce,
                Retain               = true,
                CleanSession         = false,
                WillQualityOfService = QualityOfService.AtLeastOnce,
                WillRetainFlag       = false,
            };

            try
            {
                var msg1 = msgBuilder.GetMessage() as MqttConnectMessage;
            }
            catch (ArgumentException)
            {
                return;
            }
            catch (Exception)
            {
                Assert.Fail("Incorrect exception type thrown.");
            }

            Assert.Fail("No exception thrown for invalid ClientId");
        }
Example #2
0
        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 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 CanReadUserName()
        {
            var msgBuilder = new MqttConnectMessageBuilder
            {
                Duplicate            = false,
                QualityOfService     = QualityOfService.ExactlyOnce,
                Retain               = false,
                CleanSession         = false,
                WillQualityOfService = QualityOfService.AtLeastOnce,
                WillRetainFlag       = false,
                ClientId             = "A_Device",
                UserName             = "******"
            };

            var msg = msgBuilder.GetMessage() as MqttConnectMessage;

            Assert.AreEqual("Slartibartfast", msg.UserName);
        }
        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 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 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 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);
        }
        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);
        }