Skip to content

Commit

Permalink
test: fix and sync
Browse files Browse the repository at this point in the history
  • Loading branch information
rtgiskard committed Oct 1, 2024
1 parent ec3f2e0 commit 4b6ed9e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
4 changes: 3 additions & 1 deletion test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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') ]
Expand Down
6 changes: 3 additions & 3 deletions test/test_log.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include <doctest.h>

#include "../src/log.h"
#include "../src/utils/log.h"

#include <chrono>
#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -29,7 +29,7 @@ void bench_logger(int howmany, std::shared_ptr<spdlog::logger> 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();

Expand Down Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions test/test_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest.h"

#include <cstdint>
#include <array>
#include <string>

#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<uint8_t, 28> hash;

utils.seed();
utils.genRandBytes(hash.data(), hash.size());
}

TEST_CASE("key hash and verify") {
// provide hash buffer and a sample key
std::array<uint8_t, 32> 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);
}

0 comments on commit 4b6ed9e

Please sign in to comment.