Skip to content

Commit

Permalink
refactor(test): Slightly nicer C++ interface to tox Random.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 10, 2024
1 parent c66e10f commit 9592d59
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
43 changes: 18 additions & 25 deletions toxcore/crypto_core_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#include <cstring>
#include <iomanip>

Random_Funcs const Random_Class::vtable = {
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
};

Random_Class::~Random_Class() = default;

void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length)
{
std::generate(bytes, &bytes[length], std::ref(lcg));
}

uint32_t Test_Random::random_uniform(void *obj, uint32_t upper_bound)

Check warning on line 18 in toxcore/crypto_core_test_util.cc

View check run for this annotation

Codecov / codecov/patch

toxcore/crypto_core_test_util.cc#L18

Added line #L18 was not covered by tests
{
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
return distrib(lcg);
}

Check warning on line 22 in toxcore/crypto_core_test_util.cc

View check run for this annotation

Codecov / codecov/patch

toxcore/crypto_core_test_util.cc#L20-L22

Added lines #L20 - L22 were not covered by tests

PublicKey random_pk(const Random *rng)
{
PublicKey pk;
Expand All @@ -19,28 +37,3 @@ std::ostream &operator<<(std::ostream &out, PublicKey const &pk)
out << '"';
return out;
}

static void test_random_bytes(void *obj, uint8_t *bytes, size_t length)
{
Test_Random *self = static_cast<Test_Random *>(obj);
std::generate(bytes, &bytes[length], std::ref(self->lcg));
}

static uint32_t test_random_uniform(void *obj, uint32_t upper_bound)
{
Test_Random *self = static_cast<Test_Random *>(obj);
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
return distrib(self->lcg);
}

Random_Funcs const Test_Random::vtable = {
test_random_bytes,
test_random_uniform,
};

Test_Random::Test_Random()
: self{&vtable, this}
{
}

Test_Random::operator Random const *() const { return &self; }
29 changes: 21 additions & 8 deletions toxcore/crypto_core_test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,34 @@
#include "crypto_core.h"
#include "test_util.hh"

struct Random_Class {
static Random_Funcs const vtable;
Random const self;

operator Random const *() const { return &self; }

Random_Class(Random_Class const &) = default;
Random_Class()
: self{&vtable, this}
{
}

virtual ~Random_Class();
virtual crypto_random_bytes_cb random_bytes = 0;
virtual crypto_random_uniform_cb random_uniform = 0;
};

/**
* A very simple, fast, and deterministic PRNG just for testing.
*
* We generally don't want to use system_random(), since it's a
* cryptographically secure PRNG and we don't need that in unit tests.
*/
class Test_Random {
static Random_Funcs const vtable;
Random const self;

public:
Test_Random();
operator Random const *() const;

class Test_Random : public Random_Class {
std::minstd_rand lcg;

void random_bytes(void *obj, uint8_t *bytes, size_t length) override;
uint32_t random_uniform(void *obj, uint32_t upper_bound) override;
};

struct PublicKey : private std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> {
Expand Down
13 changes: 13 additions & 0 deletions toxcore/test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@ struct Function_Deleter {
void operator()(T *ptr) const { Delete(ptr); }
};

// No default deleter, because we want to catch when we forget to specialise this one.
template <typename T>
struct Deleter;

template <typename T>
using Ptr = std::unique_ptr<T, Deleter<T>>;

template <typename Func, typename Class>
struct Method;

template <typename R, typename Class, typename... Args>
struct Method<R(void *, Args...), Class> {
template <R (Class::*M)(void *, Args...)>
static R invoke(void *self, Args... args)
{
return (static_cast<Class *>(self)->*M)(self, args...);
}
};

template <typename T, std::size_t N>
std::array<T, N> to_array(T const (&arr)[N])
{
Expand Down

0 comments on commit 9592d59

Please sign in to comment.