Files
tomato/toxcore/crypto_core_test_util.hh
Green Sky 596ea37298 Squashed 'external/toxcore/c-toxcore/' changes from 640e6cace..e58eb27a8
e58eb27a8 fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.
206ea3530 refactor: Explicitly pass dependencies to constructors.
7cefa93cf fix(toxencryptsave): Wipe salt and passkey after usage.
7c3be2342 refactor: Add file/line to tox-bootstrapd logging.
f84e8cdce refactor: Move loglogdata out of network.c.
390f7db06 refactor: Move random and memory OS-specifics to `os_*` files.
REVERT: 640e6cace fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: e58eb27a84f9fa0cd996868e079f39e90a5c04b6
2025-11-04 21:18:05 +01:00

90 lines
2.2 KiB
C++

#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H
#include <algorithm>
#include <array>
#include <iosfwd>
#include <random>
#include "crypto_core.h"
#include "test_util.hh"
#include "tox_random_impl.h"
struct Random_Class {
static Tox_Random_Funcs const vtable;
Tox_Random const self;
operator Tox_Random const *() const { return &self; }
Random_Class(Random_Class const &) = default;
Random_Class()
: self{&vtable, this}
{
}
virtual ~Random_Class();
virtual tox_random_bytes_cb random_bytes = 0;
virtual tox_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 : public Random_Class {
std::minstd_rand lcg;
void random_bytes(void *obj, uint8_t *bytes, uint32_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> {
using Base = std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>;
using Base::begin;
using Base::data;
using Base::end;
using Base::size;
using Base::operator[];
PublicKey() = default;
explicit PublicKey(uint8_t const (&arr)[CRYPTO_PUBLIC_KEY_SIZE])
: PublicKey(to_array(arr))
{
}
explicit PublicKey(std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> const &arr)
{
std::copy(arr.begin(), arr.end(), begin());
}
PublicKey(std::initializer_list<uint8_t> const &arr)
{
std::copy(arr.begin(), arr.end(), begin());
}
Base const &base() const { return *this; }
};
inline bool operator!=(PublicKey const &pk1, PublicKey const &pk2)
{
return pk1.base() != pk2.base();
}
inline bool operator==(PublicKey const &pk1, PublicKey const &pk2)
{
return pk1.base() == pk2.base();
}
inline bool operator==(PublicKey::Base const &pk1, PublicKey const &pk2)
{
return pk1 == pk2.base();
}
std::ostream &operator<<(std::ostream &out, PublicKey const &pk);
PublicKey random_pk(const Tox_Random *rng);
#endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H