*: 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.
This commit is contained in:
Lily Tsuru 2024-03-21 08:18:18 -04:00
parent f2c01cf924
commit a5bf226d11
8 changed files with 88 additions and 1 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -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

View File

@ -98,4 +98,20 @@ namespace ls::aries {
outStr = std::move(tagFieldBuffer);
}
std::vector<u8> 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

View File

@ -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<u8> 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).
}

34
lib/aries/Tags.test.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <aries/Tags.hpp>
#include <catch2/catch_all.hpp>
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<u8> expectedOutput = { 0x12, 0x34, 0x56, 0x78 };
REQUIRE(aries::DecodeBinaryTagData(testCorpus) == expectedOutput);
}

View File

@ -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()

View File

@ -3,3 +3,7 @@ add_subdirectory(tomlplusplus)
set(SPDLOG_USE_STD_FORMAT ON)
add_subdirectory(spdlog)
if(LS_BUILD_TESTING)
add_subdirectory(catch2)
endif()

1
third_party/catch2 vendored Submodule

@ -0,0 +1 @@
Subproject commit 8ac8190e494a381072c89f5e161b92a08d98b37b