summaryrefslogtreecommitdiff
path: root/auto_tests/dht_test.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-09-05 16:10:48 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-09-06 11:54:37 +0100
commitad2656051697899e960694bb68ac104fcc5e92f1 (patch)
tree7e69fcd03db88b3839ee523f5d1b51ef9a38c372 /auto_tests/dht_test.c
parent4e6c86d1cb228308678f89ff6e4e09b3f46347aa (diff)
Improve static and const correctness.
- Any non-externally-visible declarations should be `static`. - Casting away the `const` qualifier from pointers-to-const is dangerous. All but one instance of this are now correct. The one instance where we can't keep `const` is one where toxav code actually writes to a chunk of memory marked as `const`. This code also assumes 4 byte alignment of data packets. I don't know whether that is a valid assumption, but it's likely unportable, and *not* obviously correct. - Replaced empty parameter lists with `(void)` to avoid passing parameters to it. Empty parameter lists are old style declarations for unknown number and type of arguments. - Commented out (as `#if DHT_HARDENING` block) the hardening code that was never executed. - Minor style fix: don't use `default` in enum-switches unless the number of enumerators in the default case is very large. In this case, it was 2, so we want to list them both explicitly to be warned about missing one if we add one in the future. - Removed the only two function declarations from nTox.h and put them into nTox.c. They are not used outside and nTox is not a library.
Diffstat (limited to 'auto_tests/dht_test.c')
-rw-r--r--auto_tests/dht_test.c66
1 files changed, 34 insertions, 32 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c
index 7aab9573..dd68ffbe 100644
--- a/auto_tests/dht_test.c
+++ b/auto_tests/dht_test.c
@@ -10,6 +10,9 @@
10 10
11#include "helpers.h" 11#include "helpers.h"
12 12
13
14// These tests currently fail.
15#if 0
13#define swap(x,y) do \ 16#define swap(x,y) do \
14 { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \ 17 { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
15 memcpy(swap_temp,&y,sizeof(x)); \ 18 memcpy(swap_temp,&y,sizeof(x)); \
@@ -18,7 +21,7 @@
18 } while(0) 21 } while(0)
19 22
20 23
21void mark_bad(IPPTsPng *ipptp) 24static void mark_bad(IPPTsPng *ipptp)
22{ 25{
23 ipptp->timestamp = unix_time() - 2 * BAD_NODE_TIMEOUT; 26 ipptp->timestamp = unix_time() - 2 * BAD_NODE_TIMEOUT;
24 ipptp->hardening.routes_requests_ok = 0; 27 ipptp->hardening.routes_requests_ok = 0;
@@ -26,7 +29,7 @@ void mark_bad(IPPTsPng *ipptp)
26 ipptp->hardening.testing_requests = 0; 29 ipptp->hardening.testing_requests = 0;
27} 30}
28 31
29void mark_possible_bad(IPPTsPng *ipptp) 32static void mark_possible_bad(IPPTsPng *ipptp)
30{ 33{
31 ipptp->timestamp = unix_time(); 34 ipptp->timestamp = unix_time();
32 ipptp->hardening.routes_requests_ok = 0; 35 ipptp->hardening.routes_requests_ok = 0;
@@ -34,7 +37,7 @@ void mark_possible_bad(IPPTsPng *ipptp)
34 ipptp->hardening.testing_requests = 0; 37 ipptp->hardening.testing_requests = 0;
35} 38}
36 39
37void mark_good(IPPTsPng *ipptp) 40static void mark_good(IPPTsPng *ipptp)
38{ 41{
39 ipptp->timestamp = unix_time(); 42 ipptp->timestamp = unix_time();
40 ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1; 43 ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1;
@@ -42,7 +45,7 @@ void mark_good(IPPTsPng *ipptp)
42 ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1; 45 ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1;
43} 46}
44 47
45void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) 48static void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)
46{ 49{
47 uint32_t i; 50 uint32_t i;
48 51
@@ -57,7 +60,7 @@ void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)
57 60
58/* Returns 1 if public_key has a furthest distance to comp_client_id 61/* Returns 1 if public_key has a furthest distance to comp_client_id
59 than all public_key's in the list */ 62 than all public_key's in the list */
60uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key) 63static uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key)
61{ 64{
62 uint32_t i; 65 uint32_t i;
63 66
@@ -70,7 +73,7 @@ uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t l
70 return 1; 73 return 1;
71} 74}
72 75
73int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key) 76static int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key)
74{ 77{
75 int i; 78 int i;
76 79
@@ -83,10 +86,10 @@ int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key
83 return -1; 86 return -1;
84} 87}
85 88
86void test_addto_lists_update(DHT *dht, 89static void test_addto_lists_update(DHT *dht,
87 Client_data *list, 90 Client_data *list,
88 uint32_t length, 91 uint32_t length,
89 IP_Port *ip_port) 92 IP_Port *ip_port)
90{ 93{
91 int used, test, test1, test2, found; 94 int used, test, test1, test2, found;
92 IP_Port test_ipp; 95 IP_Port test_ipp;
@@ -157,10 +160,10 @@ void test_addto_lists_update(DHT *dht,
157 "Client IP_Port is incorrect"); 160 "Client IP_Port is incorrect");
158} 161}
159 162
160void test_addto_lists_bad(DHT *dht, 163static void test_addto_lists_bad(DHT *dht,
161 Client_data *list, 164 Client_data *list,
162 uint32_t length, 165 uint32_t length,
163 IP_Port *ip_port) 166 IP_Port *ip_port)
164{ 167{
165 // check "bad" clients replacement 168 // check "bad" clients replacement
166 int used, test1, test2, test3; 169 int used, test1, test2, test3;
@@ -200,11 +203,11 @@ void test_addto_lists_bad(DHT *dht,
200 ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed"); 203 ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed");
201} 204}
202 205
203void test_addto_lists_possible_bad(DHT *dht, 206static void test_addto_lists_possible_bad(DHT *dht,
204 Client_data *list, 207 Client_data *list,
205 uint32_t length, 208 uint32_t length,
206 IP_Port *ip_port, 209 IP_Port *ip_port,
207 const uint8_t *comp_client_id) 210 const uint8_t *comp_client_id)
208{ 211{
209 // check "possibly bad" clients replacement 212 // check "possibly bad" clients replacement
210 int used, test1, test2, test3; 213 int used, test1, test2, test3;
@@ -265,11 +268,11 @@ void test_addto_lists_possible_bad(DHT *dht,
265 } 268 }
266} 269}
267 270
268void test_addto_lists_good(DHT *dht, 271static void test_addto_lists_good(DHT *dht,
269 Client_data *list, 272 Client_data *list,
270 uint32_t length, 273 uint32_t length,
271 IP_Port *ip_port, 274 IP_Port *ip_port,
272 const uint8_t *comp_client_id) 275 const uint8_t *comp_client_id)
273{ 276{
274 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 277 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
275 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; 278 uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
@@ -295,8 +298,6 @@ void test_addto_lists_good(DHT *dht,
295 ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list"); 298 ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
296} 299}
297 300
298// These tests currently fail.
299#if 0
300static void test_addto_lists(IP ip) 301static void test_addto_lists(IP ip)
301{ 302{
302 Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT); 303 Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT);
@@ -382,7 +383,7 @@ END_TEST
382 383
383#define DHT_LIST_LENGTH 128 384#define DHT_LIST_LENGTH 128
384 385
385void print_pk(uint8_t *public_key) 386static void print_pk(uint8_t *public_key)
386{ 387{
387 uint32_t j; 388 uint32_t j;
388 389
@@ -393,8 +394,9 @@ void print_pk(uint8_t *public_key)
393 printf("\n"); 394 printf("\n");
394} 395}
395 396
396void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigned int length, const uint8_t *pk, 397static void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1],
397 const uint8_t *cmp_pk) 398 unsigned int length, const uint8_t *pk,
399 const uint8_t *cmp_pk)
398{ 400{
399 uint8_t p_b[crypto_box_PUBLICKEYBYTES]; 401 uint8_t p_b[crypto_box_PUBLICKEYBYTES];
400 unsigned int i; 402 unsigned int i;
@@ -423,7 +425,7 @@ void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigne
423 425
424#define NUM_DHT 100 426#define NUM_DHT 100
425 427
426void test_list_main() 428static void test_list_main(void)
427{ 429{
428 DHT *dhts[NUM_DHT]; 430 DHT *dhts[NUM_DHT];
429 431
@@ -555,7 +557,7 @@ START_TEST(test_list)
555} 557}
556END_TEST 558END_TEST
557 559
558void ip_callback(void *data, int32_t number, IP_Port ip_port) 560static void ip_callback(void *data, int32_t number, IP_Port ip_port)
559{ 561{
560} 562}
561 563
@@ -646,7 +648,7 @@ loop_top:
646} 648}
647END_TEST 649END_TEST
648 650
649Suite *dht_suite(void) 651static Suite *dht_suite(void)
650{ 652{
651 Suite *s = suite_create("DHT"); 653 Suite *s = suite_create("DHT");
652 654