From 2cc8dafd4c17fb9220fcf9c1017f13d508e3e8f2 Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 3 Mar 2022 20:58:05 +0000 Subject: [PATCH] cleanup: Remove redundant Messenger and DHT tests. These don't test anything that isn't covered by higher level tox tests. These are also not unit tests and have never found any bug that wasn't also caught by other tests. This makes them a pure maintenance burden. --- .cirrus.yml | 2 - CMakeLists.txt | 2 - auto_tests/BUILD.bazel | 2 - auto_tests/Makefile.inc | 10 - auto_tests/dht_test.c | 739 ------------------------------------ auto_tests/messenger_test.c | 322 ---------------- other/analysis/gen-file.sh | 1 - other/emscripten/Dockerfile | 4 +- toxcore/BUILD.bazel | 24 -- 9 files changed, 2 insertions(+), 1104 deletions(-) delete mode 100644 auto_tests/dht_test.c delete mode 100644 auto_tests/messenger_test.c diff --git a/.cirrus.yml b/.cirrus.yml index b160b0e6..a3f70757 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -86,7 +86,6 @@ bazel-tsan_task: //c-toxcore/... -//c-toxcore/auto_tests:conference_av_test -//c-toxcore/auto_tests:conference_test - -//c-toxcore/auto_tests:dht_test -//c-toxcore/auto_tests:file_transfer_test -//c-toxcore/auto_tests:onion_test -//c-toxcore/auto_tests:tcp_relay_test @@ -111,7 +110,6 @@ bazel-valgrind_task: //c-toxcore/... -//c-toxcore/auto_tests:conference_av_test -//c-toxcore/auto_tests:conference_test - -//c-toxcore/auto_tests:dht_test -//c-toxcore/auto_tests:encryptsave_test -//c-toxcore/auto_tests:file_transfer_test -//c-toxcore/auto_tests:onion_test diff --git a/CMakeLists.txt b/CMakeLists.txt index 062926b6..3af96fb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -454,7 +454,6 @@ auto_test(conference_peer_nick) auto_test(conference_simple) auto_test(conference_two) auto_test(crypto) -#auto_test(dht) # Doesn't work with UNITY_BUILD. auto_test(dht_getnodes_api) auto_test(encryptsave) auto_test(file_transfer) @@ -467,7 +466,6 @@ auto_test(invalid_udp_proxy) auto_test(lan_discovery) auto_test(lossless_packet) auto_test(lossy_packet) -auto_test(messenger) auto_test(network) auto_test(onion) auto_test(overflow_recvq) diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index 8efb4aa5..08ce43c0 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel @@ -44,8 +44,6 @@ flaky_tests = { ":check_compat", "//c-toxcore/testing:misc_tools", "//c-toxcore/toxav", - "//c-toxcore/toxcore:DHT_srcs", - "//c-toxcore/toxcore:Messenger", "//c-toxcore/toxcore:TCP_client", "//c-toxcore/toxcore:TCP_common", "//c-toxcore/toxcore:TCP_connection", diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index b3bc82fb..e2bea449 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc @@ -12,7 +12,6 @@ TESTS = \ conference_test \ conference_two_test \ crypto_test \ - dht_test \ file_transfer_test \ friend_connection_test \ friend_request_test \ @@ -21,7 +20,6 @@ TESTS = \ lan_discovery_test \ lossless_packet_test \ lossy_packet_test \ - messenger_test \ network_test \ onion_test \ overflow_recvq_test \ @@ -99,10 +97,6 @@ crypto_test_SOURCES = ../auto_tests/crypto_test.c crypto_test_CFLAGS = $(AUTOTEST_CFLAGS) crypto_test_LDADD = $(AUTOTEST_LDADD) -dht_test_SOURCES = ../auto_tests/dht_test.c -dht_test_CFLAGS = $(AUTOTEST_CFLAGS) -dht_test_LDADD = $(AUTOTEST_LDADD) - encryptsave_test_SOURCES = ../auto_tests/encryptsave_test.c encryptsave_test_CFLAGS = $(AUTOTEST_CFLAGS) encryptsave_test_LDADD = $(AUTOTEST_LDADD) @@ -143,10 +137,6 @@ lossy_packet_test_SOURCES = ../auto_tests/lossy_packet_test.c lossy_packet_test_CFLAGS = $(AUTOTEST_CFLAGS) lossy_packet_test_LDADD = $(AUTOTEST_LDADD) -messenger_test_SOURCES = ../auto_tests/messenger_test.c -messenger_test_CFLAGS = $(AUTOTEST_CFLAGS) -messenger_test_LDADD = $(AUTOTEST_LDADD) - network_test_SOURCES = ../auto_tests/network_test.c network_test_CFLAGS = $(AUTOTEST_CFLAGS) network_test_LDADD = $(AUTOTEST_LDADD) diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c deleted file mode 100644 index b07800f1..00000000 --- a/auto_tests/dht_test.c +++ /dev/null @@ -1,739 +0,0 @@ -#include "auto_test_support.h" -#include "check_compat.h" -#include "../testing/misc_tools.h" -#include "../toxcore/crypto_core.h" - -#ifndef DHT_C_INCLUDED -#include "../toxcore/DHT.c" -#endif // DHT_C_INCLUDED -#include "../toxcore/tox.h" - - -// These tests currently fail. -static bool enable_broken_tests = false; - -#ifndef USE_IPV6 -#define USE_IPV6 1 -#endif - -static inline IP get_loopback(void) -{ - IP ip; -#if USE_IPV6 - ip.family = net_family_ipv6; - ip.ip.v6 = get_ip6_loopback(); -#else - ip.family = net_family_ipv4; - ip.ip.v4 = get_ip4_loopback(); -#endif - return ip; -} - -static void mark_bad(const Mono_Time *mono_time, IPPTsPng *ipptp) -{ - ipptp->timestamp = mono_time_get(mono_time) - 2 * BAD_NODE_TIMEOUT; -} - -static void mark_good(const Mono_Time *mono_time, IPPTsPng *ipptp) -{ - ipptp->timestamp = mono_time_get(mono_time); -} - -static void mark_all_good(const Mono_Time *mono_time, Client_data *list, uint32_t length, uint8_t ipv6) -{ - uint32_t i; - - for (i = 0; i < length; ++i) { - if (ipv6) { - mark_good(mono_time, &list[i].assoc6); - } else { - mark_good(mono_time, &list[i].assoc4); - } - } -} - -/* Returns 1 if public_key has a furthest distance to comp_client_id - than all public_key's in the list */ -static uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key) -{ - uint32_t i; - - for (i = 0; i < length; ++i) { - if (id_closest(comp_client_id, public_key, list[i].public_key) == 1) { - return 0; - } - } - - return 1; -} - -static int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key) -{ - uint32_t i; - - for (i = 0; i < length; ++i) { - if (pk_equal(public_key, list[i].public_key)) { - return i; - } - } - - return -1; -} - -static void test_addto_lists_update(DHT *dht, - Client_data *list, - uint32_t length, - IP_Port *ip_port) -{ - uint32_t used, test, test1, test2, found; - IP_Port test_ipp; - uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; - - // check id update for existing ip_port - test = random_u32() % length; - ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port); - - random_bytes(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 = random_u32() % length; - test_ipp.port = random_u32() % TOX_PORT_DEFAULT; - pk_copy(test_id, list[test].public_key); - - 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 = random_u32() % (length / 2); - test2 = random_u32() % (length / 2) + length / 2; - - ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port); - pk_copy(test_id, list[test2].public_key); - - 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 = random_u32() % (length / 2); - test2 = random_u32() % (length / 2) + length / 2; - - ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port); - pk_copy(test_id, list[test1].public_key); - - 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"); -} - -static 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_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], - test_id3[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; - - random_bytes(public_key, sizeof(public_key)); - mark_all_good(dht->mono_time, list, length, ipv6); - - test1 = random_u32() % (length / 3); - test2 = random_u32() % (length / 3) + length / 3; - test3 = random_u32() % (length / 3) + 2 * length / 3; - ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen"); - - pk_copy((uint8_t *)&test_id1, list[test1].public_key); - pk_copy((uint8_t *)&test_id2, list[test2].public_key); - pk_copy((uint8_t *)&test_id3, list[test3].public_key); - - // mark nodes as "bad" - if (ipv6) { - mark_bad(dht->mono_time, &list[test1].assoc6); - mark_bad(dht->mono_time, &list[test2].assoc6); - mark_bad(dht->mono_time, &list[test3].assoc6); - } else { - mark_bad(dht->mono_time, &list[test1].assoc4); - mark_bad(dht->mono_time, &list[test2].assoc4); - mark_bad(dht->mono_time, &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"); -} - -static 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_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; - - mark_all_good(dht->mono_time, list, length, ipv6); - - // check "good" client id replacement - do { - random_bytes(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 { - random_bytes(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"); -} - -#ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#endif - -static void test_addto_lists(IP ip) -{ - Logger *log = logger_new(); - uint32_t index = 1; - logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index); - - Mono_Time *mono_time = mono_time_new(); - ck_assert_msg(mono_time != nullptr, "Failed to create Mono_Time"); - - Networking_Core *net = new_networking(log, &ip, TOX_PORT_DEFAULT); - ck_assert_msg(net != nullptr, "Failed to create Networking_Core"); - - DHT *dht = new_dht(log, mono_time, net, true); - ck_assert_msg(dht != nullptr, "Failed to create DHT"); - - IP_Port ip_port; - ip_port.ip = ip; - ip_port.port = TOX_PORT_DEFAULT; - uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; - uint16_t i, used; - - // check lists filling - for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { - random_bytes(public_key, sizeof(public_key)); - used = addto_lists(dht, &ip_port, public_key); - ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); - } - - for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { - ip_port.port += 1; - used = addto_lists(dht, &ip_port, public_key); - ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing public_key"); - } - - for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { - ip_port.port += 1; - random_bytes(public_key, sizeof(public_key)); - used = addto_lists(dht, &ip_port, public_key); - ck_assert_msg(used >= 1, "Wrong number of added clients"); - } - - /*check: Current behavior if there are two clients with the same id is - * to replace the first ip by the second. */ - test_addto_lists_update(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port); - - for (i = 0; i < dht->num_friends; ++i) { - test_addto_lists_update(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port); - } - - // check "bad" entries - test_addto_lists_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port); - - for (i = 0; i < dht->num_friends; ++i) { - test_addto_lists_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port); - } - - // check "good" entries - test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); - - for (i = 0; i < dht->num_friends; ++i) { - test_addto_lists_good(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port, - dht->friends_list[i].public_key); - } - - kill_dht(dht); - kill_networking(net); - logger_kill(log); -} - -static void test_addto_lists_ipv4(void) -{ - IP ip; - ip_init(&ip, 0); - test_addto_lists(ip); -} - -static void test_addto_lists_ipv6(void) -{ - IP ip; - ip_init(&ip, 1); - test_addto_lists(ip); -} - -#define DHT_DEFAULT_PORT (TOX_PORT_DEFAULT + 1000) - -static void print_pk(uint8_t *public_key) -{ - uint32_t j; - - for (j = 0; j < CRYPTO_PUBLIC_KEY_SIZE; j++) { - printf("%02X", public_key[j]); - } - - printf("\n"); -} - -static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1], - uint16_t length, const uint8_t *pk, - const uint8_t *cmp_pk) -{ - uint8_t p_b[CRYPTO_PUBLIC_KEY_SIZE]; - uint16_t i; - - for (i = 0; i < length; ++i) { - if (!cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE]) { - memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE); - cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE] = 1; - return; - } - - if (memcmp(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE) == 0) { - return; - } - } - - for (i = 0; i < length; ++i) { - if (id_closest(cmp_pk, cmp_list[i], pk) == 2) { - memcpy(p_b, cmp_list[i], CRYPTO_PUBLIC_KEY_SIZE); - memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE); - test_add_to_list(cmp_list, length, p_b, cmp_pk); - break; - } - } -} - -#define NUM_DHT 100 - -static void test_list_main(void) -{ - DHT *dhts[NUM_DHT]; - Logger *logs[NUM_DHT]; - Mono_Time *mono_times[NUM_DHT]; - uint32_t index[NUM_DHT]; - - uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; - memset(cmp_list1, 0, sizeof(cmp_list1)); - - uint16_t i, j, k, l; - - for (i = 0; i < NUM_DHT; ++i) { - IP ip; - ip_init(&ip, 1); - - logs[i] = logger_new(); - index[i] = i + 1; - logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); - - mono_times[i] = mono_time_new(); - - dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true); - ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); - ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, - "Bound to wrong port: %d", net_port(dhts[i]->net)); - } - - for (i = 0; i < NUM_DHT; ++i) { - for (j = 1; j < NUM_DHT; ++j) { - test_add_to_list(cmp_list1[i], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[i]->self_public_key); - } - } - - for (i = 0; i < NUM_DHT; ++i) { - for (j = 0; j < NUM_DHT; ++j) { - if (i == j) { - continue; - } - - IP_Port ip_port; - ip_init(&ip_port.ip, 0); - ip_port.ip.ip.v4.uint32 = random_u32(); - ip_port.port = random_u32() % (UINT16_MAX - 1); - ++ip_port.port; - addto_lists(dhts[i], &ip_port, dhts[j]->self_public_key); - } - } - -#if 0 - print_pk(dhts[0]->self_public_key); - - for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - printf("----Entry %u----\n", i); - - print_pk(cmp_list1[i]); - } - -#endif - uint16_t m_count = 0; - - for (l = 0; l < NUM_DHT; ++l) { - for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - for (j = 1; j < NUM_DHT; ++j) { - if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, CRYPTO_PUBLIC_KEY_SIZE) != 0) { - continue; - } - - uint16_t count = 0; - - for (k = 0; k < LCLIENT_LIST; ++k) { - if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key, - CRYPTO_PUBLIC_KEY_SIZE) == 0) { - ++count; - } - } - - if (count != 1) { - print_pk(dhts[l]->self_public_key); - - for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) { - printf("----Entry %u----\n", k); - - print_pk(cmp_list1[l][k]); - } - - for (k = 0; k < LCLIENT_LIST; ++k) { - printf("----Closel %u----\n", k); - print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key); - } - - print_pk(dhts[(l + j) % NUM_DHT]->self_public_key); - } - - ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); - - Node_format ln[MAX_SENT_NODES]; - uint16_t n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1); - ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); - - count = 0; - - for (k = 0; k < MAX_SENT_NODES; ++k) { - if (memcmp(dhts[l]->self_public_key, ln[k].public_key, CRYPTO_PUBLIC_KEY_SIZE) == 0) { - ++count; - } - } - - ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); -#if 0 - - for (k = 0; k < MAX_SENT_NODES; ++k) { - printf("----gn %u----\n", k); - print_pk(ln[k].public_key); - } - -#endif - ++m_count; - } - } - } - - ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count, - (NUM_DHT) * (MAX_FRIEND_CLIENTS)); - - for (i = 0; i < NUM_DHT; ++i) { - Networking_Core *n = dhts[i]->net; - kill_dht(dhts[i]); - kill_networking(n); - mono_time_free(mono_times[i]); - logger_kill(logs[i]); - } -} - - -static void test_list(void) -{ - uint8_t i; - - for (i = 0; i < 10; ++i) { - test_list_main(); - } -} - -static void ip_callback(void *data, int32_t number, const IP_Port *ip_port) -{ -} - -#define NUM_DHT_FRIENDS 20 - -static uint64_t get_clock_callback(Mono_Time *mono_time, void *user_data) -{ - const uint64_t *clock = (const uint64_t *)user_data; - return *clock; -} - -static void test_DHT_test(void) -{ - uint32_t to_comp = 8394782; - DHT *dhts[NUM_DHT]; - Logger *logs[NUM_DHT]; - Mono_Time *mono_times[NUM_DHT]; - uint64_t clock[NUM_DHT]; - uint32_t index[NUM_DHT]; - - uint32_t i, j; - - for (i = 0; i < NUM_DHT; ++i) { - IP ip; - ip_init(&ip, 1); - - logs[i] = logger_new(); - index[i] = i + 1; - logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); - - mono_times[i] = mono_time_new(); - clock[i] = current_time_monotonic(mono_times[i]); - mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]); - - dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true); - ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); - ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port"); - } - - struct { - uint16_t tox1; - uint16_t tox2; - } pairs[NUM_DHT_FRIENDS]; - - for (i = 0; i < NUM_DHT_FRIENDS; ++i) { - // TODO(hugbubby): remove use of goto. -loop_top: - pairs[i].tox1 = random_u32() % NUM_DHT; - pairs[i].tox2 = (pairs[i].tox1 + (random_u32() % (NUM_DHT - 1)) + 1) % NUM_DHT; - - for (j = 0; j < i; ++j) { - if (pairs[j].tox2 == pairs[i].tox2 && pairs[j].tox1 == pairs[i].tox1) { - goto loop_top; - } - } - - uint16_t lock_count = 0; - ck_assert_msg(dht_addfriend(dhts[pairs[i].tox2], dhts[pairs[i].tox1]->self_public_key, &ip_callback, &to_comp, 1337, - &lock_count) == 0, "Failed to add friend"); - ck_assert_msg(lock_count == 1, "bad lock count: %u %u", lock_count, i); - } - - for (i = 0; i < NUM_DHT; ++i) { - IP_Port ip_port; - ip_port.ip = get_loopback(); - ip_port.port = net_htons(DHT_DEFAULT_PORT + i); - dht_bootstrap(dhts[(i - 1) % NUM_DHT], &ip_port, dhts[i]->self_public_key); - } - - while (true) { - uint16_t counter = 0; - - for (i = 0; i < NUM_DHT_FRIENDS; ++i) { - IP_Port a; - - if (dht_getfriendip(dhts[pairs[i].tox2], dhts[pairs[i].tox1]->self_public_key, &a) == 1) { - ++counter; - } - } - - if (counter == NUM_DHT_FRIENDS) { - break; - } - - for (i = 0; i < NUM_DHT; ++i) { - mono_time_update(mono_times[i]); - networking_poll(dhts[i]->net, nullptr); - do_dht(dhts[i]); - clock[i] += 500; - } - - c_sleep(20); - } - - for (i = 0; i < NUM_DHT; ++i) { - Networking_Core *n = dhts[i]->net; - kill_dht(dhts[i]); - kill_networking(n); - mono_time_free(mono_times[i]); - logger_kill(logs[i]); - } -} - -static void test_dht_create_packet(void) -{ - uint8_t plain[100] = {0}; - uint8_t pkt[1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE]; - - uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE]; - new_symmetric_key(key); - - uint16_t length = dht_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt); - - ck_assert_msg(pkt[0] == NET_PACKET_GET_NODES, "Malformed packet."); - ck_assert_msg(memcmp(pkt + 1, key, CRYPTO_SYMMETRIC_KEY_SIZE) == 0, "Malformed packet."); - ck_assert_msg(length == 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE, - "Invalid size. Should be %u got %d", (unsigned)sizeof(pkt), length); - - printf("Create Packet Successful!\n"); -} - -#define MAX_COUNT 3 - -static void dht_pack_unpack(const Logger *logger, const Node_format *nodes, size_t size, uint8_t *data, size_t length) -{ - int16_t packed_size = pack_nodes(logger, data, length, nodes, size); - ck_assert_msg(packed_size != -1, "Wrong pack_nodes result"); - - uint16_t processed = 0; - VLA(Node_format, nodes_unpacked, size); - const uint8_t tcp_enabled = 1; - - uint16_t unpacked_count = unpack_nodes(nodes_unpacked, size, &processed, data, length, tcp_enabled); - ck_assert_msg(unpacked_count == size, "Wrong unpack_nodes result"); - ck_assert_msg(processed == packed_size, "unpack_nodes did not process all data"); - - for (size_t i = 0; i < size; i++) { - const IP_Port *ipp1 = &nodes[i].ip_port; - const IP_Port *ipp2 = &nodes_unpacked[i].ip_port; - ck_assert_msg(ip_equal(&ipp1->ip, &ipp2->ip), "Unsuccessful ip unpack"); - ck_assert_msg(ipp1->port == ipp2->port, "Unsuccessful port unpack"); - - const uint8_t *pk1 = nodes[i].public_key; - const uint8_t *pk2 = nodes_unpacked[i].public_key; - ck_assert_msg(!memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE), "Unsuccessful pk unpack"); - } -} - -static void random_ip(IP_Port *ipp, int family) -{ - uint8_t *ip = nullptr; - size_t size; - - if (family == TOX_AF_INET || family == TCP_INET) { - ip = (uint8_t *)&ipp->ip.ip.v4; - size = sizeof(ipp->ip.ip.v4); - } else if (family == TOX_AF_INET6 || family == TCP_INET6) { - ip = (uint8_t *)&ipp->ip.ip.v6; - size = sizeof(ipp->ip.ip.v6); - } else { - return; - } - - uint8_t *port = (uint8_t *)&ipp->port; - random_bytes(port, sizeof(ipp->port)); - random_bytes(ip, size); - // TODO(iphydf): Pass the net_family variant to random_ip. - ipp->ip.family.value = family; -} - -#define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE) - -static void test_dht_node_packing(void) -{ - Logger *log = logger_new(); - uint32_t index = 1; - logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index); - - const uint16_t length = MAX_COUNT * PACKED_NODES_SIZE; - uint8_t *data = (uint8_t *)malloc(length); - - Node_format nodes[MAX_COUNT]; - const size_t pk_size = sizeof(nodes[0].public_key); - - random_bytes(nodes[0].public_key, pk_size); - random_bytes(nodes[1].public_key, pk_size); - random_bytes(nodes[2].public_key, pk_size); - - random_ip(&nodes[0].ip_port, TOX_AF_INET); - random_ip(&nodes[1].ip_port, TOX_AF_INET); - random_ip(&nodes[2].ip_port, TOX_AF_INET); - dht_pack_unpack(log, nodes, 3, data, length); - - random_ip(&nodes[0].ip_port, TOX_AF_INET); - random_ip(&nodes[1].ip_port, TOX_AF_INET); - random_ip(&nodes[2].ip_port, TCP_INET); - dht_pack_unpack(log, nodes, 3, data, length); - - random_ip(&nodes[0].ip_port, TOX_AF_INET); - random_ip(&nodes[1].ip_port, TOX_AF_INET6); - random_ip(&nodes[2].ip_port, TCP_INET6); - dht_pack_unpack(log, nodes, 3, data, length); - - random_ip(&nodes[0].ip_port, TCP_INET); - random_ip(&nodes[1].ip_port, TCP_INET6); - random_ip(&nodes[2].ip_port, TCP_INET); - dht_pack_unpack(log, nodes, 3, data, length); - - random_ip(&nodes[0].ip_port, TOX_AF_INET6); - random_ip(&nodes[1].ip_port, TOX_AF_INET6); - random_ip(&nodes[2].ip_port, TOX_AF_INET6); - dht_pack_unpack(log, nodes, 3, data, length); - - logger_kill(log); - free(data); -} - -int main(void) -{ - setvbuf(stdout, nullptr, _IONBF, 0); - - test_dht_create_packet(); - test_dht_node_packing(); - - test_list(); - test_DHT_test(); - - if (enable_broken_tests) { - test_addto_lists_ipv4(); - test_addto_lists_ipv6(); - } - - return 0; -} diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c deleted file mode 100644 index 3feeed90..00000000 --- a/auto_tests/messenger_test.c +++ /dev/null @@ -1,322 +0,0 @@ -/* unit tests for /core/Messenger.c - * Design: - * Just call every non-static function in Messenger.c, checking that - * they return as they should with check calls. "Bad" calls of the type - * function(bad_data, good_length) are _not_ checked for, this type - * of call is the fault of the client code. - * - * Note: - * None of the functions here test things that rely on the network, i.e. - * checking that status changes are received, messages can be sent, etc. - * All of that is done in a separate test, with two local clients running. */ - -#include - -#ifdef VANILLA_NACL -#include // crypto_box_PUBLICKEYBYTES and other defines. -#else -#include -#endif - -#include "auto_test_support.h" -#include "check_compat.h" -#include "../testing/misc_tools.h" -#include "../toxcore/Messenger.h" - -#define REALLY_BIG_NUMBER ((1) << (sizeof(uint16_t) * 7)) - -static bool enable_broken_tests = false; - -static const char *friend_id_str = "e4b3d5030bc99494605aecc33ceec8875640c1d74aa32790e821b17e98771c4a00000000f1db"; -static const char *good_id_str = "d3f14b6d384d8f5f2a66cff637e69f28f539c5de61bc29744785291fa4ef4d64"; -static const char *bad_id_str = "9B569D14ff637e69f2"; - -static uint8_t *friend_id = nullptr; -static uint8_t *good_id = nullptr; -static uint8_t *bad_id = nullptr; - -static int friend_id_num = 0; - -static Messenger *m; - -static void test_m_sendmesage(void) -{ - const char *message = "h-hi :3"; - int good_len = strlen(message); - int bad_len = MAX_CRYPTO_PACKET_SIZE; - - - ck_assert(m_send_message_generic( - m, -1, MESSAGE_NORMAL, (const uint8_t *)message, good_len, nullptr) == -1); - ck_assert(m_send_message_generic( - m, REALLY_BIG_NUMBER, MESSAGE_NORMAL, (const uint8_t *)message, good_len, nullptr) == -1); - ck_assert(m_send_message_generic( - m, 17, MESSAGE_NORMAL, (const uint8_t *)message, good_len, nullptr) == -1); - ck_assert(m_send_message_generic( - m, friend_id_num, MESSAGE_NORMAL, (const uint8_t *)message, bad_len, nullptr) == -2); -} - -static void test_m_get_userstatus_size(void) -{ - int rc = 0; - ck_assert_msg((m_get_statusmessage_size(m, -1) == -1), - "m_get_statusmessage_size did NOT catch an argument of -1"); - ck_assert_msg((m_get_statusmessage_size(m, REALLY_BIG_NUMBER) == -1), - "m_get_statusmessage_size did NOT catch the following argument: %d\n", - REALLY_BIG_NUMBER); - rc = m_get_statusmessage_size(m, friend_id_num); - - /* this WILL error if the original m_addfriend_norequest() failed */ - ck_assert_msg((rc >= 0 && rc <= MAX_STATUSMESSAGE_LENGTH), - "m_get_statusmessage_size is returning out of range values! (%i)\n" - "(this can be caused by the error of m_addfriend_norequest" - " in the beginning of the suite)\n", rc); -} - -static void test_m_set_userstatus(void) -{ - const char *status = "online!"; - uint16_t good_length = strlen(status); - uint16_t bad_length = REALLY_BIG_NUMBER; - - ck_assert_msg((m_set_statusmessage(m, (const uint8_t *)status, bad_length) == -1), - "m_set_userstatus did NOT catch the following length: %d\n", - REALLY_BIG_NUMBER); - - ck_assert_msg((m_set_statusmessage(m, (const uint8_t *)status, good_length) == 0), - "m_set_userstatus did NOT return 0 on the following length: %d\n" - "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH); -} - -static void test_m_get_friend_connectionstatus(void) -{ - ck_assert_msg((m_get_friend_connectionstatus(m, -1) == -1), - "m_get_friend_connectionstatus did NOT catch an argument of -1.\n"); - ck_assert_msg((m_get_friend_connectionstatus(m, REALLY_BIG_NUMBER) == -1), - "m_get_friend_connectionstatus did NOT catch an argument of %d.\n", - REALLY_BIG_NUMBER); -} - -static void test_m_friend_exists(void) -{ - ck_assert_msg((m_friend_exists(m, -1) == 0), - "m_friend_exists did NOT catch an argument of -1.\n"); - ck_assert_msg((m_friend_exists(m, REALLY_BIG_NUMBER) == 0), - "m_friend_exists did NOT catch an argument of %d.\n", - REALLY_BIG_NUMBER); -} - -static void test_m_delfriend(void) -{ - ck_assert_msg((m_delfriend(m, -1) == -1), - "m_delfriend did NOT catch an argument of -1\n"); - ck_assert_msg((m_delfriend(m, REALLY_BIG_NUMBER) == -1), - "m_delfriend did NOT catch the following number: %d\n", - REALLY_BIG_NUMBER); -} - -static void test_m_addfriend(void) -{ - const char *good_data = "test"; - const char *bad_data = ""; - - int good_len = strlen(good_data); - int bad_len = strlen(bad_data); - int really_bad_len = (MAX_CRYPTO_PACKET_SIZE - crypto_box_PUBLICKEYBYTES - - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES - + crypto_box_ZEROBYTES + 100); - - /* TODO(irungentoo): Update this properly to latest master */ - if (m_addfriend(m, friend_id, (const uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG) { - ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len); - } - - /* this will return an error if the original m_addfriend_norequest() failed */ - if (m_addfriend(m, friend_id, (const uint8_t *)good_data, good_len) != FAERR_ALREADYSENT) { - ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n" - "(this can be caused by the error of m_addfriend_norequest in" - " the beginning of the suite)\n"); - } - - if (m_addfriend(m, good_id, (const uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE) { - ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); - } - - /* this should REALLY return an error */ - /* TODO(irungentoo): validate client_id in m_addfriend? */ - if (m_addfriend(m, bad_id, (const uint8_t *)good_data, good_len) >= 0) { - ck_abort_msg("The following ID passed through " - "m_addfriend without an error:\n'%s'\n", bad_id_str); - } -} - -static void test_setname(void) -{ - const char *good_name = "consensualCorn"; - int good_length = strlen(good_name); - int bad_length = REALLY_BIG_NUMBER; - - ck_assert_msg((setname(m, (const uint8_t *)good_name, bad_length) == -1), - "setname() did NOT error on %d as a length argument!\n", bad_length); - - ck_assert_msg((setname(m, (const uint8_t *)good_name, good_length) == 0), - "setname() did NOT return 0 on good arguments!\n"); -} - -static void test_getself_name(void) -{ - const char *nickname = "testGallop"; - size_t len = strlen(nickname); - char *nick_check = (char *)calloc(len + 1, 1); - ck_assert(nick_check != nullptr); - - setname(m, (const uint8_t *)nickname, len); - getself_name(m, (uint8_t *)nick_check); - - ck_assert_msg((memcmp(nickname, nick_check, len) == 0), - "getself_name failed to return the known name!\n" - "known name: %s\nreturned: %s\n", nickname, nick_check); - free(nick_check); -} - -/* this test is excluded for now, due to lack of a way - * to set a friend's status for now. - * ideas: - * if we have access to the friends list, we could - * just add a status manually ourselves. */ -#if 0 -static void test_m_copy_userstatus(void) -{ - assert(m_copy_userstatus(-1, buf, MAX_USERSTATUS_LENGTH) == -1); - assert(m_copy_userstatus(REALLY_BIG_NUMBER, buf, MAX_USERSTATUS_LENGTH) == -1); - m_copy_userstatus(friend_id_num, buf, MAX_USERSTATUS_LENGTH + 6); - - assert(strcmp(name_buf, friend_id_status) == 0); -} -#endif - -static void test_getname(void) -{ - uint8_t name_buf[MAX_NAME_LENGTH]; - uint8_t test_name[] = {'f', 'o', 'o'}; - - ck_assert(getname(m, -1, name_buf) == -1); - ck_assert(getname(m, REALLY_BIG_NUMBER, name_buf) == -1); - - memcpy(m->friendlist[0].name, &test_name[0], 3); - m->friendlist[0].name_length = 4; - ck_assert(getname(m, 0, &name_buf[0]) == 4); - - ck_assert(strcmp((char *)&name_buf[0], "foo") == 0); -} - -static void test_dht_state_saveloadsave(void) -{ - /* validate that: - * a) saving stays within the confined space - * b) a save()d state can be load()ed back successfully - * c) a second save() is of equal size - * d) the second save() is of equal content */ - const size_t extra = 64; - const size_t size = dht_size(m->dht); - VLA(uint8_t, buffer, size + 2 * extra); - memset(buffer, 0xCD, extra); - memset(buffer + extra + size, 0xCD, extra); - dht_save(m->dht, buffer + extra); - - for (size_t i = 0; i < extra; i++) { - ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from dht_save() @%u", (unsigned)i); - ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from dht_save() @%u", (unsigned)i); - } - - const int res = dht_load(m->dht, buffer + extra, size); - - if (res == -1) { - ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); - } else { - const size_t offset = res >> 4; - const uint8_t *ptr = buffer + extra + offset; - ck_assert_msg(res == 0, "Failed to load back stored buffer: 0x%02x%02x%02x%02x%02x%02x%02x%02x @%u/%u, code %d", - ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], - (unsigned)offset, (unsigned)size, res & 0x0F); - } - - const size_t size2 = dht_size(m->dht); - ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", (unsigned)size, - (unsigned)size2); - - VLA(uint8_t, buffer2, size2); - dht_save(m->dht, buffer2); - - ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "DHT state changed by store/load/store cycle"); -} - -static void messenger_suite(void) -{ - test_dht_state_saveloadsave(); - - test_getself_name(); - test_m_get_userstatus_size(); - test_m_set_userstatus(); - - if (enable_broken_tests) { - test_m_addfriend(); - } - - test_m_friend_exists(); - test_m_get_friend_connectionstatus(); - test_m_delfriend(); - - test_setname(); - test_getname(); - test_m_sendmesage(); -} - -int main(void) -{ - setvbuf(stdout, nullptr, _IONBF, 0); - - friend_id = hex_string_to_bin(friend_id_str); - good_id = hex_string_to_bin(good_id_str); - bad_id = hex_string_to_bin(bad_id_str); - - ck_assert(friend_id != nullptr); - ck_assert(good_id != nullptr); - ck_assert(bad_id != nullptr); - - Mono_Time *mono_time = mono_time_new(); - - /* IPv6 status from global define */ - Messenger_Options options = {0}; - options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; - options.port_range[0] = 41234; - options.port_range[1] = 44234; - options.log_callback = (logger_cb *)print_debug_log; - Messenger_Error err; - m = new_messenger(mono_time, &options, &err); - ck_assert(err == MESSENGER_ERROR_NONE); - - /* setup a default friend and friendnum */ - if (m_addfriend_norequest(m, friend_id) < 0) { - fputs("m_addfriend_norequest() failed on a valid ID!\n" - "this was CRITICAL to the test, and the build WILL fail.\n" - "the tests will continue now...\n\n", stderr); - } - - if ((friend_id_num = getfriend_id(m, friend_id)) < 0) { - fputs("getfriend_id() failed on a valid ID!\n" - "this was CRITICAL to the test, and the build WILL fail.\n" - "the tests will continue now...\n\n", stderr); - } - - messenger_suite(); - - free(friend_id); - free(good_id); - free(bad_id); - - kill_messenger(m); - mono_time_free(mono_time); - - return 0; -} diff --git a/other/analysis/gen-file.sh b/other/analysis/gen-file.sh index 0e433212..504b4e37 100644 --- a/other/analysis/gen-file.sh +++ b/other/analysis/gen-file.sh @@ -54,7 +54,6 @@ FIND_QUERY="$FIND_QUERY -and -not -wholename './super_donators/*'" FIND_QUERY="$FIND_QUERY -and -not -name amalgamation.cc" FIND_QUERY="$FIND_QUERY -and -not -name av_test.c" FIND_QUERY="$FIND_QUERY -and -not -name cracker.c" -FIND_QUERY="$FIND_QUERY -and -not -name dht_test.c" FIND_QUERY="$FIND_QUERY -and -not -name version_test.c" FIND_QUERY="$FIND_QUERY -and -not -wholename './testing/fuzzing/*'" diff --git a/other/emscripten/Dockerfile b/other/emscripten/Dockerfile index 7b84d064..8ba2d14e 100644 --- a/other/emscripten/Dockerfile +++ b/other/emscripten/Dockerfile @@ -23,8 +23,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ WORKDIR /work/emsdk RUN git clone --depth=1 https://github.com/emscripten-core/emsdk /work/emsdk \ - && ./emsdk install latest \ - && ./emsdk activate latest + && ./emsdk install 3.1.3 \ + && ./emsdk activate 3.1.3 # Build libsodium. RUN . "/work/emsdk/emsdk_env.sh" \ diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index ff76d50f..846db642 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -245,30 +245,6 @@ cc_test( ], ) -cc_library( - name = "DHT_srcs", - hdrs = [ - "DHT.c", - "DHT.h", - "LAN_discovery.c", - "LAN_discovery.h", - "ping.c", - "ping.h", - ], - visibility = [ - "//c-toxcore/auto_tests:__pkg__", - "//c-toxcore/other:__pkg__", - "//c-toxcore/other/bootstrap_daemon:__pkg__", - ], - deps = [ - ":logger", - ":mono_time", - ":network", - ":ping_array", - ":state", - ], -) - cc_library( name = "onion", srcs = ["onion.c"],