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