From 37d4a0b2ca1377268a82c085809edf63d19cc782 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 28 Mar 2018 13:36:14 +0000 Subject: Avoid the use of rand() in tests. We control the random functions in crypto_core, so we can make them deterministic more easily. This will help test reproducibility in the future. --- auto_tests/dht_test.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'auto_tests/dht_test.c') diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 034147b9..803dad76 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -13,6 +13,7 @@ #include "helpers.h" +#include "../toxcore/crypto_core.h" #ifndef DHT_C_INCLUDED #include "../toxcore/DHT.c" #endif // DHT_C_INCLUDED @@ -115,7 +116,7 @@ static void test_addto_lists_update(DHT *dht, uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; // check id update for existing ip_port - test = rand() % length; + test = random_u32() % length; ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); random_bytes(test_id, sizeof(test_id)); @@ -128,8 +129,8 @@ static void test_addto_lists_update(DHT *dht, "Client IP_Port is incorrect"); // check ip_port update for existing id - test = rand() % length; - test_ipp.port = rand() % TOX_PORT_DEFAULT; + test = random_u32() % length; + test_ipp.port = random_u32() % TOX_PORT_DEFAULT; id_copy(test_id, list[test].public_key); used = addto_lists(dht, test_ipp, test_id); @@ -140,8 +141,8 @@ static void test_addto_lists_update(DHT *dht, "Client IP_Port is incorrect"); // check ip_port update for existing id and ip_port (... port ... id ...) - test1 = rand() % (length / 2); - test2 = rand() % (length / 2) + length / 2; + test1 = random_u32() % (length / 2); + test2 = random_u32() % (length / 2) + length / 2; ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); id_copy(test_id, list[test2].public_key); @@ -159,8 +160,8 @@ static void test_addto_lists_update(DHT *dht, "Client IP_Port is incorrect"); // check ip_port update for existing id and ip_port (... id ... port ...) - test1 = rand() % (length / 2); - test2 = rand() % (length / 2) + length / 2; + test1 = random_u32() % (length / 2); + test2 = random_u32() % (length / 2) + length / 2; ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); id_copy(test_id, list[test1].public_key); @@ -192,9 +193,9 @@ static void test_addto_lists_bad(DHT *dht, random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); - test1 = rand() % (length / 3); - test2 = rand() % (length / 3) + length / 3; - test3 = rand() % (length / 3) + 2 * length / 3; + test1 = random_u32() % (length / 3); + test2 = random_u32() % (length / 3) + length / 3; + test3 = random_u32() % (length / 3) + 2 * length / 3; ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); id_copy((uint8_t *)&test_id1, list[test1].public_key); @@ -236,9 +237,9 @@ static void test_addto_lists_possible_bad(DHT *dht, random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); - test1 = rand() % (length / 3); - test2 = rand() % (length / 3) + length / 3; - test3 = rand() % (length / 3) + 2 * length / 3; + test1 = random_u32() % (length / 3); + test2 = random_u32() % (length / 3) + length / 3; + test3 = random_u32() % (length / 3) + 2 * length / 3; ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); id_copy((uint8_t *)&test_id1, list[test1].public_key); @@ -491,8 +492,8 @@ static void test_list_main(void) IP_Port ip_port; ip_init(&ip_port.ip, 0); - ip_port.ip.ip.v4.uint32 = rand(); - ip_port.port = rand() % (UINT16_MAX - 1); + ip_port.ip.ip.v4.uint32 = random_u32(); + ip_port.port = random_u32() % (UINT16_MAX - 1); ++ip_port.port; addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); } @@ -626,8 +627,8 @@ START_TEST(test_DHT_test) for (i = 0; i < NUM_DHT_FRIENDS; ++i) { loop_top: - pairs[i].tox1 = rand() % NUM_DHT; - pairs[i].tox2 = (pairs[i].tox1 + (rand() % (NUM_DHT - 1)) + 1) % NUM_DHT; + pairs[i].tox1 = random_u32() % NUM_DHT; + pairs[i].tox2 = (pairs[i].tox1 + (random_u32() % (NUM_DHT - 1)) + 1) % NUM_DHT; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox2 && pairs[j].tox1 == pairs[i].tox1) { -- cgit v1.2.3