From decbbe973c19b62db930a0412d7ca7e33614bbf7 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 20 Apr 2022 15:36:31 +0200 Subject: [PATCH] tests: random_helper minor improvements --- tests/core_tests/random_helper.cpp | 46 ---------------------------- tests/core_tests/random_helper.h | 49 +++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 51 deletions(-) diff --git a/tests/core_tests/random_helper.cpp b/tests/core_tests/random_helper.cpp index eac32896..724d9108 100644 --- a/tests/core_tests/random_helper.cpp +++ b/tests/core_tests/random_helper.cpp @@ -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() diff --git a/tests/core_tests/random_helper.h b/tests/core_tests/random_helper.h index c9826665..360b326d 100644 --- a/tests/core_tests/random_helper.h +++ b/tests/core_tests/random_helper.h @@ -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();