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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, \
81 "Now at stage %u at %s:%u\n", ok, __FILE__, \
82 __LINE__); } while (0)
86 struct GNUNET_MessageHeader header;
87 uint32_t num GNUNET_PACKED;
92 get_size (unsigned int iter)
97 return iter + sizeof(struct TestMessage);
98 ret = (iter * iter * iter);
99 return sizeof(struct TestMessage) + (ret % 60000);
104 terminate_peer (struct PeerContext *p)
108 GNUNET_CORE_disconnect (p->ch);
113 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
118 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
121 if (NULL != p->ats_sh)
123 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
128 GNUNET_ATS_connectivity_done (p->ats);
135 terminate_task_error (void *cls)
139 GNUNET_SCHEDULER_shutdown ();
145 do_shutdown (void *cls)
147 unsigned long long delta;
149 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
153 "\nThroughput was %llu kb/s\n",
154 total_bytes * 1000000LL / 1024 / delta);
157 total_bytes * 1000000LL / 1024 / delta,
159 if (NULL != err_task)
161 GNUNET_SCHEDULER_cancel (err_task);
164 terminate_peer (&p1);
165 terminate_peer (&p2);
170 send_message (struct GNUNET_MQ_Handle *mq,
173 struct GNUNET_MQ_Envelope *env;
174 struct TestMessage *hdr;
177 GNUNET_assert (NULL != mq);
178 GNUNET_assert (tr_n < TOTAL_MSGS);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Sending message %u of size %u\n",
184 env = GNUNET_MQ_msg_extra (hdr,
185 s - sizeof(struct TestMessage),
187 hdr->num = htonl (tr_n);
190 s - sizeof(struct TestMessage));
192 GNUNET_SCHEDULER_cancel (err_task);
194 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
195 &terminate_task_error,
204 connect_notify (void *cls,
205 const struct GNUNET_PeerIdentity *peer,
206 struct GNUNET_MQ_Handle *mq)
208 struct PeerContext *pc = cls;
210 if (0 == memcmp (&pc->id,
212 sizeof(struct GNUNET_PeerIdentity)))
213 return (void *) peer;
215 GNUNET_assert (0 == pc->connect_status);
216 pc->connect_status = 1;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Encrypted connection established to peer `%s'\n",
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Asking core (1) for transmission to peer `%s'\n",
224 GNUNET_i2s (&p2.id));
225 GNUNET_SCHEDULER_cancel (err_task);
227 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
228 &terminate_task_error,
230 start_time = GNUNET_TIME_absolute_get ();
234 return (void *) peer;
239 disconnect_notify (void *cls,
240 const struct GNUNET_PeerIdentity *peer,
243 struct PeerContext *pc = cls;
245 if (0 == memcmp (&pc->id,
247 sizeof(struct GNUNET_PeerIdentity)))
250 pc->connect_status = 0;
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Encrypted connection to `%s' cut\n",
258 check_test (void *cls,
259 const struct TestMessage *hdr)
261 return GNUNET_OK; /* accept all */
266 handle_test (void *cls,
267 const struct TestMessage *hdr)
273 if (ntohs (hdr->header.size) != s)
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 "Expected message %u of size %u, got %u bytes of message %u\n",
279 ntohs (hdr->header.size),
281 GNUNET_SCHEDULER_cancel (err_task);
282 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
286 if (ntohl (hdr->num) != n)
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 "Expected message %u of size %u, got %u bytes of message %u\n",
292 (unsigned int) ntohs (hdr->header.size),
293 (unsigned int) ntohl (hdr->num));
294 GNUNET_SCHEDULER_cancel (err_task);
295 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Got message %u of size %u\n",
301 (unsigned int) ntohl (hdr->num),
302 (unsigned int) ntohs (hdr->header.size));
304 if (0 == (n % (TOTAL_MSGS / 100)))
311 GNUNET_SCHEDULER_shutdown ();
325 init_notify (void *cls,
326 const struct GNUNET_PeerIdentity *my_identity)
328 struct PeerContext *p = cls;
329 struct GNUNET_MQ_MessageHandler handlers[] = {
330 GNUNET_MQ_hd_var_size (test,
334 GNUNET_MQ_handler_end ()
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Connection to CORE service of `%s' established\n",
339 GNUNET_i2s (my_identity));
340 p->id = *my_identity;
343 GNUNET_assert (ok == 2);
346 GNUNET_assert (NULL !=
347 (p2.ch = GNUNET_CORE_connect (p2.cfg,
356 GNUNET_assert (ok == 3);
358 GNUNET_assert (cls == &p2);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Asking transport (1) to connect to peer `%s'\n",
361 GNUNET_i2s (&p2.id));
362 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
370 offer_hello_done (void *cls)
372 struct PeerContext *p = cls;
379 process_hello (void *cls,
380 const struct GNUNET_MessageHeader *message)
382 struct PeerContext *p = cls;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Received (my) `%s' from transport service\n", "HELLO");
386 GNUNET_assert (message != NULL);
387 GNUNET_free_non_null (p->hello);
388 p->hello = GNUNET_copy_message (message);
389 if ((p == &p1) && (NULL == p2.oh))
390 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
394 if ((p == &p2) && (NULL == p1.oh))
395 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
400 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh))
401 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
405 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh))
406 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
414 setup_peer (struct PeerContext *p,
419 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
420 p->cfg = GNUNET_CONFIGURATION_create ();
422 = GNUNET_OS_start_process (GNUNET_YES,
423 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
426 "gnunet-service-arm",
430 GNUNET_assert (GNUNET_OK ==
431 GNUNET_CONFIGURATION_load (p->cfg,
433 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
434 GNUNET_assert (NULL != p->ats);
435 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
436 GNUNET_TRANSPORT_AC_ANY,
439 GNUNET_free (binary);
447 const struct GNUNET_CONFIGURATION_Handle *cfg)
449 struct GNUNET_MQ_MessageHandler handlers[] = {
450 GNUNET_MQ_hd_fixed_size (test,
454 GNUNET_MQ_handler_end ()
457 GNUNET_assert (ok == 1);
460 "test_core_api_peer1.conf");
462 "test_core_api_peer2.conf");
464 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
465 &terminate_task_error,
467 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
470 GNUNET_assert (NULL !=
471 (p1.ch = GNUNET_CORE_connect (p1.cfg,
481 stop_arm (struct PeerContext *p)
483 if (0 != GNUNET_OS_process_kill (p->arm_proc,
485 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
487 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
488 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "ARM process %u stopped\n",
492 GNUNET_OS_process_get_pid (p->arm_proc));
493 GNUNET_OS_process_destroy (p->arm_proc);
495 GNUNET_CONFIGURATION_destroy (p->cfg);
503 char *const argv[] = {
504 "test-core-api-reliability",
506 "test_core_api_data.conf",
509 struct GNUNET_GETOPT_CommandLineOption options[] = {
510 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_free_non_null (p1.hello);
527 GNUNET_free_non_null (p2.hello);
528 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
529 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
535 /* end of test_core_api_reliability.c */