Skip to content

Commit

Permalink
Ignore Acknowledge with invalid Code Class
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Dec 4, 2017
1 parent 7272c34 commit c5da5f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/CoAPNet/CoapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ await SendAsync(new CoapMessage
Type = CoapMessageType.Reset
}, payload.Endpoint);
}
if (message.Type == CoapMessageType.Acknowledgement
&& Coap.ReservedMessageCodeClasses.Contains(message.Code.Class))
continue;

throw;
}

Expand Down Expand Up @@ -279,7 +283,7 @@ public void Dispose()
throw new ArgumentOutOfRangeException($"The current message id ({messageId}) is not pending a due response");

if (responseTask.Task.IsCompleted)
return responseTask.Task.Result;
return await responseTask.Task;

if (Endpoint == null)
throw new InvalidOperationException($"{nameof(CoapClient)} is in an invalid state");
Expand Down
2 changes: 1 addition & 1 deletion src/CoAPNet/CoapMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void FromBytes(byte[] data)
}

// Performing this check after parsing the options to allow the chance of reading the message token
if (new int[] {1, 6, 7}.Contains(Code.Class))
if (Coap.ReservedMessageCodeClasses.Contains(Code.Class))
throw new CoapMessageFormatException("Message.Code can not use reserved classes");

if (badOptions.Count == 1)
Expand Down
2 changes: 2 additions & 0 deletions src/CoAPNet/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,7 @@ public static string GetMulticastIPv6ForScope(int scope)
throw new ArgumentOutOfRangeException(nameof(scope), "Scope must be in the range from 1 to 14. 0 and 15 are reserved. (See RFC 7346)");
return MulticastIPv6.Replace("X", scope.ToString("X"));
}

public static readonly int[] ReservedMessageCodeClasses = new int[] { 1, 6, 7 };
}
}
34 changes: 32 additions & 2 deletions tests/CoAPNet.Tests/CoapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,39 @@ public async Task TestIgnoreNonEmptyResetMessages()
// TODO: Test Ignore Acknowledgement Messages With Reserved Code
[Test]
[Category("[RFC7252] Section 4.2")]
public void TestIgnoreAcknowledgementMessagesWithReservedCode()
public async Task TestIgnoreAcknowledgementMessagesWithReservedCode()
{
Assert.Inconclusive("Not Implemented");
// 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())
.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));
}
}

// TODO: Test Reached Max Failed Retransmit Attempts
Expand Down

0 comments on commit c5da5f2

Please sign in to comment.