public void TestMulticastIPv6_Offset()
        {
            Uri             uri;
            CoapClient      client;
            List <Response> responseList = new List <Response>();
            AutoResetEvent  trigger      = new AutoResetEvent(false);

            if (!SupportsIPV6())
            {
                Assert.Inconclusive("System does not support IP v6");
            }


            uri    = new Uri($"coap://[{multicastAddress2}]:{_serverPort + PortJump}/{MulticastTarget}");
            client = new CoapClient(uri);
            client.UseNONs();
            client.GetAsync(r => {
                responseList.Add(r);
                trigger.Set();
            });

            Assert.IsTrue(trigger.WaitOne(2 * 1000));

            Console.WriteLine($"response count = {responseList.Count}");
            Assert.IsTrue(responseList.Count == 1);
            foreach (Response r in responseList)
            {
                Assert.AreEqual(StatusCode.Content, r.StatusCode);
                Assert.AreEqual(MulticastResponse, r.ResponseText);
            }
        }
        public void TestMulticastV4_Offset()
        {
            Uri        uri;
            CoapClient client;

            //  Check that multicast returns on multicast

            List <Response> responseList = new List <Response>();
            AutoResetEvent  trigger      = new AutoResetEvent(false);

            uri    = new Uri($"coap://{multicastAddress}:{_serverPort + PortJump}/{MulticastTarget}");
            client = new CoapClient(uri);
            client.UseNONs();
            client.GetAsync(r => {
                responseList.Add(r);
                trigger.Set();
            });

            trigger.WaitOne(1000);

            Assert.IsTrue(responseList.Count == 1);
            foreach (Response r in responseList)
            {
                Assert.AreEqual(StatusCode.Content, r.StatusCode);
                Assert.AreEqual(MulticastResponse, r.ResponseText);
            }
        }
        public void TestMulticastV6OfUnicastResource()
        {
            Uri            uri;
            CoapClient     client;
            AutoResetEvent trigger = new AutoResetEvent(false);

            uri    = new Uri($"coap://[{multicastAddress2}]:{_serverPort + PortJump}/{UnicastTarget}");
            client = new CoapClient(uri);
            client.UseNONs();
            client.GetAsync(r => { trigger.Set(); });

            Assert.IsFalse(trigger.WaitOne(1000));
        }
        public async Task TestClientResponseWithDelay()
        {
            // Arrange
            var mockClientEndpoint = new Mock <MockEndpoint> {
                CallBase = true
            };

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

            mockClientEndpoint
            .SetupSequence(c => c.MockReceiveAsync(It.IsAny <CancellationToken>()))
            .Returns(Task.Delay(500).ContinueWith(t => new CoapPacket {
                Payload = expected.ToBytes()
            }))
            .Throws(new CoapEndpointException("Endpoint closed"));

            // Ack
            using (var client = new CoapClient(mockClientEndpoint.Object))
            {
                var ct = new CancellationTokenSource(MaxTaskTimeout);

                client.RetransmitTimeout     = TimeSpan.FromMilliseconds(200);
                client.MaxRetransmitAttempts = 3;
                client.SetNextMessageId(0x1234);

                // Sned message
                var messageId = await client.GetAsync("coap://example.com/.well-known/core", ct.Token);

                // Receive msssage
                await client.GetResponseAsync(messageId, ct.Token);
            }

            // Assert
            mockClientEndpoint.Verify(x => x.ReceiveAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce);
        }
Beispiel #5
0
        public async Task TestClientResponse()
        {
            // Arrange
            var mockClientEndpoint = new Mock <MockEndpoint> {
                CallBase = true
            };

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

            mockClientEndpoint
            .SetupSequence(c => c.MockReceiveAsync())
            .Returns(Task.FromResult(new CoapPacket {
                Payload = expected.Serialise()
            }))
            .Throws(new CoapEndpointException("disposed"));

            // Ack
            using (var client = new CoapClient(mockClientEndpoint.Object))
            {
                var ct = new CancellationTokenSource(MaxTaskTimeout);

                client.SetNextMessageId(0x1234);
                // Sned message
                var messageId = await client.GetAsync("coap://example.com/.well-known/core", ct.Token);

                // Receive msssage
                await client.GetResponseAsync(messageId, ct.Token).ConfigureAwait(false);
            }

            // Assert
            mockClientEndpoint.Verify(x => x.ReceiveAsync(), Times.AtLeastOnce);
        }
        public void TestIgnoreAcknowledgementMessagesWithReservedCode()
        {
            // Arrange
            var mockClientEndpoint = new Mock <MockEndpoint> {
                CallBase = true
            };

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

            mockClientEndpoint
            .SetupSequence(c => c.MockReceiveAsync(It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new CoapPacket {
                Payload = expected.ToBytes()
            }))
            .Returns(Task.Delay(2000).ContinueWith <CoapPacket>(_ => throw new CoapEndpointException("Endpoint closed")));

            // Ack
            using (var client = new CoapClient(mockClientEndpoint.Object))
            {
                var ct = new CancellationTokenSource(MaxTaskTimeout);
                client.RetransmitTimeout     = TimeSpan.FromMilliseconds(200);
                client.MaxRetransmitAttempts = 3;
                client.SetNextMessageId(0x1234);

                // Assert
                Assert.ThrowsAsync <CoapClientException>(async() => await client.GetAsync("coap://example.com/.well-known/core", ct.Token));
            }
        }
        public async Task TestRepeatedMessagesAfterExpirey()
        {
            // Arrange
            var mockClientEndpoint = new Mock <MockEndpoint> {
                CallBase = true
            };

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

            mockClientEndpoint
            .SetupSequence(c => c.MockReceiveAsync(It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new CoapPacket {
                Payload = expected.ToBytes()
            }))                                                                            // Received
            .Returns(Task.FromResult(new CoapPacket {
                Payload = expected.ToBytes()
            }))                                                                            // Ignored (considered duplicate)
            .Returns(Task.Delay(500).ContinueWith(_ => new CoapPacket {
                Payload = expected.ToBytes()
            }))                                                                                              // Received after expirey
            .Throws(new CoapEndpointException("disposed"));

            int receiveCount = 0;

            // Ack
            using (var client = new CoapClient(mockClientEndpoint.Object))
            {
                var ct = new CancellationTokenSource(MaxTaskTimeout);
                client.MessageCacheTimeSpan = TimeSpan.FromMilliseconds(250);
                client.SetNextMessageId(0x1234);
                // Sned message
                var messageId = await client.GetAsync("coap://example.com/.well-known/core", ct.Token);

                try
                {
                    while (true)
                    {
                        await client.ReceiveAsync(ct.Token);

                        receiveCount++;
                    }
                }
                catch (CoapEndpointException)
                {
                    Debug.WriteLine($"Caught CoapEndpointException", nameof(TestReceiveMulticastMessagFromMulticastEndpoint));
                }
            }

            // Assert
            Assert.AreEqual(2, receiveCount, "Did not receive same message exactly twice");
        }