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)
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)
47 * What delay do we request from the core service for transmission?
49 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
54 static unsigned long long total_bytes;
56 static struct GNUNET_TIME_Absolute start_time;
58 static struct GNUNET_SCHEDULER_Task *err_task;
63 struct GNUNET_CONFIGURATION_Handle *cfg;
64 struct GNUNET_CORE_Handle *ch;
65 struct GNUNET_MQ_Handle *mq;
66 struct GNUNET_PeerIdentity id;
67 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
68 struct GNUNET_MessageHeader *hello;
69 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
70 struct GNUNET_ATS_ConnectivityHandle *ats;
71 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
73 struct GNUNET_OS_Process *arm_proc;
76 static struct PeerContext p1;
78 static struct PeerContext p2;
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);
165 terminate_peer (&p1);
166 terminate_peer (&p2);
172 send_message (struct GNUNET_MQ_Handle *mq,
175 struct GNUNET_MQ_Envelope *env;
176 struct TestMessage *hdr;
179 GNUNET_assert (NULL != mq);
180 GNUNET_assert (tr_n < TOTAL_MSGS);
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Sending message %u of size %u\n",
186 env = GNUNET_MQ_msg_extra (hdr,
187 s - sizeof (struct TestMessage),
189 hdr->num = htonl (tr_n);
192 s - sizeof (struct TestMessage));
194 GNUNET_SCHEDULER_cancel (err_task);
196 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
197 &terminate_task_error,
206 connect_notify (void *cls,
207 const struct GNUNET_PeerIdentity *peer,
208 struct GNUNET_MQ_Handle *mq)
210 struct PeerContext *pc = cls;
212 if (0 == memcmp (&pc->id,
214 sizeof (struct GNUNET_PeerIdentity)))
215 return (void *) peer;
217 GNUNET_assert (0 == pc->connect_status);
218 pc->connect_status = 1;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Encrypted connection established to peer `%s'\n",
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Asking core (1) for transmission to peer `%s'\n",
226 GNUNET_i2s (&p2.id));
227 GNUNET_SCHEDULER_cancel (err_task);
229 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
230 &terminate_task_error,
232 start_time = GNUNET_TIME_absolute_get ();
236 return (void *) peer;
241 disconnect_notify (void *cls,
242 const struct GNUNET_PeerIdentity *peer,
245 struct PeerContext *pc = cls;
247 if (0 == memcmp (&pc->id,
249 sizeof (struct GNUNET_PeerIdentity)))
252 pc->connect_status = 0;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Encrypted connection to `%s' cut\n",
260 check_test (void *cls,
261 const struct TestMessage *hdr)
263 return GNUNET_OK; /* accept all */
268 handle_test (void *cls,
269 const struct TestMessage *hdr)
275 if (ntohs (hdr->header.size) != s)
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 "Expected message %u of size %u, got %u bytes of message %u\n",
281 ntohs (hdr->header.size),
283 GNUNET_SCHEDULER_cancel (err_task);
284 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
288 if (ntohl (hdr->num) != n)
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291 "Expected message %u of size %u, got %u bytes of message %u\n",
294 (unsigned int) ntohs (hdr->header.size),
295 (unsigned int) ntohl (hdr->num));
296 GNUNET_SCHEDULER_cancel (err_task);
297 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Got message %u of size %u\n",
303 (unsigned int) ntohl (hdr->num),
304 (unsigned int) ntohs (hdr->header.size));
306 if (0 == (n % (TOTAL_MSGS / 100)))
313 GNUNET_SCHEDULER_shutdown ();
327 init_notify (void *cls,
328 const struct GNUNET_PeerIdentity *my_identity)
330 GNUNET_MQ_hd_var_size (test,
333 struct PeerContext *p = cls;
334 struct GNUNET_MQ_MessageHandler handlers[] = {
335 make_test_handler (NULL),
336 GNUNET_MQ_handler_end ()
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Connection to CORE service of `%s' established\n",
341 GNUNET_i2s (my_identity));
342 p->id = *my_identity;
345 GNUNET_assert (ok == 2);
348 GNUNET_assert (NULL !=
349 (p2.ch = GNUNET_CORE_connecT (p2.cfg,
358 GNUNET_assert (ok == 3);
360 GNUNET_assert (cls == &p2);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Asking transport (1) to connect to peer `%s'\n",
363 GNUNET_i2s (&p2.id));
364 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
372 offer_hello_done (void *cls)
374 struct PeerContext *p = cls;
381 process_hello (void *cls,
382 const struct GNUNET_MessageHeader *message)
384 struct PeerContext *p = cls;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Received (my) `%s' from transport service\n", "HELLO");
388 GNUNET_assert (message != NULL);
389 p->hello = GNUNET_copy_message (message);
390 if ((p == &p1) && (NULL == p2.oh))
391 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
395 if ((p == &p2) && (NULL == p1.oh))
396 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
401 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
402 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
406 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
407 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
415 setup_peer (struct PeerContext *p,
420 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
421 p->cfg = GNUNET_CONFIGURATION_create ();
423 = GNUNET_OS_start_process (GNUNET_YES,
424 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
427 "gnunet-service-arm",
431 GNUNET_assert (GNUNET_OK ==
432 GNUNET_CONFIGURATION_load (p->cfg,
434 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
435 GNUNET_assert (NULL != p->ats);
436 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
437 GNUNET_TRANSPORT_AC_ANY,
440 GNUNET_free (binary);
448 const struct GNUNET_CONFIGURATION_Handle *cfg)
450 GNUNET_MQ_hd_fixed_size (test,
453 struct GNUNET_MQ_MessageHandler handlers[] = {
454 make_test_handler (NULL),
455 GNUNET_MQ_handler_end ()
458 GNUNET_assert (ok == 1);
461 "test_core_api_peer1.conf");
463 "test_core_api_peer2.conf");
465 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
466 &terminate_task_error,
468 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
471 GNUNET_assert (NULL !=
472 (p1.ch = GNUNET_CORE_connecT (p1.cfg,
482 stop_arm (struct PeerContext *p)
484 if (0 != GNUNET_OS_process_kill (p->arm_proc,
486 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
488 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
489 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "ARM process %u stopped\n",
493 GNUNET_OS_process_get_pid (p->arm_proc));
494 GNUNET_OS_process_destroy (p->arm_proc);
496 GNUNET_CONFIGURATION_destroy (p->cfg);
504 char *const argv[] = {
505 "test-core-api-reliability",
507 "test_core_api_data.conf",
510 struct GNUNET_GETOPT_CommandLineOption options[] = {
511 GNUNET_GETOPT_OPTION_END
514 GNUNET_log_setup ("test-core-api-reliability",
517 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
519 "test-core-api-reliability",
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 */