summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-16 22:46:02 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-21 20:44:26 +0000
commit7245ac11ef9be2420c8356c12acc79f93ea211bb (patch)
treee971c5c6e10c2310afe4b2cd80212feac9839f2a /testing
parent7c2b95ef5e4ccdae01bd104aa7400294c9ea391b (diff)
Avoid implementations in .h files or #including .c files.
Also, avoid the need for putting `_XOPEN_SOURCE` in every test file.
Diffstat (limited to 'testing')
-rw-r--r--testing/BUILD.bazel17
-rw-r--r--testing/DHT_test.c15
-rw-r--r--testing/Makefile.inc15
-rw-r--r--testing/Messenger_test.c15
-rw-r--r--testing/misc_tools.c97
-rw-r--r--testing/misc_tools.h29
-rw-r--r--testing/random_testing.cc4
7 files changed, 148 insertions, 44 deletions
diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel
index 117eb69d..4d0674b4 100644
--- a/testing/BUILD.bazel
+++ b/testing/BUILD.bazel
@@ -2,11 +2,10 @@ load("//tools:no_undefined.bzl", "cc_library")
2 2
3cc_library( 3cc_library(
4 name = "misc_tools", 4 name = "misc_tools",
5 hdrs = [ 5 srcs = ["misc_tools.c"],
6 "misc_tools.c", 6 hdrs = ["misc_tools.h"],
7 ],
8 visibility = ["//c-toxcore:__subpackages__"], 7 visibility = ["//c-toxcore:__subpackages__"],
9 deps = ["//c-toxcore/toxcore:ccompat"], 8 deps = ["//c-toxcore/toxcore"],
10) 9)
11 10
12cc_binary( 11cc_binary(
@@ -53,3 +52,13 @@ cc_binary(
53 "//c-toxcore/toxcore", 52 "//c-toxcore/toxcore",
54 ], 53 ],
55) 54)
55
56cc_library(
57 name = "monolith",
58 hdrs = [
59 "misc_tools.c",
60 "misc_tools.h",
61 ],
62 visibility = ["//c-toxcore/other:__pkg__"],
63 deps = ["//c-toxcore/toxcore"],
64)
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 336e7e18..c242953a 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -30,21 +30,18 @@
30 */ 30 */
31#define _XOPEN_SOURCE 600 31#define _XOPEN_SOURCE 600
32 32
33#ifdef HAVE_CONFIG_H 33#include <stdlib.h>
34#include "config.h" 34#include <stdio.h>
35#endif
36
37//#include "../core/network.h"
38#include "../toxcore/DHT.h"
39#include "../toxcore/friend_requests.h"
40#include "misc_tools.c"
41
42#include <string.h> 35#include <string.h>
43 36
44#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) 37#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
45#include <arpa/inet.h> 38#include <arpa/inet.h>
46#endif 39#endif
47 40
41#include "../toxcore/DHT.h"
42#include "../toxcore/friend_requests.h"
43#include "misc_tools.h"
44
48#define PORT 33445 45#define PORT 33445
49 46
50static uint8_t zeroes_cid[CRYPTO_PUBLIC_KEY_SIZE]; 47static uint8_t zeroes_cid[CRYPTO_PUBLIC_KEY_SIZE];
diff --git a/testing/Makefile.inc b/testing/Makefile.inc
index f4ed568c..fa1c1029 100644
--- a/testing/Makefile.inc
+++ b/testing/Makefile.inc
@@ -1,3 +1,13 @@
1if BUILD_TESTS
2TEST_LIB = misc_tools
3endif
4if BUILD_TESTING
5TEST_LIB = misc_tools
6endif
7
8noinst_LTLIBRARIES += libmisc_tools.la
9libmisc_tools_la_SOURCES = ../testing/misc_tools.c ../testing/misc_tools.h
10
1if BUILD_TESTING 11if BUILD_TESTING
2 12
3noinst_PROGRAMS += DHT_test \ 13noinst_PROGRAMS += DHT_test \
@@ -10,6 +20,7 @@ DHT_test_CFLAGS = $(LIBSODIUM_CFLAGS) \
10 20
11DHT_test_LDADD = $(LIBSODIUM_LDFLAGS) \ 21DHT_test_LDADD = $(LIBSODIUM_LDFLAGS) \
12 $(NACL_LDFLAGS) \ 22 $(NACL_LDFLAGS) \
23 libmisc_tools.la \
13 libtoxcore.la \ 24 libtoxcore.la \
14 $(LIBSODIUM_LIBS) \ 25 $(LIBSODIUM_LIBS) \
15 $(NACL_OBJECTS) \ 26 $(NACL_OBJECTS) \
@@ -25,13 +36,11 @@ Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS) \
25 36
26Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \ 37Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \
27 $(NACL_LDFLAGS) \ 38 $(NACL_LDFLAGS) \
39 libmisc_tools.la \
28 libtoxcore.la \ 40 libtoxcore.la \
29 $(LIBSODIUM_LIBS) \ 41 $(LIBSODIUM_LIBS) \
30 $(NACL_OBJECTS) \ 42 $(NACL_OBJECTS) \
31 $(NACL_LIBS) \ 43 $(NACL_LIBS) \
32 $(WINSOCK2_LIBS) 44 $(WINSOCK2_LIBS)
33 45
34
35EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c
36
37endif 46endif
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index 7885779b..a3380a09 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -38,20 +38,17 @@
38 * You should have received a copy of the GNU General Public License 38 * You should have received a copy of the GNU General Public License
39 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 39 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
40 */ 40 */
41#define _XOPEN_SOURCE 600 41#include <stdio.h>
42 42#include <stdlib.h>
43#ifdef HAVE_CONFIG_H 43#include <string.h>
44#include "config.h"
45#endif
46
47#include "../toxcore/Messenger.h"
48#include "misc_tools.c"
49 44
50#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) 45#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
51#include <arpa/inet.h> 46#include <arpa/inet.h>
52
53#endif 47#endif
54 48
49#include "../toxcore/Messenger.h"
50#include "misc_tools.h"
51
55static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length, 52static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length,
56 void *userdata) 53 void *userdata)
57{ 54{
diff --git a/testing/misc_tools.c b/testing/misc_tools.c
index 9b0d9956..8683830d 100644
--- a/testing/misc_tools.c
+++ b/testing/misc_tools.c
@@ -21,35 +21,40 @@
21 * You should have received a copy of the GNU General Public License 21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>. 22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 */ 23 */
24#ifndef MISC_TOOLS_C 24#ifndef _POSIX_C_SOURCE
25#define MISC_TOOLS_C 25// For nanosleep().
26 26#define _POSIX_C_SOURCE 199309L
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif 27#endif
30 28
29#include "misc_tools.h"
30
31#include <assert.h>
31#include <ctype.h> 32#include <ctype.h>
32#include <stdint.h> 33#include <stdint.h>
33#include <stdio.h> 34#include <stdio.h>
34#include <stdlib.h> 35#include <stdlib.h>
35#include <string.h> 36#include <string.h>
36 37
37//Sleep function (x = milliseconds) 38#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
38#ifndef c_sleep
39#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
40#include <windows.h> 39#include <windows.h>
41#define c_sleep(x) Sleep(x)
42#else 40#else
43#include <unistd.h> 41#include <time.h>
44#define c_sleep(x) usleep(1000*(x))
45#endif
46#endif 42#endif
47 43
48#include "../toxcore/ccompat.h" 44#include "../toxcore/ccompat.h"
45#include "../toxcore/tox.h"
49 46
50uint8_t *hex_string_to_bin(const char *hex_string); 47void c_sleep(uint32_t x)
51int tox_strncasecmp(const char *s1, const char *s2, size_t n); 48{
52int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); 49#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
50 Sleep(x);
51#else
52 struct timespec req;
53 req.tv_sec = x / 1000;
54 req.tv_nsec = (long)x % 1000 * 1000 * 1000;
55 nanosleep(&req, nullptr);
56#endif
57}
53 58
54// You are responsible for freeing the return value! 59// You are responsible for freeing the return value!
55uint8_t *hex_string_to_bin(const char *hex_string) 60uint8_t *hex_string_to_bin(const char *hex_string)
@@ -123,4 +128,64 @@ int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)
123 return argvoffset; 128 return argvoffset;
124} 129}
125 130
126#endif // MISC_TOOLS_C 131static const char *tox_log_level_name(TOX_LOG_LEVEL level)
132{
133 switch (level) {
134 case TOX_LOG_LEVEL_TRACE:
135 return "TRACE";
136
137 case TOX_LOG_LEVEL_DEBUG:
138 return "DEBUG";
139
140 case TOX_LOG_LEVEL_INFO:
141 return "INFO";
142
143 case TOX_LOG_LEVEL_WARNING:
144 return "WARNING";
145
146 case TOX_LOG_LEVEL_ERROR:
147 return "ERROR";
148 }
149
150 return "<unknown>";
151}
152
153void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func,
154 const char *message, void *user_data)
155{
156 if (level == TOX_LOG_LEVEL_TRACE) {
157 return;
158 }
159
160 uint32_t index = user_data ? *(uint32_t *)user_data : 0;
161 fprintf(stderr, "[#%u] %s %s:%u\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
162}
163
164Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery)
165{
166 struct Tox_Options *log_options = options;
167
168 if (log_options == nullptr) {
169 log_options = tox_options_new(nullptr);
170 }
171
172 assert(log_options != nullptr);
173
174 tox_options_set_local_discovery_enabled(log_options, lan_discovery);
175 tox_options_set_start_port(log_options, 33445);
176 tox_options_set_end_port(log_options, 33445 + 2000);
177 tox_options_set_log_callback(log_options, &print_debug_log);
178 tox_options_set_log_user_data(log_options, log_user_data);
179 Tox *tox = tox_new(log_options, err);
180
181 if (options == nullptr) {
182 tox_options_free(log_options);
183 }
184
185 return tox;
186}
187
188Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data)
189{
190 return tox_new_log_lan(options, err, log_user_data, false);
191}
diff --git a/testing/misc_tools.h b/testing/misc_tools.h
new file mode 100644
index 00000000..deaa177d
--- /dev/null
+++ b/testing/misc_tools.h
@@ -0,0 +1,29 @@
1#ifndef C_TOXCORE_TESTING_MISC_TOOLS_H
2#define C_TOXCORE_TESTING_MISC_TOOLS_H
3
4#include "../toxcore/tox.h"
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10// Amount of time in milliseconds to wait between tox_iterate calls.
11#define ITERATION_INTERVAL 200
12
13void c_sleep(uint32_t x);
14
15uint8_t *hex_string_to_bin(const char *hex_string);
16int tox_strncasecmp(const char *s1, const char *s2, size_t n);
17int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled);
18
19void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func,
20 const char *message, void *user_data);
21
22Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data);
23Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery);
24
25#ifdef __cplusplus
26}
27#endif
28
29#endif
diff --git a/testing/random_testing.cc b/testing/random_testing.cc
index f4175675..bcdb8e70 100644
--- a/testing/random_testing.cc
+++ b/testing/random_testing.cc
@@ -11,7 +11,7 @@
11#include <vector> 11#include <vector>
12 12
13#include "../toxcore/tox.h" 13#include "../toxcore/tox.h"
14#include "misc_tools.c" 14#include "misc_tools.h"
15 15
16namespace { 16namespace {
17 17
@@ -32,8 +32,6 @@ constexpr uint32_t MAX_ACTIONS = 10000;
32constexpr uint32_t MAX_ACTION_ATTEMPTS = 100; 32constexpr uint32_t MAX_ACTION_ATTEMPTS = 100;
33// Number of tox_iterate calls between each action. 33// Number of tox_iterate calls between each action.
34constexpr uint32_t ITERATIONS_PER_ACTION = 1; 34constexpr uint32_t ITERATIONS_PER_ACTION = 1;
35// Amount of time in milliseconds to wait between tox_iterate calls.
36constexpr uint32_t ITERATION_INTERVAL = 5;
37 35
38struct Tox_Options_Deleter { 36struct Tox_Options_Deleter {
39 void operator()(Tox_Options *options) const { tox_options_free(options); } 37 void operator()(Tox_Options *options) const { tox_options_free(options); }