From 47d311539854d51774a63f034039f3097eab62fa Mon Sep 17 00:00:00 2001 From: NZSmartie Date: Thu, 30 Nov 2017 19:06:33 +1300 Subject: [PATCH] Reject Confirmable Empty messages --- src/CoAPNet/CoapClient.cs | 8 +++++ tests/CoAPNet.Tests/CoapClientTests.cs | 46 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/CoAPNet/CoapClient.cs b/src/CoAPNet/CoapClient.cs index 778c607..c42990a 100644 --- a/src/CoAPNet/CoapClient.cs +++ b/src/CoAPNet/CoapClient.cs @@ -182,6 +182,14 @@ private async Task ReceiveAsyncInternal() if (message.Type == CoapMessageType.Reset && message.Code != CoapMessageCode.None) continue; + // Reject confirmable empty messages + if (message.Type == CoapMessageType.Confirmable && message.Code == CoapMessageCode.None) + { + await SendAsync(new CoapMessage { Id = message.Id, Type = CoapMessageType.Reset }, payload.Endpoint); + continue; + } + + // Ignore repeated messages if (IsRepeated(payload.Endpoint, message.Id)) continue; diff --git a/tests/CoAPNet.Tests/CoapClientTests.cs b/tests/CoAPNet.Tests/CoapClientTests.cs index f5bf2ee..de0df36 100644 --- a/tests/CoAPNet.Tests/CoapClientTests.cs +++ b/tests/CoAPNet.Tests/CoapClientTests.cs @@ -499,12 +499,52 @@ public void TestCancelRequestRetransmitAttempts() Assert.Inconclusive("Not Implemented"); } - // TODO: Test Reject Empty Non-Confirmable Message + // TODO: Test Reject Empty Confirmable Message [Test] [Category("[RFC7252] Section 4.3")] - public void TestRejectEmptyNonConfirmableMessage() + public async Task TestRejectEmptyConfirmableMessage() { - Assert.Inconclusive("Not Implemented"); + // Arrange + var mockClientEndpoint = new Mock { CallBase = true }; + + var ping = new CoapMessage + { + Id = 0x1234, + Type = CoapMessageType.Confirmable, + }; + + var expected = new CoapMessage + { + Id = 0x1234, + Type = CoapMessageType.Reset, + }; + + mockClientEndpoint + .SetupSequence(c => c.MockReceiveAsync()) + .Returns(Task.FromResult(new CoapPacket { Payload = ping.ToBytes() })) + .Throws(new CoapEndpointException("disposed")); + + // Ack + using (var client = new CoapClient(mockClientEndpoint.Object)) + { + var ct = new CancellationTokenSource(MaxTaskTimeout); + + try + { + while (true) + { + await client.ReceiveAsync(ct.Token); + } + } + catch (CoapEndpointException) + { + Debug.WriteLine($"Caught CoapEndpointException", nameof(TestReceiveMulticastMessagFromMulticastEndpoint)); + } + } + + // Assert + mockClientEndpoint.Verify(x => x.ReceiveAsync(), Times.AtLeastOnce); + mockClientEndpoint.Verify(x => x.SendAsync(It.Is(p => p.Payload.SequenceEqual(expected.ToBytes()))), Times.AtLeastOnce); } // TODO: Test Multicast Message Is Marked Multicast