diff --git a/CMakeLists.txt b/CMakeLists.txt index fba7fd32..35da4d82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,8 @@ set(toxcore_SOURCES toxcore/friend_requests.h toxcore/group.c toxcore/group.h + toxcore/group_announce.c + toxcore/group_announce.h toxcore/group_moderation.c toxcore/group_moderation.h toxcore/LAN_discovery.c @@ -414,6 +416,7 @@ unit_test(toxav rtp) unit_test(toxcore DHT) unit_test(toxcore bin_pack) unit_test(toxcore crypto_core) +unit_test(toxcore group_announce) unit_test(toxcore group_moderation) unit_test(toxcore mono_time) unit_test(toxcore ping_array) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index cda3bbd3..ac5852d8 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -95ae45707c9a19ea9c8c0a537c5defb228f8d7eca1c51c0225a3bc07a50891c6 /usr/local/bin/tox-bootstrapd +39a74721e1730757bdb96020acebe25bafb450a4298e4bc9556e3ad2d59260b1 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index c852eb05..0656707d 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -486,6 +486,46 @@ cc_library( ], ) +cc_library( + name = "group_announce", + srcs = ["group_announce.c"], + hdrs = ["group_announce.h"], + visibility = [ + "//c-toxcore/auto_tests:__pkg__", + "//c-toxcore/other:__pkg__", + "//c-toxcore/other/bootstrap_daemon:__pkg__", + ], + deps = [ + ":DHT", + ":LAN_discovery", + ":ccompat", + ":mono_time", + ":util", + ], +) + +cc_test( + name = "group_announce_test", + size = "small", + srcs = ["group_announce_test.cc"], + deps = [ + ":group_announce", + ":mono_time", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + ], +) + +cc_fuzz_test( + name = "group_announce_fuzz_test", + srcs = ["group_announce_fuzz_test.cc"], + #corpus = ["//tools/toktok-fuzzer/corpus:group_announce_fuzz_test"], + deps = [ + ":group_announce", + "//c-toxcore/testing/fuzzing:fuzz_support", + ], +) + cc_library( name = "onion_client", srcs = ["onion_client.c"], diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c new file mode 100644 index 00000000..7806f5c3 --- /dev/null +++ b/toxcore/group_announce.c @@ -0,0 +1,461 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2020 The TokTok team. + * Copyright © 2015 Tox project. + */ + +#include "group_announce.h" + +#include +#include + +#include "LAN_discovery.h" +#include "ccompat.h" +#include "mono_time.h" +#include "util.h" + +/** + * Removes `announces` from `gc_announces_list`. + */ +non_null() +static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces *announces) +{ + if (announces == nullptr || gc_announces_list == nullptr) { + return; + } + + if (announces->prev_announce != nullptr) { + announces->prev_announce->next_announce = announces->next_announce; + } else { + gc_announces_list->root_announces = announces->next_announce; + } + + if (announces->next_announce != nullptr) { + announces->next_announce->prev_announce = announces->prev_announce; + } + + free(announces); +} + +/** + * Returns the announce designated by `chat_id`. + * Returns null if no announce is found. + */ +non_null() +static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +{ + GC_Announces *announces = gc_announces_list->root_announces; + + while (announces != nullptr) { + if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { + return announces; + } + + announces = announces->next_announce; + } + + return nullptr; +} + +int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes, + const uint8_t *chat_id, const uint8_t *except_public_key) +{ + if (gc_announces == nullptr || gc_announces_list == nullptr || chat_id == nullptr || max_nodes == 0 + || except_public_key == nullptr) { + return -1; + } + + const GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, chat_id); + + if (announces == nullptr) { + return 0; + } + + uint16_t added_count = 0; + + for (size_t i = 0; i < announces->index && i < GCA_MAX_SAVED_ANNOUNCES_PER_GC && added_count < max_nodes; ++i) { + const size_t index = i % GCA_MAX_SAVED_ANNOUNCES_PER_GC; + + if (memcmp(except_public_key, &announces->peer_announces[index].base_announce.peer_public_key, + ENC_PUBLIC_KEY_SIZE) == 0) { + continue; + } + + bool already_added = false; + + for (size_t j = 0; j < added_count; ++j) { + if (memcmp(&gc_announces[j].peer_public_key, &announces->peer_announces[index].base_announce.peer_public_key, + ENC_PUBLIC_KEY_SIZE) == 0) { + already_added = true; + break; + } + } + + if (!already_added) { + gc_announces[added_count] = announces->peer_announces[index].base_announce; + ++added_count; + } + } + + return added_count; +} + +uint16_t gca_pack_announces_list_size(uint16_t count) +{ + return count * GCA_ANNOUNCE_MAX_SIZE; +} + +int gca_pack_announce(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announce) +{ + if (length < GCA_ANNOUNCE_MAX_SIZE) { + LOGGER_ERROR(log, "Invalid announce length: %u", length); + return -1; + } + + if (data == nullptr) { + LOGGER_ERROR(log, "data is null"); + return -1; + } + + if (announce == nullptr) { + LOGGER_ERROR(log, "announce is null"); + return -1; + } + + uint16_t offset = 0; + memcpy(data + offset, announce->peer_public_key, ENC_PUBLIC_KEY_SIZE); + offset += ENC_PUBLIC_KEY_SIZE; + + data[offset] = announce->ip_port_is_set ? 1 : 0; + ++offset; + + data[offset] = announce->tcp_relays_count; + ++offset; + + if (!announce->ip_port_is_set && announce->tcp_relays_count == 0) { + LOGGER_ERROR(log, "Failed to pack announce: no valid ip_port or tcp relay"); + return -1; + } + + if (announce->ip_port_is_set) { + const int ip_port_length = pack_ip_port(log, data + offset, length - offset, &announce->ip_port); + + if (ip_port_length == -1) { + LOGGER_ERROR(log, "Failed to pack ip_port"); + return -1; + } + + offset += ip_port_length; + } + + const int nodes_length = pack_nodes(log, data + offset, length - offset, announce->tcp_relays, + announce->tcp_relays_count); + + if (nodes_length == -1) { + LOGGER_ERROR(log, "Failed to pack TCP nodes"); + return -1; + } + + return nodes_length + offset; +} + +/** + * Unpacks `announce` into `data` buffer of size `length`. + * + * Returns the size of the unpacked data on success. + * Returns -1 on failure. + */ +non_null() +static int gca_unpack_announce(const Logger *log, const uint8_t *data, uint16_t length, GC_Announce *announce) +{ + if (length < ENC_PUBLIC_KEY_SIZE + 2) { + LOGGER_ERROR(log, "Invalid announce length: %u", length); + return -1; + } + + if (data == nullptr) { + LOGGER_ERROR(log, "data is null"); + return -1; + } + + if (announce == nullptr) { + LOGGER_ERROR(log, "announce is null"); + return -1; + } + + uint16_t offset = 0; + memcpy(announce->peer_public_key, data + offset, ENC_PUBLIC_KEY_SIZE); + offset += ENC_PUBLIC_KEY_SIZE; + + announce->ip_port_is_set = data[offset] == 1; + ++offset; + + announce->tcp_relays_count = data[offset]; + ++offset; + + if (announce->tcp_relays_count > GCA_MAX_ANNOUNCED_TCP_RELAYS) { + return -1; + } + + if (announce->ip_port_is_set) { + if (length - offset == 0) { + return -1; + } + + const int ip_port_length = unpack_ip_port(&announce->ip_port, data + offset, length - offset, false); + + if (ip_port_length == -1) { + LOGGER_ERROR(log, "Failed to unpack ip_port"); + return -1; + } + + offset += ip_port_length; + } + + uint16_t nodes_length; + const int nodes_count = unpack_nodes(announce->tcp_relays, announce->tcp_relays_count, &nodes_length, + data + offset, length - offset, true); + + if (nodes_count != announce->tcp_relays_count) { + LOGGER_ERROR(log, "Failed to unpack TCP nodes"); + return -1; + } + + return offset + nodes_length; +} + +int gca_pack_public_announce(const Logger *log, uint8_t *data, uint16_t length, + const GC_Public_Announce *public_announce) +{ + if (public_announce == nullptr || data == nullptr || length < CHAT_ID_SIZE) { + return -1; + } + + memcpy(data, public_announce->chat_public_key, CHAT_ID_SIZE); + + const int packed_size = gca_pack_announce(log, data + CHAT_ID_SIZE, length - CHAT_ID_SIZE, + &public_announce->base_announce); + + if (packed_size < 0) { + LOGGER_ERROR(log, "Failed to pack public group announce"); + return -1; + } + + return packed_size + CHAT_ID_SIZE; +} + +int gca_unpack_public_announce(const Logger *log, const uint8_t *data, uint16_t length, + GC_Public_Announce *public_announce) +{ + if (length < CHAT_ID_SIZE) { + LOGGER_ERROR(log, "invalid public announce length: %u", length); + } + + if (data == nullptr) { + LOGGER_ERROR(log, "data is null"); + return -1; + } + + if (public_announce == nullptr) { + LOGGER_ERROR(log, "public_announce is null"); + return -1; + } + + memcpy(public_announce->chat_public_key, data, CHAT_ID_SIZE); + + const int base_announce_size = gca_unpack_announce(log, data + ENC_PUBLIC_KEY_SIZE, length - ENC_PUBLIC_KEY_SIZE, + &public_announce->base_announce); + + if (base_announce_size == -1) { + LOGGER_ERROR(log, "Failed to unpack group announce"); + return -1; + } + + return base_announce_size + CHAT_ID_SIZE; +} + +int gca_pack_announces_list(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announces, + uint8_t announces_count, size_t *processed) +{ + if (data == nullptr) { + LOGGER_ERROR(log, "data is null"); + return -1; + } + + if (announces == nullptr) { + LOGGER_ERROR(log, "announces is null"); + return -1; + } + + uint16_t offset = 0; + + for (size_t i = 0; i < announces_count; ++i) { + const int packed_length = gca_pack_announce(log, data + offset, length - offset, &announces[i]); + + if (packed_length < 0) { + LOGGER_ERROR(log, "Failed to pack group announce"); + return -1; + } + + offset += packed_length; + } + + if (processed != nullptr) { + *processed = offset; + } + + return announces_count; +} + +int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t length, GC_Announce *announces, + uint8_t max_count) +{ + if (data == nullptr) { + LOGGER_ERROR(log, "data is null"); + return -1; + } + + if (announces == nullptr) { + LOGGER_ERROR(log, "announces is null"); + return -1; + } + + uint16_t offset = 0; + int announces_count = 0; + + for (size_t i = 0; i < max_count && length > offset; ++i) { + const int unpacked_length = gca_unpack_announce(log, data + offset, length - offset, &announces[i]); + + if (unpacked_length == -1) { + LOGGER_WARNING(log, "Failed to unpack group announce: %d %d", length, offset); + return -1; + } + + offset += unpacked_length; + ++announces_count; + } + + return announces_count; +} + +GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, + const GC_Public_Announce *public_announce) +{ + if (gc_announces_list == nullptr || public_announce == nullptr) { + return nullptr; + } + + GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key); + + // No entry for this chat_id exists so we create one + if (announces == nullptr) { + announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + + if (announces == nullptr) { + return nullptr; + } + + announces->index = 0; + announces->prev_announce = nullptr; + + if (gc_announces_list->root_announces != nullptr) { + gc_announces_list->root_announces->prev_announce = announces; + } + + announces->next_announce = gc_announces_list->root_announces; + gc_announces_list->root_announces = announces; + memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); + } + + const uint64_t cur_time = mono_time_get(mono_time); + + announces->last_announce_received_timestamp = cur_time; + + const uint64_t index = announces->index % GCA_MAX_SAVED_ANNOUNCES_PER_GC; + + GC_Peer_Announce *gc_peer_announce = &announces->peer_announces[index]; + + gc_peer_announce->base_announce = public_announce->base_announce; + + gc_peer_announce->timestamp = cur_time; + + ++announces->index; + + return gc_peer_announce; +} + +bool gca_is_valid_announce(const GC_Announce *announce) +{ + if (announce == nullptr) { + return false; + } + + return announce->tcp_relays_count > 0 || announce->ip_port_is_set; +} + +GC_Announces_List *new_gca_list(void) +{ + GC_Announces_List *announces_list = (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); + return announces_list; +} + +void kill_gca(GC_Announces_List *announces_list) +{ + if (announces_list == nullptr) { + return; + } + + GC_Announces *root = announces_list->root_announces; + + while (root != nullptr) { + GC_Announces *next = root->next_announce; + free(root); + root = next; + } + + free(announces_list); +} + +/* How long we save a peer's announce before we consider it stale and remove it. */ +#define GCA_ANNOUNCE_SAVE_TIMEOUT 30 + +/* How often we run do_gca() */ +#define GCA_DO_GCA_TIMEOUT 1 + +void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list) +{ + if (gc_announces_list == nullptr) { + return; + } + + if (!mono_time_is_timeout(mono_time, gc_announces_list->last_timeout_check, GCA_DO_GCA_TIMEOUT)) { + return; + } + + gc_announces_list->last_timeout_check = mono_time_get(mono_time); + + GC_Announces *announces = gc_announces_list->root_announces; + + while (announces != nullptr) { + if (mono_time_is_timeout(mono_time, announces->last_announce_received_timestamp, GCA_ANNOUNCE_SAVE_TIMEOUT)) { + GC_Announces *to_delete = announces; + announces = announces->next_announce; + remove_announces(gc_announces_list, to_delete); + continue; + } + + announces = announces->next_announce; + } +} + +void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +{ + if (gc_announces_list == nullptr || chat_id == nullptr) { + return; + } + + GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, chat_id); + + if (announces != nullptr) { + remove_announces(gc_announces_list, announces); + } +} diff --git a/toxcore/group_announce.h b/toxcore/group_announce.h new file mode 100644 index 00000000..11bba7d5 --- /dev/null +++ b/toxcore/group_announce.h @@ -0,0 +1,218 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2020 The TokTok team. + * Copyright © 2015 Tox project. + */ + +/** + * Similar to ping.h, but designed for group chat purposes + */ +#ifndef GROUP_ANNOUNCE_H +#define GROUP_ANNOUNCE_H + +#include + +#include "DHT.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* The maximum number of announces to save for a particular group chat. */ +#define GCA_MAX_SAVED_ANNOUNCES_PER_GC 16 + +/* Maximum number of TCP relays that can be in an annoucne. */ +#define GCA_MAX_ANNOUNCED_TCP_RELAYS 1 + +/* Maximum number of announces we can send in an announce response. */ +#define GCA_MAX_SENT_ANNOUNCES 4 + +/* Maximum size of an announce. */ +#define GCA_ANNOUNCE_MAX_SIZE (ENC_PUBLIC_KEY_SIZE + 1 + 1 + (PACKED_NODE_SIZE_IP6 * 2)) + +/* Maximum size of a public announce. */ +#define GCA_PUBLIC_ANNOUNCE_MAX_SIZE (ENC_PUBLIC_KEY_SIZE + GCA_ANNOUNCE_MAX_SIZE) + +typedef struct GC_Announce GC_Announce; +typedef struct GC_Peer_Announce GC_Peer_Announce; +typedef struct GC_Announces GC_Announces; +typedef struct GC_Announces_List GC_Announces_List; +typedef struct GC_Public_Announce GC_Public_Announce; + +/* Base announce. */ +struct GC_Announce { + Node_format tcp_relays[GCA_MAX_ANNOUNCED_TCP_RELAYS]; + uint8_t tcp_relays_count; + bool ip_port_is_set; + IP_Port ip_port; + uint8_t peer_public_key[ENC_PUBLIC_KEY_SIZE]; +}; + +/* Peer announce for specific group. */ +struct GC_Peer_Announce { + GC_Announce base_announce; + uint64_t timestamp; +}; + +/* Used for announces in public groups. */ +struct GC_Public_Announce { + GC_Announce base_announce; + uint8_t chat_public_key[ENC_PUBLIC_KEY_SIZE]; +}; + +/* A linked list that holds all announces for a particular group. */ +struct GC_Announces { + uint8_t chat_id[CHAT_ID_SIZE]; + uint64_t index; + uint64_t last_announce_received_timestamp; + + GC_Peer_Announce peer_announces[GCA_MAX_SAVED_ANNOUNCES_PER_GC]; + + GC_Announces *next_announce; + GC_Announces *prev_announce; +}; + +/* A list of all announces. */ +struct GC_Announces_List { + GC_Announces *root_announces; + uint64_t last_timeout_check; +}; + + +/** @brief Returns a new group announces list. + * + * The caller is responsible for freeing the memory with `kill_gca`. + */ +GC_Announces_List *new_gca_list(void); + +/** @brief Frees all dynamically allocated memory associated with `announces_list`. */ +nullable(1) +void kill_gca(GC_Announces_List *announces_list); + +/** @brief Iterates through the announces list and removes announces that are considered stale. + * + * @param gc_announces_list The list of announces to iterate. + * + * This function should be called from the main loop, and will iterate the list a + * maxmimum of once per second. + */ +non_null() +void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list); + +/** @brief Frees all dynamically allocated memory associated with an announces list entry. + * + * @param gc_announces_list The announces list we want to search through. + * @param chat_id The chat ID that designates the entry we want to remove. + */ +non_null() +void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id); + +/** @brief Puts a set of announces from the announces list in supplied list. + * + * @param gc_announces_list The announces list we want to search for entries in. + * @param gc_announces An empty announces list that will be filled with matches. + * @param max_nodes The maximum number of matches that we want to add to the list. + * @param chat_id The chat ID associated with the announces that we want to add. + * @param except_public_key The public key associated with announces that we want to ignore. + * + * @return the number of added nodes on success. + * @retval -1 on failure. + */ +non_null() +int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes, + const uint8_t *chat_id, const uint8_t *except_public_key); + +/** @brief Adds a public_announce to list of announces. + * + * @param gc_announces_list The announces list that we want to add an entry to. + * @param public_announce The public announce that we want to add. + * + * @return the peer announce on success. + * @retval null on failure. + */ +non_null() +GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, + const GC_Public_Announce *public_announce); + +/** @brief Packs an announce into a data buffer. + * + * @param data The data buffer being packed. + * @param length The size in bytes of the data buffer. Must be at least GCA_ANNOUNCE_MAX_SIZE. + * @param announce The announce being packed into the data buffer. + * + * @return the size of the packed data on success. + * @retval -1 on failure. + */ +non_null() +int gca_pack_announce(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announce); + +/** @brief Returns the number of bytes needed for a buff in which to pack `count` announces. */ +uint16_t gca_pack_announces_list_size(uint16_t count); + +/** @brief Packs a list of announces into a data buffer. + * + * @param data The data buffer being packed. + * @param length The size in bytes of the data buffer. Use gca_pack_announces_list_size to get the + * required length. + * @param announces The announces to be packed into the data buffer. + * @param announces_count The number of announces in the announces list. + * @param processed If non-null, will contain the number of bytes packed (only on success). + * + * @return the number of packed announces on success. + * @retval -1 on failure. + */ +non_null(1, 2, 4) nullable(6) +int gca_pack_announces_list(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announces, + uint8_t announces_count, size_t *processed); + +/** @brief Unpacks packed announces from a data buffer into a supplied list. + * + * @param data The data buffer to unpack from. + * @param length The size of the data buffer. + * @param announces The announces list that the data buffer will be unpacked to. + * @param max_count The maximum number of announces to unpack. + * + * @return the number of unpacked announces on success. + * @retval -1 on failure. + */ +non_null() +int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t length, GC_Announce *announces, + uint8_t max_count); + +/** @brief Packs a public announce into a data buffer. + * + * @param data The data buffer being packed. + * @param length The size in bytes of the data buffer. Must be at least GCA_PUBLIC_ANNOUNCE_MAX_SIZE. + * @param public_announce The public announce being packed into the data buffer. + * + * @return the size of the packed data on success. + * @retval -1 on failure. + */ +non_null() +int gca_pack_public_announce(const Logger *log, uint8_t *data, uint16_t length, + const GC_Public_Announce *public_announce); + +/** @brief Unpacks a public announce from a data buffer into a supplied public announce. + * + * @param data The data buffer to unpack from. + * @param length The size of the data buffer. + * @param public_announce The public announce to unpack the data buffer into. + * + * @return the size of the unpacked data on success. + * @retval -1 on failure. + */ +non_null() +int gca_unpack_public_announce(const Logger *log, const uint8_t *data, uint16_t length, + GC_Public_Announce *public_announce); + +/** @brief Returns true if the announce is valid. + * + * An announce is considered valid if there is at least one TCP relay, or the ip_port is set. + */ +non_null() +bool gca_is_valid_announce(const GC_Announce *announce); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // GROUP_ANNOUNCE_H diff --git a/toxcore/group_announce_fuzz_test.cc b/toxcore/group_announce_fuzz_test.cc new file mode 100644 index 00000000..c090176c --- /dev/null +++ b/toxcore/group_announce_fuzz_test.cc @@ -0,0 +1,106 @@ +#include "group_announce.h" + +#include +#include +#include + +#include "../testing/fuzzing/fuzz_support.h" + +namespace { + +void TestUnpackAnnouncesList(Fuzz_Data &input) +{ + CONSUME1_OR_RETURN(const uint8_t max_count, input); + std::vector announces(max_count); + + // TODO(iphydf): How do we know the packed size? + CONSUME1_OR_RETURN(const uint16_t packed_size, input); + + Logger *logger = logger_new(); + if (gca_unpack_announces_list(logger, input.data, input.size, announces.data(), max_count) + != -1) { + std::vector packed(packed_size); + size_t processed; + gca_pack_announces_list( + logger, packed.data(), packed.size(), announces.data(), announces.size(), &processed); + } + logger_kill(logger); +} + +void TestUnpackPublicAnnounce(Fuzz_Data &input) +{ + GC_Public_Announce public_announce; + + // TODO(iphydf): How do we know the packed size? + CONSUME1_OR_RETURN(const uint16_t packed_size, input); + + Logger *logger = logger_new(); + if (gca_unpack_public_announce(logger, input.data, input.size, &public_announce) != -1) { + std::vector packed(packed_size); + gca_pack_public_announce(logger, packed.data(), packed.size(), &public_announce); + } + logger_kill(logger); +} + +void TestDoGca(Fuzz_Data &input) +{ + std::unique_ptr logger(logger_new(), logger_kill); + std::unique_ptr mono_time( + mono_time_new(nullptr, nullptr), mono_time_free); + assert(mono_time != nullptr); + uint64_t clock = 1; + mono_time_set_current_time_callback( + mono_time.get(), [](void *user_data) { return *static_cast(user_data); }, + &clock); + std::unique_ptr gca(new_gca_list(), kill_gca); + assert(gca != nullptr); + + while (input.size > 0) { + CONSUME1_OR_RETURN(const uint8_t choice, input); + switch (choice) { + case 0: { + // Add an announce. + CONSUME1_OR_RETURN(const uint16_t length, input); + CONSUME_OR_RETURN(const uint8_t *data, input, length); + GC_Public_Announce public_announce; + if (gca_unpack_public_announce(logger.get(), data, length, &public_announce) != -1) { + gca_add_announce(mono_time.get(), gca.get(), &public_announce); + } + break; + } + case 1: { + // Advance the time by a number of tox_iteration_intervals. + CONSUME1_OR_RETURN(const uint8_t iterations, input); + clock += iterations * 20; + // Do an iteration. + do_gca(mono_time.get(), gca.get()); + break; + } + case 2: { + // Get announces. + CONSUME1_OR_RETURN(const uint8_t max_nodes, input); + std::vector gc_announces(max_nodes); + CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE); + CONSUME_OR_RETURN(const uint8_t *except_public_key, input, ENC_PUBLIC_KEY_SIZE); + gca_get_announces( + gca.get(), gc_announces.data(), max_nodes, chat_id, except_public_key); + break; + } + case 3: { + // Remove a chat. + CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE); + cleanup_gca(gca.get(), chat_id); + break; + } + } + } +} + +} // namespace + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + fuzz_select_target(data, size, TestUnpackAnnouncesList, TestUnpackPublicAnnounce, TestDoGca); + return 0; +} diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc new file mode 100644 index 00000000..eca1da2b --- /dev/null +++ b/toxcore/group_announce_test.cc @@ -0,0 +1,261 @@ +#include "group_announce.h" + +#include + +#include "mono_time.h" + +namespace { + +struct Announces : ::testing::Test { +protected: + uint64_t clock_ = 0; + Mono_Time *mono_time_ = nullptr; + GC_Announces_List *gca_ = nullptr; + GC_Announce _ann1; + GC_Announce _ann2; + + void SetUp() override + { + mono_time_ = mono_time_new(nullptr, nullptr); + ASSERT_NE(mono_time_, nullptr); + mono_time_set_current_time_callback( + mono_time_, [](void *user_data) { return *static_cast(user_data); }, + &clock_); + gca_ = new_gca_list(); + ASSERT_NE(gca_, nullptr); + } + + ~Announces() override + { + kill_gca(gca_); + mono_time_free(mono_time_); + } + + void advance_clock(uint64_t increment) + { + clock_ += increment; + mono_time_update(mono_time_); + } +}; + +TEST_F(Announces, KillGcaOnNullptrIsNoop) +{ + // All kill functions should be nullable. + kill_gca(nullptr); +} + +TEST_F(Announces, CanBeCreatedAndDeleted) +{ + GC_Public_Announce ann{}; + ann.chat_public_key[0] = 0x88; + ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_EQ(gca_add_announce(mono_time_, gca_, nullptr), nullptr); + ASSERT_EQ(gca_add_announce(mono_time_, nullptr, &ann), nullptr); +} + +TEST_F(Announces, AnnouncesCanTimeOut) +{ + advance_clock(100); + ASSERT_EQ(gca_->root_announces, nullptr); + GC_Public_Announce ann{}; + ann.chat_public_key[0] = 0xae; + ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_NE(gca_->root_announces, nullptr); + ASSERT_EQ(gca_->root_announces->chat_id[0], 0xae); + + // One iteration without having any time passed => announce is still here. + do_gca(mono_time_, gca_); + ASSERT_NE(gca_->root_announces, nullptr); + + // 29 seconds later, still there + advance_clock(29000); + do_gca(mono_time_, gca_); + ASSERT_NE(gca_->root_announces, nullptr); + + // One more second and it's gone. + advance_clock(1000); + do_gca(mono_time_, gca_); + ASSERT_EQ(gca_->root_announces, nullptr); +} + +TEST_F(Announces, AnnouncesGetAndCleanup) +{ + GC_Public_Announce ann1{}; + GC_Public_Announce ann2{}; + ann1.chat_public_key[0] = 0x91; + ann1.base_announce.peer_public_key[0] = 0x7f; + ann2.chat_public_key[0] = 0x92; + ann2.base_announce.peer_public_key[0] = 0x7c; + + ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann1), nullptr); + ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); + ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); + + uint8_t empty_pk[ENC_PUBLIC_KEY_SIZE] = {0}; + + GC_Announce announces; + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann1.chat_public_key, empty_pk), 1); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann2.chat_public_key, empty_pk), 1); + + cleanup_gca(gca_, ann1.chat_public_key); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann1.chat_public_key, empty_pk), 0); + + cleanup_gca(gca_, ann2.chat_public_key); + ASSERT_EQ(gca_get_announces(gca_, &announces, 1, ann2.chat_public_key, empty_pk), 0); + ASSERT_EQ(gca_get_announces(gca_, nullptr, 1, ann2.chat_public_key, empty_pk), -1); +} + +struct AnnouncesPack : ::testing::Test { +protected: + std::vector announces_; + Logger *logger_ = nullptr; + + void SetUp() override + { + logger_ = logger_new(); + ASSERT_NE(logger_, nullptr); + + // Add an announce without TCP relay. + announces_.emplace_back(); + auto &ann1 = announces_.back(); + + ann1.peer_public_key[0] = 0xae; + ann1.ip_port.ip.family = net_family_ipv4(); + ann1.ip_port.ip.ip.v4.uint8[0] = 0x7f; // 127.0.0.1 + ann1.ip_port.ip.ip.v4.uint8[3] = 0x1; + ann1.ip_port_is_set = 1; + + // Add an announce with TCP relay. + announces_.emplace_back(); + auto &ann2 = announces_.back(); + + ann2.peer_public_key[0] = 0xaf; // different key + ann2.ip_port.ip.family = net_family_ipv4(); + ann2.ip_port.ip.ip.v4.uint8[0] = 0x7f; // 127.0.0.2 + ann2.ip_port.ip.ip.v4.uint8[3] = 0x2; + ann2.ip_port_is_set = 1; + ann2.tcp_relays_count = 1; + ann2.tcp_relays[0].ip_port.ip.family = net_family_ipv4(); + ann2.tcp_relays[0].ip_port.ip.ip.v4 = ip4_broadcast; + ann2.tcp_relays[0].public_key[0] = 0xea; + } + + ~AnnouncesPack() override { logger_kill(logger_); } +}; + +TEST_F(AnnouncesPack, PublicAnnounceCanBePackedAndUnpacked) +{ + GC_Public_Announce ann{}; + ann.chat_public_key[0] = 0x88; + ann.base_announce = announces_[0]; + + std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); + const int packed_size = gca_pack_public_announce(logger_, packed.data(), packed.size(), &ann); + + EXPECT_GT(packed_size, 0); + + GC_Public_Announce unpacked_ann{}; + EXPECT_EQ(gca_unpack_public_announce(logger_, packed.data(), packed.size(), &unpacked_ann), + packed_size); +} + +TEST_F(AnnouncesPack, UnpackEmptyPublicAnnounce) +{ + GC_Public_Announce ann{}; + std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); + + EXPECT_EQ(gca_unpack_public_announce(logger_, nullptr, 0, &ann), -1); + EXPECT_EQ(gca_unpack_public_announce(logger_, packed.data(), packed.size(), nullptr), -1); +} + +TEST_F(AnnouncesPack, PackEmptyPublicAnnounce) +{ + GC_Public_Announce ann{}; + std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); + EXPECT_EQ(gca_pack_public_announce(logger_, packed.data(), packed.size(), nullptr), -1); + EXPECT_EQ(gca_pack_public_announce(logger_, nullptr, 0, &ann), -1); +} + +TEST_F(AnnouncesPack, PublicAnnouncePackNull) +{ + GC_Public_Announce ann{}; + std::vector packed(GCA_PUBLIC_ANNOUNCE_MAX_SIZE); + EXPECT_EQ(gca_pack_public_announce(logger_, packed.data(), packed.size(), &ann), -1); + + ann.chat_public_key[0] = 0x88; + ann.base_announce = announces_[0]; + + std::vector packedTooSmall(GCA_PUBLIC_ANNOUNCE_MAX_SIZE - 1); + EXPECT_EQ( + gca_pack_public_announce(logger_, packedTooSmall.data(), packedTooSmall.size(), &ann), -1); + + ann.base_announce.ip_port_is_set = 0; + ann.base_announce.tcp_relays_count = 0; + + EXPECT_EQ(gca_pack_public_announce(logger_, packed.data(), packed.size(), &ann), -1); +} + +TEST_F(AnnouncesPack, AnnouncesValidationCheck) +{ + EXPECT_EQ(gca_is_valid_announce(nullptr), false); + + GC_Announce announce = {0}; + EXPECT_EQ(gca_is_valid_announce(&announce), false); + EXPECT_EQ(gca_is_valid_announce(&announces_[0]), true); + EXPECT_EQ(gca_is_valid_announce(&announces_[1]), true); + announces_[0].ip_port_is_set = 0; + announces_[0].tcp_relays_count = 0; + EXPECT_EQ(gca_is_valid_announce(&announces_[0]), false); +} + +TEST_F(AnnouncesPack, UnpackIncompleteAnnouncesList) +{ + const uint8_t data[] = {0x00, 0x24, 0x3d, 0x00, 0x3d, 0xff, 0xff, 0x5b, 0x04, 0x20, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}; + + GC_Announce announce; + EXPECT_EQ(gca_unpack_announces_list(logger_, data, sizeof(data), &announce, 1), -1); + EXPECT_EQ(gca_unpack_announces_list(logger_, data, sizeof(data), nullptr, 1), -1); + EXPECT_EQ(gca_unpack_announces_list(logger_, nullptr, 0, &announce, 1), -1); +} + +TEST_F(AnnouncesPack, PackedAnnouncesListCanBeUnpacked) +{ + const uint16_t size = gca_pack_announces_list_size(announces_.size()); + std::vector packed(size); + + size_t processed = 0; + + EXPECT_GT(gca_pack_announces_list(logger_, packed.data(), packed.size(), announces_.data(), + announces_.size(), &processed), + 0); + ASSERT_GE(processed, ENC_PUBLIC_KEY_SIZE + 2); + ASSERT_LE(processed, size); + + std::vector announces_unpacked(announces_.size()); + ASSERT_EQ(gca_unpack_announces_list(logger_, packed.data(), packed.size(), + announces_unpacked.data(), announces_unpacked.size()), + announces_unpacked.size()); +} + +TEST_F(AnnouncesPack, PackingEmptyAnnounceFails) +{ + GC_Announce announce{}; // all zeroes + std::vector packed(gca_pack_announces_list_size(1)); + EXPECT_EQ( + gca_pack_announces_list(logger_, packed.data(), packed.size(), &announce, 1, nullptr), -1); + EXPECT_EQ( + gca_pack_announces_list(logger_, packed.data(), packed.size(), nullptr, 1, nullptr), -1); + EXPECT_EQ(gca_pack_announces_list(logger_, nullptr, 0, &announce, 1, nullptr), -1); +} + +TEST_F(AnnouncesPack, PackAnnounceNull) +{ + std::vector data(GCA_ANNOUNCE_MAX_SIZE); + GC_Announce announce; + ASSERT_EQ(gca_pack_announce(logger_, nullptr, 0, &announce), -1); + ASSERT_EQ(gca_pack_announce(logger_, data.data(), data.size(), nullptr), -1); +} + +} // namespace