first step to remove plibc
[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 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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
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
24  */
25 #include "platform.h"
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"
32 #include <gauger.h>
33
34 /**
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.
38  */
39 #define TOTAL_MSGS (600 * 10)
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
45
46 #define MTYPE 12345
47
48
49 static unsigned long long total_bytes;
50
51 static struct GNUNET_TIME_Absolute start_time;
52
53 static struct GNUNET_SCHEDULER_Task *err_task;
54
55
56 struct PeerContext
57 {
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;
67   int connect_status;
68   struct GNUNET_OS_Process *arm_proc;
69 };
70
71 static struct PeerContext p1;
72
73 static struct PeerContext p2;
74
75 static int ok;
76
77 static int32_t tr_n;
78
79
80 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
81
82 struct TestMessage
83 {
84   struct GNUNET_MessageHeader header;
85   uint32_t num GNUNET_PACKED;
86 };
87
88
89 static unsigned int
90 get_size (unsigned int iter)
91 {
92   unsigned int ret;
93
94   if (iter < 60000)
95     return iter + sizeof (struct TestMessage);
96   ret = (iter * iter * iter);
97   return sizeof (struct TestMessage) + (ret % 60000);
98 }
99
100
101 static void
102 terminate_peer (struct PeerContext *p)
103 {
104   if (NULL != p->ch)
105   {
106     GNUNET_CORE_disconnect (p->ch);
107     p->ch = NULL;
108   }
109   if (NULL != p->ghh)
110   {
111     GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
112     p->ghh = NULL;
113   }
114   if (NULL != p->oh)
115   {
116     GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
117     p->oh = NULL;
118   }
119   if (NULL != p->ats_sh)
120   {
121     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
122     p->ats_sh = NULL;
123   }
124   if (NULL != p->ats)
125   {
126     GNUNET_ATS_connectivity_done (p->ats);
127     p->ats = NULL;
128   }
129 }
130
131
132 static void
133 terminate_task_error (void *cls)
134 {
135   err_task = NULL;
136   GNUNET_break (0);
137   GNUNET_SCHEDULER_shutdown ();
138   ok = 42;
139 }
140
141
142 static void
143 do_shutdown (void *cls)
144 {
145   unsigned long long delta;
146
147   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
148   if (0 == delta)
149     delta = 1;
150   fprintf (stderr,
151            "\nThroughput was %llu kb/s\n",
152            total_bytes * 1000000LL / 1024 / delta);
153   GAUGER ("CORE",
154           "Core throughput/s",
155           total_bytes * 1000000LL / 1024 / delta,
156           "kb/s");
157   if (NULL != err_task)
158   {
159     GNUNET_SCHEDULER_cancel (err_task);
160     err_task = NULL;
161   }
162   terminate_peer (&p1);
163   terminate_peer (&p2);
164
165 }
166
167
168 static void
169 send_message (struct GNUNET_MQ_Handle *mq,
170               int32_t num)
171 {
172   struct GNUNET_MQ_Envelope *env;
173   struct TestMessage *hdr;
174   unsigned int s;
175
176   GNUNET_assert (NULL != mq);
177   GNUNET_assert (tr_n < TOTAL_MSGS);
178   s = get_size (tr_n);
179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180               "Sending message %u of size %u\n",
181               tr_n,
182               s);
183   env = GNUNET_MQ_msg_extra (hdr,
184                              s - sizeof (struct TestMessage),
185                              MTYPE);
186   hdr->num = htonl (tr_n);
187   memset (&hdr[1],
188           tr_n,
189           s - sizeof (struct TestMessage));
190   tr_n++;
191   GNUNET_SCHEDULER_cancel (err_task);
192   err_task =
193       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
194                                     &terminate_task_error,
195                                     NULL);
196   total_bytes += s;
197   GNUNET_MQ_send (mq,
198                   env);
199 }
200
201
202 static void *
203 connect_notify (void *cls,
204                 const struct GNUNET_PeerIdentity *peer,
205                 struct GNUNET_MQ_Handle *mq)
206 {
207   struct PeerContext *pc = cls;
208
209   if (0 == memcmp (&pc->id,
210                    peer,
211                    sizeof (struct GNUNET_PeerIdentity)))
212     return (void *) peer;
213   pc->mq = mq;
214   GNUNET_assert (0 == pc->connect_status);
215   pc->connect_status = 1;
216   if (pc == &p1)
217   {
218     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219                 "Encrypted connection established to peer `%s'\n",
220                 GNUNET_i2s (peer));
221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222                 "Asking core (1) for transmission to peer `%s'\n",
223                 GNUNET_i2s (&p2.id));
224     GNUNET_SCHEDULER_cancel (err_task);
225     err_task =
226         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
227                                       &terminate_task_error,
228                                       NULL);
229     start_time = GNUNET_TIME_absolute_get ();
230     send_message (mq,
231                   0);
232   }
233   return (void *) peer;
234 }
235
236
237 static void
238 disconnect_notify (void *cls,
239                    const struct GNUNET_PeerIdentity *peer,
240                    void *internal_cls)
241 {
242   struct PeerContext *pc = cls;
243
244   if (0 == memcmp (&pc->id,
245                    peer,
246                    sizeof (struct GNUNET_PeerIdentity)))
247     return;
248   pc->mq = NULL;
249   pc->connect_status = 0;
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "Encrypted connection to `%s' cut\n",
252               GNUNET_i2s (peer));
253 }
254
255
256 static int
257 check_test (void *cls,
258             const struct TestMessage *hdr)
259 {
260   return GNUNET_OK; /* accept all */
261 }
262
263
264 static void
265 handle_test (void *cls,
266              const struct TestMessage *hdr)
267 {
268   static int n;
269   unsigned int s;
270
271   s = get_size (n);
272   if (ntohs (hdr->header.size) != s)
273   {
274     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275                 "Expected message %u of size %u, got %u bytes of message %u\n",
276                 n,
277                 s,
278                 ntohs (hdr->header.size),
279                 ntohl (hdr->num));
280     GNUNET_SCHEDULER_cancel (err_task);
281     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
282                                          NULL);
283     return;
284   }
285   if (ntohl (hdr->num) != n)
286   {
287     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288                 "Expected message %u of size %u, got %u bytes of message %u\n",
289                 n,
290                 s,
291                 (unsigned int) ntohs (hdr->header.size),
292                 (unsigned int) ntohl (hdr->num));
293     GNUNET_SCHEDULER_cancel (err_task);
294     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
295                                          NULL);
296     return;
297   }
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299               "Got message %u of size %u\n",
300               (unsigned int) ntohl (hdr->num),
301               (unsigned int) ntohs (hdr->header.size));
302   n++;
303   if (0 == (n % (TOTAL_MSGS / 100)))
304     fprintf (stderr,
305              "%s",
306              ".");
307   if (n == TOTAL_MSGS)
308   {
309     ok = 0;
310     GNUNET_SCHEDULER_shutdown ();
311   }
312   else
313   {
314     if (n == tr_n)
315     {
316       send_message (p1.mq,
317                     tr_n);
318     }
319   }
320 }
321
322
323 static void
324 init_notify (void *cls,
325              const struct GNUNET_PeerIdentity *my_identity)
326 {
327   struct PeerContext *p = cls;
328   struct GNUNET_MQ_MessageHandler handlers[] = {
329     GNUNET_MQ_hd_var_size (test,
330                            MTYPE,
331                            struct TestMessage,
332                            NULL),
333     GNUNET_MQ_handler_end ()
334   };
335
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337               "Connection to CORE service of `%s' established\n",
338               GNUNET_i2s (my_identity));
339   p->id = *my_identity;
340   if (cls == &p1)
341   {
342     GNUNET_assert (ok == 2);
343     OKPP;
344     /* connect p2 */
345     GNUNET_assert (NULL !=
346                    (p2.ch = GNUNET_CORE_connect (p2.cfg,
347                                                  &p2,
348                                                  &init_notify,
349                                                  &connect_notify,
350                                                  &disconnect_notify,
351                                                  handlers)));
352   }
353   else
354   {
355     GNUNET_assert (ok == 3);
356     OKPP;
357     GNUNET_assert (cls == &p2);
358     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359                 "Asking transport (1) to connect to peer `%s'\n",
360                 GNUNET_i2s (&p2.id));
361     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
362                                                  &p2.id,
363                                                  1);
364   }
365 }
366
367
368 static void
369 offer_hello_done (void *cls)
370 {
371   struct PeerContext *p = cls;
372
373   p->oh = NULL;
374 }
375
376
377 static void
378 process_hello (void *cls,
379                const struct GNUNET_MessageHeader *message)
380 {
381   struct PeerContext *p = cls;
382
383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384               "Received (my) `%s' from transport service\n", "HELLO");
385   GNUNET_assert (message != NULL);
386   GNUNET_free_non_null (p->hello);
387   p->hello = GNUNET_copy_message (message);
388   if ((p == &p1) && (NULL == p2.oh))
389     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
390                                           message,
391                                           &offer_hello_done,
392                                           &p2);
393   if ((p == &p2) && (NULL == p1.oh))
394     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
395                                           message,
396                                           &offer_hello_done,
397                                           &p1);
398
399   if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
400     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
401                                           p2.hello,
402                                           &offer_hello_done,
403                                           &p1);
404   if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
405     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
406                                           p1.hello,
407                                           &offer_hello_done,
408                                           &p2);
409 }
410
411
412 static void
413 setup_peer (struct PeerContext *p,
414             const char *cfgname)
415 {
416   char *binary;
417
418   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
419   p->cfg = GNUNET_CONFIGURATION_create ();
420   p->arm_proc
421     = GNUNET_OS_start_process (GNUNET_YES,
422                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
423                                NULL, NULL, NULL,
424                                binary,
425                                "gnunet-service-arm",
426                                "-c",
427                                cfgname,
428                                NULL);
429   GNUNET_assert (GNUNET_OK ==
430                  GNUNET_CONFIGURATION_load (p->cfg,
431                                             cfgname));
432   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
433   GNUNET_assert (NULL != p->ats);
434   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
435                                        GNUNET_TRANSPORT_AC_ANY,
436                                        &process_hello,
437                                        p);
438   GNUNET_free (binary);
439 }
440
441
442 static void
443 run (void *cls,
444      char *const *args,
445      const char *cfgfile,
446      const struct GNUNET_CONFIGURATION_Handle *cfg)
447 {
448   struct GNUNET_MQ_MessageHandler handlers[] = {
449     GNUNET_MQ_hd_fixed_size (test,
450                              MTYPE,
451                              struct TestMessage,
452                              NULL),
453     GNUNET_MQ_handler_end ()
454   };
455
456   GNUNET_assert (ok == 1);
457   OKPP;
458   setup_peer (&p1,
459               "test_core_api_peer1.conf");
460   setup_peer (&p2,
461               "test_core_api_peer2.conf");
462   err_task =
463       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
464                                     &terminate_task_error,
465                                     NULL);
466   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
467                                  NULL);
468
469   GNUNET_assert (NULL !=
470                  (p1.ch = GNUNET_CORE_connect (p1.cfg,
471                                                &p1,
472                                                &init_notify,
473                                                &connect_notify,
474                                                &disconnect_notify,
475                                                handlers)));
476 }
477
478
479 static void
480 stop_arm (struct PeerContext *p)
481 {
482   if (0 != GNUNET_OS_process_kill (p->arm_proc,
483                                    GNUNET_TERM_SIG))
484     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
485                          "kill");
486   if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
487     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
488                          "waitpid");
489   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490               "ARM process %u stopped\n",
491               GNUNET_OS_process_get_pid (p->arm_proc));
492   GNUNET_OS_process_destroy (p->arm_proc);
493   p->arm_proc = NULL;
494   GNUNET_CONFIGURATION_destroy (p->cfg);
495 }
496
497
498 int
499 main (int argc,
500       char *argv1[])
501 {
502   char *const argv[] = {
503     "test-core-api-reliability",
504     "-c",
505     "test_core_api_data.conf",
506     NULL
507   };
508   struct GNUNET_GETOPT_CommandLineOption options[] = {
509     GNUNET_GETOPT_OPTION_END
510   };
511   ok = 1;
512   GNUNET_log_setup ("test-core-api-reliability",
513                     "WARNING",
514                     NULL);
515   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
516                       argv,
517                       "test-core-api-reliability",
518                       "nohelp",
519                       options,
520                       &run,
521                       &ok);
522   stop_arm (&p1);
523   stop_arm (&p2);
524   GNUNET_free_non_null (p1.hello);
525   GNUNET_free_non_null (p2.hello);
526   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
527   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
528
529   return ok;
530 }
531
532 /* end of test_core_api_reliability.c */