From dc5d89d23cd1bff104f5e4b08db55908f84dbe6d Mon Sep 17 00:00:00 2001 From: Jeroen de Bruijn Date: Sun, 14 Jun 2020 16:04:26 +0200 Subject: [PATCH] fix: use `Token` in the `Coap::token` function --- src/coap.cpp | 7 +++++-- src/coap.hpp | 3 ++- test/coap.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/coap.cpp b/src/coap.cpp index 7d1a6f1..f5f5e01 100644 --- a/src/coap.cpp +++ b/src/coap.cpp @@ -6,8 +6,11 @@ Coap::Coap(std::uint8_t data) {} Coap::Coap(std::uint8_t data, Header header) {} -Coap& Coap::token() { - // set Header TokenLength +Coap& Coap::token(Token value) { + /** + * - Write length to header. + * - Write token to data. + */ return *this; } diff --git a/src/coap.hpp b/src/coap.hpp index ac66b77..ed54a79 100644 --- a/src/coap.hpp +++ b/src/coap.hpp @@ -3,6 +3,7 @@ #include #include "header.hpp" +#include "token.hpp" namespace coap { @@ -27,7 +28,7 @@ class Coap { Coap(std::uint8_t data, Header header); - Coap& token(); + Coap& token(Token value); Coap& option(); Coap& payload(); }; diff --git a/test/coap.cpp b/test/coap.cpp index 4cb8a8d..edb0ca2 100644 --- a/test/coap.cpp +++ b/test/coap.cpp @@ -1,9 +1,25 @@ #include "coap.hpp" +#include // +#include +#include +#include // +#include // +#include + #include "catch2/catch.hpp" namespace coap::test { +auto randomNumberInRange = [](std::uint8_t lowest_value, + std::uint8_t highest_value) { + return [distribution = std::uniform_int_distribution( + lowest_value, highest_value), + random_engine = std::mt19937{std::random_device{}()}]() mutable { + return distribution(random_engine); + }; +}; + std::string const kTestGroup("CoAP "); std::string const kTags("[coap]"); @@ -12,11 +28,29 @@ using Coap = coap::Coap; TEST_CASE(kTestGroup + "correctly parses code", kTags) { Coap coap(20); - coap.option().payload().token(); + coap::Token::Value token{{0xff, 0x18}, 4}; + coap::Token::Value token2{{0xff, 0x18}, 4}; + + REQUIRE(token == token2); + + coap::Token::Vector v = coap::Token(token); + printf("Test token of %lu bytes: ", v.size()); + for (auto const& e : v) { + printf("%02hhx, ", e); + } + printf("\n"); + + // token. + coap.option().payload().token( // + token); + // coap::Token({0xaa, 0x11})); Coap coap2(20, coap::Header(coap::Type::Value::kConfirmable, coap::Code::Value::kPost, 123)); - coap2.option().payload().token(); + // coap2.option().payload().token(); + + std::vector numbers; + std::generate_n(std::back_inserter(numbers), 100, randomNumberInRange(0, 10)); } } // namespace coap::test