- Added `setCode(Code)`

Packet (unit tests)

- Test the encoding of the request/response code
This commit is contained in:
Tristan B. Velloza Kildaire 2023-09-12 11:52:13 +02:00
parent 23994bb75b
commit 809ef394a0

View File

@ -73,6 +73,11 @@ public class CoapPacket
}
}
public void setCode(Code code)
{
this.code = code;
}
// public ubyte getVersion()
// {
@ -96,10 +101,18 @@ version(unittest)
unittest
{
CoapPacket packet = new CoapPacket();
packet.setType(MessageType.RESET);
ubyte[] token = [0, 69];
packet.setToken(token);
packet.setCode(Code.PONG);
ubyte[] encoded = packet.getBytes();
ubyte firstByte = encoded[0];
@ -116,4 +129,17 @@ unittest
ubyte tklField = firstByte & 15;
assert(tklField == token.length);
ubyte secondByte = encoded[1];
// Ensure the code is set to PONG
// Class is 7
// Code is 3
ubyte codeClass = cast(ubyte)(secondByte & 224) >> 5;
assert(codeClass == 7);
ubyte code = (secondByte & (~224));
assert(code == 3);
writeln(codeClass);
writeln(code);
assert(secondByte == Code.PONG);
}