1
0
Fork 0
forked from lthn/blockchain

tests: random_helper minor improvements

This commit is contained in:
sowle 2022-04-20 15:36:31 +02:00
parent 78372d1bde
commit decbbe973c
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 44 additions and 51 deletions

View file

@ -6,52 +6,6 @@
#include "chaingen.h"
#include "random_helper.h"
random_state_test_restorer::random_state_test_restorer()
{
crypto::random_prng_get_state(&m_state, sizeof m_state);
}
random_state_test_restorer::~random_state_test_restorer()
{
crypto::random_prng_set_state(&m_state, sizeof m_state);
}
void random_state_test_restorer::reset_random(uint64_t seed /* = 0 */)
{
crypto::random_prng_initialize_with_seed(seed);
}
std::string get_random_text(size_t len)
{
static const char text_chars[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "~!@#$%^&*()-=_+\\/?,.<>|{}[]`';:\" ";
static const uint8_t text_chars_size = sizeof text_chars - 1; // to avoid '\0'
std::string result;
if (len < 1)
return result;
result.resize(len);
char* result_buf = &result[0];
uint8_t* rnd_indices = new uint8_t[len];
crypto::generate_random_bytes(len, rnd_indices);
size_t n = len / 4, i;
for (i = 0; i < n; ++i)
{
result_buf[4 * i + 0] = text_chars[rnd_indices[4 * i + 0] % text_chars_size];
result_buf[4 * i + 1] = text_chars[rnd_indices[4 * i + 1] % text_chars_size];
result_buf[4 * i + 2] = text_chars[rnd_indices[4 * i + 2] % text_chars_size];
result_buf[4 * i + 3] = text_chars[rnd_indices[4 * i + 3] % text_chars_size];
}
for (size_t j = i * 4; j < len; ++j)
{
result_buf[j] = text_chars[rnd_indices[j] % text_chars_size];
}
delete[] rnd_indices;
return result;
}
//------------------------------------------------------------------------------
bool random_state_manupulation_test()

View file

@ -13,17 +13,56 @@
// Remebers random state at ctor, restores it at dtor
struct random_state_test_restorer
{
random_state_test_restorer();
~random_state_test_restorer();
static void reset_random(uint64_t seed = 0);
random_state_test_restorer()
{
crypto::random_prng_get_state(&m_state, sizeof m_state);
}
~random_state_test_restorer()
{
crypto::random_prng_set_state(&m_state, sizeof m_state);
}
static void reset_random(uint64_t seed = 0)
{
crypto::random_prng_initialize_with_seed(seed);
}
private:
uint8_t m_state[RANDOM_STATE_SIZE];
};
#endif // #ifdef USE_INSECURE_RANDOM_RPNG_ROUTINES
std::string get_random_text(size_t len);
inline std::string get_random_text(size_t len)
{
{
static const char text_chars[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "~!@#$%^&*()-=_+\\/?,.<>|{}[]`';:\" ";
static const uint8_t text_chars_size = sizeof text_chars - 1; // to avoid '\0'
std::string result;
if (len < 1)
return result;
result.resize(len);
char* result_buf = &result[0];
uint8_t* rnd_indices = new uint8_t[len];
crypto::generate_random_bytes(len, rnd_indices);
size_t n = len / 4, i;
for (i = 0; i < n; ++i)
{
result_buf[4 * i + 0] = text_chars[rnd_indices[4 * i + 0] % text_chars_size];
result_buf[4 * i + 1] = text_chars[rnd_indices[4 * i + 1] % text_chars_size];
result_buf[4 * i + 2] = text_chars[rnd_indices[4 * i + 2] % text_chars_size];
result_buf[4 * i + 3] = text_chars[rnd_indices[4 * i + 3] % text_chars_size];
}
for (size_t j = i * 4; j < len; ++j)
{
result_buf[j] = text_chars[rnd_indices[j] % text_chars_size];
}
delete[] rnd_indices;
return result;
}
}
bool random_state_manupulation_test();
bool random_evenness_test();