diff options
author | zugz (tox) <mbays+tox@sdf.org> | 2018-08-02 21:00:43 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-08-12 15:22:34 +0000 |
commit | fb89c03dd2b4c05847b7f3f7eeb965cae2e2087e (patch) | |
tree | 9422d8a2720e270d1ebe0cef949d30907b24ce07 /testing | |
parent | 3fe055141793d3bbcf0b0b7fc5b95cc24824d333 (diff) |
Add simple deterministic random number generator for tests
Diffstat (limited to 'testing')
-rw-r--r-- | testing/misc_tools.c | 75 | ||||
-rw-r--r-- | testing/misc_tools.h | 2 |
2 files changed, 77 insertions, 0 deletions
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 8683830d..f039740d 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -21,6 +21,10 @@ | |||
21 | * You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License |
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
23 | */ | 23 | */ |
24 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif | ||
27 | |||
24 | #ifndef _POSIX_C_SOURCE | 28 | #ifndef _POSIX_C_SOURCE |
25 | // For nanosleep(). | 29 | // For nanosleep(). |
26 | #define _POSIX_C_SOURCE 199309L | 30 | #define _POSIX_C_SOURCE 199309L |
@@ -35,6 +39,10 @@ | |||
35 | #include <stdlib.h> | 39 | #include <stdlib.h> |
36 | #include <string.h> | 40 | #include <string.h> |
37 | 41 | ||
42 | #ifndef VANILLA_NACL | ||
43 | #include "sodium.h" | ||
44 | #endif | ||
45 | |||
38 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) | 46 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
39 | #include <windows.h> | 47 | #include <windows.h> |
40 | #else | 48 | #else |
@@ -189,3 +197,70 @@ Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_d | |||
189 | { | 197 | { |
190 | return tox_new_log_lan(options, err, log_user_data, false); | 198 | return tox_new_log_lan(options, err, log_user_data, false); |
191 | } | 199 | } |
200 | |||
201 | |||
202 | #ifndef VANILLA_NACL | ||
203 | static const char *test_rng_name(void) | ||
204 | { | ||
205 | return "test_rng"; | ||
206 | } | ||
207 | |||
208 | static uint32_t rng_state; | ||
209 | |||
210 | static uint32_t test_rng_random(void) | ||
211 | { | ||
212 | rng_state = 2624534371 * rng_state + 1; | ||
213 | return rng_state; | ||
214 | } | ||
215 | |||
216 | static void test_rng_buf(void *const buf, const size_t size) | ||
217 | { | ||
218 | uint8_t *p = (uint8_t *)buf; | ||
219 | uint32_t r = 0; | ||
220 | |||
221 | for (size_t i = 0; i < size; i++) { | ||
222 | if ((i % 4) == 0) { | ||
223 | r = test_rng_random(); | ||
224 | } | ||
225 | |||
226 | *p = (r >> ((i % 4) * 8)) & 0xff; | ||
227 | ++p; | ||
228 | } | ||
229 | } | ||
230 | |||
231 | static uint32_t test_rng_uniform(const uint32_t upper_bound) | ||
232 | { | ||
233 | // XXX: Not uniform! But that's ok for testing purposes. | ||
234 | return test_rng_random() % upper_bound; | ||
235 | } | ||
236 | |||
237 | static void test_rng_stir(void) { } | ||
238 | static int test_rng_close(void) | ||
239 | { | ||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | static randombytes_implementation test_rng = { | ||
244 | test_rng_name, | ||
245 | test_rng_random, | ||
246 | test_rng_stir, | ||
247 | test_rng_uniform, | ||
248 | test_rng_buf, | ||
249 | test_rng_close | ||
250 | }; | ||
251 | |||
252 | /* Simple insecure PRNG for testing purposes */ | ||
253 | int use_test_rng(uint32_t seed) | ||
254 | { | ||
255 | rng_state = seed; | ||
256 | |||
257 | return randombytes_set_implementation(&test_rng); | ||
258 | } | ||
259 | |||
260 | #else | ||
261 | |||
262 | int use_test_rng(uint32_t seed) | ||
263 | { | ||
264 | assert(!"libsodium required for use_test_rng"); | ||
265 | } | ||
266 | #endif | ||
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index deaa177d..b715c8cc 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -22,6 +22,8 @@ void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t lin | |||
22 | Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data); | 22 | Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data); |
23 | Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery); | 23 | Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery); |
24 | 24 | ||
25 | int use_test_rng(uint32_t seed); | ||
26 | |||
25 | #ifdef __cplusplus | 27 | #ifdef __cplusplus |
26 | } | 28 | } |
27 | #endif | 29 | #endif |