From 4b6ed9e845c7febfcb8b68e4f8c8c14c52b2c988 Mon Sep 17 00:00:00 2001 From: giskard Date: Wed, 2 Oct 2024 00:51:51 +0800 Subject: [PATCH] test: fix and sync --- test/meson.build | 4 ++- test/test_log.cc | 6 ++-- test/test_utils.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 test/test_utils.cc diff --git a/test/meson.build b/test/meson.build index f1ab19b..8ecf849 100644 --- a/test/meson.build +++ b/test/meson.build @@ -2,7 +2,9 @@ cmd = run_command('find . -name *.cc'.split(), check: true) src_test = cmd.stdout().strip().split('\n') src_test += [ - '../src/log.cc', + '../src/utils/log.cc', + '../src/utils/crypto.cc', + '../src/utils/misc.cc', ] deps_test = deps + [ dependency('doctest') ] diff --git a/test/test_log.cc b/test/test_log.cc index 8059d5a..948bf39 100644 --- a/test/test_log.cc +++ b/test/test_log.cc @@ -1,7 +1,7 @@ #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL #include -#include "../src/log.h" +#include "../src/utils/log.h" #include #include @@ -29,7 +29,7 @@ void bench_logger(int howmany, std::shared_ptr log) { } TEST_CASE("spdlog_test") { - mtrx::init_log("log_test", mtrx::LogSettings{}); + mtrx::utils::init_log("log_test", mtrx::utils::LogSettings{}); auto logger = spdlog::default_logger(); @@ -59,7 +59,7 @@ TEST_CASE("spdlog_test") { } TEST_CASE("spdlog_bench" * doctest::skip(true)) { - mtrx::init_log("log_bench", mtrx::LogSettings{}); + mtrx::utils::init_log("log_bench", mtrx::utils::LogSettings{}); auto howmany = 200000; // store summary to file diff --git a/test/test_utils.cc b/test/test_utils.cc new file mode 100644 index 0000000..8e59719 --- /dev/null +++ b/test/test_utils.cc @@ -0,0 +1,87 @@ +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#include "doctest.h" + +#include +#include +#include + +#include "../src/utils/crypto.h" +#include "../src/utils/misc.h" + +auto utils = mtrx::utils::KeyUtils(mtrx::utils::HashParams()); + +TEST_CASE("gen rand bytes") { + std::array hash; + + utils.seed(); + utils.genRandBytes(hash.data(), hash.size()); +} + +TEST_CASE("key hash and verify") { + // provide hash buffer and a sample key + std::array hash; + std::string key = "key to hash and verify"; + + // initial hash should not match the key + CHECK(!utils.verifyHash(hash.data(), hash.size(), key)); + + // update hash with respect to key + utils.hashFromKey(hash.data(), hash.size(), key); + + // verify (hash, key), should match now + CHECK(utils.verifyHash(hash.data(), hash.size(), key)); + + // compare with external reference (update this if the default params changed): + // echo -n "key to hash and verify" | argon2 "0xSalty.~#@&" -id -t 4 -m 10 -p 2 -l 32 + auto ref_hex = "0c0bfec1c6faad5a6f3527d8dd46d1786164fbbf9a442caf05884ac0d6b0c737"; + CHECK(ref_hex == mtrx::utils::hexFromBytes(hash.data(), hash.size())); +} + +TEST_CASE("hexFromBytes") { + uint8_t buf[] = {0x00, 0x02, 0xc2, 0xef}; + CHECK(mtrx::utils::hexFromBytes(buf, 0) == ""); + CHECK(mtrx::utils::hexFromBytes(buf, 1) == "00"); + CHECK(mtrx::utils::hexFromBytes(buf, 2) == "0002"); + CHECK(mtrx::utils::hexFromBytes(buf, 4) == "0002c2ef"); +} + +TEST_CASE("hexToBytes") { + uint8_t buf[20]; + std::string str; + + CHECK(mtrx::utils::hexToBytes("", buf, 0)); + CHECK(!mtrx::utils::hexToBytes("", buf, 1)); + + str = "0123456789abcdef"; + CHECK(mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + CHECK(buf[0] == 0x01); + CHECK(buf[1] == 0x23); + CHECK(buf[2] == 0x45); + + str = "0123456789abcdefgh"; + CHECK(!mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + + str = "010"; + CHECK(!mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + + str = "az"; + CHECK(!mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + + str = "0 24"; + CHECK(!mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + + str = "0eFf"; + CHECK(mtrx::utils::hexToBytes(str, buf, str.size() / 2)); + CHECK(buf[0] == 0x0e); + CHECK(buf[1] == 0xff); +} + +TEST_CASE("rand hex convert") { + uint8_t buf[40]; + + mtrx::utils::KeyUtils::genRandBytes(buf, sizeof(buf)); + auto str = mtrx::utils::hexFromBytes(buf, sizeof(buf)); + + CHECK(mtrx::utils::hexToBytes(str, buf, sizeof(buf))); + CHECK(mtrx::utils::hexFromBytes(buf, sizeof(buf)) == str); +}