From 29b2dd6315ffe29cd212c4d5a846479f56b33d52 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 23 Jun 2018 12:32:41 +0000 Subject: Use clang-format for C++ code. `clang-format -style='{BasedOnStyle: Google, ColumnLimit: 100}'` --- CMakeLists.txt | 2 +- auto_tests/helpers.h | 2 + auto_tests/monolith_test.cc | 260 ++++++++++++++++++++++++++++++++++++++ auto_tests/monolith_test.cpp | 293 ------------------------------------------- cmake/CompileGTest.cmake | 2 +- testing/random_testing.cc | 77 ++++++------ toxav/BUILD.bazel | 2 +- toxav/rtp_test.cc | 64 ++++++++++ toxav/rtp_test.cpp | 80 ------------ toxcore/BUILD.bazel | 4 +- toxcore/crypto_core_test.cc | 91 ++++++++++++++ toxcore/crypto_core_test.cpp | 96 -------------- toxcore/util_test.cc | 51 ++++++++ toxcore/util_test.cpp | 55 -------- 14 files changed, 512 insertions(+), 567 deletions(-) create mode 100644 auto_tests/monolith_test.cc delete mode 100644 auto_tests/monolith_test.cpp create mode 100644 toxav/rtp_test.cc delete mode 100644 toxav/rtp_test.cpp create mode 100644 toxcore/crypto_core_test.cc delete mode 100644 toxcore/crypto_core_test.cpp create mode 100644 toxcore/util_test.cc delete mode 100644 toxcore/util_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 613297ed..7147d07b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -491,7 +491,7 @@ endfunction() if(BUILD_TOXAV) add_definitions(-D__STDC_LIMIT_MACROS=1) add_executable(auto_monolith_test ${CPUFEATURES} - auto_tests/monolith_test.cpp) + auto_tests/monolith_test.cc) target_link_libraries(auto_monolith_test ${toxcore_LINK_MODULES}) add_test(NAME monolith COMMAND ${CROSSCOMPILING_EMULATOR} auto_monolith_test) endif() diff --git a/auto_tests/helpers.h b/auto_tests/helpers.h index b953e29a..7af127f5 100644 --- a/auto_tests/helpers.h +++ b/auto_tests/helpers.h @@ -9,6 +9,7 @@ #include #include +#ifndef c_sleep #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) #include #define c_sleep(x) Sleep(x) @@ -16,6 +17,7 @@ #include #define c_sleep(x) usleep(1000 * (x)) #endif +#endif #define ITERATION_INTERVAL 200 diff --git a/auto_tests/monolith_test.cc b/auto_tests/monolith_test.cc new file mode 100644 index 00000000..9fa7ee26 --- /dev/null +++ b/auto_tests/monolith_test.cc @@ -0,0 +1,260 @@ +/* Nop-test, just to make sure our code compiles as C++. + */ + +#ifdef __FreeBSD__ +// Include this here, because _XOPEN_SOURCE hides symbols we need. +// +// https://lists.freebsd.org/pipermail/freebsd-standards/2004-March/000474.html. +#include +#endif + +#ifdef __APPLE__ +#define _DARWIN_C_SOURCE +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "../other/monolith.h" +#define DHT_C_INCLUDED + +#include "../testing/misc_tools.c" +#include "check_compat.h" +#include "helpers.h" + +#include + +namespace TCP_test { +int main(void); +#include "TCP_test.c" +} // namespace TCP_test +namespace bootstrap_test { +int main(void); +#include "bootstrap_test.c" +} // namespace bootstrap_test +namespace conference_test { +int main(void); +#include "conference_test.c" +} // namespace conference_test +namespace crypto_test { +int main(void); +#include "crypto_test.c" +} // namespace crypto_test +namespace dht_test { +int main(void); +#include "dht_test.c" +} // namespace dht_test +namespace encryptsave_test { +int main(void); +#include "encryptsave_test.c" +} // namespace encryptsave_test +namespace file_saving_test { +int main(void); +#include "file_saving_test.c" +} // namespace file_saving_test +namespace friend_request_test { +int main(void); +#include "friend_request_test.c" +} // namespace friend_request_test +namespace lan_discovery_test { +int main(void); +#include "lan_discovery_test.c" +} // namespace lan_discovery_test +namespace lossless_packet_test { +int main(void); +#include "lossless_packet_test.c" +} // namespace lossless_packet_test +namespace lossy_packet_test { +int main(void); +#include "lossy_packet_test.c" +} // namespace lossy_packet_test +namespace messenger_test { +int main(void); +#include "messenger_test.c" +} // namespace messenger_test +namespace network_test { +int main(void); +#include "network_test.c" +} // namespace network_test +namespace onion_test { +int main(void); +#include "onion_test.c" +} // namespace onion_test +namespace save_friend_test { +int main(void); +#include "save_friend_test.c" +} // namespace save_friend_test +namespace save_load_test { +int main(void); +#include "save_load_test.c" +} // namespace save_load_test +namespace selfname_change_conference_test { +int main(void); +#include "selfname_change_conference_test.c" +} // namespace selfname_change_conference_test +namespace self_conference_title_change_test { +int main(void); +#include "self_conference_title_change_test.c" +} // namespace self_conference_title_change_test +namespace send_message_test { +int main(void); +#include "send_message_test.c" +} // namespace send_message_test +namespace set_name_test { +int main(void); +#include "set_name_test.c" +} // namespace set_name_test +namespace set_status_message_test { +int main(void); +#include "set_status_message_test.c" +} // namespace set_status_message_test +namespace simple_conference_test { +int main(void); +#include "simple_conference_test.c" +} // namespace simple_conference_test +namespace skeleton_test { +int main(void); +#include "skeleton_test.c" +} // namespace skeleton_test +namespace toxav_basic_test { +int main(void); +#include "toxav_basic_test.c" +} // namespace toxav_basic_test +namespace toxav_many_test { +int main(void); +#include "toxav_many_test.c" +} // namespace toxav_many_test +namespace tox_many_tcp_test { +int main(void); +#include "tox_many_tcp_test.c" +} // namespace tox_many_tcp_test +namespace tox_many_test { +int main(void); +#include "tox_many_test.c" +} // namespace tox_many_test +namespace tox_one_test { +int main(void); +#include "tox_one_test.c" +} // namespace tox_one_test +namespace tox_strncasecmp_test { +int main(void); +#include "tox_strncasecmp_test.c" +} // namespace tox_strncasecmp_test +namespace typing_test { +int main(void); +#include "typing_test.c" +} // namespace typing_test +namespace version_test { +int main(void); +#include "version_test.c" +} // namespace version_test + +#define PRINT_SIZE 0 + +template +void check_size(char const *type) { +#if PRINT_SIZE + printf("CHECK_SIZE(%s, %zu);\n", type, Actual); +#else + static_assert(Actual == Expected, "Bad sizeof - see template expansion errors for details"); +#endif +} + +#define CHECK_SIZE(TYPE, SIZE) check_size(#TYPE) + +/** + * The main function static-asserts that we are aware of all the sizes of all + * the structs it toxcore. If you find this failing after you make a change, + * switch on the PRINT_SIZE above and copy the number into this function. + */ +int main(int argc, char *argv[]) { +#if defined(__x86_64__) && defined(__LP64__) + // toxcore/DHT + CHECK_SIZE(Client_data, 496); + CHECK_SIZE(Cryptopacket_Handles, 16); + CHECK_SIZE(DHT, 676528); + CHECK_SIZE(DHT_Friend, 5104); + CHECK_SIZE(Hardening, 144); + CHECK_SIZE(IPPTs, 40); + CHECK_SIZE(IPPTsPng, 232); + CHECK_SIZE(NAT, 48); + CHECK_SIZE(Node_format, 64); + CHECK_SIZE(Shared_Key, 80); + CHECK_SIZE(Shared_Keys, 81920); + // toxcore/friend_connection + CHECK_SIZE(Friend_Conn, 1784); + CHECK_SIZE(Friend_Connections, 72); + // toxcore/friend_requests + CHECK_SIZE(Friend_Requests, 1080); + // toxcore/group + CHECK_SIZE(Group_c, 728); + CHECK_SIZE(Group_Chats, 2120); + CHECK_SIZE(Group_Peer, 480); + // toxcore/list + CHECK_SIZE(BS_LIST, 32); + // toxcore/logger + CHECK_SIZE(Logger, 24); + // toxcore/Messenger + CHECK_SIZE(File_Transfers, 72); + CHECK_SIZE(Friend, 39264); + CHECK_SIZE(Messenger, 2008); + CHECK_SIZE(Messenger_Options, 72); + CHECK_SIZE(Receipts, 16); + // toxcore/net_crypto +#ifdef __linux__ + CHECK_SIZE(Crypto_Connection, 525392); + CHECK_SIZE(Net_Crypto, 272); +#endif + CHECK_SIZE(New_Connection, 168); + CHECK_SIZE(Packet_Data, 1384); + CHECK_SIZE(Packets_Array, 262152); + // toxcore/network + CHECK_SIZE(IP, 24); + CHECK_SIZE(IP4, 4); +#if USE_IPV6 + CHECK_SIZE(IP6, 16); +#endif + CHECK_SIZE(IP_Port, 32); + CHECK_SIZE(Networking_Core, 4112); + CHECK_SIZE(Packet_Handler, 16); + // toxcore/onion_announce + CHECK_SIZE(Cmp_data, 296); + CHECK_SIZE(Onion_Announce, 128048); + CHECK_SIZE(Onion_Announce_Entry, 288); + // toxcore/onion_client + CHECK_SIZE(Last_Pinged, 40); + CHECK_SIZE(Onion_Client, 15816); + CHECK_SIZE(Onion_Client_Cmp_data, 176); + CHECK_SIZE(Onion_Client_Paths, 2520); + CHECK_SIZE(Onion_Friend, 1936); + CHECK_SIZE(Onion_Friend, 1936); + CHECK_SIZE(Onion_Node, 168); + // toxcore/onion + CHECK_SIZE(Onion, 245832); + CHECK_SIZE(Onion_Path, 392); + // toxcore/ping_array + CHECK_SIZE(Ping_Array, 24); + CHECK_SIZE(Ping_Array_Entry, 32); + // toxcore/ping + CHECK_SIZE(Ping, 2072); + // toxcore/TCP_client + CHECK_SIZE(TCP_Client_Connection, 12064); + CHECK_SIZE(TCP_Proxy_Info, 40); + // toxcore/TCP_connection + CHECK_SIZE(TCP_con, 112); + CHECK_SIZE(TCP_Connections, 200); + CHECK_SIZE(TCP_Connection_to, 112); + // toxcore/TCP_server + CHECK_SIZE(TCP_Priority_List, 16); + CHECK_SIZE(TCP_Secure_Connection, 11816); +#ifdef TCP_SERVER_USE_EPOLL + CHECK_SIZE(TCP_Server, 6049968); // 6MB! +#else + CHECK_SIZE(TCP_Server, 6049952); // 6MB! +#endif + // toxcore/tox + CHECK_SIZE(Tox_Options, 64); +#endif + return 0; +} diff --git a/auto_tests/monolith_test.cpp b/auto_tests/monolith_test.cpp deleted file mode 100644 index 44a5b16b..00000000 --- a/auto_tests/monolith_test.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/* Nop-test, just to make sure our code compiles as C++. - */ - -#ifdef __FreeBSD__ -// Include this here, because _XOPEN_SOURCE hides symbols we need. -// -// https://lists.freebsd.org/pipermail/freebsd-standards/2004-March/000474.html. -#include -#endif - -#ifdef __APPLE__ -#define _DARWIN_C_SOURCE -#endif - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../other/monolith.h" -#define DHT_C_INCLUDED - -#include "check_compat.h" -#include "helpers.h" -#include "../testing/misc_tools.c" - -#include - -namespace TCP_test -{ -int main(void); -#include "TCP_test.c" -} -namespace bootstrap_test -{ -int main(void); -#include "bootstrap_test.c" -} -namespace conference_test -{ -int main(void); -#include "conference_test.c" -} -namespace crypto_test -{ -int main(void); -#include "crypto_test.c" -} -namespace dht_test -{ -int main(void); -#include "dht_test.c" -} -namespace encryptsave_test -{ -int main(void); -#include "encryptsave_test.c" -} -namespace file_saving_test -{ -int main(void); -#include "file_saving_test.c" -} -namespace friend_request_test -{ -int main(void); -#include "friend_request_test.c" -} -namespace lan_discovery_test -{ -int main(void); -#include "lan_discovery_test.c" -} -namespace lossless_packet_test -{ -int main(void); -#include "lossless_packet_test.c" -} -namespace lossy_packet_test -{ -int main(void); -#include "lossy_packet_test.c" -} -namespace messenger_test -{ -int main(void); -#include "messenger_test.c" -} -namespace network_test -{ -int main(void); -#include "network_test.c" -} -namespace onion_test -{ -int main(void); -#include "onion_test.c" -} -namespace save_friend_test -{ -int main(void); -#include "save_friend_test.c" -} -namespace save_load_test -{ -int main(void); -#include "save_load_test.c" -} -namespace selfname_change_conference_test -{ -int main(void); -#include "selfname_change_conference_test.c" -} -namespace self_conference_title_change_test -{ -int main(void); -#include "self_conference_title_change_test.c" -} -namespace send_message_test -{ -int main(void); -#include "send_message_test.c" -} -namespace set_name_test -{ -int main(void); -#include "set_name_test.c" -} -namespace set_status_message_test -{ -int main(void); -#include "set_status_message_test.c" -} -namespace simple_conference_test -{ -int main(void); -#include "simple_conference_test.c" -} -namespace skeleton_test -{ -int main(void); -#include "skeleton_test.c" -} -namespace toxav_basic_test -{ -int main(void); -#include "toxav_basic_test.c" -} -namespace toxav_many_test -{ -int main(void); -#include "toxav_many_test.c" -} -namespace tox_many_tcp_test -{ -int main(void); -#include "tox_many_tcp_test.c" -} -namespace tox_many_test -{ -int main(void); -#include "tox_many_test.c" -} -namespace tox_one_test -{ -int main(void); -#include "tox_one_test.c" -} -namespace tox_strncasecmp_test -{ -int main(void); -#include "tox_strncasecmp_test.c" -} -namespace typing_test -{ -int main(void); -#include "typing_test.c" -} -namespace version_test -{ -int main(void); -#include "version_test.c" -} - -#define PRINT_SIZE 0 - -template -void check_size(char const *type) -{ -#if PRINT_SIZE - printf("CHECK_SIZE(%s, %zu);\n", type, Actual); -#else - static_assert(Actual == Expected, "Bad sizeof - see template expansion errors for details"); -#endif -} - -#define CHECK_SIZE(TYPE, SIZE) check_size(#TYPE) - -/** - * The main function static-asserts that we are aware of all the sizes of all - * the structs it toxcore. If you find this failing after you make a change, - * switch on the PRINT_SIZE above and copy the number into this function. - */ -int main(int argc, char *argv[]) -{ -#if defined(__x86_64__) && defined(__LP64__) - // toxcore/DHT - CHECK_SIZE(Client_data, 496); - CHECK_SIZE(Cryptopacket_Handles, 16); - CHECK_SIZE(DHT, 676528); - CHECK_SIZE(DHT_Friend, 5104); - CHECK_SIZE(Hardening, 144); - CHECK_SIZE(IPPTs, 40); - CHECK_SIZE(IPPTsPng, 232); - CHECK_SIZE(NAT, 48); - CHECK_SIZE(Node_format, 64); - CHECK_SIZE(Shared_Key, 80); - CHECK_SIZE(Shared_Keys, 81920); - // toxcore/friend_connection - CHECK_SIZE(Friend_Conn, 1784); - CHECK_SIZE(Friend_Connections, 72); - // toxcore/friend_requests - CHECK_SIZE(Friend_Requests, 1080); - // toxcore/group - CHECK_SIZE(Group_c, 728); - CHECK_SIZE(Group_Chats, 2120); - CHECK_SIZE(Group_Peer, 480); - // toxcore/list - CHECK_SIZE(BS_LIST, 32); - // toxcore/logger - CHECK_SIZE(Logger, 24); - // toxcore/Messenger - CHECK_SIZE(File_Transfers, 72); - CHECK_SIZE(Friend, 39264); - CHECK_SIZE(Messenger, 2008); - CHECK_SIZE(Messenger_Options, 72); - CHECK_SIZE(Receipts, 16); - // toxcore/net_crypto -#ifdef __linux__ - CHECK_SIZE(Crypto_Connection, 525392); - CHECK_SIZE(Net_Crypto, 272); -#endif - CHECK_SIZE(New_Connection, 168); - CHECK_SIZE(Packet_Data, 1384); - CHECK_SIZE(Packets_Array, 262152); - // toxcore/network - CHECK_SIZE(IP, 24); - CHECK_SIZE(IP4, 4); -#if USE_IPV6 - CHECK_SIZE(IP6, 16); -#endif - CHECK_SIZE(IP_Port, 32); - CHECK_SIZE(Networking_Core, 4112); - CHECK_SIZE(Packet_Handler, 16); - // toxcore/onion_announce - CHECK_SIZE(Cmp_data, 296); - CHECK_SIZE(Onion_Announce, 128048); - CHECK_SIZE(Onion_Announce_Entry, 288); - // toxcore/onion_client - CHECK_SIZE(Last_Pinged, 40); - CHECK_SIZE(Onion_Client, 15816); - CHECK_SIZE(Onion_Client_Cmp_data, 176); - CHECK_SIZE(Onion_Client_Paths, 2520); - CHECK_SIZE(Onion_Friend, 1936); - CHECK_SIZE(Onion_Friend, 1936); - CHECK_SIZE(Onion_Node, 168); - // toxcore/onion - CHECK_SIZE(Onion, 245832); - CHECK_SIZE(Onion_Path, 392); - // toxcore/ping_array - CHECK_SIZE(Ping_Array, 24); - CHECK_SIZE(Ping_Array_Entry, 32); - // toxcore/ping - CHECK_SIZE(Ping, 2072); - // toxcore/TCP_client - CHECK_SIZE(TCP_Client_Connection, 12064); - CHECK_SIZE(TCP_Proxy_Info, 40); - // toxcore/TCP_connection - CHECK_SIZE(TCP_con, 112); - CHECK_SIZE(TCP_Connections, 200); - CHECK_SIZE(TCP_Connection_to, 112); - // toxcore/TCP_server - CHECK_SIZE(TCP_Priority_List, 16); - CHECK_SIZE(TCP_Secure_Connection, 11816); -#ifdef TCP_SERVER_USE_EPOLL - CHECK_SIZE(TCP_Server, 6049968); // 6MB! -#else - CHECK_SIZE(TCP_Server, 6049952); // 6MB! -#endif - // toxcore/tox - CHECK_SIZE(Tox_Options, 64); -#endif - return 0; -} diff --git a/cmake/CompileGTest.cmake b/cmake/CompileGTest.cmake index b7e0175f..f9aa4506 100644 --- a/cmake/CompileGTest.cmake +++ b/cmake/CompileGTest.cmake @@ -50,7 +50,7 @@ endif() function(unit_test subdir target) if(HAVE_GTEST) - add_executable(unit_${target}_test ${subdir}/${target}_test.cpp) + add_executable(unit_${target}_test ${subdir}/${target}_test.cc) target_link_modules(unit_${target}_test toxcore gtest) set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) diff --git a/testing/random_testing.cc b/testing/random_testing.cc index ecad07d3..b896e694 100644 --- a/testing/random_testing.cc +++ b/testing/random_testing.cc @@ -15,7 +15,7 @@ namespace { // Whether to write log messages when handling callbacks. -constexpr bool LOG_CALLBACKS = 0; +constexpr bool LOG_CALLBACKS = false; // Number of participants in the test run. constexpr uint32_t NUM_TOXES = 10; @@ -79,7 +79,7 @@ struct Action { uint32_t weight; char const *title; bool (*can)(Local_State const &state); - void (*run)(Local_State &state, Random &rnd, std::mt19937 &rng); + void (*run)(Local_State *state, Random *rnd, std::mt19937 *rng); }; std::vector get_action_weights(std::vector const &actions) { @@ -107,7 +107,7 @@ struct Global_State : std::vector { : actions_(actions), rnd_(actions), action_counter_(actions.size()) {} Action const &action(size_t id) const { return actions_.at(id); } - Random &rnd() { return rnd_; } + Random *rnd() { return &rnd_; } std::vector &action_counter() { return action_counter_; } private: @@ -147,7 +147,8 @@ void handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t pe Local_State *state = static_cast(user_data); if (LOG_CALLBACKS) { - std::printf("Tox #%u received a message of length %u\n", state->id(), (unsigned)length); + std::printf("Tox #%u received a message of length %u\n", state->id(), + static_cast(length)); } } @@ -227,18 +228,18 @@ bool all_connected(Global_State const &toxes) { }); } -bool bootstrap_toxes(Global_State &toxes) { +bool bootstrap_toxes(Global_State *toxes) { std::printf("Waiting for %u iterations for all friends to come online\n", MAX_BOOTSTRAP_ITERATIONS); for (uint32_t i = 0; i < MAX_BOOTSTRAP_ITERATIONS; i++) { - c_sleep(tox_iteration_interval(toxes.front().tox())); + c_sleep(tox_iteration_interval(toxes->front().tox())); - for (Local_State &state : toxes) { + for (Local_State &state : *toxes) { tox_iterate(state.tox(), &state); } - if (all_connected(toxes)) { + if (all_connected(*toxes)) { std::printf("Took %u iterations\n", i); return true; } @@ -247,25 +248,25 @@ bool bootstrap_toxes(Global_State &toxes) { return false; } -bool execute_random_action(Global_State &toxes, std::mt19937 &rng) { +bool execute_random_action(Global_State *toxes, std::mt19937 *rng) { // First, choose a random actor. - Local_State &actor = toxes.at(toxes.rnd().tox_selector(rng)); - size_t const action_id = toxes.rnd().action_selector(rng); - Action const &action = toxes.action(action_id); + Local_State &actor = toxes->at(toxes->rnd()->tox_selector(*rng)); + size_t const action_id = toxes->rnd()->action_selector(*rng); + Action const &action = toxes->action(action_id); if (!action.can(actor)) { return false; } std::printf("Tox #%u %s", actor.id(), action.title); - action.run(actor, toxes.rnd(), rng); + action.run(&actor, toxes->rnd(), rng); std::printf("\n"); - toxes.action_counter().at(action_id)++; + toxes->action_counter().at(action_id)++; return true; } -bool attempt_action(Global_State &toxes, std::mt19937 &rng) { +bool attempt_action(Global_State *toxes, std::mt19937 *rng) { for (uint32_t i = 0; i < MAX_ACTION_ATTEMPTS; i++) { if (execute_random_action(toxes, rng)) { return true; @@ -284,9 +285,9 @@ int main() { [](Local_State const &state) { return tox_conference_get_chatlist_size(state.tox()) < MAX_CONFERENCES_PER_USER; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { + [](Local_State *state, Random *rnd, std::mt19937 *rng) { TOX_ERR_CONFERENCE_NEW err; - tox_conference_new(state.tox(), &err); + tox_conference_new(state->tox(), &err); assert(err == TOX_ERR_CONFERENCE_NEW_OK); }, }, @@ -295,12 +296,12 @@ int main() { [](Local_State const &state) { return tox_conference_get_chatlist_size(state.tox()) != 0; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { + [](Local_State *state, Random *rnd, std::mt19937 *rng) { TOX_ERR_CONFERENCE_INVITE err; - tox_conference_invite(state.tox(), rnd.friend_selector(rng), - state.next_invite % tox_conference_get_chatlist_size(state.tox()), - &err); - state.next_invite++; + tox_conference_invite( + state->tox(), rnd->friend_selector(*rng), + state->next_invite % tox_conference_get_chatlist_size(state->tox()), &err); + state->next_invite++; assert(err == TOX_ERR_CONFERENCE_INVITE_OK); }, }, @@ -309,9 +310,9 @@ int main() { [](Local_State const &state) { return tox_conference_get_chatlist_size(state.tox()) != 0; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { + [](Local_State *state, Random *rnd, std::mt19937 *rng) { TOX_ERR_CONFERENCE_DELETE err; - tox_conference_delete(state.tox(), tox_conference_get_chatlist_size(state.tox()) - 1, + tox_conference_delete(state->tox(), tox_conference_get_chatlist_size(state->tox()) - 1, &err); assert(err == TOX_ERR_CONFERENCE_DELETE_OK); }, @@ -321,18 +322,18 @@ int main() { [](Local_State const &state) { return tox_conference_get_chatlist_size(state.tox()) != 0; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { - std::vector message(rnd.message_length_selector(rng)); + [](Local_State *state, Random *rnd, std::mt19937 *rng) { + std::vector message(rnd->message_length_selector(*rng)); for (uint8_t &byte : message) { - byte = rnd.byte_selector(rng); + byte = rnd->byte_selector(*rng); } TOX_ERR_CONFERENCE_SEND_MESSAGE err; tox_conference_send_message( - state.tox(), tox_conference_get_chatlist_size(state.tox()) - 1, + state->tox(), tox_conference_get_chatlist_size(state->tox()) - 1, TOX_MESSAGE_TYPE_NORMAL, message.data(), message.size(), &err); if (err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { - printf(" (OK, length = %u)", (unsigned)message.size()); + printf(" (OK, length = %u)", static_cast(message.size())); } else { printf(" (FAILED: %u)", err); } @@ -340,24 +341,24 @@ int main() { }, { 10, "changes their name", [](Local_State const &state) { return true; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { - std::vector name(rnd.name_length_selector(rng)); + [](Local_State *state, Random *rnd, std::mt19937 *rng) { + std::vector name(rnd->name_length_selector(*rng)); for (uint8_t &byte : name) { - byte = rnd.byte_selector(rng); + byte = rnd->byte_selector(*rng); } TOX_ERR_SET_INFO err; - tox_self_set_name(state.tox(), name.data(), name.size(), &err); + tox_self_set_name(state->tox(), name.data(), name.size(), &err); assert(err == TOX_ERR_SET_INFO_OK); - printf(" (length = %u)", (unsigned)name.size()); + printf(" (length = %u)", static_cast(name.size())); }, }, { 10, "sets their name to empty", [](Local_State const &state) { return true; }, - [](Local_State &state, Random &rnd, std::mt19937 &rng) { + [](Local_State *state, Random *rnd, std::mt19937 *rng) { TOX_ERR_SET_INFO err; - tox_self_set_name(state.tox(), nullptr, 0, &err); + tox_self_set_name(state->tox(), nullptr, 0, &err); assert(err == TOX_ERR_SET_INFO_OK); }, }, @@ -368,12 +369,12 @@ int main() { std::mt19937 rng; uint32_t action_number; for (action_number = 0; action_number < MAX_ACTIONS; action_number++) { - if (!all_connected(toxes) && !bootstrap_toxes(toxes)) { + if (!all_connected(toxes) && !bootstrap_toxes(&toxes)) { std::printf("Bootstrapping took too long; %u actions performed\n", action_number); return EXIT_FAILURE; } - if (!attempt_action(toxes, rng)) { + if (!attempt_action(&toxes, &rng)) { std::printf( "System is stuck after %u actions: none of the toxes can perform an action anymore\n", action_number); diff --git a/toxav/BUILD.bazel b/toxav/BUILD.bazel index cf905910..a5d49e0d 100644 --- a/toxav/BUILD.bazel +++ b/toxav/BUILD.bazel @@ -37,7 +37,7 @@ cc_library( cc_test( name = "rtp_test", - srcs = ["rtp_test.cpp"], + srcs = ["rtp_test.cc"], deps = [ ":rtp", "//c-toxcore/toxcore:crypto_core", diff --git a/toxav/rtp_test.cc b/toxav/rtp_test.cc new file mode 100644 index 00000000..b41ff905 --- /dev/null +++ b/toxav/rtp_test.cc @@ -0,0 +1,64 @@ +#include "rtp.h" + +#include "../toxcore/crypto_core.h" + +#include + +namespace { + +RTPHeader random_header() { + return { + random_u16(), random_u16(), random_u16(), random_u16(), random_u16(), + random_u16(), random_u16(), random_u32(), random_u32(), random_u64(), + random_u32(), random_u32(), random_u32(), random_u16(), random_u16(), + }; +} + +TEST(Rtp, Deserialisation) { + RTPHeader const header = random_header(); + + uint8_t rdata[RTP_HEADER_SIZE]; + EXPECT_EQ(rtp_header_pack(rdata, &header), RTP_HEADER_SIZE); + + RTPHeader unpacked = {0}; + EXPECT_EQ(rtp_header_unpack(rdata, &unpacked), RTP_HEADER_SIZE); + + EXPECT_EQ(header.ve, unpacked.ve); + EXPECT_EQ(header.pe, unpacked.pe); + EXPECT_EQ(header.xe, unpacked.xe); + EXPECT_EQ(header.cc, unpacked.cc); + EXPECT_EQ(header.ma, unpacked.ma); + EXPECT_EQ(header.pt, unpacked.pt); + EXPECT_EQ(header.sequnum, unpacked.sequnum); + EXPECT_EQ(header.timestamp, unpacked.timestamp); + EXPECT_EQ(header.ssrc, unpacked.ssrc); + EXPECT_EQ(header.flags, unpacked.flags); + EXPECT_EQ(header.offset_full, unpacked.offset_full); + EXPECT_EQ(header.data_length_full, unpacked.data_length_full); + EXPECT_EQ(header.received_length_full, unpacked.received_length_full); + EXPECT_EQ(header.offset_lower, unpacked.offset_lower); + EXPECT_EQ(header.data_length_lower, unpacked.data_length_lower); +} + +TEST(Rtp, SerialisingAllOnes) { + RTPHeader header; + memset(&header, 0xff, sizeof header); + + uint8_t rdata[RTP_HEADER_SIZE]; + rtp_header_pack(rdata, &header); + + EXPECT_EQ(std::string((char const *)rdata, sizeof rdata), + std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\xFF\xFF\xFF\xFF", + RTP_HEADER_SIZE)); +} + +} // namespace diff --git a/toxav/rtp_test.cpp b/toxav/rtp_test.cpp deleted file mode 100644 index d6717a28..00000000 --- a/toxav/rtp_test.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "rtp.h" - -#include "../toxcore/crypto_core.h" - -#include - -namespace -{ - -RTPHeader random_header() -{ - return { - random_u16(), - random_u16(), - random_u16(), - random_u16(), - random_u16(), - random_u16(), - random_u16(), - random_u32(), - random_u32(), - random_u64(), - random_u32(), - random_u32(), - random_u32(), - random_u16(), - random_u16(), - }; -} - -TEST(Rtp, Deserialisation) -{ - RTPHeader const header = random_header(); - - uint8_t rdata[RTP_HEADER_SIZE]; - EXPECT_EQ(rtp_header_pack(rdata, &header), RTP_HEADER_SIZE); - - RTPHeader unpacked = {0}; - EXPECT_EQ(rtp_header_unpack(rdata, &unpacked), RTP_HEADER_SIZE); - - EXPECT_EQ(header.ve, unpacked.ve); - EXPECT_EQ(header.pe, unpacked.pe); - EXPECT_EQ(header.xe, unpacked.xe); - EXPECT_EQ(header.cc, unpacked.cc); - EXPECT_EQ(header.ma, unpacked.ma); - EXPECT_EQ(header.pt, unpacked.pt); - EXPECT_EQ(header.sequnum, unpacked.sequnum); - EXPECT_EQ(header.timestamp, unpacked.timestamp); - EXPECT_EQ(header.ssrc, unpacked.ssrc); - EXPECT_EQ(header.flags, unpacked.flags); - EXPECT_EQ(header.offset_full, unpacked.offset_full); - EXPECT_EQ(header.data_length_full, unpacked.data_length_full); - EXPECT_EQ(header.received_length_full, unpacked.received_length_full); - EXPECT_EQ(header.offset_lower, unpacked.offset_lower); - EXPECT_EQ(header.data_length_lower, unpacked.data_length_lower); -} - -TEST(Rtp, SerialisingAllOnes) -{ - RTPHeader header; - memset(&header, 0xff, sizeof header); - - uint8_t rdata[RTP_HEADER_SIZE]; - rtp_header_pack(rdata, &header); - - EXPECT_EQ(std::string((char const *)rdata, sizeof rdata), - std::string("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" - "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" - "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" - "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" - "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\x00\x00\x00\x00" - "\x00\x00\x00\x00\xFF\xFF\xFF\xFF", - RTP_HEADER_SIZE)); -} - -} // namespace diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index b22f9b9e..1d43d828 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -30,7 +30,7 @@ cc_library( cc_test( name = "crypto_core_test", - srcs = ["crypto_core_test.cpp"], + srcs = ["crypto_core_test.cc"], deps = [ ":crypto_core", "@com_google_googletest//:gtest_main", @@ -75,7 +75,7 @@ cc_library( cc_test( name = "util_test", - srcs = ["util_test.cpp"], + srcs = ["util_test.cc"], deps = [ ":network", "@com_google_googletest//:gtest_main", diff --git a/toxcore/crypto_core_test.cc b/toxcore/crypto_core_test.cc new file mode 100644 index 00000000..1fbdd296 --- /dev/null +++ b/toxcore/crypto_core_test.cc @@ -0,0 +1,91 @@ +#include "crypto_core.h" + +#include + +#include + +namespace { + +enum { + /** + * The size of the arrays to compare. This was chosen to take around 2000 + * CPU clocks on x86_64. + */ + CRYPTO_TEST_MEMCMP_SIZE = 1024 * 1024, // 1 MiB + /** + * The number of times we run memcmp in the test. + * + * We compute the median time taken to reduce error margins. + */ + CRYPTO_TEST_MEMCMP_ITERATIONS = 500, + /** + * The margin of error (in clocks) we allow for this test. + * + * Should be within 0.5% of ~2000 CPU clocks. In reality, the code is much + * more precise and is usually within 1 CPU clock. + */ + CRYPTO_TEST_MEMCMP_EPS = 10, +}; + +clock_t memcmp_time(void *a, void *b, size_t len) { + clock_t start = clock(); + crypto_memcmp(a, b, len); + return clock() - start; +} + +/** + * This function performs the actual timing. It interleaves comparison of + * equal and non-equal arrays to reduce the influence of external effects + * such as the machine being a little more busy 1 second later. + */ +void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *same_median, + clock_t *not_same_median) { + clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; + clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; + + for (size_t i = 0; i < CRYPTO_TEST_MEMCMP_ITERATIONS; i++) { + same_results[i] = memcmp_time(src, same, len); + not_same_results[i] = memcmp_time(src, not_same, len); + } + + std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); + *same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; + std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); + *not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; +} + +/** + * This test checks whether crypto_memcmp takes the same time for equal and + * non-equal chunks of memory. + */ +TEST(CryptoCore, MemcmpTimingIsDataIndependent) { + // A random piece of memory. + auto *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE); + + // A separate piece of memory containing the same data. + auto *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE); + + // Another piece of memory containing different data. + auto *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE); + + clock_t same_median; + clock_t not_same_median; + memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, ¬_same_median); + + delete[] not_same; + delete[] same; + delete[] src; + + clock_t const delta = + same_median > not_same_median ? same_median - not_same_median : not_same_median - same_median; + + EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) + << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" + << "Time of the same data comparation: " << same_median << " clocks\n" + << "Time of the different data comparation: " << not_same_median << " clocks"; +} + +} // namespace diff --git a/toxcore/crypto_core_test.cpp b/toxcore/crypto_core_test.cpp deleted file mode 100644 index 8f91dce8..00000000 --- a/toxcore/crypto_core_test.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "crypto_core.h" - -#include - -#include - -namespace -{ - -enum { - /** - * The size of the arrays to compare. This was chosen to take around 2000 - * CPU clocks on x86_64. - */ - CRYPTO_TEST_MEMCMP_SIZE = 1024 * 1024, // 1 MiB - /** - * The number of times we run memcmp in the test. - * - * We compute the median time taken to reduce error margins. - */ - CRYPTO_TEST_MEMCMP_ITERATIONS = 500, - /** - * The margin of error (in clocks) we allow for this test. - * - * Should be within 0.5% of ~2000 CPU clocks. In reality, the code is much - * more precise and is usually within 1 CPU clock. - */ - CRYPTO_TEST_MEMCMP_EPS = 10, -}; - -clock_t memcmp_time(void *a, void *b, size_t len) -{ - clock_t start = clock(); - crypto_memcmp(a, b, len); - return clock() - start; -} - -/** - * This function performs the actual timing. It interleaves comparison of - * equal and non-equal arrays to reduce the influence of external effects - * such as the machine being a little more busy 1 second later. - */ -void memcmp_median(void *src, void *same, void *not_same, size_t len, - clock_t *same_median, clock_t *not_same_median) -{ - clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; - clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; - - for (size_t i = 0; i < CRYPTO_TEST_MEMCMP_ITERATIONS; i++) { - same_results[i] = memcmp_time(src, same, len); - not_same_results[i] = memcmp_time(src, not_same, len); - } - - std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); - *same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; - std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); - *not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; -} - -/** - * This test checks whether crypto_memcmp takes the same time for equal and - * non-equal chunks of memory. - */ -TEST(CryptoCore, MemcmpTimingIsDataIndependent) -{ - // A random piece of memory. - uint8_t *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; - random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE); - - // A separate piece of memory containing the same data. - uint8_t *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; - memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE); - - // Another piece of memory containing different data. - uint8_t *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; - random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE); - - clock_t same_median; - clock_t not_same_median; - memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, ¬_same_median); - - delete[] not_same; - delete[] same; - delete[] src; - - clock_t const delta = same_median > not_same_median - ? same_median - not_same_median - : not_same_median - same_median; - - EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) - << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" - << "Time of the same data comparation: " << same_median << " clocks\n" - << "Time of the different data comparation: " << not_same_median << " clocks"; -} - -} // namespace diff --git a/toxcore/util_test.cc b/toxcore/util_test.cc new file mode 100644 index 00000000..541aa61f --- /dev/null +++ b/toxcore/util_test.cc @@ -0,0 +1,51 @@ +#include "util.h" + +#include "crypto_core.h" + +#include + +TEST(Util, UnixTimeIncreasesOverTime) { + unix_time_update(); + uint64_t const start = unix_time(); + + while (start == unix_time()) { + unix_time_update(); + } + + uint64_t const end = unix_time(); + EXPECT_GT(end, start); +} + +TEST(Util, IsTimeout) { + uint64_t const start = unix_time(); + EXPECT_FALSE(is_timeout(start, 1)); + + while (start == unix_time()) { + unix_time_update(); + } + + EXPECT_TRUE(is_timeout(start, 1)); +} + +TEST(Util, TwoRandomIdsAreNotEqual) { + uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; + uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk2[CRYPTO_SECRET_KEY_SIZE]; + + crypto_new_keypair(pk1, sk1); + crypto_new_keypair(pk2, sk2); + + EXPECT_FALSE(id_equal(pk1, pk2)); +} + +TEST(Util, IdCopyMakesKeysEqual) { + uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; + uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0}; + + crypto_new_keypair(pk1, sk1); + id_copy(pk2, pk1); + + EXPECT_TRUE(id_equal(pk1, pk2)); +} diff --git a/toxcore/util_test.cpp b/toxcore/util_test.cpp deleted file mode 100644 index 8de63848..00000000 --- a/toxcore/util_test.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "util.h" - -#include "crypto_core.h" - -#include - -TEST(Util, UnixTimeIncreasesOverTime) -{ - unix_time_update(); - uint64_t const start = unix_time(); - - while (start == unix_time()) { - unix_time_update(); - } - - uint64_t const end = unix_time(); - EXPECT_GT(end, start); -} - -TEST(Util, IsTimeout) -{ - uint64_t const start = unix_time(); - EXPECT_FALSE(is_timeout(start, 1)); - - while (start == unix_time()) { - unix_time_update(); - } - - EXPECT_TRUE(is_timeout(start, 1)); -} - -TEST(Util, TwoRandomIdsAreNotEqual) -{ - uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; - uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t sk2[CRYPTO_SECRET_KEY_SIZE]; - - crypto_new_keypair(pk1, sk1); - crypto_new_keypair(pk2, sk2); - - EXPECT_FALSE(id_equal(pk1, pk2)); -} - -TEST(Util, IdCopyMakesKeysEqual) -{ - uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; - uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0}; - - crypto_new_keypair(pk1, sk1); - id_copy(pk2, pk1); - - EXPECT_TRUE(id_equal(pk1, pk2)); -} -- cgit v1.2.3