converting core tests to hello_get API
[oweals/gnunet.git] / src / core / test_core_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file core/test_core_api_reliability.c
22  * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
23  */
24 #include "platform.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_transport_hello_service.h"
31 #include <gauger.h>
32
33 /**
34  * Note that this value must not significantly exceed
35  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
36  * messages may be dropped even for a reliable transport.
37  */
38 #define TOTAL_MSGS (600 * 10)
39
40 /**
41  * How long until we give up on transmitting the message?
42  */
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
44
45 /**
46  * What delay do we request from the core service for transmission?
47  */
48 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49
50 #define MTYPE 12345
51
52
53 static unsigned long long total_bytes;
54
55 static struct GNUNET_TIME_Absolute start_time;
56
57 static struct GNUNET_SCHEDULER_Task *err_task;
58
59
60 struct PeerContext
61 {
62   struct GNUNET_CONFIGURATION_Handle *cfg;
63   struct GNUNET_CORE_Handle *ch;
64   struct GNUNET_PeerIdentity id;
65   struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
66   struct GNUNET_MessageHeader *hello;
67   struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
68   struct GNUNET_ATS_ConnectivityHandle *ats;
69   struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
70   int connect_status;
71   struct GNUNET_OS_Process *arm_proc;
72 };
73
74 static struct PeerContext p1;
75
76 static struct PeerContext p2;
77
78 static struct GNUNET_CORE_TransmitHandle *nth;
79
80 static int ok;
81
82 static int32_t tr_n;
83
84
85 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
86
87 struct TestMessage
88 {
89   struct GNUNET_MessageHeader header;
90   uint32_t num GNUNET_PACKED;
91 };
92
93
94 static unsigned int
95 get_size (unsigned int iter)
96 {
97   unsigned int ret;
98
99   if (iter < 60000)
100     return iter + sizeof (struct TestMessage);
101   ret = (iter * iter * iter);
102   return sizeof (struct TestMessage) + (ret % 60000);
103 }
104
105
106 static void
107 terminate_peer (struct PeerContext *p)
108 {
109   if (NULL != p->ch)
110   {
111     GNUNET_CORE_disconnect (p->ch);
112     p->ch = NULL;
113   }
114   if (NULL != p->ghh)
115   {
116     GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
117     p->ghh = NULL;
118   }
119   if (NULL != p->oh)
120   {
121     GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
122     p->oh = NULL;
123   }
124   if (NULL != p->ats_sh)
125   {
126     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
127     p->ats_sh = NULL;
128   }
129   if (NULL != p->ats)
130   {
131     GNUNET_ATS_connectivity_done (p->ats);
132     p->ats = NULL;
133   }
134 }
135
136
137 static void
138 terminate_task_error (void *cls)
139 {
140   err_task = NULL;
141   GNUNET_break (0);
142   GNUNET_SCHEDULER_shutdown ();
143   ok = 42;
144 }
145
146
147 static void
148 do_shutdown (void *cls)
149 {
150   unsigned long long delta;
151
152   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
153   FPRINTF (stderr,
154            "\nThroughput was %llu kb/s\n",
155            total_bytes * 1000000LL / 1024 / delta);
156   GAUGER ("CORE",
157           "Core throughput/s",
158           total_bytes * 1000000LL / 1024 / delta,
159           "kb/s");
160   if (NULL != err_task)
161   {
162     GNUNET_SCHEDULER_cancel (err_task);
163     err_task = NULL;
164   }
165   if (NULL != nth)
166   {
167     GNUNET_CORE_notify_transmit_ready_cancel (nth);
168     nth = NULL;
169   }
170   terminate_peer (&p1);
171   terminate_peer (&p2);
172   
173 }
174
175
176 static size_t
177 transmit_ready (void *cls,
178                 size_t size,
179                 void *buf)
180 {
181   char *cbuf = buf;
182   struct TestMessage hdr;
183   unsigned int s;
184   unsigned int ret;
185
186   nth = NULL;
187   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
188   if (NULL == buf)
189   {
190     if (NULL != p1.ch)
191       GNUNET_break (NULL !=
192                     (nth = GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO,
193                                                               GNUNET_CORE_PRIO_BEST_EFFORT,
194                                                               FAST_TIMEOUT,
195                                                               &p2.id,
196                                                               get_size (tr_n),
197                                                               &transmit_ready,
198                                                               &p1)));
199     return 0;
200   }
201   GNUNET_assert (tr_n < TOTAL_MSGS);
202   ret = 0;
203   s = get_size (tr_n);
204   GNUNET_assert (size >= s);
205   GNUNET_assert (buf != NULL);
206   cbuf = buf;
207   do
208   {
209     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210                 "Sending message %u of size %u at offset %u\n",
211                 tr_n,
212                 s,
213                 ret);
214     hdr.header.size = htons (s);
215     hdr.header.type = htons (MTYPE);
216     hdr.num = htonl (tr_n);
217     GNUNET_memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
218     ret += sizeof (struct TestMessage);
219     memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
220     ret += s - sizeof (struct TestMessage);
221     tr_n++;
222     s = get_size (tr_n);
223     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
224       break;                    /* sometimes pack buffer full, sometimes not */
225   }
226   while (size - ret >= s);
227   GNUNET_SCHEDULER_cancel (err_task);
228   err_task =
229       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
230                                     &terminate_task_error,
231                                     NULL);
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Returning total message block of size %u\n",
234               ret);
235   total_bytes += ret;
236   return ret;
237 }
238
239
240 static void
241 connect_notify (void *cls,
242                 const struct GNUNET_PeerIdentity *peer)
243 {
244   struct PeerContext *pc = cls;
245
246   if (0 == memcmp (&pc->id,
247                    peer,
248                    sizeof (struct GNUNET_PeerIdentity)))
249     return;
250   GNUNET_assert (0 == pc->connect_status);
251   pc->connect_status = 1;
252   if (pc == &p1)
253   {
254     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255                 "Encrypted connection established to peer `%s'\n",
256                 GNUNET_i2s (peer));
257     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258                 "Asking core (1) for transmission to peer `%s'\n",
259                 GNUNET_i2s (&p2.id));
260     GNUNET_SCHEDULER_cancel (err_task);
261     err_task =
262         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
263                                       &terminate_task_error,
264                                       NULL);
265     start_time = GNUNET_TIME_absolute_get ();
266     GNUNET_break (NULL !=
267                   (nth = GNUNET_CORE_notify_transmit_ready (p1.ch,
268                                                             GNUNET_NO,
269                                                             GNUNET_CORE_PRIO_BEST_EFFORT,
270                                                             TIMEOUT,
271                                                             &p2.id,
272                                                             get_size (0),
273                                                             &transmit_ready,
274                                                             &p1)));
275   }
276 }
277
278
279 static void
280 disconnect_notify (void *cls,
281                    const struct GNUNET_PeerIdentity *peer)
282 {
283   struct PeerContext *pc = cls;
284
285   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
286     return;
287   pc->connect_status = 0;
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               "Encrypted connection to `%s' cut\n",
290               GNUNET_i2s (peer));
291 }
292
293
294 static int
295 inbound_notify (void *cls,
296                 const struct GNUNET_PeerIdentity *other,
297                 const struct GNUNET_MessageHeader *message)
298 {
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300               "Core provides inbound data from `%s'.\n",
301               GNUNET_i2s (other));
302   return GNUNET_OK;
303 }
304
305
306 static int
307 outbound_notify (void *cls,
308                  const struct GNUNET_PeerIdentity *other,
309                  const struct GNUNET_MessageHeader *message)
310 {
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312               "Core notifies about outbound data for `%s'.\n",
313               GNUNET_i2s (other));
314   return GNUNET_OK;
315 }
316
317
318 static size_t
319 transmit_ready (void *cls,
320                 size_t size,
321                 void *buf);
322
323
324 static int
325 process_mtype (void *cls,
326                const struct GNUNET_PeerIdentity *peer,
327                const struct GNUNET_MessageHeader *message)
328 {
329   static int n;
330   unsigned int s;
331   const struct TestMessage *hdr;
332
333   hdr = (const struct TestMessage *) message;
334   s = get_size (n);
335   if (MTYPE != ntohs (message->type))
336     return GNUNET_SYSERR;
337   if (ntohs (message->size) != s)
338   {
339     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340                 "Expected message %u of size %u, got %u bytes of message %u\n",
341                 n, s,
342                 ntohs (message->size),
343                 ntohl (hdr->num));
344     GNUNET_SCHEDULER_cancel (err_task);
345     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
346                                          NULL);
347     return GNUNET_SYSERR;
348   }
349   if (ntohl (hdr->num) != n)
350   {
351     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352                 "Expected message %u of size %u, got %u bytes of message %u\n",
353                 n, s,
354                 ntohs (message->size),
355                 ntohl (hdr->num));
356     GNUNET_SCHEDULER_cancel (err_task);
357     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
358                                          NULL);
359     return GNUNET_SYSERR;
360   }
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362               "Got message %u of size %u\n",
363               ntohl (hdr->num),
364               ntohs (message->size));
365   n++;
366   if (0 == (n % (TOTAL_MSGS / 100)))
367     FPRINTF (stderr, "%s",  ".");
368   if (n == TOTAL_MSGS)
369   {
370     ok = 0;
371     GNUNET_SCHEDULER_shutdown ();
372   }
373   else
374   {
375     if (n == tr_n)
376       GNUNET_break (NULL !=
377                     GNUNET_CORE_notify_transmit_ready (p1.ch,
378                                                        GNUNET_NO /* no cork */,
379                                                        GNUNET_CORE_PRIO_BEST_EFFORT,
380                                                        FAST_TIMEOUT /* ignored! */,
381                                                        &p2.id,
382                                                        get_size (tr_n),
383                                                        &transmit_ready, &p1));
384   }
385   return GNUNET_OK;
386 }
387
388
389 static struct GNUNET_CORE_MessageHandler handlers[] = {
390   {&process_mtype, MTYPE, 0},
391   {NULL, 0, 0}
392 };
393
394
395 static void
396 init_notify (void *cls,
397              const struct GNUNET_PeerIdentity *my_identity)
398 {
399   struct PeerContext *p = cls;
400
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402               "Connection to CORE service of `%s' established\n",
403               GNUNET_i2s (my_identity));
404   p->id = *my_identity;
405   if (cls == &p1)
406   {
407     GNUNET_assert (ok == 2);
408     OKPP;
409     /* connect p2 */
410     GNUNET_assert (NULL !=
411                    (p2.ch = GNUNET_CORE_connect (p2.cfg,
412                                                  &p2,
413                                                  &init_notify,
414                                                  &connect_notify,
415                                                  &disconnect_notify,
416                                                  &inbound_notify,
417                                                  GNUNET_YES,
418                                                  &outbound_notify,
419                                                  GNUNET_YES,
420                                                  handlers)));
421   }
422   else
423   {
424     GNUNET_assert (ok == 3);
425     OKPP;
426     GNUNET_assert (cls == &p2);
427     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428                 "Asking transport (1) to connect to peer `%s'\n",
429                 GNUNET_i2s (&p2.id));
430     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
431                                                  &p2.id,
432                                                  1);
433   }
434 }
435
436
437 static void
438 offer_hello_done (void *cls)
439 {
440   struct PeerContext *p = cls;
441
442   p->oh = NULL;
443 }
444
445
446 static void
447 process_hello (void *cls,
448                const struct GNUNET_MessageHeader *message)
449 {
450   struct PeerContext *p = cls;
451
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453               "Received (my) `%s' from transport service\n", "HELLO");
454   GNUNET_assert (message != NULL);
455   p->hello = GNUNET_copy_message (message);
456   if ((p == &p1) && (NULL == p2.oh))
457     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
458                                           message,
459                                           &offer_hello_done,
460                                           &p2);
461   if ((p == &p2) && (NULL == p1.oh))
462     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
463                                           message,
464                                           &offer_hello_done,
465                                           &p1);
466
467   if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
468     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
469                                           p2.hello,
470                                           &offer_hello_done,
471                                           &p1);
472   if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
473     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
474                                           p1.hello,
475                                           &offer_hello_done,
476                                           &p2);
477 }
478
479
480 static void
481 setup_peer (struct PeerContext *p,
482             const char *cfgname)
483 {
484   char *binary;
485
486   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
487   p->cfg = GNUNET_CONFIGURATION_create ();
488   p->arm_proc
489     = GNUNET_OS_start_process (GNUNET_YES,
490                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
491                                NULL, NULL, NULL,
492                                binary,
493                                "gnunet-service-arm",
494                                "-c",
495                                cfgname,
496                                NULL);
497   GNUNET_assert (GNUNET_OK ==
498                  GNUNET_CONFIGURATION_load (p->cfg,
499                                             cfgname));
500   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
501   GNUNET_assert (NULL != p->ats);
502   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
503                                        GNUNET_TRANSPORT_AC_ANY,
504                                        &process_hello,
505                                        p);
506   GNUNET_free (binary);
507 }
508
509
510 static void
511 run (void *cls,
512      char *const *args,
513      const char *cfgfile,
514      const struct GNUNET_CONFIGURATION_Handle *cfg)
515 {
516   GNUNET_assert (ok == 1);
517   OKPP;
518   setup_peer (&p1,
519               "test_core_api_peer1.conf");
520   setup_peer (&p2,
521               "test_core_api_peer2.conf");
522   err_task =
523       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
524                                     &terminate_task_error,
525                                     NULL);
526   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
527                                  NULL);
528
529   GNUNET_assert (NULL !=
530                  (p1.ch = GNUNET_CORE_connect (p1.cfg,
531                                                &p1,
532                                                &init_notify,
533                                                &connect_notify,
534                                                &disconnect_notify,
535                                                &inbound_notify,
536                                                GNUNET_YES,
537                                                &outbound_notify,
538                                                GNUNET_YES,
539                                                handlers)));
540 }
541
542
543 static void
544 stop_arm (struct PeerContext *p)
545 {
546   if (0 != GNUNET_OS_process_kill (p->arm_proc,
547                                    GNUNET_TERM_SIG))
548     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
549                          "kill");
550   if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
551     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
552                          "waitpid");
553   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554               "ARM process %u stopped\n",
555               GNUNET_OS_process_get_pid (p->arm_proc));
556   GNUNET_OS_process_destroy (p->arm_proc);
557   p->arm_proc = NULL;
558   GNUNET_CONFIGURATION_destroy (p->cfg);
559 }
560
561
562 int
563 main (int argc,
564       char *argv1[])
565 {
566   char *const argv[] = {
567     "test-core-api-reliability",
568     "-c",
569     "test_core_api_data.conf",
570     NULL
571   };
572   struct GNUNET_GETOPT_CommandLineOption options[] = {
573     GNUNET_GETOPT_OPTION_END
574   };
575   ok = 1;
576   GNUNET_log_setup ("test-core-api-reliability",
577                     "WARNING",
578                     NULL);
579   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
580                       argv,
581                       "test-core-api-reliability",
582                       "nohelp",
583                       options,
584                       &run,
585                       &ok);
586   stop_arm (&p1);
587   stop_arm (&p2);
588   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
589   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
590
591   return ok;
592 }
593
594 /* end of test_core_api_reliability.c */