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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file core/test_core_api_reliability.c
22 * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_transport_hello_service.h"
34 * Note that this value must not significantly exceed
35 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
36 * messages may be dropped even for a reliable transport.
38 #define TOTAL_MSGS (600 * 10)
41 * How long until we give up on transmitting the message?
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
46 * What delay do we request from the core service for transmission?
48 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
53 static unsigned long long total_bytes;
55 static struct GNUNET_TIME_Absolute start_time;
57 static struct GNUNET_SCHEDULER_Task *err_task;
62 struct GNUNET_CONFIGURATION_Handle *cfg;
63 struct GNUNET_CORE_Handle *ch;
64 struct GNUNET_PeerIdentity id;
65 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
66 struct GNUNET_MessageHeader *hello;
67 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
68 struct GNUNET_ATS_ConnectivityHandle *ats;
69 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
71 struct GNUNET_OS_Process *arm_proc;
74 static struct PeerContext p1;
76 static struct PeerContext p2;
78 static struct GNUNET_CORE_TransmitHandle *nth;
85 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
89 struct GNUNET_MessageHeader header;
90 uint32_t num GNUNET_PACKED;
95 get_size (unsigned int iter)
100 return iter + sizeof (struct TestMessage);
101 ret = (iter * iter * iter);
102 return sizeof (struct TestMessage) + (ret % 60000);
107 terminate_peer (struct PeerContext *p)
111 GNUNET_CORE_disconnect (p->ch);
116 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
121 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
124 if (NULL != p->ats_sh)
126 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
131 GNUNET_ATS_connectivity_done (p->ats);
138 terminate_task_error (void *cls)
142 GNUNET_SCHEDULER_shutdown ();
148 do_shutdown (void *cls)
150 unsigned long long delta;
152 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
154 "\nThroughput was %llu kb/s\n",
155 total_bytes * 1000000LL / 1024 / delta);
158 total_bytes * 1000000LL / 1024 / delta,
160 if (NULL != err_task)
162 GNUNET_SCHEDULER_cancel (err_task);
167 GNUNET_CORE_notify_transmit_ready_cancel (nth);
170 terminate_peer (&p1);
171 terminate_peer (&p2);
177 transmit_ready (void *cls,
182 struct TestMessage hdr;
187 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
191 GNUNET_break (NULL !=
192 (nth = GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO,
193 GNUNET_CORE_PRIO_BEST_EFFORT,
201 GNUNET_assert (tr_n < TOTAL_MSGS);
204 GNUNET_assert (size >= s);
205 GNUNET_assert (buf != NULL);
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Sending message %u of size %u at offset %u\n",
214 hdr.header.size = htons (s);
215 hdr.header.type = htons (MTYPE);
216 hdr.num = htonl (tr_n);
217 GNUNET_memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
218 ret += sizeof (struct TestMessage);
219 memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
220 ret += s - sizeof (struct TestMessage);
223 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
224 break; /* sometimes pack buffer full, sometimes not */
226 while (size - ret >= s);
227 GNUNET_SCHEDULER_cancel (err_task);
229 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
230 &terminate_task_error,
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Returning total message block of size %u\n",
241 connect_notify (void *cls,
242 const struct GNUNET_PeerIdentity *peer)
244 struct PeerContext *pc = cls;
246 if (0 == memcmp (&pc->id,
248 sizeof (struct GNUNET_PeerIdentity)))
250 GNUNET_assert (0 == pc->connect_status);
251 pc->connect_status = 1;
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Encrypted connection established to peer `%s'\n",
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Asking core (1) for transmission to peer `%s'\n",
259 GNUNET_i2s (&p2.id));
260 GNUNET_SCHEDULER_cancel (err_task);
262 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
263 &terminate_task_error,
265 start_time = GNUNET_TIME_absolute_get ();
266 GNUNET_break (NULL !=
267 (nth = GNUNET_CORE_notify_transmit_ready (p1.ch,
269 GNUNET_CORE_PRIO_BEST_EFFORT,
280 disconnect_notify (void *cls,
281 const struct GNUNET_PeerIdentity *peer)
283 struct PeerContext *pc = cls;
285 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
287 pc->connect_status = 0;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Encrypted connection to `%s' cut\n",
295 inbound_notify (void *cls,
296 const struct GNUNET_PeerIdentity *other,
297 const struct GNUNET_MessageHeader *message)
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Core provides inbound data from `%s'.\n",
307 outbound_notify (void *cls,
308 const struct GNUNET_PeerIdentity *other,
309 const struct GNUNET_MessageHeader *message)
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Core notifies about outbound data for `%s'.\n",
319 transmit_ready (void *cls,
325 process_mtype (void *cls,
326 const struct GNUNET_PeerIdentity *peer,
327 const struct GNUNET_MessageHeader *message)
331 const struct TestMessage *hdr;
333 hdr = (const struct TestMessage *) message;
335 if (MTYPE != ntohs (message->type))
336 return GNUNET_SYSERR;
337 if (ntohs (message->size) != s)
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 "Expected message %u of size %u, got %u bytes of message %u\n",
342 ntohs (message->size),
344 GNUNET_SCHEDULER_cancel (err_task);
345 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
347 return GNUNET_SYSERR;
349 if (ntohl (hdr->num) != n)
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 "Expected message %u of size %u, got %u bytes of message %u\n",
354 ntohs (message->size),
356 GNUNET_SCHEDULER_cancel (err_task);
357 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
359 return GNUNET_SYSERR;
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Got message %u of size %u\n",
364 ntohs (message->size));
366 if (0 == (n % (TOTAL_MSGS / 100)))
367 FPRINTF (stderr, "%s", ".");
371 GNUNET_SCHEDULER_shutdown ();
376 GNUNET_break (NULL !=
377 GNUNET_CORE_notify_transmit_ready (p1.ch,
378 GNUNET_NO /* no cork */,
379 GNUNET_CORE_PRIO_BEST_EFFORT,
380 FAST_TIMEOUT /* ignored! */,
383 &transmit_ready, &p1));
389 static struct GNUNET_CORE_MessageHandler handlers[] = {
390 {&process_mtype, MTYPE, 0},
396 init_notify (void *cls,
397 const struct GNUNET_PeerIdentity *my_identity)
399 struct PeerContext *p = cls;
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Connection to CORE service of `%s' established\n",
403 GNUNET_i2s (my_identity));
404 p->id = *my_identity;
407 GNUNET_assert (ok == 2);
410 GNUNET_assert (NULL !=
411 (p2.ch = GNUNET_CORE_connect (p2.cfg,
424 GNUNET_assert (ok == 3);
426 GNUNET_assert (cls == &p2);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Asking transport (1) to connect to peer `%s'\n",
429 GNUNET_i2s (&p2.id));
430 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
438 offer_hello_done (void *cls)
440 struct PeerContext *p = cls;
447 process_hello (void *cls,
448 const struct GNUNET_MessageHeader *message)
450 struct PeerContext *p = cls;
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Received (my) `%s' from transport service\n", "HELLO");
454 GNUNET_assert (message != NULL);
455 p->hello = GNUNET_copy_message (message);
456 if ((p == &p1) && (NULL == p2.oh))
457 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
461 if ((p == &p2) && (NULL == p1.oh))
462 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
467 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
468 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
472 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
473 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
481 setup_peer (struct PeerContext *p,
486 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
487 p->cfg = GNUNET_CONFIGURATION_create ();
489 = GNUNET_OS_start_process (GNUNET_YES,
490 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
493 "gnunet-service-arm",
497 GNUNET_assert (GNUNET_OK ==
498 GNUNET_CONFIGURATION_load (p->cfg,
500 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
501 GNUNET_assert (NULL != p->ats);
502 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
503 GNUNET_TRANSPORT_AC_ANY,
506 GNUNET_free (binary);
514 const struct GNUNET_CONFIGURATION_Handle *cfg)
516 GNUNET_assert (ok == 1);
519 "test_core_api_peer1.conf");
521 "test_core_api_peer2.conf");
523 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
524 &terminate_task_error,
526 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
529 GNUNET_assert (NULL !=
530 (p1.ch = GNUNET_CORE_connect (p1.cfg,
544 stop_arm (struct PeerContext *p)
546 if (0 != GNUNET_OS_process_kill (p->arm_proc,
548 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
550 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
551 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "ARM process %u stopped\n",
555 GNUNET_OS_process_get_pid (p->arm_proc));
556 GNUNET_OS_process_destroy (p->arm_proc);
558 GNUNET_CONFIGURATION_destroy (p->cfg);
566 char *const argv[] = {
567 "test-core-api-reliability",
569 "test_core_api_data.conf",
572 struct GNUNET_GETOPT_CommandLineOption options[] = {
573 GNUNET_GETOPT_OPTION_END
576 GNUNET_log_setup ("test-core-api-reliability",
579 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
581 "test-core-api-reliability",
588 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
589 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
594 /* end of test_core_api_reliability.c */