From 37d4a0b2ca1377268a82c085809edf63d19cc782 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 28 Mar 2018 13:36:14 +0000 Subject: [PATCH] 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/TCP_test.c | 17 +++++++++-------- auto_tests/conference_test.c | 3 ++- auto_tests/crypto_test.c | 9 +++++---- auto_tests/dht_test.c | 35 +++++++++++++++++----------------- auto_tests/save_friend_test.c | 3 ++- auto_tests/tox_many_tcp_test.c | 9 +++++---- auto_tests/tox_many_test.c | 5 +++-- auto_tests/tox_one_test.c | 5 +++-- toxcore/crypto_core.api.h | 5 +++++ toxcore/crypto_core.c | 7 +++++++ toxcore/crypto_core.h | 5 +++++ 11 files changed, 64 insertions(+), 39 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 686419a680..144d7e142b 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -17,6 +17,7 @@ #include "../toxcore/TCP_client.h" #include "../toxcore/TCP_server.h" +#include "../toxcore/crypto_core.h" #include "../toxcore/util.h" #include "helpers.h" @@ -59,7 +60,7 @@ START_TEST(test_basic) Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; ip_port_loopback.ip = get_loopback(); - ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); int ret = net_connect(sock, ip_port_loopback); ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); @@ -158,7 +159,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) IP_Port ip_port_loopback; ip_port_loopback.ip = get_loopback(); - ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); int ret = net_connect(sock, ip_port_loopback); ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); @@ -423,7 +424,7 @@ START_TEST(test_client) crypto_new_keypair(f_public_key, f_secret_key); IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); c_sleep(50); @@ -452,7 +453,7 @@ START_TEST(test_client) uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(f2_public_key, f2_secret_key); - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); TCP_Client_Connection *conn2 = new_TCP_connection( ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); routing_response_handler(conn, response_callback, (char *)conn + 2); @@ -521,7 +522,7 @@ START_TEST(test_client_invalid) crypto_new_keypair(f_public_key, f_secret_key); IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); c_sleep(50); @@ -589,7 +590,7 @@ START_TEST(test_tcp_connection) IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); @@ -597,7 +598,7 @@ START_TEST(test_tcp_connection) ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, "Could not add tcp relay to connection\n"); - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); ck_assert_msg(connection == 0, "Connection id wrong"); ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, @@ -697,7 +698,7 @@ START_TEST(test_tcp_connection2) IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index 9e784e857d..9f8352a3b5 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -15,6 +15,7 @@ #include #include +#include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -209,7 +210,7 @@ static void test_many_group(void) TOX_ERR_CONFERENCE_SEND_MESSAGE err; ck_assert_msg( tox_conference_send_message( - toxes[rand() % NUM_GROUP_TOX], 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)GROUP_MESSAGE, + toxes[random_u32() % NUM_GROUP_TOX], 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)GROUP_MESSAGE, sizeof(GROUP_MESSAGE) - 1, &err) != 0, "Failed to send group message."); ck_assert_msg( err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, "Failed to send group message."); diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index 3b95dcc12f..23c45dd0bf 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -12,6 +12,7 @@ #include "helpers.h" +#include "../toxcore/crypto_core.h" #include "../toxcore/net_crypto.h" static void rand_bytes(uint8_t *b, size_t blen) @@ -19,7 +20,7 @@ static void rand_bytes(uint8_t *b, size_t blen) size_t i; for (i = 0; i < blen; i++) { - b[i] = rand(); + b[i] = random_u08(); } } @@ -166,7 +167,7 @@ START_TEST(test_endtoend) // Test 100 random messages and keypairs for (testno = 0; testno < 100; testno++) { //Generate random message (random length from 100 to 500) - mlen = (rand() % 400) + 100; + mlen = (random_u32() % 400) + 100; rand_bytes(m, mlen); rand_bytes(n, CRYPTO_NONCE_SIZE); @@ -303,7 +304,7 @@ START_TEST(test_increment_nonce) uint8_t n[CRYPTO_NONCE_SIZE]; for (i = 0; i < CRYPTO_NONCE_SIZE; ++i) { - n[i] = rand(); + n[i] = random_u08(); } uint8_t n1[CRYPTO_NONCE_SIZE]; @@ -317,7 +318,7 @@ START_TEST(test_increment_nonce) } for (i = 0; i < (1 << 18); ++i) { - uint32_t r = rand(); + const uint32_t r = random_u32(); increment_nonce_number_cmp(n, r); increment_nonce_number(n1, r); ck_assert_msg(memcmp(n, n1, CRYPTO_NONCE_SIZE) == 0, "Bad increment_nonce_number function"); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 034147b980..803dad7625 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) { diff --git a/auto_tests/save_friend_test.c b/auto_tests/save_friend_test.c index a7b41f5261..1e90e692c4 100644 --- a/auto_tests/save_friend_test.c +++ b/auto_tests/save_friend_test.c @@ -7,6 +7,7 @@ #include "helpers.h" #include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include @@ -27,7 +28,7 @@ static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TO uint32_t i; for (i = 0; i < length; ++i) { - text[i] = rand(); + text[i] = random_u08(); } setter(m, text, SIZEOF_VLA(text), nullptr); diff --git a/auto_tests/tox_many_tcp_test.c b/auto_tests/tox_many_tcp_test.c index 6c3f235051..34b01d1602 100644 --- a/auto_tests/tox_many_tcp_test.c +++ b/auto_tests/tox_many_tcp_test.c @@ -15,6 +15,7 @@ #include #include +#include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -84,8 +85,8 @@ START_TEST(test_many_clients_tcp) for (i = 0; i < NUM_FRIENDS; ++i) { loop_top: - pairs[i].tox1 = rand() % NUM_TOXES_TCP; - pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; + pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; + pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { @@ -178,8 +179,8 @@ START_TEST(test_many_clients_tcp_b) for (i = 0; i < NUM_FRIENDS; ++i) { loop_top: - pairs[i].tox1 = rand() % NUM_TOXES_TCP; - pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; + pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; + pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; for (j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { diff --git a/auto_tests/tox_many_test.c b/auto_tests/tox_many_test.c index 29f3af68e9..4318b29b4f 100644 --- a/auto_tests/tox_many_test.c +++ b/auto_tests/tox_many_test.c @@ -15,6 +15,7 @@ #include #include +#include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -61,8 +62,8 @@ static void test_many_clients(void) for (uint32_t i = 0; i < NUM_FRIENDS; ++i) { loop_top: - pairs[i].tox1 = rand() % NUM_TOXES; - pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; + pairs[i].tox1 = random_u32() % NUM_TOXES; + pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES - 1) + 1) % NUM_TOXES; for (uint32_t j = 0; j < i; ++j) { if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c index f3323bc92b..48e2f7918f 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c @@ -11,6 +11,7 @@ #include #include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -21,11 +22,11 @@ static void set_random_name_and_status_message(Tox *tox, uint8_t *name, uint8_t int i; for (i = 0; i < TOX_MAX_NAME_LENGTH; ++i) { - name[i] = rand(); + name[i] = random_u08(); } for (i = 0; i < TOX_MAX_STATUS_MESSAGE_LENGTH; ++i) { - status_message[i] = rand(); + status_message[i] = random_u08(); } } diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h index bb2c0a13b8..e9e8aeb273 100644 --- a/toxcore/crypto_core.api.h +++ b/toxcore/crypto_core.api.h @@ -116,6 +116,11 @@ static int32_t public_key_cmp( namespace random { +/** + * Return a random 8 bit integer. + */ +static uint8_t u08(); + /** * Return a random 16 bit integer. */ diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 26589219ed..b2f0e5f0d3 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -86,6 +86,13 @@ int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) return crypto_verify_32(pk1, pk2); } +uint8_t random_u08(void) +{ + uint8_t randnum; + randombytes(&randnum, 1); + return randnum; +} + uint16_t random_u16(void) { uint16_t randnum; diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 2c83fd255a..e7e913b681 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -126,6 +126,11 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length); */ int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2); +/** + * Return a random 8 bit integer. + */ +uint8_t random_u08(void); + /** * Return a random 16 bit integer. */