void test_addto_lists_good(DHT *dht, Client_data *list, uint32_t length, IP_Port *ip_port, const uint8_t *comp_client_id) { uint8_t client_id[CLIENT_ID_SIZE]; uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; mark_all_good(list, length, ipv6); // check "good" client id replacement do { randombytes(client_id, sizeof(client_id)); } while (is_furthest(comp_client_id, list, length, client_id)); ip_port->port += 1; addto_lists(dht, *ip_port, client_id); ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Good client id is not in the list"); // check "good" client id skip do { randombytes(client_id, sizeof(client_id)); } while (!is_furthest(comp_client_id, list, length, client_id)); ip_port->port += 1; addto_lists(dht, *ip_port, client_id); ck_assert_msg(client_in_list(list, length, client_id) == -1, "Good client id is in the list"); }
void test_addto_lists_good(DHT *dht, Client_data *list, uint32_t length, IP_Port *ip_port, const uint8_t *comp_client_id) { uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; mark_all_good(list, length, ipv6); // check "good" client id replacement do { randombytes(public_key, sizeof(public_key)); } while (is_furthest(comp_client_id, list, length, public_key)); ip_port->port += 1; addto_lists(dht, *ip_port, public_key); ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Good client id is not in the list"); // check "good" client id skip do { randombytes(public_key, sizeof(public_key)); } while (!is_furthest(comp_client_id, list, length, public_key)); ip_port->port += 1; addto_lists(dht, *ip_port, public_key); ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list"); }
//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist void addto_lists(IP_Port ip_port, char * client_id) { uint32_t i; //NOTE: current behaviour if there are two clients with the same id is to only keep one (the first one) if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { //if we can't replace bad nodes we try replacing good ones replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_client_id); } } for(i = 0; i < num_friends; i++) { if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { //if we can't replace bad nodes we try replacing good ones replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, self_client_id); } } } }
void test_addto_lists_bad(DHT *dht, Client_data *list, uint32_t length, IP_Port *ip_port) { // check "bad" clients replacement int used, test1, test2, test3; uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES], test_id3[crypto_box_PUBLICKEYBYTES]; uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; randombytes(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; ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); id_copy((uint8_t *)&test_id1, list[test1].public_key); id_copy((uint8_t *)&test_id2, list[test2].public_key); id_copy((uint8_t *)&test_id3, list[test3].public_key); // mark nodes as "bad" if (ipv6) { mark_bad(&list[test1].assoc6); mark_bad(&list[test2].assoc6); mark_bad(&list[test3].assoc6); } else { mark_bad(&list[test1].assoc4); mark_bad(&list[test2].assoc4); mark_bad(&list[test3].assoc4); } ip_port->port += 1; used = addto_lists(dht, *ip_port, public_key); ck_assert_msg(used >= 1, "Wrong number of added clients"); ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list"); ck_assert_msg(client_in_list(list, length, test_id2) >= 0, "Wrong bad client removed"); ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed"); }
void test_addto_lists_update(DHT *dht, Client_data *list, uint32_t length, IP_Port *ip_port) { int used, test, test1, test2, found; IP_Port test_ipp; uint8_t test_id[CLIENT_ID_SIZE]; uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; // check id update for existing ip_port test = rand() % length; ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); randombytes(test_id, sizeof(test_id)); used = addto_lists(dht, test_ipp, test_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); // it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test found = client_in_list(list, length, test_id); ck_assert_msg(found >= 0, "Client id is not in the list"); ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[found].assoc6.ip_port : &list[found].assoc4.ip_port), "Client IP_Port is incorrect"); // check ip_port update for existing id test = rand() % length; test_ipp.port = rand() % TOX_PORT_DEFAULT; id_copy(test_id, list[test].client_id); used = addto_lists(dht, test_ipp, test_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); // it is not possible to have id duplicates in the list, so id @ found must be equal id @ test ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list"); ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port), "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; ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); id_copy(test_id, list[test2].client_id); if (ipv6) list[test2].assoc6.ip_port.port = -1; else list[test2].assoc4.ip_port.port = -1; used = addto_lists(dht, test_ipp, test_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); ck_assert_msg(client_in_list(list, length, test_id) == test2, "Client id is not in the list"); ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port), "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; ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); id_copy(test_id, list[test1].client_id); if (ipv6) list[test1].assoc6.ip_port.port = -1; else list[test1].assoc4.ip_port.port = -1; used = addto_lists(dht, test_ipp, test_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); ck_assert_msg(client_in_list(list, length, test_id) == test1, "Client id is not in the list"); ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port), "Client IP_Port is incorrect"); }
void test_addto_lists_possible_bad(DHT *dht, Client_data *list, uint32_t length, IP_Port *ip_port, const uint8_t *comp_client_id) { // check "possibly bad" clients replacement int used, test1, test2, test3; uint8_t client_id[CLIENT_ID_SIZE], test_id1[CLIENT_ID_SIZE], test_id2[CLIENT_ID_SIZE], test_id3[CLIENT_ID_SIZE]; uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0; randombytes(client_id, sizeof(client_id)); mark_all_good(list, length, ipv6); test1 = rand() % (length / 3); test2 = rand() % (length / 3) + length / 3; test3 = rand() % (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].client_id); id_copy((uint8_t *)&test_id2, list[test2].client_id); id_copy((uint8_t *)&test_id3, list[test3].client_id); // mark nodes as "possibly bad" if (ipv6) { mark_possible_bad(&list[test1].assoc6); mark_possible_bad(&list[test2].assoc6); mark_possible_bad(&list[test3].assoc6); } else { mark_possible_bad(&list[test1].assoc4); mark_possible_bad(&list[test2].assoc4); mark_possible_bad(&list[test3].assoc4); } ip_port->port += 1; used = addto_lists(dht, *ip_port, client_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); ck_assert_msg(client_in_list(list, length, client_id) >= 0, "Client id is not in the list"); int inlist_id1 = client_in_list(list, length, test_id1) >= 0; int inlist_id2 = client_in_list(list, length, test_id2) >= 0; int inlist_id3 = client_in_list(list, length, test_id3) >= 0; ck_assert_msg(inlist_id1 + inlist_id2 + inlist_id3 == 2, "Wrong client removed"); if (!inlist_id1) { ck_assert_msg(id_closest(comp_client_id, test_id2, test_id1) == 1, "Id has been removed but is closer to than another one"); ck_assert_msg(id_closest(comp_client_id, test_id3, test_id1) == 1, "Id has been removed but is closer to than another one"); } else if (!inlist_id2) { ck_assert_msg(id_closest(comp_client_id, test_id1, test_id2) == 1, "Id has been removed but is closer to than another one"); ck_assert_msg(id_closest(comp_client_id, test_id3, test_id2) == 1, "Id has been removed but is closer to than another one"); } else if (!inlist_id3) { ck_assert_msg(id_closest(comp_client_id, test_id1, test_id3) == 1, "Id has been removed but is closer to than another one"); ck_assert_msg(id_closest(comp_client_id, test_id2, test_id3) == 1, "Id has been removed but is closer to than another one"); } }