From 809ef394a0ba7dcf7d4c4b7682e850ede1dde285 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Tue, 12 Sep 2023 11:52:13 +0200 Subject: [PATCH] Packet - Added `setCode(Code)` Packet (unit tests) - Test the encoding of the request/response code --- source/doap/packet.d | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/doap/packet.d b/source/doap/packet.d index 57ad813..ed00eac 100644 --- a/source/doap/packet.d +++ b/source/doap/packet.d @@ -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); + } \ No newline at end of file