From 9592d590cf4b4bad9117d9df618c7bab351cb70f Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 10 Jan 2024 21:33:04 +0000 Subject: [PATCH] refactor(test): Slightly nicer C++ interface to tox Random. --- toxcore/crypto_core_test_util.cc | 43 +++++++++++++------------------- toxcore/crypto_core_test_util.hh | 29 +++++++++++++++------ toxcore/test_util.hh | 13 ++++++++++ 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/toxcore/crypto_core_test_util.cc b/toxcore/crypto_core_test_util.cc index 61dba5b3..4d5f46c5 100644 --- a/toxcore/crypto_core_test_util.cc +++ b/toxcore/crypto_core_test_util.cc @@ -3,6 +3,24 @@ #include #include +Random_Funcs const Random_Class::vtable = { + Method::invoke<&Random_Class::random_bytes>, + Method::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) +{ + std::uniform_int_distribution distrib(0, upper_bound); + return distrib(lcg); +} + PublicKey random_pk(const Random *rng) { PublicKey pk; @@ -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(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(obj); - std::uniform_int_distribution 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; } diff --git a/toxcore/crypto_core_test_util.hh b/toxcore/crypto_core_test_util.hh index d3939749..91a9d68e 100644 --- a/toxcore/crypto_core_test_util.hh +++ b/toxcore/crypto_core_test_util.hh @@ -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 { diff --git a/toxcore/test_util.hh b/toxcore/test_util.hh index 4ab2637e..9bd35fac 100644 --- a/toxcore/test_util.hh +++ b/toxcore/test_util.hh @@ -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 struct Deleter; template using Ptr = std::unique_ptr>; +template +struct Method; + +template +struct Method { + template + static R invoke(void *self, Args... args) + { + return (static_cast(self)->*M)(self, args...); + } +}; + template std::array to_array(T const (&arr)[N]) {