From a5bf226d115de208096035d7dd01f4aaa8745cda Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 21 Mar 2024 08:18:18 -0400 Subject: [PATCH] *: begin actually unittesting! I probably should've a long time ago, but here we are. The aries library now has unit tests written for it, so we can actually (?) ensure it won't break randomly. It's now only 50% cowboy code! Also provides a stub implementation of aries::DecodeBinaryTagData, which will be completed later. --- .gitmodules | 3 +++ CMakeLists.txt | 2 ++ lib/aries/Tags.cpp | 16 ++++++++++++++++ lib/aries/Tags.hpp | 7 +++++++ lib/aries/Tags.test.cpp | 34 ++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 20 ++++++++++++++++++++ third_party/CMakeLists.txt | 6 +++++- third_party/catch2 | 1 + 8 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 lib/aries/Tags.test.cpp create mode 160000 third_party/catch2 diff --git a/.gitmodules b/.gitmodules index 5c918c1..05929b2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -199,3 +199,6 @@ [submodule "third_party/spdlog"] path = third_party/spdlog url = https://github.com/gabime/spdlog +[submodule "third_party/catch2"] + path = third_party/catch2 + url = https://github.com/catchorg/Catch2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a11d4d..47d4b5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ include(Policies) include(ProjectFuncs) include(CompilerFlags) +option(LS_BUILD_TESTING "Build unit tests" ON) + lobbyserver_set_alternate_linker() # third party vendor dependencies diff --git a/lib/aries/Tags.cpp b/lib/aries/Tags.cpp index a34643a..b619da9 100644 --- a/lib/aries/Tags.cpp +++ b/lib/aries/Tags.cpp @@ -98,4 +98,20 @@ namespace ls::aries { outStr = std::move(tagFieldBuffer); } + + std::vector DecodeBinaryTagData(std::string_view tagData) { + // This could be more ergonomic as an optional or something. + + if(tagData.empty()) + return {}; + + // Marker for binary data + if(tagData[0] != '$') + return {}; + + // TODO: Implement me fully! + + return {}; + } + } // namespace ls::aries \ No newline at end of file diff --git a/lib/aries/Tags.hpp b/lib/aries/Tags.hpp index a6cebfe..9782783 100644 --- a/lib/aries/Tags.hpp +++ b/lib/aries/Tags.hpp @@ -14,4 +14,11 @@ namespace ls::aries { /// Serializes a TagMap to a string. void SerializeTagFields(const TagMap& map, std::string& outStr); + /// Decodes a binary tag to binary data. + std::vector DecodeBinaryTagData(std::string_view tagData); + + + // TODO: Maybe also a in-Aries implementation of "CryptoSSC2"/other dirtysock crypto primitives so that we can rehash + // passwords to an actually sane password hash (e.g: argon2di). + } \ No newline at end of file diff --git a/lib/aries/Tags.test.cpp b/lib/aries/Tags.test.cpp new file mode 100644 index 0000000..e765b23 --- /dev/null +++ b/lib/aries/Tags.test.cpp @@ -0,0 +1,34 @@ +#include +#include +namespace aries = ls::aries; + +TEST_CASE("Aries tag field serde functions as expected", "[Aries] [TagFields]") { + const static aries::TagMap expectedMap = { + { "PROP1", "VAL1" }, + { "PROP2", "VAL2" }, + { "PROP3", "VAL3" }, + { "PROP4", "VAL4" }, + }; + + std::string validTagBuffer; + + SECTION("Serialization works as expected") { + // Do the serialization and make sure it's valid + aries::SerializeTagFields(expectedMap, validTagBuffer); + REQUIRE(validTagBuffer == "PROP4=VAL4\nPROP3=VAL3\nPROP2=VAL2\nPROP1=VAL1\n"); + } + + // For some obscure reason this can't be in a SECTION + // (maybe I'll try boost-ext/ut again if this becomes more frequent..) + aries::TagMap deserializedMap; + REQUIRE(aries::ParseTagFieldsToMap(validTagBuffer, deserializedMap) != false); + REQUIRE(deserializedMap == expectedMap); +} + +TEST_CASE("DecodeBinaryTagData() works", "[Aries] [TagFields] [DecodeBinarytagData]") { + // should decode to [ 0x12, 0x34, 0x56, 0x78 ] + std::string testCorpus = "$12345678"; + const static std::vector expectedOutput = { 0x12, 0x34, 0x56, 0x78 }; + + REQUIRE(aries::DecodeBinaryTagData(testCorpus) == expectedOutput); +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b216733..3507e60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,3 +21,23 @@ target_link_libraries(lobbyserver PRIVATE Boost::json ls::aries ) + +# == unit test driver == +if(LS_BUILD_TESTING) + + add_executable(testdriver + # this is a bit :( + ../lib/aries/Tags.test.cpp + ) + + lobbyserver_target(testdriver) + + target_link_libraries(testdriver + Catch2::Catch2WithMain + + ls::aries + ) + + # TODO: CTest integration + +endif() \ No newline at end of file diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index f3b4534..e90dbdd 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -2,4 +2,8 @@ add_subdirectory(boost) add_subdirectory(tomlplusplus) set(SPDLOG_USE_STD_FORMAT ON) -add_subdirectory(spdlog) \ No newline at end of file +add_subdirectory(spdlog) + +if(LS_BUILD_TESTING) + add_subdirectory(catch2) +endif() \ No newline at end of file diff --git a/third_party/catch2 b/third_party/catch2 new file mode 160000 index 0000000..8ac8190 --- /dev/null +++ b/third_party/catch2 @@ -0,0 +1 @@ +Subproject commit 8ac8190e494a381072c89f5e161b92a08d98b37b