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)
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;
149 "\nThroughput was %llu kb/s\n",
150 total_bytes * 1000000LL / 1024 / delta);
153 total_bytes * 1000000LL / 1024 / delta,
155 if (NULL != err_task)
157 GNUNET_SCHEDULER_cancel (err_task);
160 terminate_peer (&p1);
161 terminate_peer (&p2);
167 send_message (struct GNUNET_MQ_Handle *mq,
170 struct GNUNET_MQ_Envelope *env;
171 struct TestMessage *hdr;
174 GNUNET_assert (NULL != mq);
175 GNUNET_assert (tr_n < TOTAL_MSGS);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Sending message %u of size %u\n",
181 env = GNUNET_MQ_msg_extra (hdr,
182 s - sizeof (struct TestMessage),
184 hdr->num = htonl (tr_n);
187 s - sizeof (struct TestMessage));
189 GNUNET_SCHEDULER_cancel (err_task);
191 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
192 &terminate_task_error,
201 connect_notify (void *cls,
202 const struct GNUNET_PeerIdentity *peer,
203 struct GNUNET_MQ_Handle *mq)
205 struct PeerContext *pc = cls;
207 if (0 == memcmp (&pc->id,
209 sizeof (struct GNUNET_PeerIdentity)))
210 return (void *) peer;
212 GNUNET_assert (0 == pc->connect_status);
213 pc->connect_status = 1;
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Encrypted connection established to peer `%s'\n",
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Asking core (1) for transmission to peer `%s'\n",
221 GNUNET_i2s (&p2.id));
222 GNUNET_SCHEDULER_cancel (err_task);
224 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
225 &terminate_task_error,
227 start_time = GNUNET_TIME_absolute_get ();
231 return (void *) peer;
236 disconnect_notify (void *cls,
237 const struct GNUNET_PeerIdentity *peer,
240 struct PeerContext *pc = cls;
242 if (0 == memcmp (&pc->id,
244 sizeof (struct GNUNET_PeerIdentity)))
247 pc->connect_status = 0;
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Encrypted connection to `%s' cut\n",
255 check_test (void *cls,
256 const struct TestMessage *hdr)
258 return GNUNET_OK; /* accept all */
263 handle_test (void *cls,
264 const struct TestMessage *hdr)
270 if (ntohs (hdr->header.size) != s)
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273 "Expected message %u of size %u, got %u bytes of message %u\n",
276 ntohs (hdr->header.size),
278 GNUNET_SCHEDULER_cancel (err_task);
279 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
283 if (ntohl (hdr->num) != n)
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 "Expected message %u of size %u, got %u bytes of message %u\n",
289 (unsigned int) ntohs (hdr->header.size),
290 (unsigned int) ntohl (hdr->num));
291 GNUNET_SCHEDULER_cancel (err_task);
292 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Got message %u of size %u\n",
298 (unsigned int) ntohl (hdr->num),
299 (unsigned int) ntohs (hdr->header.size));
301 if (0 == (n % (TOTAL_MSGS / 100)))
308 GNUNET_SCHEDULER_shutdown ();
322 init_notify (void *cls,
323 const struct GNUNET_PeerIdentity *my_identity)
325 struct PeerContext *p = cls;
326 struct GNUNET_MQ_MessageHandler handlers[] = {
327 GNUNET_MQ_hd_var_size (test,
331 GNUNET_MQ_handler_end ()
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Connection to CORE service of `%s' established\n",
336 GNUNET_i2s (my_identity));
337 p->id = *my_identity;
340 GNUNET_assert (ok == 2);
343 GNUNET_assert (NULL !=
344 (p2.ch = GNUNET_CORE_connecT (p2.cfg,
353 GNUNET_assert (ok == 3);
355 GNUNET_assert (cls == &p2);
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Asking transport (1) to connect to peer `%s'\n",
358 GNUNET_i2s (&p2.id));
359 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
367 offer_hello_done (void *cls)
369 struct PeerContext *p = cls;
376 process_hello (void *cls,
377 const struct GNUNET_MessageHeader *message)
379 struct PeerContext *p = cls;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Received (my) `%s' from transport service\n", "HELLO");
383 GNUNET_assert (message != NULL);
384 p->hello = GNUNET_copy_message (message);
385 if ((p == &p1) && (NULL == p2.oh))
386 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
390 if ((p == &p2) && (NULL == p1.oh))
391 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
396 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
397 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
401 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
402 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
410 setup_peer (struct PeerContext *p,
415 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
416 p->cfg = GNUNET_CONFIGURATION_create ();
418 = GNUNET_OS_start_process (GNUNET_YES,
419 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
422 "gnunet-service-arm",
426 GNUNET_assert (GNUNET_OK ==
427 GNUNET_CONFIGURATION_load (p->cfg,
429 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
430 GNUNET_assert (NULL != p->ats);
431 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
432 GNUNET_TRANSPORT_AC_ANY,
435 GNUNET_free (binary);
443 const struct GNUNET_CONFIGURATION_Handle *cfg)
445 struct GNUNET_MQ_MessageHandler handlers[] = {
446 GNUNET_MQ_hd_fixed_size (test,
450 GNUNET_MQ_handler_end ()
453 GNUNET_assert (ok == 1);
456 "test_core_api_peer1.conf");
458 "test_core_api_peer2.conf");
460 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
461 &terminate_task_error,
463 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
466 GNUNET_assert (NULL !=
467 (p1.ch = GNUNET_CORE_connecT (p1.cfg,
477 stop_arm (struct PeerContext *p)
479 if (0 != GNUNET_OS_process_kill (p->arm_proc,
481 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
483 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
484 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "ARM process %u stopped\n",
488 GNUNET_OS_process_get_pid (p->arm_proc));
489 GNUNET_OS_process_destroy (p->arm_proc);
491 GNUNET_CONFIGURATION_destroy (p->cfg);
499 char *const argv[] = {
500 "test-core-api-reliability",
502 "test_core_api_data.conf",
505 struct GNUNET_GETOPT_CommandLineOption options[] = {
506 GNUNET_GETOPT_OPTION_END
509 GNUNET_log_setup ("test-core-api-reliability",
512 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
514 "test-core-api-reliability",
521 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
522 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
527 /* end of test_core_api_reliability.c */