/work/toxcore/crypto_core_test_util.hh
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H |
2 | | #define C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H |
3 | | |
4 | | #include <algorithm> |
5 | | #include <array> |
6 | | #include <iosfwd> |
7 | | #include <random> |
8 | | |
9 | | #include "crypto_core.h" |
10 | | #include "test_util.hh" |
11 | | |
12 | | struct Random_Class { |
13 | | static Random_Funcs const vtable; |
14 | | Random const self; |
15 | | |
16 | | operator Random const *() const { return &self; } |
17 | | |
18 | | Random_Class(Random_Class const &) = default; |
19 | | Random_Class() |
20 | | : self{&vtable, this} |
21 | | { |
22 | | } |
23 | | |
24 | | virtual ~Random_Class(); |
25 | | virtual crypto_random_bytes_cb random_bytes = 0; |
26 | | virtual crypto_random_uniform_cb random_uniform = 0; |
27 | | }; |
28 | | |
29 | | /** |
30 | | * A very simple, fast, and deterministic PRNG just for testing. |
31 | | * |
32 | | * We generally don't want to use system_random(), since it's a |
33 | | * cryptographically secure PRNG and we don't need that in unit tests. |
34 | | */ |
35 | | class Test_Random : public Random_Class { |
36 | | std::minstd_rand lcg; |
37 | | |
38 | | void random_bytes(void *obj, uint8_t *bytes, size_t length) override; |
39 | | uint32_t random_uniform(void *obj, uint32_t upper_bound) override; |
40 | | }; |
41 | | |
42 | | struct PublicKey : private std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> { |
43 | | using Base = std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>; |
44 | | |
45 | | using Base::begin; |
46 | | using Base::data; |
47 | | using Base::end; |
48 | | using Base::size; |
49 | | using Base::operator[]; |
50 | | |
51 | | PublicKey() = default; |
52 | | explicit PublicKey(uint8_t const (&arr)[CRYPTO_PUBLIC_KEY_SIZE]) |
53 | | : PublicKey(to_array(arr)) |
54 | 0 | { |
55 | 0 | } |
56 | | explicit PublicKey(std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> const &arr) |
57 | 2 | { |
58 | 2 | std::copy(arr.begin(), arr.end(), begin()); |
59 | 2 | } |
60 | | |
61 | | PublicKey(std::initializer_list<uint8_t> const &arr) |
62 | | { |
63 | | std::copy(arr.begin(), arr.end(), begin()); |
64 | | } |
65 | | |
66 | | Base const &base() const { return *this; } |
67 | | }; |
68 | | |
69 | | inline bool operator!=(PublicKey const &pk1, PublicKey const &pk2) |
70 | | { |
71 | | return pk1.base() != pk2.base(); |
72 | | } |
73 | | |
74 | | inline bool operator==(PublicKey const &pk1, PublicKey const &pk2) |
75 | | { |
76 | | return pk1.base() == pk2.base(); |
77 | | } |
78 | | |
79 | | inline bool operator==(PublicKey::Base const &pk1, PublicKey const &pk2) |
80 | | { |
81 | | return pk1 == pk2.base(); |
82 | | } |
83 | | |
84 | | std::ostream &operator<<(std::ostream &out, PublicKey const &pk); |
85 | | |
86 | | PublicKey random_pk(const Random *rng); |
87 | | |
88 | | #endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H |