2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file core/test_core_api_reliability.c
22 * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
23 * @author Christian Grothoff
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_hello_service.h"
35 * Note that this value must not significantly exceed
36 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
37 * messages may be dropped even for a reliable transport.
39 #define TOTAL_MSGS (600 * 10)
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
49 static unsigned long long total_bytes;
51 static struct GNUNET_TIME_Absolute start_time;
53 static struct GNUNET_SCHEDULER_Task *err_task;
58 struct GNUNET_CONFIGURATION_Handle *cfg;
59 struct GNUNET_CORE_Handle *ch;
60 struct GNUNET_MQ_Handle *mq;
61 struct GNUNET_PeerIdentity id;
62 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
63 struct GNUNET_MessageHeader *hello;
64 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
65 struct GNUNET_ATS_ConnectivityHandle *ats;
66 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
68 struct GNUNET_OS_Process *arm_proc;
71 static struct PeerContext p1;
73 static struct PeerContext p2;
80 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
84 struct GNUNET_MessageHeader header;
85 uint32_t num GNUNET_PACKED;
90 get_size (unsigned int iter)
95 return iter + sizeof (struct TestMessage);
96 ret = (iter * iter * iter);
97 return sizeof (struct TestMessage) + (ret % 60000);
102 terminate_peer (struct PeerContext *p)
106 GNUNET_CORE_disconnect (p->ch);
111 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
116 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
119 if (NULL != p->ats_sh)
121 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
126 GNUNET_ATS_connectivity_done (p->ats);
133 terminate_task_error (void *cls)
137 GNUNET_SCHEDULER_shutdown ();
143 do_shutdown (void *cls)
145 unsigned long long delta;
147 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
151 "\nThroughput was %llu kb/s\n",
152 total_bytes * 1000000LL / 1024 / delta);
155 total_bytes * 1000000LL / 1024 / delta,
157 if (NULL != err_task)
159 GNUNET_SCHEDULER_cancel (err_task);
162 terminate_peer (&p1);
163 terminate_peer (&p2);
169 send_message (struct GNUNET_MQ_Handle *mq,
172 struct GNUNET_MQ_Envelope *env;
173 struct TestMessage *hdr;
176 GNUNET_assert (NULL != mq);
177 GNUNET_assert (tr_n < TOTAL_MSGS);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Sending message %u of size %u\n",
183 env = GNUNET_MQ_msg_extra (hdr,
184 s - sizeof (struct TestMessage),
186 hdr->num = htonl (tr_n);
189 s - sizeof (struct TestMessage));
191 GNUNET_SCHEDULER_cancel (err_task);
193 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
194 &terminate_task_error,
203 connect_notify (void *cls,
204 const struct GNUNET_PeerIdentity *peer,
205 struct GNUNET_MQ_Handle *mq)
207 struct PeerContext *pc = cls;
209 if (0 == memcmp (&pc->id,
211 sizeof (struct GNUNET_PeerIdentity)))
212 return (void *) peer;
214 GNUNET_assert (0 == pc->connect_status);
215 pc->connect_status = 1;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Encrypted connection established to peer `%s'\n",
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Asking core (1) for transmission to peer `%s'\n",
223 GNUNET_i2s (&p2.id));
224 GNUNET_SCHEDULER_cancel (err_task);
226 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
227 &terminate_task_error,
229 start_time = GNUNET_TIME_absolute_get ();
233 return (void *) peer;
238 disconnect_notify (void *cls,
239 const struct GNUNET_PeerIdentity *peer,
242 struct PeerContext *pc = cls;
244 if (0 == memcmp (&pc->id,
246 sizeof (struct GNUNET_PeerIdentity)))
249 pc->connect_status = 0;
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Encrypted connection to `%s' cut\n",
257 check_test (void *cls,
258 const struct TestMessage *hdr)
260 return GNUNET_OK; /* accept all */
265 handle_test (void *cls,
266 const struct TestMessage *hdr)
272 if (ntohs (hdr->header.size) != s)
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 "Expected message %u of size %u, got %u bytes of message %u\n",
278 ntohs (hdr->header.size),
280 GNUNET_SCHEDULER_cancel (err_task);
281 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
285 if (ntohl (hdr->num) != n)
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288 "Expected message %u of size %u, got %u bytes of message %u\n",
291 (unsigned int) ntohs (hdr->header.size),
292 (unsigned int) ntohl (hdr->num));
293 GNUNET_SCHEDULER_cancel (err_task);
294 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Got message %u of size %u\n",
300 (unsigned int) ntohl (hdr->num),
301 (unsigned int) ntohs (hdr->header.size));
303 if (0 == (n % (TOTAL_MSGS / 100)))
310 GNUNET_SCHEDULER_shutdown ();
324 init_notify (void *cls,
325 const struct GNUNET_PeerIdentity *my_identity)
327 struct PeerContext *p = cls;
328 struct GNUNET_MQ_MessageHandler handlers[] = {
329 GNUNET_MQ_hd_var_size (test,
333 GNUNET_MQ_handler_end ()
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Connection to CORE service of `%s' established\n",
338 GNUNET_i2s (my_identity));
339 p->id = *my_identity;
342 GNUNET_assert (ok == 2);
345 GNUNET_assert (NULL !=
346 (p2.ch = GNUNET_CORE_connect (p2.cfg,
355 GNUNET_assert (ok == 3);
357 GNUNET_assert (cls == &p2);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Asking transport (1) to connect to peer `%s'\n",
360 GNUNET_i2s (&p2.id));
361 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
369 offer_hello_done (void *cls)
371 struct PeerContext *p = cls;
378 process_hello (void *cls,
379 const struct GNUNET_MessageHeader *message)
381 struct PeerContext *p = cls;
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Received (my) `%s' from transport service\n", "HELLO");
385 GNUNET_assert (message != NULL);
386 GNUNET_free_non_null (p->hello);
387 p->hello = GNUNET_copy_message (message);
388 if ((p == &p1) && (NULL == p2.oh))
389 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
393 if ((p == &p2) && (NULL == p1.oh))
394 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
399 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
400 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
404 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
405 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
413 setup_peer (struct PeerContext *p,
418 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
419 p->cfg = GNUNET_CONFIGURATION_create ();
421 = GNUNET_OS_start_process (GNUNET_YES,
422 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
425 "gnunet-service-arm",
429 GNUNET_assert (GNUNET_OK ==
430 GNUNET_CONFIGURATION_load (p->cfg,
432 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
433 GNUNET_assert (NULL != p->ats);
434 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
435 GNUNET_TRANSPORT_AC_ANY,
438 GNUNET_free (binary);
446 const struct GNUNET_CONFIGURATION_Handle *cfg)
448 struct GNUNET_MQ_MessageHandler handlers[] = {
449 GNUNET_MQ_hd_fixed_size (test,
453 GNUNET_MQ_handler_end ()
456 GNUNET_assert (ok == 1);
459 "test_core_api_peer1.conf");
461 "test_core_api_peer2.conf");
463 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
464 &terminate_task_error,
466 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
469 GNUNET_assert (NULL !=
470 (p1.ch = GNUNET_CORE_connect (p1.cfg,
480 stop_arm (struct PeerContext *p)
482 if (0 != GNUNET_OS_process_kill (p->arm_proc,
484 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
486 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
487 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "ARM process %u stopped\n",
491 GNUNET_OS_process_get_pid (p->arm_proc));
492 GNUNET_OS_process_destroy (p->arm_proc);
494 GNUNET_CONFIGURATION_destroy (p->cfg);
502 char *const argv[] = {
503 "test-core-api-reliability",
505 "test_core_api_data.conf",
508 struct GNUNET_GETOPT_CommandLineOption options[] = {
509 GNUNET_GETOPT_OPTION_END
512 GNUNET_log_setup ("test-core-api-reliability",
515 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
517 "test-core-api-reliability",
524 GNUNET_free_non_null (p1.hello);
525 GNUNET_free_non_null (p2.hello);
526 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
527 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
532 /* end of test_core_api_reliability.c */