diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-03-28 13:36:14 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-04-03 17:43:22 +0000 |
commit | 37d4a0b2ca1377268a82c085809edf63d19cc782 (patch) | |
tree | 9b0724cb55f479689cbc04c8b72f9bde99f775ab | |
parent | 7fa0c89c96bdaf45bf202d770aa56dc7c68959b9 (diff) |
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.
-rw-r--r-- | auto_tests/TCP_test.c | 17 | ||||
-rw-r--r-- | auto_tests/conference_test.c | 3 | ||||
-rw-r--r-- | auto_tests/crypto_test.c | 9 | ||||
-rw-r--r-- | auto_tests/dht_test.c | 35 | ||||
-rw-r--r-- | auto_tests/save_friend_test.c | 3 | ||||
-rw-r--r-- | auto_tests/tox_many_tcp_test.c | 9 | ||||
-rw-r--r-- | auto_tests/tox_many_test.c | 5 | ||||
-rw-r--r-- | auto_tests/tox_one_test.c | 5 | ||||
-rw-r--r-- | toxcore/crypto_core.api.h | 5 | ||||
-rw-r--r-- | toxcore/crypto_core.c | 7 | ||||
-rw-r--r-- | 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 686419a6..144d7e14 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -17,6 +17,7 @@ | |||
17 | #include "../toxcore/TCP_client.h" | 17 | #include "../toxcore/TCP_client.h" |
18 | #include "../toxcore/TCP_server.h" | 18 | #include "../toxcore/TCP_server.h" |
19 | 19 | ||
20 | #include "../toxcore/crypto_core.h" | ||
20 | #include "../toxcore/util.h" | 21 | #include "../toxcore/util.h" |
21 | 22 | ||
22 | #include "helpers.h" | 23 | #include "helpers.h" |
@@ -59,7 +60,7 @@ START_TEST(test_basic) | |||
59 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); | 60 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
60 | IP_Port ip_port_loopback; | 61 | IP_Port ip_port_loopback; |
61 | ip_port_loopback.ip = get_loopback(); | 62 | ip_port_loopback.ip = get_loopback(); |
62 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); | 63 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); |
63 | 64 | ||
64 | int ret = net_connect(sock, ip_port_loopback); | 65 | int ret = net_connect(sock, ip_port_loopback); |
65 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); | 66 | 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) | |||
158 | 159 | ||
159 | IP_Port ip_port_loopback; | 160 | IP_Port ip_port_loopback; |
160 | ip_port_loopback.ip = get_loopback(); | 161 | ip_port_loopback.ip = get_loopback(); |
161 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); | 162 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); |
162 | 163 | ||
163 | int ret = net_connect(sock, ip_port_loopback); | 164 | int ret = net_connect(sock, ip_port_loopback); |
164 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); | 165 | ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); |
@@ -423,7 +424,7 @@ START_TEST(test_client) | |||
423 | crypto_new_keypair(f_public_key, f_secret_key); | 424 | crypto_new_keypair(f_public_key, f_secret_key); |
424 | IP_Port ip_port_tcp_s; | 425 | IP_Port ip_port_tcp_s; |
425 | 426 | ||
426 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 427 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
427 | ip_port_tcp_s.ip = get_loopback(); | 428 | ip_port_tcp_s.ip = get_loopback(); |
428 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); | 429 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); |
429 | c_sleep(50); | 430 | c_sleep(50); |
@@ -452,7 +453,7 @@ START_TEST(test_client) | |||
452 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 453 | uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
453 | uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 454 | uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
454 | crypto_new_keypair(f2_public_key, f2_secret_key); | 455 | crypto_new_keypair(f2_public_key, f2_secret_key); |
455 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 456 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
456 | TCP_Client_Connection *conn2 = new_TCP_connection( | 457 | TCP_Client_Connection *conn2 = new_TCP_connection( |
457 | ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); | 458 | ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); |
458 | routing_response_handler(conn, response_callback, (char *)conn + 2); | 459 | routing_response_handler(conn, response_callback, (char *)conn + 2); |
@@ -521,7 +522,7 @@ START_TEST(test_client_invalid) | |||
521 | crypto_new_keypair(f_public_key, f_secret_key); | 522 | crypto_new_keypair(f_public_key, f_secret_key); |
522 | IP_Port ip_port_tcp_s; | 523 | IP_Port ip_port_tcp_s; |
523 | 524 | ||
524 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 525 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
525 | ip_port_tcp_s.ip = get_loopback(); | 526 | ip_port_tcp_s.ip = get_loopback(); |
526 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); | 527 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); |
527 | c_sleep(50); | 528 | c_sleep(50); |
@@ -589,7 +590,7 @@ START_TEST(test_tcp_connection) | |||
589 | 590 | ||
590 | IP_Port ip_port_tcp_s; | 591 | IP_Port ip_port_tcp_s; |
591 | 592 | ||
592 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 593 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
593 | ip_port_tcp_s.ip = get_loopback(); | 594 | ip_port_tcp_s.ip = get_loopback(); |
594 | 595 | ||
595 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); | 596 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); |
@@ -597,7 +598,7 @@ START_TEST(test_tcp_connection) | |||
597 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, | 598 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
598 | "Could not add tcp relay to connection\n"); | 599 | "Could not add tcp relay to connection\n"); |
599 | 600 | ||
600 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 601 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
601 | connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); | 602 | connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); |
602 | ck_assert_msg(connection == 0, "Connection id wrong"); | 603 | ck_assert_msg(connection == 0, "Connection id wrong"); |
603 | ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, | 604 | 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) | |||
697 | 698 | ||
698 | IP_Port ip_port_tcp_s; | 699 | IP_Port ip_port_tcp_s; |
699 | 700 | ||
700 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 701 | ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); |
701 | ip_port_tcp_s.ip = get_loopback(); | 702 | ip_port_tcp_s.ip = get_loopback(); |
702 | 703 | ||
703 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); | 704 | 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 9e784e85..9f8352a3 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c | |||
@@ -15,6 +15,7 @@ | |||
15 | #include <stdlib.h> | 15 | #include <stdlib.h> |
16 | #include <time.h> | 16 | #include <time.h> |
17 | 17 | ||
18 | #include "../toxcore/crypto_core.h" | ||
18 | #include "../toxcore/tox.h" | 19 | #include "../toxcore/tox.h" |
19 | #include "../toxcore/util.h" | 20 | #include "../toxcore/util.h" |
20 | 21 | ||
@@ -209,7 +210,7 @@ static void test_many_group(void) | |||
209 | TOX_ERR_CONFERENCE_SEND_MESSAGE err; | 210 | TOX_ERR_CONFERENCE_SEND_MESSAGE err; |
210 | ck_assert_msg( | 211 | ck_assert_msg( |
211 | tox_conference_send_message( | 212 | tox_conference_send_message( |
212 | toxes[rand() % NUM_GROUP_TOX], 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)GROUP_MESSAGE, | 213 | toxes[random_u32() % NUM_GROUP_TOX], 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)GROUP_MESSAGE, |
213 | sizeof(GROUP_MESSAGE) - 1, &err) != 0, "Failed to send group message."); | 214 | sizeof(GROUP_MESSAGE) - 1, &err) != 0, "Failed to send group message."); |
214 | ck_assert_msg( | 215 | ck_assert_msg( |
215 | err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, "Failed to send group message."); | 216 | 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 3b95dcc1..23c45dd0 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c | |||
@@ -12,6 +12,7 @@ | |||
12 | 12 | ||
13 | #include "helpers.h" | 13 | #include "helpers.h" |
14 | 14 | ||
15 | #include "../toxcore/crypto_core.h" | ||
15 | #include "../toxcore/net_crypto.h" | 16 | #include "../toxcore/net_crypto.h" |
16 | 17 | ||
17 | static void rand_bytes(uint8_t *b, size_t blen) | 18 | static void rand_bytes(uint8_t *b, size_t blen) |
@@ -19,7 +20,7 @@ static void rand_bytes(uint8_t *b, size_t blen) | |||
19 | size_t i; | 20 | size_t i; |
20 | 21 | ||
21 | for (i = 0; i < blen; i++) { | 22 | for (i = 0; i < blen; i++) { |
22 | b[i] = rand(); | 23 | b[i] = random_u08(); |
23 | } | 24 | } |
24 | } | 25 | } |
25 | 26 | ||
@@ -166,7 +167,7 @@ START_TEST(test_endtoend) | |||
166 | // Test 100 random messages and keypairs | 167 | // Test 100 random messages and keypairs |
167 | for (testno = 0; testno < 100; testno++) { | 168 | for (testno = 0; testno < 100; testno++) { |
168 | //Generate random message (random length from 100 to 500) | 169 | //Generate random message (random length from 100 to 500) |
169 | mlen = (rand() % 400) + 100; | 170 | mlen = (random_u32() % 400) + 100; |
170 | rand_bytes(m, mlen); | 171 | rand_bytes(m, mlen); |
171 | rand_bytes(n, CRYPTO_NONCE_SIZE); | 172 | rand_bytes(n, CRYPTO_NONCE_SIZE); |
172 | 173 | ||
@@ -303,7 +304,7 @@ START_TEST(test_increment_nonce) | |||
303 | uint8_t n[CRYPTO_NONCE_SIZE]; | 304 | uint8_t n[CRYPTO_NONCE_SIZE]; |
304 | 305 | ||
305 | for (i = 0; i < CRYPTO_NONCE_SIZE; ++i) { | 306 | for (i = 0; i < CRYPTO_NONCE_SIZE; ++i) { |
306 | n[i] = rand(); | 307 | n[i] = random_u08(); |
307 | } | 308 | } |
308 | 309 | ||
309 | uint8_t n1[CRYPTO_NONCE_SIZE]; | 310 | uint8_t n1[CRYPTO_NONCE_SIZE]; |
@@ -317,7 +318,7 @@ START_TEST(test_increment_nonce) | |||
317 | } | 318 | } |
318 | 319 | ||
319 | for (i = 0; i < (1 << 18); ++i) { | 320 | for (i = 0; i < (1 << 18); ++i) { |
320 | uint32_t r = rand(); | 321 | const uint32_t r = random_u32(); |
321 | increment_nonce_number_cmp(n, r); | 322 | increment_nonce_number_cmp(n, r); |
322 | increment_nonce_number(n1, r); | 323 | increment_nonce_number(n1, r); |
323 | ck_assert_msg(memcmp(n, n1, CRYPTO_NONCE_SIZE) == 0, "Bad increment_nonce_number function"); | 324 | 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 034147b9..803dad76 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -13,6 +13,7 @@ | |||
13 | 13 | ||
14 | #include "helpers.h" | 14 | #include "helpers.h" |
15 | 15 | ||
16 | #include "../toxcore/crypto_core.h" | ||
16 | #ifndef DHT_C_INCLUDED | 17 | #ifndef DHT_C_INCLUDED |
17 | #include "../toxcore/DHT.c" | 18 | #include "../toxcore/DHT.c" |
18 | #endif // DHT_C_INCLUDED | 19 | #endif // DHT_C_INCLUDED |
@@ -115,7 +116,7 @@ static void test_addto_lists_update(DHT *dht, | |||
115 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; | 116 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; |
116 | 117 | ||
117 | // check id update for existing ip_port | 118 | // check id update for existing ip_port |
118 | test = rand() % length; | 119 | test = random_u32() % length; |
119 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); | 120 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); |
120 | 121 | ||
121 | random_bytes(test_id, sizeof(test_id)); | 122 | random_bytes(test_id, sizeof(test_id)); |
@@ -128,8 +129,8 @@ static void test_addto_lists_update(DHT *dht, | |||
128 | "Client IP_Port is incorrect"); | 129 | "Client IP_Port is incorrect"); |
129 | 130 | ||
130 | // check ip_port update for existing id | 131 | // check ip_port update for existing id |
131 | test = rand() % length; | 132 | test = random_u32() % length; |
132 | test_ipp.port = rand() % TOX_PORT_DEFAULT; | 133 | test_ipp.port = random_u32() % TOX_PORT_DEFAULT; |
133 | id_copy(test_id, list[test].public_key); | 134 | id_copy(test_id, list[test].public_key); |
134 | 135 | ||
135 | used = addto_lists(dht, test_ipp, test_id); | 136 | used = addto_lists(dht, test_ipp, test_id); |
@@ -140,8 +141,8 @@ static void test_addto_lists_update(DHT *dht, | |||
140 | "Client IP_Port is incorrect"); | 141 | "Client IP_Port is incorrect"); |
141 | 142 | ||
142 | // check ip_port update for existing id and ip_port (... port ... id ...) | 143 | // check ip_port update for existing id and ip_port (... port ... id ...) |
143 | test1 = rand() % (length / 2); | 144 | test1 = random_u32() % (length / 2); |
144 | test2 = rand() % (length / 2) + length / 2; | 145 | test2 = random_u32() % (length / 2) + length / 2; |
145 | 146 | ||
146 | ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); | 147 | ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); |
147 | id_copy(test_id, list[test2].public_key); | 148 | id_copy(test_id, list[test2].public_key); |
@@ -159,8 +160,8 @@ static void test_addto_lists_update(DHT *dht, | |||
159 | "Client IP_Port is incorrect"); | 160 | "Client IP_Port is incorrect"); |
160 | 161 | ||
161 | // check ip_port update for existing id and ip_port (... id ... port ...) | 162 | // check ip_port update for existing id and ip_port (... id ... port ...) |
162 | test1 = rand() % (length / 2); | 163 | test1 = random_u32() % (length / 2); |
163 | test2 = rand() % (length / 2) + length / 2; | 164 | test2 = random_u32() % (length / 2) + length / 2; |
164 | 165 | ||
165 | ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); | 166 | ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); |
166 | id_copy(test_id, list[test1].public_key); | 167 | id_copy(test_id, list[test1].public_key); |
@@ -192,9 +193,9 @@ static void test_addto_lists_bad(DHT *dht, | |||
192 | random_bytes(public_key, sizeof(public_key)); | 193 | random_bytes(public_key, sizeof(public_key)); |
193 | mark_all_good(list, length, ipv6); | 194 | mark_all_good(list, length, ipv6); |
194 | 195 | ||
195 | test1 = rand() % (length / 3); | 196 | test1 = random_u32() % (length / 3); |
196 | test2 = rand() % (length / 3) + length / 3; | 197 | test2 = random_u32() % (length / 3) + length / 3; |
197 | test3 = rand() % (length / 3) + 2 * length / 3; | 198 | test3 = random_u32() % (length / 3) + 2 * length / 3; |
198 | ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); | 199 | ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); |
199 | 200 | ||
200 | id_copy((uint8_t *)&test_id1, list[test1].public_key); | 201 | id_copy((uint8_t *)&test_id1, list[test1].public_key); |
@@ -236,9 +237,9 @@ static void test_addto_lists_possible_bad(DHT *dht, | |||
236 | random_bytes(public_key, sizeof(public_key)); | 237 | random_bytes(public_key, sizeof(public_key)); |
237 | mark_all_good(list, length, ipv6); | 238 | mark_all_good(list, length, ipv6); |
238 | 239 | ||
239 | test1 = rand() % (length / 3); | 240 | test1 = random_u32() % (length / 3); |
240 | test2 = rand() % (length / 3) + length / 3; | 241 | test2 = random_u32() % (length / 3) + length / 3; |
241 | test3 = rand() % (length / 3) + 2 * length / 3; | 242 | test3 = random_u32() % (length / 3) + 2 * length / 3; |
242 | ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); | 243 | ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); |
243 | 244 | ||
244 | id_copy((uint8_t *)&test_id1, list[test1].public_key); | 245 | id_copy((uint8_t *)&test_id1, list[test1].public_key); |
@@ -491,8 +492,8 @@ static void test_list_main(void) | |||
491 | 492 | ||
492 | IP_Port ip_port; | 493 | IP_Port ip_port; |
493 | ip_init(&ip_port.ip, 0); | 494 | ip_init(&ip_port.ip, 0); |
494 | ip_port.ip.ip.v4.uint32 = rand(); | 495 | ip_port.ip.ip.v4.uint32 = random_u32(); |
495 | ip_port.port = rand() % (UINT16_MAX - 1); | 496 | ip_port.port = random_u32() % (UINT16_MAX - 1); |
496 | ++ip_port.port; | 497 | ++ip_port.port; |
497 | addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); | 498 | addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); |
498 | } | 499 | } |
@@ -626,8 +627,8 @@ START_TEST(test_DHT_test) | |||
626 | 627 | ||
627 | for (i = 0; i < NUM_DHT_FRIENDS; ++i) { | 628 | for (i = 0; i < NUM_DHT_FRIENDS; ++i) { |
628 | loop_top: | 629 | loop_top: |
629 | pairs[i].tox1 = rand() % NUM_DHT; | 630 | pairs[i].tox1 = random_u32() % NUM_DHT; |
630 | pairs[i].tox2 = (pairs[i].tox1 + (rand() % (NUM_DHT - 1)) + 1) % NUM_DHT; | 631 | pairs[i].tox2 = (pairs[i].tox1 + (random_u32() % (NUM_DHT - 1)) + 1) % NUM_DHT; |
631 | 632 | ||
632 | for (j = 0; j < i; ++j) { | 633 | for (j = 0; j < i; ++j) { |
633 | if (pairs[j].tox2 == pairs[i].tox2 && pairs[j].tox1 == pairs[i].tox1) { | 634 | 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 a7b41f52..1e90e692 100644 --- a/auto_tests/save_friend_test.c +++ b/auto_tests/save_friend_test.c | |||
@@ -7,6 +7,7 @@ | |||
7 | 7 | ||
8 | #include "helpers.h" | 8 | #include "helpers.h" |
9 | #include "../toxcore/ccompat.h" | 9 | #include "../toxcore/ccompat.h" |
10 | #include "../toxcore/crypto_core.h" | ||
10 | #include "../toxcore/tox.h" | 11 | #include "../toxcore/tox.h" |
11 | 12 | ||
12 | #include <assert.h> | 13 | #include <assert.h> |
@@ -27,7 +28,7 @@ static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TO | |||
27 | uint32_t i; | 28 | uint32_t i; |
28 | 29 | ||
29 | for (i = 0; i < length; ++i) { | 30 | for (i = 0; i < length; ++i) { |
30 | text[i] = rand(); | 31 | text[i] = random_u08(); |
31 | } | 32 | } |
32 | 33 | ||
33 | setter(m, text, SIZEOF_VLA(text), nullptr); | 34 | 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 6c3f2350..34b01d16 100644 --- a/auto_tests/tox_many_tcp_test.c +++ b/auto_tests/tox_many_tcp_test.c | |||
@@ -15,6 +15,7 @@ | |||
15 | #include <stdlib.h> | 15 | #include <stdlib.h> |
16 | #include <time.h> | 16 | #include <time.h> |
17 | 17 | ||
18 | #include "../toxcore/crypto_core.h" | ||
18 | #include "../toxcore/tox.h" | 19 | #include "../toxcore/tox.h" |
19 | #include "../toxcore/util.h" | 20 | #include "../toxcore/util.h" |
20 | 21 | ||
@@ -84,8 +85,8 @@ START_TEST(test_many_clients_tcp) | |||
84 | 85 | ||
85 | for (i = 0; i < NUM_FRIENDS; ++i) { | 86 | for (i = 0; i < NUM_FRIENDS; ++i) { |
86 | loop_top: | 87 | loop_top: |
87 | pairs[i].tox1 = rand() % NUM_TOXES_TCP; | 88 | pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; |
88 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; | 89 | pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; |
89 | 90 | ||
90 | for (j = 0; j < i; ++j) { | 91 | for (j = 0; j < i; ++j) { |
91 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { | 92 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { |
@@ -178,8 +179,8 @@ START_TEST(test_many_clients_tcp_b) | |||
178 | 179 | ||
179 | for (i = 0; i < NUM_FRIENDS; ++i) { | 180 | for (i = 0; i < NUM_FRIENDS; ++i) { |
180 | loop_top: | 181 | loop_top: |
181 | pairs[i].tox1 = rand() % NUM_TOXES_TCP; | 182 | pairs[i].tox1 = random_u32() % NUM_TOXES_TCP; |
182 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; | 183 | pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP; |
183 | 184 | ||
184 | for (j = 0; j < i; ++j) { | 185 | for (j = 0; j < i; ++j) { |
185 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { | 186 | 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 29f3af68..4318b29b 100644 --- a/auto_tests/tox_many_test.c +++ b/auto_tests/tox_many_test.c | |||
@@ -15,6 +15,7 @@ | |||
15 | #include <stdlib.h> | 15 | #include <stdlib.h> |
16 | #include <time.h> | 16 | #include <time.h> |
17 | 17 | ||
18 | #include "../toxcore/crypto_core.h" | ||
18 | #include "../toxcore/tox.h" | 19 | #include "../toxcore/tox.h" |
19 | #include "../toxcore/util.h" | 20 | #include "../toxcore/util.h" |
20 | 21 | ||
@@ -61,8 +62,8 @@ static void test_many_clients(void) | |||
61 | 62 | ||
62 | for (uint32_t i = 0; i < NUM_FRIENDS; ++i) { | 63 | for (uint32_t i = 0; i < NUM_FRIENDS; ++i) { |
63 | loop_top: | 64 | loop_top: |
64 | pairs[i].tox1 = rand() % NUM_TOXES; | 65 | pairs[i].tox1 = random_u32() % NUM_TOXES; |
65 | pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; | 66 | pairs[i].tox2 = (pairs[i].tox1 + random_u32() % (NUM_TOXES - 1) + 1) % NUM_TOXES; |
66 | 67 | ||
67 | for (uint32_t j = 0; j < i; ++j) { | 68 | for (uint32_t j = 0; j < i; ++j) { |
68 | if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) { | 69 | 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 f3323bc9..48e2f791 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <time.h> | 11 | #include <time.h> |
12 | 12 | ||
13 | #include "../toxcore/ccompat.h" | 13 | #include "../toxcore/ccompat.h" |
14 | #include "../toxcore/crypto_core.h" | ||
14 | #include "../toxcore/tox.h" | 15 | #include "../toxcore/tox.h" |
15 | #include "../toxcore/util.h" | 16 | #include "../toxcore/util.h" |
16 | 17 | ||
@@ -21,11 +22,11 @@ static void set_random_name_and_status_message(Tox *tox, uint8_t *name, uint8_t | |||
21 | int i; | 22 | int i; |
22 | 23 | ||
23 | for (i = 0; i < TOX_MAX_NAME_LENGTH; ++i) { | 24 | for (i = 0; i < TOX_MAX_NAME_LENGTH; ++i) { |
24 | name[i] = rand(); | 25 | name[i] = random_u08(); |
25 | } | 26 | } |
26 | 27 | ||
27 | for (i = 0; i < TOX_MAX_STATUS_MESSAGE_LENGTH; ++i) { | 28 | for (i = 0; i < TOX_MAX_STATUS_MESSAGE_LENGTH; ++i) { |
28 | status_message[i] = rand(); | 29 | status_message[i] = random_u08(); |
29 | } | 30 | } |
30 | } | 31 | } |
31 | 32 | ||
diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h index bb2c0a13..e9e8aeb2 100644 --- a/toxcore/crypto_core.api.h +++ b/toxcore/crypto_core.api.h | |||
@@ -117,6 +117,11 @@ static int32_t public_key_cmp( | |||
117 | namespace random { | 117 | namespace random { |
118 | 118 | ||
119 | /** | 119 | /** |
120 | * Return a random 8 bit integer. | ||
121 | */ | ||
122 | static uint8_t u08(); | ||
123 | |||
124 | /** | ||
120 | * Return a random 16 bit integer. | 125 | * Return a random 16 bit integer. |
121 | */ | 126 | */ |
122 | static uint16_t u16(); | 127 | static uint16_t u16(); |
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 26589219..b2f0e5f0 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) | |||
86 | return crypto_verify_32(pk1, pk2); | 86 | return crypto_verify_32(pk1, pk2); |
87 | } | 87 | } |
88 | 88 | ||
89 | uint8_t random_u08(void) | ||
90 | { | ||
91 | uint8_t randnum; | ||
92 | randombytes(&randnum, 1); | ||
93 | return randnum; | ||
94 | } | ||
95 | |||
89 | uint16_t random_u16(void) | 96 | uint16_t random_u16(void) |
90 | { | 97 | { |
91 | uint16_t randnum; | 98 | uint16_t randnum; |
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 2c83fd25..e7e913b6 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h | |||
@@ -127,6 +127,11 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length); | |||
127 | int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2); | 127 | int32_t public_key_cmp(const uint8_t *pk1, const uint8_t *pk2); |
128 | 128 | ||
129 | /** | 129 | /** |
130 | * Return a random 8 bit integer. | ||
131 | */ | ||
132 | uint8_t random_u08(void); | ||
133 | |||
134 | /** | ||
130 | * Return a random 16 bit integer. | 135 | * Return a random 16 bit integer. |
131 | */ | 136 | */ |
132 | uint16_t random_u16(void); | 137 | uint16_t random_u16(void); |