Exemplo n.º 1
0
        public void TestMulticastMessagFromMulticastEndpoint()
        {
            // Arrange
            var mockclientEndpoint = new Mock <ICoapEndpoint>();
            var mockPayload        = new Mock <CoapPayload>();

            var messageReceived = new TaskCompletionSource <bool>();

            var expected = new CoapMessage
            {
                Type    = CoapMessageType.NonConfirmable,
                Code    = CoapMessageCode.Get,
                Options = new System.Collections.Generic.List <CoapOption>
                {
                    new CoAPConnector.Options.ContentFormat(CoAPConnector.Options.ContentFormatType.ApplicationLinkFormat)
                },
                Payload = Encoding.UTF8.GetBytes("</.well-known/core>")
            };

            mockPayload
            .Setup(p => p.Payload)
            .Returns(() => expected.Serialise());

            mockclientEndpoint.Setup(c => c.IsMulticast).Returns(true);
            mockclientEndpoint
            .Setup(c => c.SendAsync(It.IsAny <CoapPayload>()))
            .Returns(Task.CompletedTask);
            mockclientEndpoint
            .SetupSequence(c => c.ReceiveAsync())
            .Returns(Task.FromResult(mockPayload.Object))
            .Throws(new CoapEndpointException("Endpoint closed"));


            // Ack
            using (var m_client = new Coapclient(mockclientEndpoint.Object))
            {
                m_client.OnMessageReceived += (s, e) => messageReceived.SetResult(e?.m_Message?.IsMulticast ?? false);
                m_client.Listen(); // enable loop back thingy

                messageReceived.Task.Wait(m_MaxTaskTimeout);
            }

            // Assert
            Assert.True(messageReceived.Task.IsCompleted, "Took too long to receive message");
            Assert.True(messageReceived.Task.Result, "Message is not marked as Multicast");
        }
Exemplo n.º 2
0
        public void TestMulticastMessageIsNonConfirmable()
        {
            // Arrange
            var mockclientEndpoint = new Mock <ICoapEndpoint>();
            var closedEventSource  = new TaskCompletionSource <bool>();

            mockclientEndpoint.Setup(c => c.IsMulticast).Returns(true);
            mockclientEndpoint
            .Setup(c => c.SendAsync(It.IsAny <CoapPayload>()))
            .Returns(Task.CompletedTask);
            mockclientEndpoint
            .SetupSequence(c => c.ReceiveAsync())
            .Returns(Task.FromResult(new CoapPayload
            {
                Payload = new byte[] { 0x40, 0x00, 0x12, 0x34, 0xFF, 0x12, 0x34 }     // "Empty" Confirmable Message with a payload
            }))
            .Returns(Task.FromResult(new CoapPayload
            {
                Payload = new byte[] { 0x60, 0x00, 0x12, 0x34, 0xFF, 0x12, 0x34 }     // "Empty" Acknowledge Message with a payload (ignored)
            }))
            .Throws(new CoapEndpointException("Endpoint closed"));


            // Ack
            using (var m_client = new Coapclient(mockclientEndpoint.Object))
            {
                m_client.OnClosed += (s, e) => closedEventSource.SetResult(true);
                m_client.Listen(); // enable loop back thingy

                closedEventSource.Task.Wait(m_MaxTaskTimeout);
            }

            // Assert
            Assert.True(closedEventSource.Task.IsCompleted, "Took too long to receive message");
            mockclientEndpoint.Verify(x => x.SendAsync(It.IsAny <CoapPayload>()), Times.Never, "Multicast Message was responded to whenn it shouldn't");
        }