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/>.
19 * @file core/test_core_api_reliability.c
20 * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
21 * @author Christian Grothoff
24 #include "gnunet_arm_service.h"
25 #include "gnunet_core_service.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_transport_hello_service.h"
33 * Note that this value must not significantly exceed
34 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
35 * messages may be dropped even for a reliable transport.
37 #define TOTAL_MSGS (600 * 10)
40 * How long until we give up on transmitting the message?
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
47 static unsigned long long total_bytes;
49 static struct GNUNET_TIME_Absolute start_time;
51 static struct GNUNET_SCHEDULER_Task *err_task;
56 struct GNUNET_CONFIGURATION_Handle *cfg;
57 struct GNUNET_CORE_Handle *ch;
58 struct GNUNET_MQ_Handle *mq;
59 struct GNUNET_PeerIdentity id;
60 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
61 struct GNUNET_MessageHeader *hello;
62 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
63 struct GNUNET_ATS_ConnectivityHandle *ats;
64 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
66 struct GNUNET_OS_Process *arm_proc;
69 static struct PeerContext p1;
71 static struct PeerContext p2;
78 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
82 struct GNUNET_MessageHeader header;
83 uint32_t num GNUNET_PACKED;
88 get_size (unsigned int iter)
93 return iter + sizeof (struct TestMessage);
94 ret = (iter * iter * iter);
95 return sizeof (struct TestMessage) + (ret % 60000);
100 terminate_peer (struct PeerContext *p)
104 GNUNET_CORE_disconnect (p->ch);
109 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
114 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
117 if (NULL != p->ats_sh)
119 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
124 GNUNET_ATS_connectivity_done (p->ats);
131 terminate_task_error (void *cls)
135 GNUNET_SCHEDULER_shutdown ();
141 do_shutdown (void *cls)
143 unsigned long long delta;
145 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
147 "\nThroughput was %llu kb/s\n",
148 total_bytes * 1000000LL / 1024 / delta);
151 total_bytes * 1000000LL / 1024 / delta,
153 if (NULL != err_task)
155 GNUNET_SCHEDULER_cancel (err_task);
158 terminate_peer (&p1);
159 terminate_peer (&p2);
165 send_message (struct GNUNET_MQ_Handle *mq,
168 struct GNUNET_MQ_Envelope *env;
169 struct TestMessage *hdr;
172 GNUNET_assert (NULL != mq);
173 GNUNET_assert (tr_n < TOTAL_MSGS);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Sending message %u of size %u\n",
179 env = GNUNET_MQ_msg_extra (hdr,
180 s - sizeof (struct TestMessage),
182 hdr->num = htonl (tr_n);
185 s - sizeof (struct TestMessage));
187 GNUNET_SCHEDULER_cancel (err_task);
189 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
190 &terminate_task_error,
199 connect_notify (void *cls,
200 const struct GNUNET_PeerIdentity *peer,
201 struct GNUNET_MQ_Handle *mq)
203 struct PeerContext *pc = cls;
205 if (0 == memcmp (&pc->id,
207 sizeof (struct GNUNET_PeerIdentity)))
208 return (void *) peer;
210 GNUNET_assert (0 == pc->connect_status);
211 pc->connect_status = 1;
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Encrypted connection established to peer `%s'\n",
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Asking core (1) for transmission to peer `%s'\n",
219 GNUNET_i2s (&p2.id));
220 GNUNET_SCHEDULER_cancel (err_task);
222 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
223 &terminate_task_error,
225 start_time = GNUNET_TIME_absolute_get ();
229 return (void *) peer;
234 disconnect_notify (void *cls,
235 const struct GNUNET_PeerIdentity *peer,
238 struct PeerContext *pc = cls;
240 if (0 == memcmp (&pc->id,
242 sizeof (struct GNUNET_PeerIdentity)))
245 pc->connect_status = 0;
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Encrypted connection to `%s' cut\n",
253 check_test (void *cls,
254 const struct TestMessage *hdr)
256 return GNUNET_OK; /* accept all */
261 handle_test (void *cls,
262 const struct TestMessage *hdr)
268 if (ntohs (hdr->header.size) != s)
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Expected message %u of size %u, got %u bytes of message %u\n",
274 ntohs (hdr->header.size),
276 GNUNET_SCHEDULER_cancel (err_task);
277 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
281 if (ntohl (hdr->num) != n)
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "Expected message %u of size %u, got %u bytes of message %u\n",
287 (unsigned int) ntohs (hdr->header.size),
288 (unsigned int) ntohl (hdr->num));
289 GNUNET_SCHEDULER_cancel (err_task);
290 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Got message %u of size %u\n",
296 (unsigned int) ntohl (hdr->num),
297 (unsigned int) ntohs (hdr->header.size));
299 if (0 == (n % (TOTAL_MSGS / 100)))
306 GNUNET_SCHEDULER_shutdown ();
320 init_notify (void *cls,
321 const struct GNUNET_PeerIdentity *my_identity)
323 struct PeerContext *p = cls;
324 struct GNUNET_MQ_MessageHandler handlers[] = {
325 GNUNET_MQ_hd_var_size (test,
329 GNUNET_MQ_handler_end ()
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Connection to CORE service of `%s' established\n",
334 GNUNET_i2s (my_identity));
335 p->id = *my_identity;
338 GNUNET_assert (ok == 2);
341 GNUNET_assert (NULL !=
342 (p2.ch = GNUNET_CORE_connect (p2.cfg,
351 GNUNET_assert (ok == 3);
353 GNUNET_assert (cls == &p2);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Asking transport (1) to connect to peer `%s'\n",
356 GNUNET_i2s (&p2.id));
357 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
365 offer_hello_done (void *cls)
367 struct PeerContext *p = cls;
374 process_hello (void *cls,
375 const struct GNUNET_MessageHeader *message)
377 struct PeerContext *p = cls;
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Received (my) `%s' from transport service\n", "HELLO");
381 GNUNET_assert (message != NULL);
382 GNUNET_free_non_null (p->hello);
383 p->hello = GNUNET_copy_message (message);
384 if ((p == &p1) && (NULL == p2.oh))
385 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
389 if ((p == &p2) && (NULL == p1.oh))
390 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
395 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
396 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
400 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
401 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
409 setup_peer (struct PeerContext *p,
414 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
415 p->cfg = GNUNET_CONFIGURATION_create ();
417 = GNUNET_OS_start_process (GNUNET_YES,
418 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
421 "gnunet-service-arm",
425 GNUNET_assert (GNUNET_OK ==
426 GNUNET_CONFIGURATION_load (p->cfg,
428 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
429 GNUNET_assert (NULL != p->ats);
430 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
431 GNUNET_TRANSPORT_AC_ANY,
434 GNUNET_free (binary);
442 const struct GNUNET_CONFIGURATION_Handle *cfg)
444 struct GNUNET_MQ_MessageHandler handlers[] = {
445 GNUNET_MQ_hd_fixed_size (test,
449 GNUNET_MQ_handler_end ()
452 GNUNET_assert (ok == 1);
455 "test_core_api_peer1.conf");
457 "test_core_api_peer2.conf");
459 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
460 &terminate_task_error,
462 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
465 GNUNET_assert (NULL !=
466 (p1.ch = GNUNET_CORE_connect (p1.cfg,
476 stop_arm (struct PeerContext *p)
478 if (0 != GNUNET_OS_process_kill (p->arm_proc,
480 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
482 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
483 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "ARM process %u stopped\n",
487 GNUNET_OS_process_get_pid (p->arm_proc));
488 GNUNET_OS_process_destroy (p->arm_proc);
490 GNUNET_CONFIGURATION_destroy (p->cfg);
498 char *const argv[] = {
499 "test-core-api-reliability",
501 "test_core_api_data.conf",
504 struct GNUNET_GETOPT_CommandLineOption options[] = {
505 GNUNET_GETOPT_OPTION_END
508 GNUNET_log_setup ("test-core-api-reliability",
511 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
513 "test-core-api-reliability",
520 GNUNET_free_non_null (p1.hello);
521 GNUNET_free_non_null (p2.hello);
522 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
523 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
528 /* end of test_core_api_reliability.c */