diff options
Diffstat (limited to 'auto_tests/dht_test.c')
-rw-r--r-- | auto_tests/dht_test.c | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 091b50fb..6d7e70ad 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -44,7 +44,7 @@ void mark_good(IPPTsPng *ipptp) | |||
44 | 44 | ||
45 | void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) | 45 | void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) |
46 | { | 46 | { |
47 | int i; | 47 | uint32_t i; |
48 | for (i = 0; i < length; ++i) { | 48 | for (i = 0; i < length; ++i) { |
49 | if (ipv6) | 49 | if (ipv6) |
50 | mark_good(&list[i].assoc6); | 50 | mark_good(&list[i].assoc6); |
@@ -57,17 +57,17 @@ void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6) | |||
57 | than all client_id's in the list */ | 57 | than all client_id's in the list */ |
58 | uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *client_id) | 58 | uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *client_id) |
59 | { | 59 | { |
60 | int i; | 60 | uint32_t i; |
61 | for (i = 0; i < length; ++i) | 61 | for (i = 0; i < length; ++i) |
62 | if (id_closest(comp_client_id, client_id, list[i].client_id) == 1) | 62 | if (id_closest(comp_client_id, client_id, list[i].client_id) == 1) |
63 | return 0; | 63 | return 0; |
64 | return 1; | 64 | return 1; |
65 | } | 65 | } |
66 | 66 | ||
67 | int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id) | 67 | int client_in_list(Client_data *list, uint32_t length, const uint8_t *client_id) |
68 | { | 68 | { |
69 | int i; | 69 | int i; |
70 | for (i = 0; i < length; ++i) | 70 | for (i = 0; i < (int)length; ++i) |
71 | if (id_equal(client_id, list[i].client_id)) | 71 | if (id_equal(client_id, list[i].client_id)) |
72 | return i; | 72 | return i; |
73 | return -1; | 73 | return -1; |
@@ -78,28 +78,38 @@ void test_addto_lists_update(DHT *dht, | |||
78 | uint32_t length, | 78 | uint32_t length, |
79 | IP_Port *ip_port) | 79 | IP_Port *ip_port) |
80 | { | 80 | { |
81 | int used, test = rand() % length, test1 = rand() % (length / 2), test2 = rand() % (length / 2) + length / 2; | 81 | int used, test, test1, test2, found; |
82 | uint8_t test_id[CLIENT_ID_SIZE]; | ||
83 | IP_Port test_ipp; | 82 | IP_Port test_ipp; |
83 | uint8_t test_id[CLIENT_ID_SIZE]; | ||
84 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 84 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
85 | 85 | ||
86 | // check id update for existing ip_port | ||
87 | test = rand() % length; | ||
86 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); | 88 | ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); |
87 | 89 | ||
88 | // check id update for existing ip_port | ||
89 | randombytes(test_id, sizeof(test_id)); | 90 | randombytes(test_id, sizeof(test_id)); |
90 | used = addto_lists(dht, test_ipp, test_id); | 91 | used = addto_lists(dht, test_ipp, test_id); |
91 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 92 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
92 | ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list"); | 93 | // it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test |
93 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port), "Client IP_Port is incorrect"); | 94 | found = client_in_list(list, length, test_id); |
95 | ck_assert_msg(found >= 0, "Client id is not in the list"); | ||
96 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[found].assoc6.ip_port : &list[found].assoc4.ip_port), "Client IP_Port is incorrect"); | ||
94 | 97 | ||
95 | // check ip_port update for existing id | 98 | // check ip_port update for existing id |
99 | test = rand() % length; | ||
96 | test_ipp.port = rand() % TOX_PORT_DEFAULT; | 100 | test_ipp.port = rand() % TOX_PORT_DEFAULT; |
101 | id_copy(test_id, list[test].client_id); | ||
102 | |||
97 | used = addto_lists(dht, test_ipp, test_id); | 103 | used = addto_lists(dht, test_ipp, test_id); |
98 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 104 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
105 | // it is not possible to have id duplicates in the list, so id @ found must be equal id @ test | ||
99 | ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list"); | 106 | ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list"); |
100 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port), "Client IP_Port is incorrect"); | 107 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port), "Client IP_Port is incorrect"); |
101 | 108 | ||
102 | // check ip_port update for existing id and ip_port (... port ... id ...) | 109 | // check ip_port update for existing id and ip_port (... port ... id ...) |
110 | test1 = rand() % (length / 2); | ||
111 | test2 = rand() % (length / 2) + length / 2; | ||
112 | |||
103 | ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); | 113 | ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); |
104 | id_copy(test_id, list[test2].client_id); | 114 | id_copy(test_id, list[test2].client_id); |
105 | if (ipv6) list[test2].assoc6.ip_port.port = -1; else list[test2].assoc4.ip_port.port = -1; | 115 | if (ipv6) list[test2].assoc6.ip_port.port = -1; else list[test2].assoc4.ip_port.port = -1; |
@@ -109,6 +119,9 @@ void test_addto_lists_update(DHT *dht, | |||
109 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port), "Client IP_Port is incorrect"); | 119 | ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port), "Client IP_Port is incorrect"); |
110 | 120 | ||
111 | // check ip_port update for existing id and ip_port (... id ... port ...) | 121 | // check ip_port update for existing id and ip_port (... id ... port ...) |
122 | test1 = rand() % (length / 2); | ||
123 | test2 = rand() % (length / 2) + length / 2; | ||
124 | |||
112 | ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); | 125 | ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); |
113 | id_copy(test_id, list[test1].client_id); | 126 | id_copy(test_id, list[test1].client_id); |
114 | if (ipv6) list[test1].assoc6.ip_port.port = -1; else list[test1].assoc4.ip_port.port = -1; | 127 | if (ipv6) list[test1].assoc6.ip_port.port = -1; else list[test1].assoc4.ip_port.port = -1; |
@@ -124,8 +137,8 @@ void test_addto_lists_bad(DHT *dht, | |||
124 | IP_Port *ip_port) | 137 | IP_Port *ip_port) |
125 | { | 138 | { |
126 | // check "bad" clients replacement | 139 | // check "bad" clients replacement |
140 | int used, test1, test2, test3; | ||
127 | uint8_t client_id[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; | 141 | uint8_t client_id[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; |
128 | int test1, test2, test3; | ||
129 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 142 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
130 | 143 | ||
131 | randombytes(client_id, sizeof(client_id)); | 144 | randombytes(client_id, sizeof(client_id)); |
@@ -152,7 +165,7 @@ void test_addto_lists_bad(DHT *dht, | |||
152 | } | 165 | } |
153 | 166 | ||
154 | ip_port->port += 1; | 167 | ip_port->port += 1; |
155 | int used = addto_lists(dht, *ip_port, client_id); | 168 | used = addto_lists(dht, *ip_port, client_id); |
156 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 169 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
157 | 170 | ||
158 | ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Client id is not in the list"); | 171 | ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Client id is not in the list"); |
@@ -168,8 +181,8 @@ void test_addto_lists_possible_bad(DHT *dht, | |||
168 | const uint8_t *comp_client_id) | 181 | const uint8_t *comp_client_id) |
169 | { | 182 | { |
170 | // check "possibly bad" clients replacement | 183 | // check "possibly bad" clients replacement |
184 | int used, test1, test2, test3; | ||
171 | uint8_t client_id[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; | 185 | uint8_t client_id[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; |
172 | int test1, test2, test3; | ||
173 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; | 186 | uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; |
174 | 187 | ||
175 | randombytes(client_id, sizeof(client_id)); | 188 | randombytes(client_id, sizeof(client_id)); |
@@ -196,7 +209,7 @@ void test_addto_lists_possible_bad(DHT *dht, | |||
196 | } | 209 | } |
197 | 210 | ||
198 | ip_port->port += 1; | 211 | ip_port->port += 1; |
199 | int used = addto_lists(dht, *ip_port, client_id); | 212 | used = addto_lists(dht, *ip_port, client_id); |
200 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 213 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
201 | 214 | ||
202 | ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Client id is not in the list"); | 215 | ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Client id is not in the list"); |
@@ -258,25 +271,25 @@ void test_addto_lists(IP ip) | |||
258 | 271 | ||
259 | IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; | 272 | IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; |
260 | uint8_t client_id[CLIENT_ID_SIZE]; | 273 | uint8_t client_id[CLIENT_ID_SIZE]; |
261 | int i; | 274 | int i, used; |
262 | 275 | ||
263 | // check lists filling | 276 | // check lists filling |
264 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { | 277 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { |
265 | randombytes(client_id, sizeof(client_id)); | 278 | randombytes(client_id, sizeof(client_id)); |
266 | int used = addto_lists(dht, ip_port, client_id); | 279 | used = addto_lists(dht, ip_port, client_id); |
267 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); | 280 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); |
268 | } | 281 | } |
269 | 282 | ||
270 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { | 283 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { |
271 | ip_port.port += 1; | 284 | ip_port.port += 1; |
272 | int used = addto_lists(dht, ip_port, client_id); | 285 | used = addto_lists(dht, ip_port, client_id); |
273 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing client_id"); | 286 | ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing client_id"); |
274 | } | 287 | } |
275 | 288 | ||
276 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { | 289 | for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { |
277 | ip_port.port += 1; | 290 | ip_port.port += 1; |
278 | randombytes(client_id, sizeof(client_id)); | 291 | randombytes(client_id, sizeof(client_id)); |
279 | int used = addto_lists(dht, ip_port, client_id); | 292 | used = addto_lists(dht, ip_port, client_id); |
280 | ck_assert_msg(used >= 1, "Wrong number of added clients"); | 293 | ck_assert_msg(used >= 1, "Wrong number of added clients"); |
281 | } | 294 | } |
282 | 295 | ||