Skip to content

Commit

Permalink
Rename Serialise and Deserialise to ToBytes and FromBytes respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Nov 29, 2017
1 parent 7a21091 commit 4436a9f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/CoAPNet.Server/CoapHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task ProcessRequestAsync(ICoapConnectionInformation connection, byt
try
{
_logger?.LogDebug(CoapLoggingEvents.HandlerProcessRequest, "Deserialising payload");
message.Deserialise(payload);
message.FromBytes(payload);

//TODO: check if message is multicast, ignore Confirmable requests and delay response

Expand Down Expand Up @@ -116,7 +116,7 @@ await connection.LocalEndpoint.SendAsync(
new CoapPacket
{
Endpoint = connection.RemoteEndpoint,
Payload = result.Serialise()
Payload = result.ToBytes()
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/CoAPNet/CoapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ private async Task ReceiveAsyncInternal()

var payload = await Endpoint.ReceiveAsync();

var message = new CoapMessage(Endpoint.IsMulticast);
var message = new CoapMessage { IsMulticast = Endpoint.IsMulticast };
try
{
message.Deserialise(payload.Payload);
message.FromBytes(payload.Payload);
}
catch (CoapMessageFormatException)
{
Expand Down Expand Up @@ -363,7 +363,7 @@ private async Task SendAsyncInternal(CoapMessage message, ICoapEndpoint remoteEn

await Task.Run(async () => await (Endpoint?.SendAsync(new CoapPacket
{
Payload = message.Serialise(),
Payload = message.ToBytes(),
Endpoint = remoteEndpoint
}) ?? Task.CompletedTask), token).ConfigureAwait(false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/CoAPNet/CoapMessage.Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public partial class CoapMessage
/// <param name="payload"></param>
/// <param name="isMulticast">Indicates if this message was received from a multicast endpoint.</param>
/// <returns></returns>
public static CoapMessage Parse(byte[] payload, bool isMulticast = false)
public static CoapMessage FromBytes(byte[] payload, bool isMulticast = false)
{
var message = new CoapMessage(isMulticast);
message.Deserialise(payload);
message.FromBytes(payload);
return message;
}

Expand Down
4 changes: 2 additions & 2 deletions src/CoAPNet/CoapMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public CoapMessage(bool multicast = false)
/// Serialises the message into bytes, ready to be encrypted or transported to the destination endpoint.
/// </summary>
/// <returns></returns>
public byte[] Serialise()
public byte[] ToBytes()
{
var result = new List<byte>();
byte optCode = 0;
Expand Down Expand Up @@ -280,7 +280,7 @@ public byte[] Serialise()
return result.ToArray();
}

public void Deserialise(byte[] data)
public void FromBytes(byte[] data)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
Expand Down
14 changes: 7 additions & 7 deletions tests/CoAPNet.Tests/CoapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task TestClientResponse()

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

// Ack
Expand Down Expand Up @@ -124,7 +124,7 @@ public async Task TestClientResponseWithDelay()

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

// Ack
Expand Down Expand Up @@ -190,7 +190,7 @@ public void TestRejectEmptyMessageWithFormatError()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expected.Serialise()))),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expected.ToBytes()))),
Times.Exactly(1));
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public async Task TestRetransmissionAttempts()
{
Id = 0x1234,
Type = CoapMessageType.Acknowledgement
}.Serialise()
}.ToBytes()
}))
.Throws(new CoapEndpointException("Endpoint closed"));

Expand All @@ -321,7 +321,7 @@ public async Task TestRetransmissionAttempts()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise()))),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.ToBytes()))),
Times.Exactly(2));
}

Expand Down Expand Up @@ -359,7 +359,7 @@ public void TestRetransmissionMaxAttempts()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise()))),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.ToBytes()))),
Times.Exactly(3));
}

Expand Down Expand Up @@ -446,7 +446,7 @@ public async Task TestReceiveMulticastMessagFromMulticastEndpoint()
.Returns(Task.CompletedTask);
mockClientEndpoint
.SetupSequence(c => c.MockReceiveAsync())
.Returns(Task.FromResult(new CoapPacket{Payload = expected.Serialise()}))
.Returns(Task.FromResult(new CoapPacket{Payload = expected.ToBytes()}))
.Throws(new CoapEndpointException("Endpoint closed"));


Expand Down
2 changes: 1 addition & 1 deletion tests/CoAPNet.Tests/CoapHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void TestResponse()
// Act
var service = new CoapHandler();

service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint);
Expand Down
30 changes: 15 additions & 15 deletions tests/CoAPNet.Tests/CoapMessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void TestMessageEmpty()
_message.Id = 1234;

var expected = new byte[] { 0x40, 0x00, 0x04, 0xD2 };
var actual = _message.Serialise();
var actual = _message.ToBytes();

Assert.IsTrue(expected.SequenceEqual(actual));
}
Expand All @@ -64,7 +64,7 @@ public void TestMessageEmptyWithToken()
_message.Token = new byte[] { 0xC0, 0xff, 0xee };

var expected = new byte[] { 0x60, 0x00, 0x04, 0xD2 };
var actual = _message.Serialise();
var actual = _message.ToBytes();

Assert.IsTrue(expected.SequenceEqual(actual));
}
Expand All @@ -79,7 +79,7 @@ public void TestMessageEmptyAckknowledgement()
_message.Id = 1235;

var expected = new byte[] { 0x60, 0x43, 0x04, 0xD3 };
var actual = _message.Serialise();
var actual = _message.ToBytes();

Assert.IsTrue(expected.SequenceEqual(actual));
}
Expand All @@ -100,7 +100,7 @@ public void TestMessageEncodeRequest()
0x44, 0x01, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xBB, 0x2E, 0x77, 0x65, 0x6C, 0x6C, 0x2D,
0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x04, 0x63, 0x6F, 0x72, 0x65
};
var actual = _message.Serialise();
var actual = _message.ToBytes();

Assert.IsTrue(expected.SequenceEqual(actual));
}
Expand All @@ -121,7 +121,7 @@ public void TestMessageEncodeResponse()
0x64, 0x45, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc1, 0x28, 0xff, 0x3c, 0x2e, 0x77, 0x65, 0x6c, 0x6c, 0x2d, 0x6b, 0x6e,
0x6f, 0x77, 0x6e, 0x2f, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x3e
};
var actual = _message.Serialise();
var actual = _message.ToBytes();

Assert.IsTrue(expected.SequenceEqual(actual));
}
Expand All @@ -130,7 +130,7 @@ public void TestMessageEncodeResponse()
[Category("[RFC7252] Section 3"), Category("Decode")]
public void TestMessageDecodeRequest()
{
this._message.Deserialise(new byte[] {
this._message.FromBytes(new byte[] {
0x44, 0x01, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xBB, 0x2E, 0x77, 0x65, 0x6C, 0x6C, 0x2D,
0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x04, 0x63, 0x6F, 0x72, 0x65
});
Expand All @@ -151,16 +151,16 @@ public void TestMessageDecodeRequest()
[Category("[RFC7252] Section 3"), Category("Decode")]
public void TestMessageDecodeRequest_WithBadCodes()
{
Assert.Throws<CoapMessageFormatException>(() => _message.Deserialise(new byte[] { 0x64, 0x20, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
Assert.Throws<CoapMessageFormatException>(() => _message.Deserialise(new byte[] { 0x64, 0xC0, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
Assert.Throws<CoapMessageFormatException>(() => _message.Deserialise(new byte[] { 0x64, 0xE0, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
Assert.Throws<CoapMessageFormatException>(() => _message.FromBytes(new byte[] { 0x64, 0x20, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
Assert.Throws<CoapMessageFormatException>(() => _message.FromBytes(new byte[] { 0x64, 0xC0, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
Assert.Throws<CoapMessageFormatException>(() => _message.FromBytes(new byte[] { 0x64, 0xE0, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f }));
}

[Test]
[Category("[RFC7252] Section 3"), Category("Decode")]
public void TestMessageDecodeResponse()
{
_message.Deserialise(new byte[] {
_message.FromBytes(new byte[] {
0x64, 0x45, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc1, 0x28, 0xff, 0x3c, 0x2e, 0x77, 0x65, 0x6c, 0x6c, 0x2d, 0x6b, 0x6e,
0x6f, 0x77, 0x6e, 0x2f, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x3e
});
Expand All @@ -183,7 +183,7 @@ public void TestMessageDecodeResponse_WithUnknownCriticalOption()
Assert.Throws<CoapOptionException>(() =>
{
// TODO: Use a fake unknwon option number, right now it's set to "Block" which is going to be supported
_message.Deserialise(new byte[] {
_message.FromBytes(new byte[] {
0x64, 0x45, 0x42, 0x42, 0xde, 0xad, 0xbe, 0xef, 0xc0, 0xb1, 0x0c, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f
});
});
Expand All @@ -195,31 +195,31 @@ public void TestMessageFormatError()
{
Assert.Throws<CoapMessageFormatException>(() =>
{
_message.Deserialise(new byte[] { 0x40, 0x00, 0x10, 0x00, 0xFF, 0x12, 0x34 });
_message.FromBytes(new byte[] { 0x40, 0x00, 0x10, 0x00, 0xFF, 0x12, 0x34 });
}, "Empty message with payload");

// Verify that Message.Id was decoded
Assert.AreEqual(0x1000, _message.Id);

Assert.Throws<CoapMessageFormatException>(() =>
{
_message.Deserialise(new byte[] { 0x52, 0x00, 0xAA, 0x55, 0x12, 0x34 });
_message.FromBytes(new byte[] { 0x52, 0x00, 0xAA, 0x55, 0x12, 0x34 });
}, "Empty message with tag");

// Verify that Message.Id was decoded
Assert.AreEqual(0xAA55, _message.Id);

Assert.Throws<CoapMessageFormatException>(() =>
{
_message.Deserialise(new byte[] { 0x60, 0x00, 0xC3, 0x3C, 0xc1, 0x28 });
_message.FromBytes(new byte[] { 0x60, 0x00, 0xC3, 0x3C, 0xc1, 0x28 });
}, "Empty message with options");

// Verify that Message.Id was decoded
Assert.AreEqual(0xC33C, _message.Id);

Assert.Throws<CoapMessageFormatException>(() =>
{
_message.Deserialise(new byte[] { 0x40, 0x20, 0x12, 0x34, 0xc1, 0x28 });
_message.FromBytes(new byte[] { 0x40, 0x20, 0x12, 0x34, 0xc1, 0x28 });
}, "Message with invalid Message.Code class");
}

Expand Down
26 changes: 13 additions & 13 deletions tests/CoAPNet.Tests/CoapResourceHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void TestResponse()
var service = new CoapResourceHandler();
service.Resources.Add(mockResource.Object);

service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();
//_client.Raise(c => c.OnMessageReceived += null, new CoapMessageReceivedEventArgs {Message = request});

// Assert
Expand All @@ -89,7 +89,7 @@ public void TestGetRequest()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint, mockResource);
Expand All @@ -112,7 +112,7 @@ public void TestPostRequest()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint, mockResource);
Expand All @@ -135,7 +135,7 @@ public void TestPutRequest()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint, mockResource);
Expand All @@ -158,7 +158,7 @@ public void TestDeleteRequest()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint, mockResource);
Expand All @@ -168,7 +168,7 @@ public void TestDeleteRequest()
public void TestResourceMethodNotImplemented()
{
// Arrange
var expectedMessage = CoapMessage.FromException(new NotImplementedException()).Serialise();
var expectedMessage = CoapMessage.FromException(new NotImplementedException()).ToBytes();

_endpoint
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
Expand All @@ -187,7 +187,7 @@ public void TestResourceMethodNotImplemented()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint);
Expand All @@ -197,7 +197,7 @@ public void TestResourceMethodNotImplemented()
public void TestResourceMethodBadOption()
{
// Arrange
var expectedMessage = CoapMessage.FromException(new CoapOptionException("Unsupported critical option (45575)")).Serialise();
var expectedMessage = CoapMessage.FromException(new CoapOptionException("Unsupported critical option (45575)")).ToBytes();

_endpoint
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
Expand All @@ -216,7 +216,7 @@ public void TestResourceMethodBadOption()
var service = new CoapResourceHandler();

service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint);
Expand All @@ -226,7 +226,7 @@ public void TestResourceMethodBadOption()
public void TestResourceNotFound()
{
// Arrange
var expectedMessage = CoapMessage.Create(CoapMessageCode.NotFound, $"Resouce {new Uri(_baseUri, "/test")} was not found", CoapMessageType.Acknowledgement).Serialise();
var expectedMessage = CoapMessage.Create(CoapMessageCode.NotFound, $"Resouce {new Uri(_baseUri, "/test")} was not found", CoapMessageType.Acknowledgement).ToBytes();

_endpoint
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
Expand All @@ -239,7 +239,7 @@ public void TestResourceNotFound()
// Act
var service = new CoapResourceHandler();

service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).Wait();

// Assert
Mock.Verify(_endpoint);
Expand All @@ -249,7 +249,7 @@ public void TestResourceNotFound()
public void TestResourceInternalError()
{
// Arrange
var expectedMessage = CoapMessage.Create(CoapMessageCode.InternalServerError, "An unexpected error occured", CoapMessageType.Reset).Serialise();
var expectedMessage = CoapMessage.Create(CoapMessageCode.InternalServerError, "An unexpected error occured", CoapMessageType.Reset).ToBytes();

_endpoint
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
Expand All @@ -268,7 +268,7 @@ public void TestResourceInternalError()
var service = new CoapResourceHandler();
service.Resources.Add(mockResource.Object);

Assert.Throws<Exception>(()=> service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).GetAwaiter().GetResult());
Assert.Throws<Exception>(()=> service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.ToBytes()).GetAwaiter().GetResult());

// Assert
Mock.Verify(_endpoint);
Expand Down

0 comments on commit 4436a9f

Please sign in to comment.