Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid the use of rand() in tests. #857

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -589,15 +590,15 @@ 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);
ck_assert_msg(connection == 0, "Connection id wrong");
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,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdlib.h>
#include <time.h>

#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand Down Expand Up @@ -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.");
Expand Down
9 changes: 5 additions & 4 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

#include "helpers.h"

#include "../toxcore/crypto_core.h"
#include "../toxcore/net_crypto.h"

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();
}
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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];
Expand All @@ -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");
Expand Down
35 changes: 18 additions & 17 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "helpers.h"

#include "../toxcore/crypto_core.h"
#ifndef DHT_C_INCLUDED
#include "../toxcore/DHT.c"
#endif // DHT_C_INCLUDED
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion auto_tests/save_friend_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "helpers.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"

#include <assert.h>
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions auto_tests/tox_many_tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdlib.h>
#include <time.h>

#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions auto_tests/tox_many_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdlib.h>
#include <time.h>

#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions auto_tests/tox_one_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <time.h>

#include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand All @@ -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();
}
}

Expand Down
5 changes: 5 additions & 0 deletions toxcore/crypto_core.api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
7 changes: 7 additions & 0 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions toxcore/crypto_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down