summaryrefslogtreecommitdiff
path: root/auto_tests/dht_test.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-03-28 13:36:14 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-04-03 17:43:22 +0000
commit37d4a0b2ca1377268a82c085809edf63d19cc782 (patch)
tree9b0724cb55f479689cbc04c8b72f9bde99f775ab /auto_tests/dht_test.c
parent7fa0c89c96bdaf45bf202d770aa56dc7c68959b9 (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.
Diffstat (limited to 'auto_tests/dht_test.c')
-rw-r--r--auto_tests/dht_test.c35
1 files changed, 18 insertions, 17 deletions
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) {
628loop_top: 629loop_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) {