diff --git a/.travis.yml b/.travis.yml index ea1ffea7b..67af3416e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,6 +46,7 @@ matrix: - libcv-dev # For av_test. - libhighgui-dev # For av_test. - libopencv-contrib-dev # For av_test. + - libsndfile1-dev # For av_test. - portaudio19-dev # For av_test. before_install: # Clear bazel installation. diff --git a/CMakeLists.txt b/CMakeLists.txt index cd0e9944f..181d4155a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -371,6 +371,11 @@ unit_test(toxcore util) # ################################################################################ +add_library(misc_tools + testing/misc_tools.c + testing/misc_tools.h) +target_link_modules(misc_tools toxcore) + set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the number of seconds specified") if(ANDROID_CPU_FEATURES) @@ -389,7 +394,7 @@ function(auto_test target) if(NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD")) add_executable(auto_${target}_test ${CPUFEATURES} auto_tests/${target}_test.c) - target_link_modules(auto_${target}_test toxcore) + target_link_modules(auto_${target}_test toxcore misc_tools) if(NOT ARGV1 STREQUAL "DONT_RUN") add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") @@ -459,7 +464,7 @@ if(DHT_BOOTSTRAP) add_executable(DHT_bootstrap ${CPUFEATURES} other/DHT_bootstrap.c other/bootstrap_node_packets.c) - target_link_modules(DHT_bootstrap toxcore) + target_link_modules(DHT_bootstrap toxcore misc_tools) endif() option(BOOTSTRAP_DAEMON "Enable building of tox-bootstrapd" ON) @@ -519,12 +524,12 @@ endif() add_executable(DHT_test ${CPUFEATURES} testing/DHT_test.c) -target_link_modules(DHT_test toxcore) +target_link_modules(DHT_test toxcore misc_tools) add_executable(Messenger_test ${CPUFEATURES} testing/Messenger_test.c) -target_link_modules(Messenger_test toxcore) +target_link_modules(Messenger_test toxcore misc_tools) add_executable(random_testing ${CPUFEATURES} testing/random_testing.cc) -target_link_modules(random_testing toxcore) +target_link_modules(random_testing toxcore misc_tools) diff --git a/auto_tests/BUILD.bazel b/auto_tests/BUILD.bazel index a9c0c20d5..21c403ab8 100644 --- a/auto_tests/BUILD.bazel +++ b/auto_tests/BUILD.bazel @@ -1,11 +1,13 @@ cc_library( - name = "helpers", + name = "check_compat", testonly = True, - hdrs = [ - "check_compat.h", - "helpers.h", - "run_auto_test.h", - ], + hdrs = ["check_compat.h"], +) + +cc_library( + name = "run_auto_test", + testonly = True, + hdrs = ["run_auto_test.h"], ) test_sizes = { @@ -21,7 +23,8 @@ test_sizes = { srcs = [src], copts = ["-Wno-sign-compare"], deps = [ - ":helpers", + ":check_compat", + ":run_auto_test", "//c-toxcore/other:monolith", "//c-toxcore/testing:misc_tools", "//c-toxcore/toxav", @@ -44,8 +47,9 @@ cc_test( srcs = ["monolith_test.cc"], copts = ["-Wno-sign-compare"], deps = [ - ":helpers", + ":check_compat", ":monolith", + ":run_auto_test", "//c-toxcore/other:monolith", "//c-toxcore/testing:misc_tools", ], diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index 94762969b..67c720163 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc @@ -10,6 +10,7 @@ AUTOTEST_CFLAGS = \ AUTOTEST_LDADD = \ $(LIBSODIUM_LDFLAGS) \ $(NACL_LDFLAGS) \ + libmisc_tools.la \ libtoxcore.la \ libtoxencryptsave.la \ $(LIBSODIUM_LIBS) \ @@ -115,4 +116,3 @@ tox_strncasecmp_test_LDADD = $(AUTOTEST_LDADD) EXTRA_DIST += $(top_srcdir)/auto_tests/check_compat.h -EXTRA_DIST += $(top_srcdir)/auto_tests/helpers.h diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index f5bc24712..072665438 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -1,27 +1,17 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - -#include #include #include -#include -#include +#include "../testing/misc_tools.h" #include "../toxcore/TCP_client.h" #include "../toxcore/TCP_server.h" - #include "../toxcore/crypto_core.h" #include "../toxcore/mono_time.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define NUM_PORTS 3 @@ -836,8 +826,6 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); - Suite *TCP = TCP_suite(); SRunner *test_runner = srunner_create(TCP); diff --git a/auto_tests/bootstrap_test.c b/auto_tests/bootstrap_test.c index d6138855c..cbe81ebc4 100644 --- a/auto_tests/bootstrap_test.c +++ b/auto_tests/bootstrap_test.c @@ -1,8 +1,11 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" +#include + +#include "../testing/misc_tools.h" +#include "check_compat.h" static uint8_t const key[] = { 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, @@ -29,7 +32,9 @@ int main(void) c_sleep(ITERATION_INTERVAL); } - assert(tox_self_get_connection_status(tox_udp) == TOX_CONNECTION_UDP); + const TOX_CONNECTION status = tox_self_get_connection_status(tox_udp); + ck_assert_msg(status == TOX_CONNECTION_UDP, + "expected connection status to be UDP, but got %d", status); printf("Connection (UDP): %d\n", tox_self_get_connection_status(tox_udp)); tox_kill(tox_udp); diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c index 7b6467265..55aabfb62 100644 --- a/auto_tests/conference_double_invite_test.c +++ b/auto_tests/conference_double_invite_test.c @@ -1,5 +1,5 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include diff --git a/auto_tests/conference_peer_nick_test.c b/auto_tests/conference_peer_nick_test.c index a1b33e9f4..4d54f1fb5 100644 --- a/auto_tests/conference_peer_nick_test.c +++ b/auto_tests/conference_peer_nick_test.c @@ -1,5 +1,5 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include @@ -57,15 +57,18 @@ static void rebuild_peer_list(Tox *tox) ++conference_number) { TOX_ERR_CONFERENCE_PEER_QUERY err; uint32_t const count = tox_conference_peer_count(tox, conference_number, &err); - assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK, + "failed to get conference peer count for conference %u: err = %d", conference_number, err); for (uint32_t peer_number = 0; peer_number < count; peer_number++) { size_t size = tox_conference_peer_get_name_size(tox, conference_number, peer_number, &err); - assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK, + "failed to get conference peer %u's name size (conference = %u): err = %d", peer_number, conference_number, err); uint8_t *const name = malloc(size); tox_conference_peer_get_name(tox, conference_number, peer_number, name, &err); - assert(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK, + "failed to get conference peer %u's name (conference = %u): err = %d", peer_number, conference_number, err); free(name); } } diff --git a/auto_tests/conference_simple_test.c b/auto_tests/conference_simple_test.c index 3e2f0f94d..8d95bba8d 100644 --- a/auto_tests/conference_simple_test.c +++ b/auto_tests/conference_simple_test.c @@ -1,13 +1,13 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "../toxcore/tox.h" - -#include +#include #include -#include "helpers.h" +#include "../testing/misc_tools.h" +#include "../toxcore/tox.h" +#include "check_compat.h" typedef struct State { uint32_t id; @@ -51,7 +51,7 @@ static void handle_conference_invite(Tox *tox, uint32_t friend_number, TOX_CONFE { TOX_ERR_CONFERENCE_JOIN err; state->conference = tox_conference_join(tox, friend_number, cookie, length, &err); - assert(err == TOX_ERR_CONFERENCE_JOIN_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "failed to join a conference: err = %d", err); fprintf(stderr, "tox%d Joined conference %d\n", state->id, state->conference); state->joined = true; } @@ -186,7 +186,7 @@ int main(void) TOX_ERR_CONFERENCE_NEW err; state1.conference = tox_conference_new(tox1, &err); state1.joined = true; - assert(err == TOX_ERR_CONFERENCE_NEW_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create a conference: err = %d", err); fprintf(stderr, "Created conference: id=%d\n", state1.conference); } @@ -194,7 +194,7 @@ int main(void) // Invite friend. TOX_ERR_CONFERENCE_INVITE err; tox_conference_invite(tox1, 0, state1.conference, &err); - assert(err == TOX_ERR_CONFERENCE_INVITE_OK); + ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err); fprintf(stderr, "tox1 invited tox2\n"); } diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index 706dad382..92e805c9e 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -1,25 +1,19 @@ /* Auto Tests: Conferences. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - -#include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define NUM_GROUP_TOX 5 #define GROUP_MESSAGE "Install Gentoo" @@ -125,8 +119,8 @@ static void run_conference_tests(Tox **toxes, uint32_t *tox_index) for (uint16_t i = 0; i < k - 1; ++i) { uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); ck_assert_msg(peer_count == (k - 1), "\n\tBad number of group peers (post check)." - "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n", - (k - 1), i, peer_count); + "\n\t\t\tExpected: %u but tox_instance(%u) only has: %u\n\n", + k - 1, i, (unsigned)peer_count); } } } @@ -240,8 +234,8 @@ static void test_many_group(void) uint32_t peer_count = tox_conference_peer_count(toxes[i], 0, nullptr); ck_assert_msg(peer_count == NUM_GROUP_TOX, "\n\tBad number of group peers (pre check)." - "\n\t\t\tExpected: %u but tox_instance(%u) only has: %" PRIu32 "\n\n", - NUM_GROUP_TOX, i, peer_count); + "\n\t\t\tExpected: %u but tox_instance(%u) only has: %u\n\n", + NUM_GROUP_TOX, i, (unsigned)peer_count); uint8_t title[2048]; size_t ret = tox_conference_get_title_size(toxes[i], 0, nullptr); diff --git a/auto_tests/conference_two_test.c b/auto_tests/conference_two_test.c index c3036165d..c6c3aa03d 100644 --- a/auto_tests/conference_two_test.c +++ b/auto_tests/conference_two_test.c @@ -2,14 +2,14 @@ // // This test triggers a different code path than if we only allocate a single // conference. This is the simplest test possible that triggers it. -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 + +#ifdef HAVE_CONFIG_H +#include "config.h" #endif +#include "../testing/misc_tools.h" #include "../toxcore/tox.h" - #include "check_compat.h" -#include "helpers.h" int main(void) { diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index c87ffa54f..8f8a786ce 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -2,18 +2,14 @@ #include "config.h" #endif -#include "check_compat.h" - #include #include #include -#include -#include - -#include "helpers.h" +#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" #include "../toxcore/net_crypto.h" +#include "check_compat.h" static void rand_bytes(uint8_t *b, size_t blen) { @@ -356,7 +352,6 @@ static Suite *crypto_suite(void) int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); Suite *crypto = crypto_suite(); SRunner *test_runner = srunner_create(crypto); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index f57d706b7..8b96f09ae 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -1,19 +1,11 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "check_compat.h" - -#include -#include - -#include "helpers.h" - +#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" + #ifndef DHT_C_INCLUDED #include "../toxcore/DHT.c" #endif // DHT_C_INCLUDED @@ -819,7 +811,6 @@ static Suite *dht_suite(void) int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); Suite *dht = dht_suite(); SRunner *test_runner = srunner_create(dht); diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index 48a43c863..365a9dbb8 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -2,27 +2,24 @@ #include "config.h" #endif -#include "check_compat.h" - #include #include #include #include -#include -#include "helpers.h" - -#include "../toxcore/tox.h" - -#include "../toxcore/ccompat.h" -#include "../toxcore/crypto_core.h" -#include "../toxencryptsave/toxencryptsave.h" #ifdef VANILLA_NACL #include "../toxencryptsave/crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h" #else #include #endif +#include "../testing/misc_tools.h" +#include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" +#include "../toxcore/tox.h" +#include "../toxencryptsave/toxencryptsave.h" +#include "check_compat.h" + static unsigned char test_salt[TOX_PASS_SALT_LENGTH] = {0xB1, 0xC2, 0x09, 0xEE, 0x50, 0x6C, 0xF0, 0x20, 0xC4, 0xD6, 0xEB, 0xC0, 0x44, 0x51, 0x3B, 0x60, 0x4B, 0x39, 0x4A, 0xCF, 0x09, 0x53, 0x4F, 0xEA, 0x08, 0x41, 0xFA, 0xCA, 0x66, 0xD2, 0x68, 0x7F}; static unsigned char known_key[TOX_PASS_KEY_LENGTH] = {0x29, 0x36, 0x1c, 0x9e, 0x65, 0xbb, 0x46, 0x8b, 0xde, 0xa1, 0xac, 0xf, 0xd5, 0x11, 0x81, 0xc8, 0x29, 0x28, 0x17, 0x23, 0xa6, 0xc3, 0x6b, 0x77, 0x2e, 0xd7, 0xd3, 0x10, 0xeb, 0xd2, 0xf7, 0xc8}; static const char *pw = "hunter2"; @@ -207,7 +204,6 @@ static Suite *encryptsave_suite(void) int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); Suite *encryptsave = encryptsave_suite(); SRunner *test_runner = srunner_create(encryptsave); diff --git a/auto_tests/file_saving_test.c b/auto_tests/file_saving_test.c index 23a4c7013..0f27f1d62 100644 --- a/auto_tests/file_saving_test.c +++ b/auto_tests/file_saving_test.c @@ -23,15 +23,21 @@ * along with Tox. If not, see . */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include -#include "helpers.h" - +#include "../testing/misc_tools.h" +#include "../toxcore/ccompat.h" #include "../toxencryptsave/toxencryptsave.h" -static const char *pphrase = "bar", *name = "foo", *savefile = "./save"; +static const char *pphrase = "bar"; +static const char *name = "foo"; +static const char *savefile = "./save"; static void save_data_encrypted(void) { diff --git a/auto_tests/file_transfer_test.c b/auto_tests/file_transfer_test.c index 6e8a3c53c..a6e45431c 100644 --- a/auto_tests/file_transfer_test.c +++ b/auto_tests/file_transfer_test.c @@ -1,25 +1,20 @@ /* File transfer test. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" /* The Travis-CI container responds poorly to ::1 as a localhost address * You're encouraged to -D FORCE_TESTS_IPV6 on a local test */ diff --git a/auto_tests/friend_connection_test.c b/auto_tests/friend_connection_test.c index 13d08b65d..a165678c0 100644 --- a/auto_tests/friend_connection_test.c +++ b/auto_tests/friend_connection_test.c @@ -5,8 +5,8 @@ * features. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include diff --git a/auto_tests/friend_request_test.c b/auto_tests/friend_request_test.c index 2fbc2166c..eefcb0561 100644 --- a/auto_tests/friend_request_test.c +++ b/auto_tests/friend_request_test.c @@ -1,25 +1,20 @@ /* Tests that we can add friends. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "../testing/misc_tools.h" +#include "check_compat.h" #define FR_MESSAGE "Gentoo" diff --git a/auto_tests/helpers.h b/auto_tests/helpers.h deleted file mode 100644 index 7af127f5c..000000000 --- a/auto_tests/helpers.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef TOXCORE_TEST_HELPERS_H -#define TOXCORE_TEST_HELPERS_H - -#include "../toxcore/tox.h" - -#include "../toxcore/ccompat.h" - -#include -#include -#include - -#ifndef c_sleep -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) -#include -#define c_sleep(x) Sleep(x) -#else -#include -#define c_sleep(x) usleep(1000 * (x)) -#endif -#endif - -#define ITERATION_INTERVAL 200 - -Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data); - -static const char *tox_log_level_name(TOX_LOG_LEVEL level) -{ - switch (level) { - case TOX_LOG_LEVEL_TRACE: - return "TRACE"; - - case TOX_LOG_LEVEL_DEBUG: - return "DEBUG"; - - case TOX_LOG_LEVEL_INFO: - return "INFO"; - - case TOX_LOG_LEVEL_WARNING: - return "WARNING"; - - case TOX_LOG_LEVEL_ERROR: - return "ERROR"; - } - - return ""; -} - -static void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, - const char *message, void *user_data) -{ - if (level == TOX_LOG_LEVEL_TRACE) { - return; - } - - uint32_t index = user_data ? *(uint32_t *)user_data : 0; - fprintf(stderr, "[#%d] %s %s:%d\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message); -} - -static Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery) -{ - struct Tox_Options *log_options = options; - - if (log_options == nullptr) { - log_options = tox_options_new(nullptr); - } - - assert(log_options != nullptr); - - tox_options_set_local_discovery_enabled(log_options, lan_discovery); - tox_options_set_start_port(log_options, 33445); - tox_options_set_end_port(log_options, 33445 + 2000); - tox_options_set_log_callback(log_options, &print_debug_log); - tox_options_set_log_user_data(log_options, log_user_data); - Tox *tox = tox_new(log_options, err); - - if (options == nullptr) { - tox_options_free(log_options); - } - - return tox; -} - -Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data) -{ - return tox_new_log_lan(options, err, log_user_data, false); -} - -#endif // TOXCORE_TEST_HELPERS_H diff --git a/auto_tests/invalid_proxy_test.c b/auto_tests/invalid_proxy_test.c index 600efa9e4..3e729b5aa 100644 --- a/auto_tests/invalid_proxy_test.c +++ b/auto_tests/invalid_proxy_test.c @@ -1,10 +1,14 @@ // Test that if UDP is enabled, and a proxy is provided that does not support // UDP proxying, we disable UDP. -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 + +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" +#include + +#include "../testing/misc_tools.h" +#include "check_compat.h" static uint8_t const key[] = { 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, @@ -37,7 +41,9 @@ int main(void) tox_iterate(tox, nullptr); c_sleep(ITERATION_INTERVAL); // None of the iterations should have a connection. - assert(tox_self_get_connection_status(tox) == TOX_CONNECTION_NONE); + const TOX_CONNECTION status = tox_self_get_connection_status(tox); + ck_assert_msg(status == TOX_CONNECTION_NONE, + "unexpectedly got a connection (%d)", status); } tox_kill(tox); diff --git a/auto_tests/invalid_tcp_proxy_test.c b/auto_tests/invalid_tcp_proxy_test.c index edbc3237a..211b04eb8 100644 --- a/auto_tests/invalid_tcp_proxy_test.c +++ b/auto_tests/invalid_tcp_proxy_test.c @@ -1,10 +1,14 @@ // Test to make sure that when UDP is disabled, and we set an invalid proxy, // i.e. one that doesn't run a proxy server, then we don't get any connection. -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 + +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" +#include + +#include "../testing/misc_tools.h" +#include "check_compat.h" static uint8_t const key[] = { 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, @@ -37,7 +41,9 @@ int main(void) tox_iterate(tox, nullptr); c_sleep(ITERATION_INTERVAL); // None of the iterations should have a connection. - assert(tox_self_get_connection_status(tox) == TOX_CONNECTION_NONE); + const TOX_CONNECTION status = tox_self_get_connection_status(tox); + ck_assert_msg(status == TOX_CONNECTION_NONE, + "unexpectedly got a connection (%d)", status); } tox_kill(tox); diff --git a/auto_tests/lan_discovery_test.c b/auto_tests/lan_discovery_test.c index 3192fc28e..28a5ddf03 100644 --- a/auto_tests/lan_discovery_test.c +++ b/auto_tests/lan_discovery_test.c @@ -1,8 +1,12 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" +#include +#include + +#include "../testing/misc_tools.h" +#include "../toxcore/ccompat.h" int main(void) { diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c index 05d56c4d1..dfe713a81 100644 --- a/auto_tests/lossless_packet_test.c +++ b/auto_tests/lossless_packet_test.c @@ -1,25 +1,20 @@ /* Tests that we can send lossless packets. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define LOSSLESS_PACKET_FILLER 160 diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c index c0af148f5..71f451493 100644 --- a/auto_tests/lossy_packet_test.c +++ b/auto_tests/lossy_packet_test.c @@ -1,25 +1,18 @@ /* Tests that we can send lossy packets. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include -#include "../toxcore/ccompat.h" -#include "../toxcore/tox.h" +#include "../testing/misc_tools.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define LOSSY_PACKET_FILLER 200 diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index d7f2b9e77..b8fbbc1f9 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -14,16 +14,7 @@ #include "config.h" #endif -#include "check_compat.h" - -#include "helpers.h" - -#include "../testing/misc_tools.c" // hex_string_to_bin -#include "../toxcore/Messenger.h" - -#include #include -#include #ifdef VANILLA_NACL #include // crypto_box_PUBLICKEYBYTES and other defines. @@ -31,6 +22,10 @@ #include #endif +#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; diff --git a/auto_tests/monolith_test.cc b/auto_tests/monolith_test.cc index 5c3a1cceb..5b1b910dd 100644 --- a/auto_tests/monolith_test.cc +++ b/auto_tests/monolith_test.cc @@ -21,7 +21,6 @@ #include "../testing/misc_tools.c" #include "check_compat.h" -#include "helpers.h" #include diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index dc67e0c04..78f411958 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -2,18 +2,12 @@ #include "config.h" #endif -#include "check_compat.h" - -#include -#include #include #include -#include -#include +#include "../testing/misc_tools.h" #include "../toxcore/network.h" - -#include "helpers.h" +#include "check_compat.h" #ifndef USE_IPV6 #define USE_IPV6 1 @@ -181,7 +175,6 @@ static Suite *network_suite(void) int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); Suite *network = network_suite(); SRunner *test_runner = srunner_create(network); diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 45679205e..18b7b523d 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -1,26 +1,17 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - -#include #include #include -#include -#include +#include "../testing/misc_tools.h" #include "../toxcore/mono_time.h" #include "../toxcore/onion.h" #include "../toxcore/onion_announce.h" #include "../toxcore/onion_client.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #ifndef USE_IPV6 #define USE_IPV6 1 @@ -570,7 +561,6 @@ static Suite *onion_suite(void) int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); Suite *onion = onion_suite(); SRunner *test_runner = srunner_create(onion); diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c index cdad24421..55f9e4591 100644 --- a/auto_tests/overflow_recvq_test.c +++ b/auto_tests/overflow_recvq_test.c @@ -1,8 +1,8 @@ /* Try to overflow the net_crypto packet buffer. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include diff --git a/auto_tests/overflow_sendq_test.c b/auto_tests/overflow_sendq_test.c index cc6eeda9d..0bdc60738 100644 --- a/auto_tests/overflow_sendq_test.c +++ b/auto_tests/overflow_sendq_test.c @@ -1,8 +1,8 @@ /* Try to overflow the net_crypto packet buffer. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h index a4c638ff2..04d24791f 100644 --- a/auto_tests/run_auto_test.h +++ b/auto_tests/run_auto_test.h @@ -1,7 +1,7 @@ #include // calloc, free #include "check_compat.h" -#include "helpers.h" +#include "../testing/misc_tools.h" static bool all_connected(uint32_t tox_count, Tox **toxes) { diff --git a/auto_tests/save_friend_test.c b/auto_tests/save_friend_test.c index 1e90e692c..077531278 100644 --- a/auto_tests/save_friend_test.c +++ b/auto_tests/save_friend_test.c @@ -1,20 +1,20 @@ /* Auto Tests: Save and load friends. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" -#include "../toxcore/ccompat.h" -#include "../toxcore/crypto_core.h" -#include "../toxcore/tox.h" - -#include #include #include #include +#include "../testing/misc_tools.h" +#include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" +#include "../toxcore/tox.h" +#include "check_compat.h" + struct test_data { uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t status_message[TOX_MAX_STATUS_MESSAGE_LENGTH]; @@ -124,8 +124,10 @@ int main(void) tox_friend_get_name(tox_to_compare, 0, to_compare.name, nullptr); tox_friend_get_status_message(tox_to_compare, 0, to_compare.status_message, nullptr); - assert(memcmp(reference_name, to_compare.name, TOX_MAX_NAME_LENGTH) == 0); - assert(memcmp(reference_status, to_compare.status_message, TOX_MAX_STATUS_MESSAGE_LENGTH) == 0); + ck_assert_msg(memcmp(reference_name, to_compare.name, TOX_MAX_NAME_LENGTH) == 0, + "incorrect name: should be all zeroes"); + ck_assert_msg(memcmp(reference_status, to_compare.status_message, TOX_MAX_STATUS_MESSAGE_LENGTH) == 0, + "incorrect status message: should be all zeroes"); tox_options_free(options); tox_kill(tox1); diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c index 50facb2c4..90d0263fe 100644 --- a/auto_tests/save_load_test.c +++ b/auto_tests/save_load_test.c @@ -1,25 +1,20 @@ /* Tests that we can save and load Tox data. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) { diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 822ea64f9..d7747ebea 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -1,12 +1,13 @@ /* Tests that we can send messages to friends. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include #include +#include typedef struct State { uint32_t index; diff --git a/auto_tests/set_name_test.c b/auto_tests/set_name_test.c index 20fb0a67d..6f467bdba 100644 --- a/auto_tests/set_name_test.c +++ b/auto_tests/set_name_test.c @@ -1,25 +1,20 @@ /* Tests that we can set our name. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define NICKNAME "Gentoo" diff --git a/auto_tests/set_status_message_test.c b/auto_tests/set_status_message_test.c index fd6c35586..3ffdd3c4b 100644 --- a/auto_tests/set_status_message_test.c +++ b/auto_tests/set_status_message_test.c @@ -1,25 +1,20 @@ /* Tests that we can set our status message */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define STATUS_MESSAGE "Installing Gentoo" diff --git a/auto_tests/skeleton_test.c b/auto_tests/skeleton_test.c index 1d391a864..30fb185c5 100644 --- a/auto_tests/skeleton_test.c +++ b/auto_tests/skeleton_test.c @@ -2,12 +2,10 @@ #include "config.h" #endif -#include "check_compat.h" - #include -#include -#include "helpers.h" +#include "../testing/misc_tools.h" +#include "check_compat.h" /* #include "../" @@ -33,8 +31,6 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); - Suite *creativesuitenamegoeshere = creativesuitenamegoeshere_suite(); SRunner *test_runner = srunner_create(creativesuitenamegoeshere); diff --git a/auto_tests/tcp_relay_test.c b/auto_tests/tcp_relay_test.c index d0c709343..54a28e4ae 100644 --- a/auto_tests/tcp_relay_test.c +++ b/auto_tests/tcp_relay_test.c @@ -1,8 +1,11 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#ifdef HAVE_CONFIG_H +#include "config.h" #endif -#include "helpers.h" +#include + +#include "../testing/misc_tools.h" +#include "check_compat.h" static uint8_t const key[] = { 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, @@ -33,8 +36,10 @@ int main(void) c_sleep(ITERATION_INTERVAL); } - assert(tox_self_get_connection_status(tox_tcp) == TOX_CONNECTION_TCP); - printf("Connection (TCP): %d\n", tox_self_get_connection_status(tox_tcp)); + const TOX_CONNECTION status = tox_self_get_connection_status(tox_tcp); + ck_assert_msg(status == TOX_CONNECTION_TCP, + "expected TCP connection, but got %d", status); + printf("Connection (TCP): %d\n", status); tox_kill(tox_tcp); return 0; diff --git a/auto_tests/tox_many_tcp_test.c b/auto_tests/tox_many_tcp_test.c index 34b01d160..7ce2c9a53 100644 --- a/auto_tests/tox_many_tcp_test.c +++ b/auto_tests/tox_many_tcp_test.c @@ -1,25 +1,20 @@ /* Auto Tests: Many TCP. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" /* The Travis-CI container responds poorly to ::1 as a localhost address * You're encouraged to -D FORCE_TESTS_IPV6 on a local test */ @@ -255,8 +250,6 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); - Suite *tox = tox_suite(); SRunner *test_runner = srunner_create(tox); diff --git a/auto_tests/tox_many_test.c b/auto_tests/tox_many_test.c index 4318b29b4..ff217e870 100644 --- a/auto_tests/tox_many_test.c +++ b/auto_tests/tox_many_test.c @@ -1,25 +1,20 @@ /* Auto Tests: Many clients. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include +#include #include +#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) { diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c index 48e2f7918..69ec4461c 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c @@ -5,17 +5,15 @@ #include "config.h" #endif -#include "check_compat.h" - #include -#include +#include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" static void set_random_name_and_status_message(Tox *tox, uint8_t *name, uint8_t *status_message) { @@ -146,8 +144,6 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); - Suite *tox = tox_suite(); SRunner *test_runner = srunner_create(tox); diff --git a/auto_tests/tox_strncasecmp_test.c b/auto_tests/tox_strncasecmp_test.c index 377ec2e30..7209facff 100644 --- a/auto_tests/tox_strncasecmp_test.c +++ b/auto_tests/tox_strncasecmp_test.c @@ -2,17 +2,11 @@ #include "config.h" #endif -#include "check_compat.h" - -#include #include #include -#include -#include -#include "helpers.h" - -#include "../testing/misc_tools.c" +#include "../testing/misc_tools.h" +#include "check_compat.h" typedef enum { NEGATIVE, @@ -181,8 +175,6 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - srand((unsigned int) time(nullptr)); - Suite *s = tox_strncasecmp_suite(); SRunner *test_runner = srunner_create(s); diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c index b2b922555..859c7a0c5 100644 --- a/auto_tests/toxav_basic_test.c +++ b/auto_tests/toxav_basic_test.c @@ -1,29 +1,21 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - -#include #include #include #include -#include #include #include +#include "../testing/misc_tools.h" #include "../toxav/toxav.h" #include "../toxcore/crypto_core.h" #include "../toxcore/logger.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" #define TEST_REGULAR_AV 1 #define TEST_REGULAR_A 1 diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c index 941834215..ed005e2fb 100644 --- a/auto_tests/toxav_many_test.c +++ b/auto_tests/toxav_many_test.c @@ -1,13 +1,7 @@ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include #include @@ -15,19 +9,19 @@ #include #include +#if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) +#include +#endif + #include +#include "../testing/misc_tools.h" #include "../toxav/toxav.h" #include "../toxcore/crypto_core.h" #include "../toxcore/logger.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" - -#if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) -#include -#endif +#include "check_compat.h" typedef struct { bool incoming; diff --git a/auto_tests/typing_test.c b/auto_tests/typing_test.c index 1262fa3df..041e0b25b 100644 --- a/auto_tests/typing_test.c +++ b/auto_tests/typing_test.c @@ -1,25 +1,19 @@ /* Tests that our typing notifications work. */ -#ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "check_compat.h" - #include #include #include +#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" - -#include "helpers.h" +#include "check_compat.h" static void typing_callback(Tox *m, uint32_t friendnumber, bool typing, void *userdata) { diff --git a/auto_tests/version_test.c b/auto_tests/version_test.c index 7509397ad..aa59fdd84 100644 --- a/auto_tests/version_test.c +++ b/auto_tests/version_test.c @@ -1,9 +1,10 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "../toxcore/tox.h" #include "check_compat.h" -#include -#include - #define check(major, minor, patch, expected) \ do_check(TOX_VERSION_MAJOR, TOX_VERSION_MINOR, TOX_VERSION_PATCH, \ major, minor, patch, \ diff --git a/build/Makefile.am b/build/Makefile.am index c5c170a33..d9d2911ec 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -1,5 +1,6 @@ bin_PROGRAMS = noinst_PROGRAMS = +noinst_LTLIBRARIES = lib_LTLIBRARIES = noinst_bindir = $(top_builddir)/build EXTRA_DIST= diff --git a/other/BUILD.bazel b/other/BUILD.bazel index beec23973..8bcdc4b4b 100644 --- a/other/BUILD.bazel +++ b/other/BUILD.bazel @@ -5,6 +5,7 @@ cc_library( hdrs = ["monolith.h"], visibility = ["//c-toxcore/auto_tests:__pkg__"], deps = [ + "//c-toxcore/testing:monolith", "//c-toxcore/toxav:monolith", "//c-toxcore/toxcore:monolith", "//c-toxcore/toxencryptsave:monolith", diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index b71ab0b19..347e5c825 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -23,11 +23,9 @@ * You should have received a copy of the GNU General Public License * along with Tox. If not, see . */ -#define _XOPEN_SOURCE 600 - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include +#include +#include #include "../toxcore/DHT.h" #include "../toxcore/LAN_discovery.h" @@ -43,7 +41,7 @@ #include "../toxcore/TCP_server.h" #endif -#include "../testing/misc_tools.c" +#include "../testing/misc_tools.h" #ifdef DHT_NODE_EXTRA_PACKETS #include "./bootstrap_node_packets.h" diff --git a/other/Makefile.inc b/other/Makefile.inc index 10cd48b49..6d4ec593a 100644 --- a/other/Makefile.inc +++ b/other/Makefile.inc @@ -14,6 +14,7 @@ DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \ DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \ $(NACL_LDFLAGS) \ + libmisc_tools.la \ libtoxcore.la \ $(LIBSODIUM_LIBS) \ $(NACL_OBJECTS) \ diff --git a/other/fun/cracker.c b/other/fun/cracker.c index 9cfe96d73..502a8af0b 100644 --- a/other/fun/cracker.c +++ b/other/fun/cracker.c @@ -9,15 +9,16 @@ * Will try to find a public key starting with: ABCDEF */ -#include "../../testing/misc_tools.c" -#include +#include +#include +#include -/* NaCl includes*/ +/* Sodium includes*/ #include #include -/* Sodium include*/ -//#include +#include "../../testing/misc_tools.h" +#include "../../toxcore/ccompat.h" void print_key(uint8_t *client_id) { diff --git a/other/fun/sign.c b/other/fun/sign.c index 369bc8484..007d19a01 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c @@ -15,10 +15,12 @@ * * NOTE: The signature is appended to the end of the file. */ -#include "../../testing/misc_tools.c" // hex_string_to_bin #include #include +#include "../../testing/misc_tools.h" // hex_string_to_bin +#include "../../toxcore/ccompat.h" + int load_file(char *filename, char **result) { int size = 0; diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index 117eb69d0..4d0674b4c 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -2,11 +2,10 @@ load("//tools:no_undefined.bzl", "cc_library") cc_library( name = "misc_tools", - hdrs = [ - "misc_tools.c", - ], + srcs = ["misc_tools.c"], + hdrs = ["misc_tools.h"], visibility = ["//c-toxcore:__subpackages__"], - deps = ["//c-toxcore/toxcore:ccompat"], + deps = ["//c-toxcore/toxcore"], ) cc_binary( @@ -53,3 +52,13 @@ cc_binary( "//c-toxcore/toxcore", ], ) + +cc_library( + name = "monolith", + hdrs = [ + "misc_tools.c", + "misc_tools.h", + ], + visibility = ["//c-toxcore/other:__pkg__"], + deps = ["//c-toxcore/toxcore"], +) diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 336e7e183..c242953a3 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -30,21 +30,18 @@ */ #define _XOPEN_SOURCE 600 -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -//#include "../core/network.h" -#include "../toxcore/DHT.h" -#include "../toxcore/friend_requests.h" -#include "misc_tools.c" - +#include +#include #include #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) #include #endif +#include "../toxcore/DHT.h" +#include "../toxcore/friend_requests.h" +#include "misc_tools.h" + #define PORT 33445 static uint8_t zeroes_cid[CRYPTO_PUBLIC_KEY_SIZE]; diff --git a/testing/Makefile.inc b/testing/Makefile.inc index f4ed568cd..fa1c10294 100644 --- a/testing/Makefile.inc +++ b/testing/Makefile.inc @@ -1,3 +1,13 @@ +if BUILD_TESTS +TEST_LIB = misc_tools +endif +if BUILD_TESTING +TEST_LIB = misc_tools +endif + +noinst_LTLIBRARIES += libmisc_tools.la +libmisc_tools_la_SOURCES = ../testing/misc_tools.c ../testing/misc_tools.h + if BUILD_TESTING noinst_PROGRAMS += DHT_test \ @@ -10,6 +20,7 @@ DHT_test_CFLAGS = $(LIBSODIUM_CFLAGS) \ DHT_test_LDADD = $(LIBSODIUM_LDFLAGS) \ $(NACL_LDFLAGS) \ + libmisc_tools.la \ libtoxcore.la \ $(LIBSODIUM_LIBS) \ $(NACL_OBJECTS) \ @@ -25,13 +36,11 @@ Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS) \ Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \ $(NACL_LDFLAGS) \ + libmisc_tools.la \ libtoxcore.la \ $(LIBSODIUM_LIBS) \ $(NACL_OBJECTS) \ $(NACL_LIBS) \ $(WINSOCK2_LIBS) - -EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c - endif diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 7885779b4..a3380a095 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -38,20 +38,17 @@ * You should have received a copy of the GNU General Public License * along with Tox. If not, see . */ -#define _XOPEN_SOURCE 600 - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../toxcore/Messenger.h" -#include "misc_tools.c" +#include +#include +#include #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) #include - #endif +#include "../toxcore/Messenger.h" +#include "misc_tools.h" + static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length, void *userdata) { diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 9b0d99564..8683830d9 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -21,35 +21,40 @@ * You should have received a copy of the GNU General Public License * along with Tox. If not, see . */ -#ifndef MISC_TOOLS_C -#define MISC_TOOLS_C - -#ifdef HAVE_CONFIG_H -#include "config.h" +#ifndef _POSIX_C_SOURCE +// For nanosleep(). +#define _POSIX_C_SOURCE 199309L #endif +#include "misc_tools.h" + +#include #include #include #include #include #include -//Sleep function (x = milliseconds) -#ifndef c_sleep -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) #include -#define c_sleep(x) Sleep(x) #else -#include -#define c_sleep(x) usleep(1000*(x)) -#endif +#include #endif #include "../toxcore/ccompat.h" +#include "../toxcore/tox.h" -uint8_t *hex_string_to_bin(const char *hex_string); -int tox_strncasecmp(const char *s1, const char *s2, size_t n); -int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); +void c_sleep(uint32_t x) +{ +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + Sleep(x); +#else + struct timespec req; + req.tv_sec = x / 1000; + req.tv_nsec = (long)x % 1000 * 1000 * 1000; + nanosleep(&req, nullptr); +#endif +} // You are responsible for freeing the return value! uint8_t *hex_string_to_bin(const char *hex_string) @@ -123,4 +128,64 @@ int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) return argvoffset; } -#endif // MISC_TOOLS_C +static const char *tox_log_level_name(TOX_LOG_LEVEL level) +{ + switch (level) { + case TOX_LOG_LEVEL_TRACE: + return "TRACE"; + + case TOX_LOG_LEVEL_DEBUG: + return "DEBUG"; + + case TOX_LOG_LEVEL_INFO: + return "INFO"; + + case TOX_LOG_LEVEL_WARNING: + return "WARNING"; + + case TOX_LOG_LEVEL_ERROR: + return "ERROR"; + } + + return ""; +} + +void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, + const char *message, void *user_data) +{ + if (level == TOX_LOG_LEVEL_TRACE) { + return; + } + + uint32_t index = user_data ? *(uint32_t *)user_data : 0; + fprintf(stderr, "[#%u] %s %s:%u\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message); +} + +Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery) +{ + struct Tox_Options *log_options = options; + + if (log_options == nullptr) { + log_options = tox_options_new(nullptr); + } + + assert(log_options != nullptr); + + tox_options_set_local_discovery_enabled(log_options, lan_discovery); + tox_options_set_start_port(log_options, 33445); + tox_options_set_end_port(log_options, 33445 + 2000); + tox_options_set_log_callback(log_options, &print_debug_log); + tox_options_set_log_user_data(log_options, log_user_data); + Tox *tox = tox_new(log_options, err); + + if (options == nullptr) { + tox_options_free(log_options); + } + + return tox; +} + +Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data) +{ + return tox_new_log_lan(options, err, log_user_data, false); +} diff --git a/testing/misc_tools.h b/testing/misc_tools.h new file mode 100644 index 000000000..deaa177d0 --- /dev/null +++ b/testing/misc_tools.h @@ -0,0 +1,29 @@ +#ifndef C_TOXCORE_TESTING_MISC_TOOLS_H +#define C_TOXCORE_TESTING_MISC_TOOLS_H + +#include "../toxcore/tox.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Amount of time in milliseconds to wait between tox_iterate calls. +#define ITERATION_INTERVAL 200 + +void c_sleep(uint32_t x); + +uint8_t *hex_string_to_bin(const char *hex_string); +int tox_strncasecmp(const char *s1, const char *s2, size_t n); +int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); + +void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, + const char *message, void *user_data); + +Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data); +Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/testing/random_testing.cc b/testing/random_testing.cc index f41756751..bcdb8e70e 100644 --- a/testing/random_testing.cc +++ b/testing/random_testing.cc @@ -11,7 +11,7 @@ #include #include "../toxcore/tox.h" -#include "misc_tools.c" +#include "misc_tools.h" namespace { @@ -32,8 +32,6 @@ constexpr uint32_t MAX_ACTIONS = 10000; constexpr uint32_t MAX_ACTION_ATTEMPTS = 100; // Number of tox_iterate calls between each action. constexpr uint32_t ITERATIONS_PER_ACTION = 1; -// Amount of time in milliseconds to wait between tox_iterate calls. -constexpr uint32_t ITERATION_INTERVAL = 5; struct Tox_Options_Deleter { void operator()(Tox_Options *options) const { tox_options_free(options); }