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 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.
16 * @file core/test_core_api_reliability.c
17 * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
18 * @author Christian Grothoff
21 #include "gnunet_arm_service.h"
22 #include "gnunet_core_service.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_ats_service.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_transport_hello_service.h"
30 * Note that this value must not significantly exceed
31 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
32 * messages may be dropped even for a reliable transport.
34 #define TOTAL_MSGS (600 * 10)
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
44 static unsigned long long total_bytes;
46 static struct GNUNET_TIME_Absolute start_time;
48 static struct GNUNET_SCHEDULER_Task *err_task;
53 struct GNUNET_CONFIGURATION_Handle *cfg;
54 struct GNUNET_CORE_Handle *ch;
55 struct GNUNET_MQ_Handle *mq;
56 struct GNUNET_PeerIdentity id;
57 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
58 struct GNUNET_MessageHeader *hello;
59 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
60 struct GNUNET_ATS_ConnectivityHandle *ats;
61 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
63 struct GNUNET_OS_Process *arm_proc;
66 static struct PeerContext p1;
68 static struct PeerContext p2;
75 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
79 struct GNUNET_MessageHeader header;
80 uint32_t num GNUNET_PACKED;
85 get_size (unsigned int iter)
90 return iter + sizeof (struct TestMessage);
91 ret = (iter * iter * iter);
92 return sizeof (struct TestMessage) + (ret % 60000);
97 terminate_peer (struct PeerContext *p)
101 GNUNET_CORE_disconnect (p->ch);
106 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
111 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
114 if (NULL != p->ats_sh)
116 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
121 GNUNET_ATS_connectivity_done (p->ats);
128 terminate_task_error (void *cls)
132 GNUNET_SCHEDULER_shutdown ();
138 do_shutdown (void *cls)
140 unsigned long long delta;
142 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
144 "\nThroughput was %llu kb/s\n",
145 total_bytes * 1000000LL / 1024 / delta);
148 total_bytes * 1000000LL / 1024 / delta,
150 if (NULL != err_task)
152 GNUNET_SCHEDULER_cancel (err_task);
155 terminate_peer (&p1);
156 terminate_peer (&p2);
162 send_message (struct GNUNET_MQ_Handle *mq,
165 struct GNUNET_MQ_Envelope *env;
166 struct TestMessage *hdr;
169 GNUNET_assert (NULL != mq);
170 GNUNET_assert (tr_n < TOTAL_MSGS);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Sending message %u of size %u\n",
176 env = GNUNET_MQ_msg_extra (hdr,
177 s - sizeof (struct TestMessage),
179 hdr->num = htonl (tr_n);
182 s - sizeof (struct TestMessage));
184 GNUNET_SCHEDULER_cancel (err_task);
186 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
187 &terminate_task_error,
196 connect_notify (void *cls,
197 const struct GNUNET_PeerIdentity *peer,
198 struct GNUNET_MQ_Handle *mq)
200 struct PeerContext *pc = cls;
202 if (0 == memcmp (&pc->id,
204 sizeof (struct GNUNET_PeerIdentity)))
205 return (void *) peer;
207 GNUNET_assert (0 == pc->connect_status);
208 pc->connect_status = 1;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Encrypted connection established to peer `%s'\n",
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Asking core (1) for transmission to peer `%s'\n",
216 GNUNET_i2s (&p2.id));
217 GNUNET_SCHEDULER_cancel (err_task);
219 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
220 &terminate_task_error,
222 start_time = GNUNET_TIME_absolute_get ();
226 return (void *) peer;
231 disconnect_notify (void *cls,
232 const struct GNUNET_PeerIdentity *peer,
235 struct PeerContext *pc = cls;
237 if (0 == memcmp (&pc->id,
239 sizeof (struct GNUNET_PeerIdentity)))
242 pc->connect_status = 0;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Encrypted connection to `%s' cut\n",
250 check_test (void *cls,
251 const struct TestMessage *hdr)
253 return GNUNET_OK; /* accept all */
258 handle_test (void *cls,
259 const struct TestMessage *hdr)
265 if (ntohs (hdr->header.size) != s)
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 "Expected message %u of size %u, got %u bytes of message %u\n",
271 ntohs (hdr->header.size),
273 GNUNET_SCHEDULER_cancel (err_task);
274 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
278 if (ntohl (hdr->num) != n)
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 "Expected message %u of size %u, got %u bytes of message %u\n",
284 (unsigned int) ntohs (hdr->header.size),
285 (unsigned int) ntohl (hdr->num));
286 GNUNET_SCHEDULER_cancel (err_task);
287 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Got message %u of size %u\n",
293 (unsigned int) ntohl (hdr->num),
294 (unsigned int) ntohs (hdr->header.size));
296 if (0 == (n % (TOTAL_MSGS / 100)))
303 GNUNET_SCHEDULER_shutdown ();
317 init_notify (void *cls,
318 const struct GNUNET_PeerIdentity *my_identity)
320 struct PeerContext *p = cls;
321 struct GNUNET_MQ_MessageHandler handlers[] = {
322 GNUNET_MQ_hd_var_size (test,
326 GNUNET_MQ_handler_end ()
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Connection to CORE service of `%s' established\n",
331 GNUNET_i2s (my_identity));
332 p->id = *my_identity;
335 GNUNET_assert (ok == 2);
338 GNUNET_assert (NULL !=
339 (p2.ch = GNUNET_CORE_connect (p2.cfg,
348 GNUNET_assert (ok == 3);
350 GNUNET_assert (cls == &p2);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Asking transport (1) to connect to peer `%s'\n",
353 GNUNET_i2s (&p2.id));
354 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
362 offer_hello_done (void *cls)
364 struct PeerContext *p = cls;
371 process_hello (void *cls,
372 const struct GNUNET_MessageHeader *message)
374 struct PeerContext *p = cls;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Received (my) `%s' from transport service\n", "HELLO");
378 GNUNET_assert (message != NULL);
379 GNUNET_free_non_null (p->hello);
380 p->hello = GNUNET_copy_message (message);
381 if ((p == &p1) && (NULL == p2.oh))
382 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
386 if ((p == &p2) && (NULL == p1.oh))
387 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
392 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
393 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
397 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
398 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
406 setup_peer (struct PeerContext *p,
411 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
412 p->cfg = GNUNET_CONFIGURATION_create ();
414 = GNUNET_OS_start_process (GNUNET_YES,
415 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
418 "gnunet-service-arm",
422 GNUNET_assert (GNUNET_OK ==
423 GNUNET_CONFIGURATION_load (p->cfg,
425 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
426 GNUNET_assert (NULL != p->ats);
427 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
428 GNUNET_TRANSPORT_AC_ANY,
431 GNUNET_free (binary);
439 const struct GNUNET_CONFIGURATION_Handle *cfg)
441 struct GNUNET_MQ_MessageHandler handlers[] = {
442 GNUNET_MQ_hd_fixed_size (test,
446 GNUNET_MQ_handler_end ()
449 GNUNET_assert (ok == 1);
452 "test_core_api_peer1.conf");
454 "test_core_api_peer2.conf");
456 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
457 &terminate_task_error,
459 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
462 GNUNET_assert (NULL !=
463 (p1.ch = GNUNET_CORE_connect (p1.cfg,
473 stop_arm (struct PeerContext *p)
475 if (0 != GNUNET_OS_process_kill (p->arm_proc,
477 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
479 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
480 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "ARM process %u stopped\n",
484 GNUNET_OS_process_get_pid (p->arm_proc));
485 GNUNET_OS_process_destroy (p->arm_proc);
487 GNUNET_CONFIGURATION_destroy (p->cfg);
495 char *const argv[] = {
496 "test-core-api-reliability",
498 "test_core_api_data.conf",
501 struct GNUNET_GETOPT_CommandLineOption options[] = {
502 GNUNET_GETOPT_OPTION_END
505 GNUNET_log_setup ("test-core-api-reliability",
508 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
510 "test-core-api-reliability",
517 GNUNET_free_non_null (p1.hello);
518 GNUNET_free_non_null (p2.hello);
519 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
520 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
525 /* end of test_core_api_reliability.c */