Пример #1
0
        public void SingleQos_ExactlyOnce()
        {
            var expected = new[]
            {
                (byte)0x90,
                (byte)0x03,
                (byte)0x00,
                (byte)0x02,
                (byte)0x02,
            };

            MqttMessage msg = new MqttSubscribeAckMessage()
                              .WithMessageIdentifier(2)
                              .AddQosGrant(MqttQos.ExactlyOnce);

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]); // header byte 1
            Assert.Equal <byte>(expected[1], actual[1]); // remaining length
            Assert.Equal <byte>(expected[2], actual[2]); // message id b1
            Assert.Equal <byte>(expected[3], actual[3]); // message id b2
            Assert.Equal <byte>(expected[4], actual[4]); // QOS
        }
Пример #2
0
        public void QosLevel2ExactlyOnce()
        {
            // expected response
            var expected = new[]
            {
                (byte)0x34,
                (byte)0x0E,
                (byte)0x0,
                (byte)0x4,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x0,
                (byte)0x0A,
                // message payload is here
                (byte)'h',
                (byte)'e',
                (byte)'l',
                (byte)'l',
                (byte)'o',
                (byte)'!',
            };

            MqttMessage msg = new MqttPublishMessage()
                              .WithQos(MqttQos.ExactlyOnce)
                              .WithMessageIdentifier(10)
                              .ToTopic("fred")
                              .PublishData(new[]
            {
                (byte)'h',
                (byte)'e',
                (byte)'l',
                (byte)'l',
                (byte)'o',
                (byte)'!',
            }
                                           );

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]);   // msg type of header + other bits
            Assert.Equal <byte>(expected[1], actual[1]);   // remaining length
            Assert.Equal <byte>(expected[2], actual[2]);   // first topic length byte
            Assert.Equal <byte>(expected[3], actual[3]);   // second topic length byte
            Assert.Equal <byte>(expected[4], actual[4]);   // f
            Assert.Equal <byte>(expected[5], actual[5]);   // r
            Assert.Equal <byte>(expected[6], actual[6]);   // e
            Assert.Equal <byte>(expected[7], actual[7]);   // d
            Assert.Equal <byte>(expected[8], actual[8]);   // h
            Assert.Equal <byte>(expected[9], actual[9]);   // e
            Assert.Equal <byte>(expected[10], actual[10]); // l
            Assert.Equal <byte>(expected[11], actual[11]); // l
            Assert.Equal <byte>(expected[12], actual[12]); // o
            Assert.Equal <byte>(expected[13], actual[13]); // !
        }
Пример #3
0
        public void BasicSerialization()
        {
            MqttPingRequestMessage pingReqMsg = new MqttPingRequestMessage();

            byte[] bytes = MessageSerializationHelper.GetMessageBytes(pingReqMsg);

            Assert.Equal <byte>(192, bytes[0]);
        }
Пример #4
0
        public void MultiTopic()
        {
            // double topic Subscribe
            var expected = new[]
            {
                (byte)0x82,
                (byte)0x10,
                (byte)0x00,
                (byte)0x03,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x01,
                (byte)0x00,
                (byte)0x04,
                (byte)'m',
                (byte)'a',
                (byte)'r',
                (byte)'k',
                (byte)0x02
            };

            MqttMessage msg = new MqttSubscribeMessage()
                              .ToTopic("fred")
                              .AtQos(MqttQos.AtLeastOnce)
                              .ToTopic("mark")
                              .AtQos(MqttQos.ExactlyOnce)
                              .WithMessageIdentifier(3)
                              .ExpectAcknowledgement();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]);   // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]);   // remaining length
            Assert.Equal <byte>(expected[2], actual[2]);   // Start of VH: MsgID Byte1
            Assert.Equal <byte>(expected[3], actual[3]);   // MsgID Byte 2
            Assert.Equal <byte>(expected[4], actual[4]);   // Topic Length B1
            Assert.Equal <byte>(expected[5], actual[5]);   // Topic Length B2
            Assert.Equal <byte>(expected[6], actual[6]);   // f
            Assert.Equal <byte>(expected[7], actual[7]);   // r
            Assert.Equal <byte>(expected[8], actual[8]);   // e
            Assert.Equal <byte>(expected[9], actual[9]);   // d
            Assert.Equal <byte>(expected[10], actual[10]); // Qos (LeastOnce)
            Assert.Equal <byte>(expected[11], actual[11]); // Topic Length B1
            Assert.Equal <byte>(expected[12], actual[12]); // Topic Length B2
            Assert.Equal <byte>(expected[13], actual[13]); // m
            Assert.Equal <byte>(expected[14], actual[14]); // a
            Assert.Equal <byte>(expected[15], actual[15]); // r
            Assert.Equal <byte>(expected[16], actual[16]); // k
            Assert.Equal <byte>(expected[17], actual[17]); // Qos (ExactlyOnce)
        }
Пример #5
0
        public void WithNonDefaultQos()
        {
            // set up some publish data
            MqttPublishMessage msg = new MqttPublishMessage()
                                     .ToTopic("mark")
                                     .WithQos(MqttQos.AtLeastOnce)
                                     .WithMessageIdentifier(4)
                                     .PublishData(new[] { (byte)9 });

            var msgBytes = MessageSerializationHelper.GetMessageBytes(msg);

            // 2 (header + 6 (topic) + 2 (msg id) + 1 (data size)
            Assert.Equal <int>(11, msgBytes.Length);
        }
Пример #6
0
        public void BasicSerialization()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                                     .WithClientIdentifier("mark")
                                     .KeepAliveFor(30)
                                     .StartClean();

            Console.WriteLine(msg);

            byte[] mb = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal <byte>(18, mb[1]);
        }
Пример #7
0
        public void BasicDeserialization()
        {
            // standard byte array of a ping response message.
            var expected = new[]
            {
                (byte)0xD0,
                (byte)0x00,
            };

            MqttPingResponseMessage msg = new MqttPingResponseMessage();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]); // first header byte
            Assert.Equal <byte>(expected[1], actual[1]); // message size byte
        }
Пример #8
0
        public void SingleTopic()
        {
            // simple single topic Subscribe message
            var expected = new[]
            {
                (byte)0x8A,
                (byte)0x09,
                (byte)0x00,
                (byte)0x02,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x01,
            };

            MqttMessage msg = new MqttSubscribeMessage()
                              .ToTopic("fred")
                              .AtQos(MqttQos.AtLeastOnce)
                              .WithMessageIdentifier(2)
                              .ExpectAcknowledgement()
                              .IsDuplicate();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]);   // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]);   // remaining length
            Assert.Equal <byte>(expected[2], actual[2]);   // Start of VH: MsgID Byte1
            Assert.Equal <byte>(expected[3], actual[3]);   // MsgID Byte 2
            Assert.Equal <byte>(expected[4], actual[4]);   // Topic Length B1
            Assert.Equal <byte>(expected[5], actual[5]);   // Topic Length B2
            Assert.Equal <byte>(expected[6], actual[6]);   // f
            Assert.Equal <byte>(expected[7], actual[7]);   // r
            Assert.Equal <byte>(expected[8], actual[8]);   // e
            Assert.Equal <byte>(expected[9], actual[9]);   // d
            Assert.Equal <byte>(expected[10], actual[10]); // d
        }
Пример #9
0
        public void BasicSerialization()
        {
            // Message Specs________________
            // <E0><00>
            var expected = new[]
            {
                (byte)0xE0,
                (byte)0x00
            };

            MqttDisconnectMessage msg = new MqttDisconnectMessage();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]); // first header byte
            Assert.Equal <byte>(expected[1], actual[1]); // message size byte
        }
Пример #10
0
        public void BasicSerialization()
        {
            var expected = new[]
            {
                (byte)0x70,
                (byte)0x02,
                (byte)0x0,
                (byte)0x4,
            };

            MqttPublishCompleteMessage msg = new MqttPublishCompleteMessage().WithMessageIdentifier(4);

            Console.WriteLine(msg.ToString());

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]); // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]); // remaining length
            Assert.Equal <byte>(expected[2], actual[2]); // connect ack - compression? always empty
            Assert.Equal <byte>(expected[3], actual[3]); // return code.
        }
Пример #11
0
        public void WithWillSet()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                                     .WithProtocolName("MQIsdp")
                                     .WithProtocolVersion(3)
                                     .WithClientIdentifier("mark")
                                     .KeepAliveFor(30)
                                     .StartClean()
                                     .Will()
                                     .WithWillQos(MqttQos.AtLeastOnce)
                                     .WithWillRetain()
                                     .WithWillTopic("willTopic")
                                     .WithWillMessage("willMessage");

            Console.WriteLine(msg);

            byte[] mb = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal <byte>(42, mb[1]);
        }
Пример #12
0
        public void IdentifierRejected()
        {
            // our expected result
            var expected = new[]
            {
                (byte)0x20,
                (byte)0x02,
                (byte)0x0,
                (byte)0x2,
            };

            MqttConnectAckMessage msg = new MqttConnectAckMessage().WithReturnCode(MqttConnectReturnCode.IdentifierRejected);

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]); // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]); // remaining length
            Assert.Equal <byte>(expected[2], actual[2]); // connect ack - compression? always empty
            Assert.Equal <byte>(expected[3], actual[3]); // return code.
        }