Skip to content

Commit

Permalink
Reject Confirmable Empty messages
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Dec 4, 2017
1 parent c5da5f2 commit 47d3115
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/CoAPNet/CoapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
46 changes: 43 additions & 3 deletions tests/CoAPNet.Tests/CoapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MockEndpoint> { 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<CoapPacket>(p => p.Payload.SequenceEqual(expected.ToBytes()))), Times.AtLeastOnce);
}

// TODO: Test Multicast Message Is Marked Multicast
Expand Down

0 comments on commit 47d3115

Please sign in to comment.