From 03b5746e4ba53a1538eb3367b01560f9d70e45b8 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Sun, 21 Feb 2021 21:02:57 +0100 Subject: TNG: Add a the api test; WIP --- src/transport/Makefile.am | 18 +- src/transport/gnunet-service-tng.c | 3 +- src/transport/test_transport_api2.c | 126 ++++++ src/transport/transport-testing-filenames2.c | 202 +++++++++ src/transport/transport-testing-loggers2.c | 80 ++++ src/transport/transport-testing-main2.c | 613 +++++++++++++++++++++++++++ src/transport/transport-testing-send2.c | 240 +++++++++++ src/transport/transport-testing2.c | 6 +- 8 files changed, 1283 insertions(+), 5 deletions(-) create mode 100644 src/transport/test_transport_api2.c create mode 100644 src/transport/transport-testing-filenames2.c create mode 100644 src/transport/transport-testing-loggers2.c create mode 100644 src/transport/transport-testing-main2.c create mode 100644 src/transport/transport-testing-send2.c (limited to 'src') diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 0251b001e..67b35365d 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am @@ -124,9 +124,7 @@ endif noinst_PROGRAMS = \ gnunet-transport-profiler \ - gnunet-communicator-tcp \ gnunet-communicator-udp \ - gnunet-service-tng \ $(WLAN_BIN_SENDER) \ $(WLAN_BIN_RECEIVER) @@ -170,6 +168,10 @@ libgnunettransporttesting_la_LDFLAGS = \ libgnunettransporttesting2_la_SOURCES = \ transport-testing2.c transport-testing2.h \ + transport-testing-filenames2.c \ + transport-testing-loggers2.c \ + transport-testing-main2.c \ + transport-testing-send2.c \ transport-testing-communicator.c transport-testing-communicator.h libgnunettransporttesting2_la_LIBADD = \ libgnunettransport.la \ @@ -253,6 +255,7 @@ libexec_PROGRAMS = \ $(WLAN_BIN_DUMMY) \ $(BT_BIN) \ gnunet-service-transport \ + gnunet-service-tng \ gnunet-communicator-unix \ gnunet-communicator-udp \ gnunet-communicator-tcp @@ -554,6 +557,7 @@ check_PROGRAMS = \ test_transport_api_blacklisting_tcp \ test_transport_api_disconnect_tcp \ test_transport_api_tcp \ + test_transport_api2_tcp \ test_transport_api_restart_1peer \ test_transport_api_restart_2peers \ test_transport_api_timeout_tcp \ @@ -949,6 +953,14 @@ test_transport_api_tcp_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la \ libgnunettransporttesting.la +test_transport_api2_tcp_SOURCES = \ + test_transport_api.c +test_transport_api2_tcp_LDADD = \ + libgnunettransport.la \ + $(top_builddir)/src/hello/libgnunethello.la \ + $(top_builddir)/src/util/libgnunetutil.la \ + libgnunettransporttesting2.la + test_transport_api_restart_1peer_SOURCES = \ test_transport_api_restart_reconnect.c test_transport_api_restart_1peer_LDADD = \ @@ -1503,6 +1515,8 @@ test_transport_api_tcp_nat_peer1.conf\ test_transport_api_tcp_nat_peer2.conf\ test_transport_api_tcp_peer1.conf\ test_transport_api_tcp_peer2.conf\ +test_transport_api2_tcp_peer1.conf\ +test_transport_api2_tcp_peer2.conf\ test_transport_api_udp_nat_peer1.conf\ test_transport_api_udp_nat_peer2.conf\ test_transport_api_udp_peer1.conf\ diff --git a/src/transport/gnunet-service-tng.c b/src/transport/gnunet-service-tng.c index bd9acd0cf..9c37f15e2 100644 --- a/src/transport/gnunet-service-tng.c +++ b/src/transport/gnunet-service-tng.c @@ -9505,7 +9505,8 @@ handle_add_queue_message (void *cls, const char *addr; uint16_t addr_len; - if (ntohl (aqm->mtu) <= sizeof(struct TransportFragmentBoxMessage)) + if ((0 != ntohl (aqm->mtu)) && + (ntohl (aqm->mtu) <= sizeof(struct TransportFragmentBoxMessage))) { /* MTU so small as to be useless for transmissions, required for #fragment_message()! */ diff --git a/src/transport/test_transport_api2.c b/src/transport/test_transport_api2.c new file mode 100644 index 000000000..e1606e0be --- /dev/null +++ b/src/transport/test_transport_api2.c @@ -0,0 +1,126 @@ +/* + This file is part of GNUnet. + Copyright (C) 2009, 2010, 2016 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/** + * @file transport/test_transport_api.c + * @brief base test case for transport implementations + * @author Christian Grothoff + * + * This test case serves as a base for tcp, udp, and udp-nat + * transport test cases. Based on the executable being run + * the correct test case will be performed. Conservation of + * C code apparently. + */ +#include "platform.h" +//#include "gnunet_transport_service.h" +#include "transport-testing2.h" + +/** + * How long until we give up on transmitting the message? + */ +#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) + +static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; + + +static void +notify_receive (void *cls, + struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, + const struct GNUNET_PeerIdentity *sender, + const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) +{ + { + char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id)); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n", + receiver->no, + ps, + ntohs (message->header.type), + ntohs (message->header.size), + GNUNET_i2s (sender)); + GNUNET_free (ps); + } + + if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) && + (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE == ntohs ( + message->header.size))) + { + ccc->global_ret = GNUNET_OK; + GNUNET_SCHEDULER_shutdown (); + } + else + { + GNUNET_break (0); + ccc->global_ret = GNUNET_SYSERR; + GNUNET_SCHEDULER_shutdown (); + } +} + + +/** + * Runs the test. + * + * @param argv the argv argument from main() + * @param bi_directional should we try to establish connections + * in both directions simultaneously? + */ +static int +test (char *argv[], + int bi_directional) +{ + struct GNUNET_TRANSPORT_TESTING_SendClosure sc = { + .num_messages = 1 + }; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = { + .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send, + .connect_continuation_cls = &sc, + .config_file = "test_transport_api_data.conf", + .rec = ¬ify_receive, + .nc = &GNUNET_TRANSPORT_TESTING_log_connect, + .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect, + .timeout = TIMEOUT, + .bi_directional = bi_directional + }; + + ccc = &my_ccc; + sc.ccc = ccc; + if (GNUNET_OK != + GNUNET_TRANSPORT_TESTING_main (2, + &GNUNET_TRANSPORT_TESTING_connect_check, + ccc)) + return 1; + return 0; +} + + +int +main (int argc, + char *argv[]) +{ + if ((0 != test (argv, + GNUNET_NO)) || + (0 != test (argv, + GNUNET_YES))) + return 1; + return 0; +} + + +/* end of test_transport_api.c */ diff --git a/src/transport/transport-testing-filenames2.c b/src/transport/transport-testing-filenames2.c new file mode 100644 index 000000000..7d6fd529d --- /dev/null +++ b/src/transport/transport-testing-filenames2.c @@ -0,0 +1,202 @@ +/* + This file is part of GNUnet. + Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/** + * @file transport-testing-filenames.c + * @brief convenience string manipulation functions for tests + * @author Matthias Wachs + * @author Christian Grothoff + */ +#include "transport-testing2.h" + + +/** + * Removes all directory separators from absolute filename + * + * @param file the absolute file name, e.g. as found in argv[0] + * @return extracted file name, has to be freed by caller + */ +static char * +extract_filename (const char *file) +{ + char *pch = GNUNET_strdup (file); + char *backup = pch; + char *filename = NULL; + char *res; + + if (NULL != strstr (pch, "/")) + { + pch = strtok (pch, "/"); + while (pch != NULL) + { + pch = strtok (NULL, "/"); + if (pch != NULL) + { + filename = pch; + } + } + } + else + filename = pch; + + res = GNUNET_strdup (filename); + GNUNET_free (backup); + return res; +} + + +/** + * Extracts the test filename from an absolute file name and removes + * the extension + * + * @param file absolute file name + * @return the result + */ +char * +GNUNET_TRANSPORT_TESTING_get_test_name (const char *file) +{ + char *backup = extract_filename (file); + char *filename = backup; + char *dotexe; + char *ret; + + if (NULL == filename) + return NULL; + + /* remove "lt-" */ + filename = strstr (filename, "test"); + if (NULL == filename) + { + GNUNET_free (backup); + return NULL; + } + + /* remove ".exe" */ + if (NULL != (dotexe = strstr (filename, ".exe"))) + dotexe[0] = '\0'; + ret = GNUNET_strdup (filename); + GNUNET_free (backup); + return ret; +} + + +/** + * Extracts the filename from an absolute file name and removes the extension + * + * @param file absolute file name + * @return the result + */ +char * +GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file) +{ + char *src = extract_filename (file); + char *split; + + split = strstr (src, "."); + if (NULL != split) + split[0] = '\0'; + return src; +} + + +/** + * Extracts the plugin name from an absolute file name and the test name + * + * @param file absolute file name + * @param test test name + * @return the result + */ +char * +GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file, + const char *test) +{ + char *filename; + char *dotexe; + char *e = extract_filename (file); + char *t = extract_filename (test); + char *ret; + + if (NULL == e) + goto fail; + /* remove "lt-" */ + filename = strstr (e, "tes"); + if (NULL == filename) + goto fail; + /* remove ".exe" */ + if (NULL != (dotexe = strstr (filename, ".exe"))) + dotexe[0] = '\0'; + + /* find last _ */ + filename = strstr (filename, t); + if (NULL == filename) + goto fail; + /* copy plugin */ + filename += strlen (t); + if ('\0' != *filename) + filename++; + ret = GNUNET_strdup (filename); + goto suc; +fail: + ret = NULL; +suc: + GNUNET_free (t); + GNUNET_free (e); + return ret; +} + + +/** + * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and + * if existing ".exe"-prefix and adds the peer-number + * + * @param file filename of the test, e.g. argv[0] + * @param count peer number + * @return the result + */ +char * +GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, + int count) +{ + char *filename = extract_filename (file); + char *backup = filename; + char *dotexe; + char *ret; + + if (NULL == filename) + return NULL; + /* remove "lt-" */ + filename = strstr (filename, "test"); + if (NULL == filename) + goto fail; + /* remove ".exe" */ + if (NULL != (dotexe = strstr (filename, ".exe"))) + dotexe[0] = '\0'; + GNUNET_asprintf (&ret, + "%s_peer%u.conf", + filename, + count); + GNUNET_free (backup); + return ret; +fail: + GNUNET_free (backup); + return NULL; +} + + +/* end of transport-testing-filenames.c */ diff --git a/src/transport/transport-testing-loggers2.c b/src/transport/transport-testing-loggers2.c new file mode 100644 index 000000000..ead4fa365 --- /dev/null +++ b/src/transport/transport-testing-loggers2.c @@ -0,0 +1,80 @@ +/* + This file is part of GNUnet. + Copyright (C) 2016 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/** + * @file transport-testing-loggers.c + * @brief convenience functions for logging common events in tests + * @author Christian Grothoff + */ +#include "transport-testing2.h" + + +/** + * Log a connect event. + * + * @param cls NULL + * @param me peer that had the event + * @param other peer that connected. + */ +void +GNUNET_TRANSPORT_TESTING_log_connect (void *cls, + struct + GNUNET_TRANSPORT_TESTING_PeerContext *me, + const struct GNUNET_PeerIdentity *other) +{ + char *ps; + + ps = GNUNET_strdup (GNUNET_i2s (&me->id)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peer %s connected to %u (%s)!\n", + GNUNET_i2s (other), + me->no, + ps); + GNUNET_free (ps); +} + + +/** + * Log a disconnect event. + * + * @param cls NULL + * @param me peer that had the event + * @param other peer that disconnected. + */ +void +GNUNET_TRANSPORT_TESTING_log_disconnect (void *cls, + struct + GNUNET_TRANSPORT_TESTING_PeerContext * + me, + const struct + GNUNET_PeerIdentity *other) +{ + char *ps; + + ps = GNUNET_strdup (GNUNET_i2s (&me->id)); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peer `%s' disconnected from %u (%s)!\n", + GNUNET_i2s (other), + me->no, + ps); + GNUNET_free (ps); +} + + +/* end of transport-testing-loggers.c */ diff --git a/src/transport/transport-testing-main2.c b/src/transport/transport-testing-main2.c new file mode 100644 index 000000000..62aa3ceb7 --- /dev/null +++ b/src/transport/transport-testing-main2.c @@ -0,0 +1,613 @@ +/* + This file is part of GNUnet. + Copyright (C) 2016 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/** + * @file transport-testing-main.c + * @brief convenience main function for tests + * @author Christian Grothoff + */ +#include "transport-testing2.h" + + +/** + * Closure for #connect_cb. + */ +struct GNUNET_TRANSPORT_TESTING_ConnectRequestList +{ + /** + * Stored in a DLL. + */ + struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *next; + + /** + * Stored in a DLL. + */ + struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *prev; + + /** + * Overall context we are in. + */ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; + + /** + * Connect request this is about. + */ + struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr; + + /** + * Peer being connected. + */ + struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; + + /** + * Peer being connected. + */ + struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; +}; + + +/** + * Shutdown function for the test. Stops all peers. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *` + */ +static void +do_shutdown (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls; + struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Testcase shutting down\n"); + if (NULL != ccc->shutdown_task) + ccc->shutdown_task (ccc->shutdown_task_cls); + if (NULL != ccc->timeout_task) + { + GNUNET_SCHEDULER_cancel (ccc->timeout_task); + ccc->timeout_task = NULL; + } + if (NULL != ccc->connect_task) + { + GNUNET_SCHEDULER_cancel (ccc->connect_task); + ccc->connect_task = NULL; + } + while (NULL != (crl = ccc->crl_head)) + { + GNUNET_CONTAINER_DLL_remove (ccc->crl_head, + ccc->crl_tail, + crl); + GNUNET_TRANSPORT_TESTING_connect_peers_cancel (crl->cr); + GNUNET_free (crl); + } + for (unsigned int i = 0; i < ccc->num_peers; i++) + { + if (NULL != ccc->p[i]) + { + GNUNET_TRANSPORT_TESTING_stop_peer (ccc->p[i]); + ccc->p[i] = NULL; + } + } +} + + +/** + * Testcase hit timeout, shut it down with error. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *` + */ +static void +do_timeout (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls; + + ccc->timeout_task = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Testcase timed out\n"); + ccc->global_ret = GNUNET_SYSERR; + GNUNET_SCHEDULER_shutdown (); +} + + +/** + * Internal data structure. Closure for + * #connect_cb, #disconnect_cb, #my_nc and #start_cb. + * Allows us to identify which peer this is about. + */ +struct GNUNET_TRANSPORT_TESTING_InternalPeerContext +{ + /** + * Overall context of the callback. + */ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; + + /** + * Offset of the peer this is about. + */ + unsigned int off; +}; + + +/** + * Information tracked per connected peer. + */ +struct ConnectPairInfo +{ + /** + * Peer this is about. + */ + const struct GNUNET_PeerIdentity *sender; + + /** + * Information about the receiving peer. + */ + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi; +}; + + +/** + * Function called when we connected two peers. Once we have gotten + * to the clique, launch test-specific logic. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *` + */ +static void +connect_cb (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl = cls; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = crl->ccc; + + GNUNET_CONTAINER_DLL_remove (ccc->crl_head, + ccc->crl_tail, + crl); + { + char *p1_c = GNUNET_strdup (GNUNET_i2s (&crl->p1->id)); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peers connected: %u (%s) <-> %u (%s)\n", + crl->p1->no, + p1_c, + crl->p2->no, + GNUNET_i2s (&crl->p2->id)); + GNUNET_free (p1_c); + GNUNET_free (crl); + } + if (NULL == ccc->crl_head) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "All connections UP, launching custom test logic.\n"); + GNUNET_SCHEDULER_add_now (ccc->connect_continuation, + ccc->connect_continuation_cls); + } +} + + +/** + * Find peer by peer ID. + * + * @param ccc context to search + * @param peer peer to look for + * @return NULL if @a peer was not found + */ +struct GNUNET_TRANSPORT_TESTING_PeerContext * +GNUNET_TRANSPORT_TESTING_find_peer (struct + GNUNET_TRANSPORT_TESTING_ConnectCheckContext + *ccc, + const struct GNUNET_PeerIdentity *peer) +{ + for (unsigned int i = 0; i < ccc->num_peers; i++) + if ((NULL != ccc->p[i]) && + (0 == memcmp (peer, + &ccc->p[i]->id, + sizeof(*peer)))) + return ccc->p[i]; + return NULL; +} + + +/** + * Wrapper around peers connecting. Calls client's nc function. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *` + * @param peer peer we got connected to + * @param mq message queue for transmissions to @a peer + * @return closure for message handlers + */ +static void * +my_nc (void *cls, + const struct GNUNET_PeerIdentity *peer, + struct GNUNET_MQ_Handle *mq) +{ + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc; + struct ConnectPairInfo *cpi; + + if (NULL != ccc->nc) + ccc->nc (ccc->cls, + ccc->p[ipi->off], + peer); + cpi = GNUNET_new (struct ConnectPairInfo); + cpi->ipi = ipi; + cpi->sender = peer; /* valid until disconnect */ + return cpi; +} + + +/** + * Wrapper around peers disconnecting. Calls client's nd function. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *` + * @param peer peer we got disconnected from + * @param custom_cls return value from @my_nc + */ +static void +my_nd (void *cls, + const struct GNUNET_PeerIdentity *peer, + void *custom_cls) +{ + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc; + struct ConnectPairInfo *cpi = custom_cls; + + if (NULL != ccc->nd) + ccc->nd (ccc->cls, + ccc->p[ipi->off], + peer); + GNUNET_free (cpi); +} + + +/** + * Wrapper around receiving data. Calls client's rec function. + * + * @param cls our `struct ConnectPairInfo *` + * @param message message we received + * @return #GNUNET_OK (all messages are fine) + */ +static int +check_test (void *cls, + const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) +{ + return GNUNET_OK; +} + + +/** + * Wrapper around receiving data. Calls client's rec function. + * + * @param cls our `struct ConnectPairInfo *` + * @param message message we received + */ +static void +handle_test (void *cls, + const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) +{ + struct ConnectPairInfo *cpi = cls; + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc; + + if (NULL != ccc->rec) + ccc->rec (ccc->cls, + ccc->p[ipi->off], + cpi->sender, + message); +} + + +/** + * Wrapper around receiving data. Calls client's rec function. + * + * @param cls our `struct ConnectPairInfo *` + * @param message message we received + * @return #GNUNET_OK (all messages are fine) + */ +static int +check_test2 (void *cls, + const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) +{ + return GNUNET_OK; +} + + +/** + * Wrapper around receiving data. Calls client's rec function. + * + * @param cls our `struct ConnectPairInfo *` + * @param message message we received + */ +static void +handle_test2 (void *cls, + const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) +{ + struct ConnectPairInfo *cpi = cls; + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cpi->ipi; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc; + + if (NULL != ccc->rec) + ccc->rec (ccc->cls, + ccc->p[ipi->off], + cpi->sender, + message); +} + + +/** + * Connect the peers as a clique. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` + */ +static void +do_connect (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls; + + ccc->connect_task = NULL; + for (unsigned int i = 0; i < ccc->num_peers; i++) + for (unsigned int j = (ccc->bi_directional ? 0 : i + 1); j < ccc->num_peers; + j++) + { + struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl; + + if (i == j) + continue; + crl = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequestList); + GNUNET_CONTAINER_DLL_insert (ccc->crl_head, + ccc->crl_tail, + crl); + crl->ccc = ccc; + crl->p1 = ccc->p[i]; + crl->p2 = ccc->p[j]; + { + char *sender_c = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id)); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", + ccc->p[0]->no, + sender_c, + ccc->p[1]->no, + GNUNET_i2s (&ccc->p[1]->id)); + GNUNET_free (sender_c); + } + crl->cr = GNUNET_TRANSPORT_TESTING_connect_peers (ccc->p[i], + ccc->p[j], + &connect_cb, + crl); + } +} + + +/** + * Function called once we have successfully launched a peer. + * Once all peers have been launched, we connect all of them + * in a clique. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *` + */ +static void +start_cb (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ipi = cls; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = ipi->ccc; + struct GNUNET_TRANSPORT_TESTING_PeerContext *p = ccc->p[ipi->off]; + + ccc->started++; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Peer %u (`%s') started\n", + p->no, + GNUNET_i2s (&p->id)); + if (ccc->started != ccc->num_peers) + return; + if (NULL != ccc->pre_connect_task) + { + /* Run the custom per-connect job, then give it a second to + go into effect before we continue connecting peers. */ + ccc->pre_connect_task (ccc->pre_connect_task_cls); + ccc->connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, + &do_connect, + ccc); + } + else + { + do_connect (ccc); + } +} + + +/** + * Function run from #GNUNET_TRANSPORT_TESTING_connect_check + * once the scheduler is up. Should launch the peers and + * then in the continuations try to connect them. + * + * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *` + * @param args ignored + * @param cfgfile ignored + * @param cfg configuration + */ +static void +connect_check_run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls; + int ok; + + ccc->cfg = cfg; + ccc->timeout_task = GNUNET_SCHEDULER_add_delayed (ccc->timeout, + &do_timeout, + ccc); + GNUNET_SCHEDULER_add_shutdown (&do_shutdown, + ccc); + ok = GNUNET_OK; + for (unsigned int i = 0; i < ccc->num_peers; i++) + { + struct GNUNET_MQ_MessageHandler handlers[] = { + GNUNET_MQ_hd_var_size (test, + GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, + struct GNUNET_TRANSPORT_TESTING_TestMessage, + NULL), + GNUNET_MQ_hd_var_size (test2, + GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2, + struct GNUNET_TRANSPORT_TESTING_TestMessage, + NULL), + GNUNET_MQ_handler_end () + }; + ccc->p[i] = GNUNET_TRANSPORT_TESTING_start_peer (ccc->tth, + ccc->cfg_files[i], + i + 1, + handlers, + &my_nc, + &my_nd, + &ccc->ip[i], + &start_cb, + &ccc->ip[i]); + if (NULL == ccc->p[i]) + ok = GNUNET_SYSERR; + } + if (GNUNET_OK != ok) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Fail! Could not start peers!\n"); + GNUNET_SCHEDULER_shutdown (); + } +} + + +/** + * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback. + * Starts and connects the two peers, then invokes the + * `connect_continuation` from @a cls. Sets up a timeout to + * abort the test, and a shutdown handler to clean up properly + * on exit. + * + * @param cls closure of type `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` + * @param tth_ initialized testing handle + * @param test_plugin_ name of the plugin + * @param test_name_ name of the test + * @param num_peers number of entries in the @a cfg_file array + * @param cfg_files array of names of configuration files for the peers + * @return #GNUNET_SYSERR on error + */ +int +GNUNET_TRANSPORT_TESTING_connect_check (void *cls, + struct GNUNET_TRANSPORT_TESTING_Handle * + tth_, + const char *test_plugin_, + const char *test_name_, + unsigned int num_peers, + char *cfg_files[]) +{ + static struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc = cls; + struct GNUNET_TRANSPORT_TESTING_PeerContext *p[num_peers]; + struct GNUNET_TRANSPORT_TESTING_InternalPeerContext ip[num_peers]; + char *argv[] = { + (char *) test_name_, + "-c", + (char *) ccc->config_file, + NULL + }; + + ccc->num_peers = num_peers; + ccc->cfg_files = cfg_files; + ccc->test_plugin = test_plugin_; + ccc->test_name = test_name_; + ccc->tth = tth_; + ccc->global_ret = GNUNET_OK; + ccc->p = p; + ccc->ip = ip; + for (unsigned int i = 0; i < num_peers; i++) + { + ip[i].off = i; + ip[i].ccc = ccc; + } + if (GNUNET_OK != + GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, + argv, + test_name_, + "nohelp", + options, + &connect_check_run, + ccc)) + return GNUNET_SYSERR; + return ccc->global_ret; +} + + +/** + * Setup testcase. Calls @a check with the data the test needs. + * + * @param argv0 binary name (argv[0]) + * @param filename source file name (__FILE__) + * @param num_peers number of peers to start + * @param check main function to run + * @param check_cls closure for @a check + * @return #GNUNET_OK on success + */ +int +GNUNET_TRANSPORT_TESTING_main_ (const char *argv0, + const char *filename, + unsigned int num_peers, + GNUNET_TRANSPORT_TESTING_CheckCallback check, + void *check_cls) +{ + struct GNUNET_TRANSPORT_TESTING_Handle *tth; + char *test_name; + char *test_source; + char *test_plugin; + char *cfg_names[num_peers]; + int ret; + + ret = GNUNET_OK; + test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0); + GNUNET_log_setup (test_name, + "WARNING", + NULL); + test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (filename); + test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv0, + test_source); + for (unsigned int i = 0; i < num_peers; i++) + cfg_names[i] = GNUNET_TRANSPORT_TESTING_get_config_name (argv0, + i + 1); + tth = GNUNET_TRANSPORT_TESTING_init (); + if (NULL == tth) + { + ret = GNUNET_SYSERR; + } + else + { + ret = check (check_cls, + tth, + test_plugin, + test_name, + num_peers, + cfg_names); + GNUNET_TRANSPORT_TESTING_done (tth); + } + for (unsigned int i = 0; i < num_peers; i++) + GNUNET_free (cfg_names[i]); + GNUNET_free (test_source); + GNUNET_free (test_plugin); + GNUNET_free (test_name); + return ret; +} + + +/* end of transport-testing-main.c */ diff --git a/src/transport/transport-testing-send2.c b/src/transport/transport-testing-send2.c new file mode 100644 index 000000000..bd2afb9b0 --- /dev/null +++ b/src/transport/transport-testing-send2.c @@ -0,0 +1,240 @@ +/* + This file is part of GNUnet. + Copyright (C) 2016 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/** + * @file transport-testing-send.c + * @brief convenience transmission function for tests + * @author Christian Grothoff + */ +#include "transport-testing2.h" + +/** + * Acceptable transmission delay. + */ +#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply ( \ + GNUNET_TIME_UNIT_SECONDS, 30) + + +/** + * Return @a cx in @a cls. + */ +static void +find_cr (void *cls, + struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls; + + if (GNUNET_NO == cx->connected) + return; + *cr = cx; +} + + +/** + * Send a test message of type @a mtype and size @a msize from + * peer @a sender to peer @a receiver. The peers should be + * connected when this function is called. + * + * @param sender the sending peer + * @param receiver the receiving peer + * @param mtype message type to use + * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)` + * @param num unique message number + * @param cont continuation to call after transmission + * @param cont_cls closure for @a cont + * @return #GNUNET_OK if message was queued, + * #GNUNET_NO if peers are not connected + * #GNUNET_SYSERR if @a msize is illegal + */ +int +GNUNET_TRANSPORT_TESTING_send (struct + GNUNET_TRANSPORT_TESTING_PeerContext *sender, + struct GNUNET_TRANSPORT_TESTING_PeerContext * + receiver, + uint16_t mtype, + uint16_t msize, + uint32_t num, + GNUNET_SCHEDULER_TaskCallback cont, + void *cont_cls) +{ + struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr; + struct GNUNET_MQ_Envelope *env; + struct GNUNET_TRANSPORT_TESTING_TestMessage *test; + + if (msize < sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + cr = NULL; + GNUNET_TRANSPORT_TESTING_find_connecting_context (sender, + receiver, + &find_cr, + &cr); + if (NULL == cr) + GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver, + sender, + &find_cr, + &cr); + if (NULL == cr) + { + GNUNET_break (0); + return GNUNET_NO; + } + if (NULL == cr->mq) + { + GNUNET_break (0); + return GNUNET_NO; + } + { + char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id)); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Sending message from peer %u (`%s') -> peer %u (`%s') !\n", + sender->no, + GNUNET_i2s (&sender->id), + receiver->no, + receiver_s); + GNUNET_free (receiver_s); + } + env = GNUNET_MQ_msg_extra (test, + msize - sizeof(*test), + mtype); + test->num = htonl (num); + memset (&test[1], + num, + msize - sizeof(*test)); + GNUNET_MQ_notify_sent (env, + cont, + cont_cls); + GNUNET_MQ_send (cr->mq, + env); + return GNUNET_OK; +} + + +/** + * Task that sends a test message from the + * first peer to the second peer. + * + * @param ccc context which should contain at least two peers, the + * first two of which should be currently connected + * @param size desired message size + * @param cont continuation to call after transmission + * @param cont_cls closure for @a cont + */ +static void +do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc, + uint16_t size, + GNUNET_SCHEDULER_TaskCallback cont, + void *cont_cls) +{ + int ret; + + ccc->global_ret = GNUNET_SYSERR; + ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0], + ccc->p[1], + GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, + size, + ccc->send_num_gen++, + cont, + cont_cls); + GNUNET_assert (GNUNET_SYSERR != ret); + if (GNUNET_NO == ret) + { + GNUNET_break (0); + ccc->global_ret = GNUNET_SYSERR; + GNUNET_SCHEDULER_shutdown (); + } +} + + +/** + * Task that sends a minimalistic test message from the + * first peer to the second peer. + * + * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` + * which should contain at least two peers, the first two + * of which should be currently connected + */ +void +GNUNET_TRANSPORT_TESTING_simple_send (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls; + int done; + size_t msize; + + if (0 < sc->num_messages) + { + sc->num_messages--; + done = (0 == sc->num_messages); + } + else + { + done = 0; /* infinite loop */ + } + msize = sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage); + if (NULL != sc->get_size_cb) + msize = sc->get_size_cb (sc->num_messages); + /* if this was the last message, call the continuation, + otherwise call this function again */ + do_send (sc->ccc, + msize, + done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send, + done ? sc->cont_cls : sc); +} + + +/** + * Task that sends a large test message from the + * first peer to the second peer. + * + * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext` + * which should contain at least two peers, the first two + * of which should be currently connected + */ +void +GNUNET_TRANSPORT_TESTING_large_send (void *cls) +{ + struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls; + int done; + size_t msize; + + if (0 < sc->num_messages) + { + sc->num_messages--; + done = (0 == sc->num_messages); + } + else + { + done = 0; /* infinite loop */ + } + msize = 2600; + if (NULL != sc->get_size_cb) + msize = sc->get_size_cb (sc->num_messages); + /* if this was the last message, call the continuation, + otherwise call this function again */ + do_send (sc->ccc, + msize, + done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send, + done ? sc->cont_cls : sc); +} + + +/* end of transport-testing-send.c */ diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c index 374d78f67..d9f1d5e56 100644 --- a/src/transport/transport-testing2.c +++ b/src/transport/transport-testing2.c @@ -344,6 +344,7 @@ hello_iter_cb (void *cb_cls, p->hello_size = record->value_size; p->hello = GNUNET_malloc (p->hello_size); memcpy (p->hello, record->value, p->hello_size); + p->hello[p->hello_size-1] = '\0'; GNUNET_PEERSTORE_iterate_cancel (p->pic); if (NULL != p->start_cb) @@ -751,7 +752,7 @@ offer_hello (void *cls) struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1; struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2; struct GNUNET_TIME_Absolute t; - enum GNUNET_NetworkType nt; + enum GNUNET_NetworkType nt = 0; char *addr; cc->tct = NULL; @@ -769,11 +770,12 @@ offer_hello (void *cls) } addr = GNUNET_HELLO_extract_address (p2->hello, - strlen (p2->hello), + p2->hello_size, &p2->id, &nt, &t); GNUNET_assert (NULL != addr); + GNUNET_assert (NULL != p1->hello); GNUNET_TRANSPORT_application_validate (p1->ah, &p2->id, nt, -- cgit v1.2.3