01eafbfe0e47479317c7bbec70bc265f9eafafb2
[oweals/gnunet.git] / src / core / test_core_quota_compliance.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_quota_compliance.c
22  * @brief testcase for core_api.c focusing quota compliance on core level
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 "gnunet_statistics_service.h"
32
33
34 #define SYMMETRIC 0
35 #define ASYMMETRIC_SEND_LIMITED 1
36 #define ASYMMETRIC_RECV_LIMITED 2
37
38 /**
39  * Note that this value must not significantly exceed
40  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
41  * messages may be dropped even for a reliable transport.
42  */
43 #define TOTAL_MSGS (60000 * 10)
44
45 /**
46  * How long until we give up on transmitting the message?
47  */
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
49
50 /**
51  * What delay do we request from the core service for transmission?
52  */
53 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
54
55 #define MTYPE 12345
56 #define MESSAGESIZE 1024
57 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58
59 static unsigned long long total_bytes_sent;
60 static unsigned long long total_bytes_recv;
61
62 static struct GNUNET_TIME_Absolute start_time;
63
64 static struct GNUNET_SCHEDULER_Task *err_task;
65
66 static struct GNUNET_SCHEDULER_Task *measure_task;
67
68
69 struct PeerContext
70 {
71   struct GNUNET_CONFIGURATION_Handle *cfg;
72   struct GNUNET_CORE_Handle *ch;
73   struct GNUNET_CORE_TransmitHandle *nth;
74   struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
75   struct GNUNET_PeerIdentity id;
76   struct GNUNET_MessageHeader *hello;
77   struct GNUNET_STATISTICS_Handle *stats;
78   struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
79   struct GNUNET_ATS_ConnectivityHandle *ats;
80   struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
81   int connect_status;
82   struct GNUNET_OS_Process *arm_proc;
83 };
84
85 static struct PeerContext p1;
86 static struct PeerContext p2;
87
88 static unsigned long long current_quota_p1_in;
89 static unsigned long long current_quota_p1_out;
90 static unsigned long long current_quota_p2_in;
91 static unsigned long long current_quota_p2_out;
92
93 static int ok;
94 static int test;
95 static int32_t tr_n;
96
97 static int running;
98
99
100 #if VERBOSE
101 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
102 #else
103 #define OKPP do { ok++; } while (0)
104 #endif
105
106 struct TestMessage
107 {
108   struct GNUNET_MessageHeader header;
109   uint32_t num;
110 };
111
112
113 static void
114 terminate_peer (struct PeerContext *p)
115 {
116   if (NULL != p->nth)
117   {
118     GNUNET_CORE_notify_transmit_ready_cancel (p->nth);
119     p->nth = NULL;
120   }
121   if (NULL != p->ch)
122   {
123     GNUNET_CORE_disconnect (p->ch);
124     p->ch = NULL;
125   }
126   if (NULL != p->ghh)
127   {
128     GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
129     p->ghh = NULL;
130   }
131   if (NULL != p->oh)
132   {
133     GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
134     p->oh = NULL;
135   }
136   if (NULL != p->ats_sh)
137   {
138     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
139     p->ats_sh = NULL;
140   }
141   if (NULL != p->ats)
142   {
143     GNUNET_ATS_connectivity_done (p->ats);
144     p->ats = NULL;
145   }
146   if (NULL != p->stats)
147   {
148     GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
149     p->stats = NULL;
150   }
151   if (NULL != p->hello)
152   {
153     GNUNET_free (p->hello);
154     p->hello = NULL;
155   }
156 }
157
158
159 static void
160 shutdown_task (void *cls)
161 {
162   if (NULL != err_task)
163   {
164     GNUNET_SCHEDULER_cancel (err_task);
165     err_task = NULL;
166   }
167   if (NULL != measure_task)
168   {
169     GNUNET_SCHEDULER_cancel (measure_task);
170     measure_task = NULL;
171   }
172   terminate_peer (&p1);
173   terminate_peer (&p2);
174 }
175
176
177 static void
178 terminate_task_error (void *cls)
179 {
180   err_task = NULL;
181   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182               "Testcase failed (timeout)!\n");
183   GNUNET_SCHEDULER_shutdown ();
184   ok = 42;
185 }
186
187
188 /**
189  * Callback function to process statistic values.
190  *
191  * @param cls closure
192  * @param subsystem name of subsystem that created the statistic
193  * @param name the name of the datum
194  * @param value the current value
195  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
196  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
197  */
198 static int
199 print_stat (void *cls,
200             const char *subsystem,
201             const char *name,
202             uint64_t value,
203             int is_persistent)
204 {
205   if (cls == &p1)
206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207                 "Peer1 %50s = %12llu\n",
208                 name,
209                 (unsigned long long) value);
210   if (cls == &p2)
211     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212                 "Peer2 %50s = %12llu\n",
213                 name,
214                 (unsigned long long) value);
215   return GNUNET_OK;
216 }
217
218
219 static void
220 measurement_stop (void *cls)
221 {
222   unsigned long long delta;
223   unsigned long long throughput_out;
224   unsigned long long throughput_in;
225   unsigned long long max_quota_in;
226   unsigned long long max_quota_out;
227   unsigned long long quota_delta;
228   enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
229
230   measure_task = NULL;
231   FPRINTF (stdout, "%s",  "\n");
232   running = GNUNET_NO;
233
234   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
235
236   throughput_out = total_bytes_sent * 1000000LL / delta;     /* convert to bytes/s */
237   throughput_in = total_bytes_recv * 1000000LL / delta;      /* convert to bytes/s */
238
239   max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
240   max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
241   if (max_quota_out < max_quota_in)
242     quota_delta = max_quota_in / 3;
243   else
244     quota_delta = max_quota_out / 3;
245
246   if ((throughput_out > (max_quota_out + quota_delta)) ||
247       (throughput_in > (max_quota_in + quota_delta)))
248     ok = 1; /* fail */
249   else
250     ok = 0; /* pass */
251   GNUNET_STATISTICS_get (p1.stats,
252                          "core",
253                          "# discarded CORE_SEND requests",
254                          NULL,
255                          &print_stat,
256                          &p1);
257   GNUNET_STATISTICS_get (p1.stats,
258                          "core",
259                          "# discarded CORE_SEND request bytes",
260                          NULL,
261                          &print_stat,
262                          &p1);
263   GNUNET_STATISTICS_get (p1.stats,
264                          "core",
265                          "# discarded lower priority CORE_SEND requests",
266                          NULL,
267                          &print_stat,
268                          NULL);
269   GNUNET_STATISTICS_get (p1.stats,
270                          "core",
271                          "# discarded lower priority CORE_SEND request bytes",
272                          NULL,
273                          &print_stat,
274                          &p1);
275   GNUNET_STATISTICS_get (p2.stats,
276                          "core",
277                          "# discarded CORE_SEND requests",
278                          NULL,
279                          &print_stat,
280                          &p2);
281
282   GNUNET_STATISTICS_get (p2.stats,
283                          "core",
284                          "# discarded CORE_SEND request bytes",
285                          NULL,
286                          &print_stat,
287                          &p2);
288   GNUNET_STATISTICS_get (p2.stats,
289                          "core",
290                          "# discarded lower priority CORE_SEND requests",
291                          NULL,
292                          &print_stat,
293                          &p2);
294   GNUNET_STATISTICS_get (p2.stats,
295                          "core",
296                          "# discarded lower priority CORE_SEND request bytes",
297                          NULL,
298                          &print_stat,
299                          &p2);
300
301   if (ok != 0)
302     kind = GNUNET_ERROR_TYPE_ERROR;
303   switch (test)
304   {
305   case SYMMETRIC:
306     GNUNET_log (kind,
307                 "Core quota compliance test with symmetric quotas: %s\n",
308                 (0 == ok) ? "PASSED" : "FAILED");
309     break;
310   case ASYMMETRIC_SEND_LIMITED:
311     GNUNET_log (kind,
312                 "Core quota compliance test with limited sender quota: %s\n",
313                 (0 == ok) ? "PASSED" : "FAILED");
314     break;
315   case ASYMMETRIC_RECV_LIMITED:
316     GNUNET_log (kind,
317                 "Core quota compliance test with limited receiver quota: %s\n",
318                 (0 == ok) ? "PASSED" : "FAILED");
319     break;
320   };
321   GNUNET_log (kind,
322               "Peer 1 send  rate: %llu b/s (%llu bytes in %llu ms)\n",
323               throughput_out,
324               total_bytes_sent,
325               delta);
326   GNUNET_log (kind,
327               "Peer 1 send quota: %llu b/s\n",
328               current_quota_p1_out);
329   GNUNET_log (kind,
330               "Peer 2 receive  rate: %llu b/s (%llu bytes in %llu ms)\n",
331               throughput_in,
332               total_bytes_recv,
333               delta);
334   GNUNET_log (kind,
335               "Peer 2 receive quota: %llu b/s\n",
336               current_quota_p2_in);
337 /*
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound  quota allowed: %llu b/s\n",max_quota_in );
339   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
340 */
341   GNUNET_SCHEDULER_shutdown ();
342 }
343
344
345 static size_t
346 transmit_ready (void *cls,
347                 size_t size,
348                 void *buf)
349 {
350   char *cbuf = buf;
351   struct TestMessage hdr;
352   unsigned int ret;
353
354   p1.nth = NULL;
355   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
356   if (NULL == buf)
357   {
358     if ( (NULL != p1.ch) &&
359          (1 == p1.connect_status) )
360       GNUNET_break (NULL !=
361                     (p1.nth =
362                      GNUNET_CORE_notify_transmit_ready (p1.ch,
363                                                         GNUNET_NO,
364                                                         GNUNET_CORE_PRIO_BEST_EFFORT,
365                                                         FAST_TIMEOUT,
366                                                         &p2.id,
367                                                         MESSAGESIZE,
368                                                         &transmit_ready,
369                                                         &p1)));
370     return 0;
371   }
372   GNUNET_assert (tr_n < TOTAL_MSGS);
373   ret = 0;
374   GNUNET_assert (size >= MESSAGESIZE);
375   GNUNET_assert (NULL != buf);
376   cbuf = buf;
377   do
378   {
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380                 "Sending message %u of size %u at offset %u\n",
381                 tr_n,
382                 MESSAGESIZE,
383                 ret);
384     hdr.header.size = htons (MESSAGESIZE);
385     hdr.header.type = htons (MTYPE);
386     hdr.num = htonl (tr_n);
387     GNUNET_memcpy (&cbuf[ret],
388                    &hdr,
389                    sizeof (struct TestMessage));
390     ret += sizeof (struct TestMessage);
391     memset (&cbuf[ret],
392             tr_n,
393             MESSAGESIZE - sizeof (struct TestMessage));
394     ret += MESSAGESIZE - sizeof (struct TestMessage);
395     tr_n++;
396     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
397                                        16))
398       break;                    /* sometimes pack buffer full, sometimes not */
399   }
400   while (size - ret >= MESSAGESIZE);
401   GNUNET_SCHEDULER_cancel (err_task);
402   err_task =
403       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
404                                     &terminate_task_error,
405                                     NULL);
406
407   total_bytes_sent += ret;
408   return ret;
409 }
410
411
412 static void
413 connect_notify (void *cls,
414                 const struct GNUNET_PeerIdentity *peer)
415 {
416   struct PeerContext *pc = cls;
417
418   if (0 == memcmp (&pc->id,
419                    peer,
420                    sizeof (struct GNUNET_PeerIdentity)))
421     return;                     /* loopback */
422   GNUNET_assert (0 == pc->connect_status);
423   pc->connect_status = 1;
424   if (pc == &p1)
425   {
426     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427                 "Encrypted connection established to peer `%s'\n",
428                 GNUNET_i2s (peer));
429     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430                 "Asking core (1) for transmission to peer `%s'\n",
431                 GNUNET_i2s (&p2.id));
432     GNUNET_SCHEDULER_cancel (err_task);
433     err_task =
434         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
435                                       &terminate_task_error,
436                                       NULL);
437     start_time = GNUNET_TIME_absolute_get ();
438     running = GNUNET_YES;
439     measure_task =
440         GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
441                                       &measurement_stop,
442                                       NULL);
443
444     GNUNET_break (NULL !=
445                   (p1.nth =
446                    GNUNET_CORE_notify_transmit_ready (p1.ch,
447                                                       GNUNET_NO,
448                                                       GNUNET_CORE_PRIO_BEST_EFFORT,
449                                                       TIMEOUT,
450                                                       &p2.id,
451                                                       MESSAGESIZE,
452                                                       &transmit_ready,
453                                                       &p1)));
454   }
455 }
456
457
458 static void
459 disconnect_notify (void *cls,
460                    const struct GNUNET_PeerIdentity *peer)
461 {
462   struct PeerContext *pc = cls;
463
464   if (0 == memcmp (&pc->id,
465                    peer,
466                    sizeof (struct GNUNET_PeerIdentity)))
467     return;                     /* loopback */
468   pc->connect_status = 0;
469   if (NULL != measure_task)
470   {
471     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472                 "Measurement aborted due to disconnect!\n");
473     GNUNET_SCHEDULER_cancel (measure_task);
474     measure_task = NULL;
475   }
476   if (NULL != pc->nth)
477   {
478     GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
479     pc->nth = NULL;
480   }
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482               "Encrypted connection to `%s' cut\n",
483               GNUNET_i2s (peer));
484 }
485
486
487 static int
488 inbound_notify (void *cls,
489                 const struct GNUNET_PeerIdentity *other,
490                 const struct GNUNET_MessageHeader *message)
491 {
492   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493               "Core provides inbound data from `%s' %u.\n",
494               GNUNET_i2s (other),
495               (unsigned int) ntohs (message->size));
496   total_bytes_recv += ntohs (message->size);
497   return GNUNET_OK;
498 }
499
500
501 static int
502 outbound_notify (void *cls,
503                  const struct GNUNET_PeerIdentity *other,
504                  const struct GNUNET_MessageHeader *message)
505 {
506   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507               "Core notifies about outbound data for `%s'.\n",
508               GNUNET_i2s (other));
509   return GNUNET_OK;
510 }
511
512
513 static size_t
514 transmit_ready (void *cls, size_t size, void *buf);
515
516
517 static int
518 process_mtype (void *cls,
519                const struct GNUNET_PeerIdentity *peer,
520                const struct GNUNET_MessageHeader *message)
521 {
522   static int n;
523   const struct TestMessage *hdr;
524
525   hdr = (const struct TestMessage *) message;
526   if (MTYPE != ntohs (message->type))
527     return GNUNET_SYSERR;
528   if (ntohs (message->size) != MESSAGESIZE)
529   {
530     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531                 "Expected message %u of size %u, got %u bytes of message %u\n",
532                 n,
533                 MESSAGESIZE,
534                 ntohs (message->size),
535                 ntohl (hdr->num));
536     GNUNET_SCHEDULER_cancel (err_task);
537     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
538                                          NULL);
539     return GNUNET_SYSERR;
540   }
541   if (ntohl (hdr->num) != n)
542   {
543     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544                 "Expected message %u of size %u, got %u bytes of message %u\n",
545                 n,
546                 MESSAGESIZE,
547                 ntohs (message->size),
548                 ntohl (hdr->num));
549     GNUNET_SCHEDULER_cancel (err_task);
550     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
551                                          NULL);
552     return GNUNET_SYSERR;
553   }
554   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555               "Got message %u of size %u\n",
556               ntohl (hdr->num),
557               ntohs (message->size));
558   n++;
559   if (0 == (n % 10))
560     FPRINTF (stderr, "%s",  ".");
561
562   if (GNUNET_YES == running)
563     GNUNET_break (NULL !=
564                   (p1.nth = GNUNET_CORE_notify_transmit_ready (p1.ch,
565                                                                GNUNET_NO,
566                                                                GNUNET_CORE_PRIO_BEST_EFFORT,
567                                                                FAST_TIMEOUT,
568                                                                &p2.id,
569                                                                MESSAGESIZE,
570                                                                &transmit_ready,
571                                                                &p1)));
572   return GNUNET_OK;
573 }
574
575
576 static struct GNUNET_CORE_MessageHandler handlers[] = {
577   {&process_mtype, MTYPE, 0},
578   {NULL, 0, 0}
579 };
580
581
582
583 static void
584 init_notify (void *cls,
585              const struct GNUNET_PeerIdentity *my_identity)
586 {
587   struct PeerContext *p = cls;
588
589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590               "Connection to CORE service of `%4s' established\n",
591               GNUNET_i2s (my_identity));
592   GNUNET_assert (NULL != my_identity);
593   p->id = *my_identity;
594   if (cls == &p1)
595   {
596     GNUNET_assert (ok == 2);
597     OKPP;
598     /* connect p2 */
599     p2.ch =
600         GNUNET_CORE_connect (p2.cfg,
601                              &p2,
602                              &init_notify,
603                              &connect_notify,
604                              &disconnect_notify,
605                              &inbound_notify,
606                              GNUNET_YES,
607                              &outbound_notify,
608                              GNUNET_YES,
609                              handlers);
610   }
611   else
612   {
613     GNUNET_assert (ok == 3);
614     OKPP;
615     GNUNET_assert (cls == &p2);
616     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617                 "Asking core (1) to connect to peer `%s' and vice-versa\n",
618                 GNUNET_i2s (&p2.id));
619     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
620                                                  &p2.id,
621                                                  1);
622     p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
623                                                  &p1.id,
624                                                  1);
625   }
626 }
627
628
629 static void
630 offer_hello_done (void *cls)
631 {
632   struct PeerContext *p = cls;
633
634   p->oh = NULL;
635 }
636
637
638 static void
639 process_hello (void *cls,
640                const struct GNUNET_MessageHeader *message)
641 {
642   struct PeerContext *p = cls;
643
644   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645               "Received (my) HELLO from transport service\n");
646   GNUNET_assert (message != NULL);
647   p->hello = GNUNET_malloc (ntohs (message->size));
648   GNUNET_memcpy (p->hello, message, ntohs (message->size));
649   if ((p == &p1) && (NULL == p2.oh))
650     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
651                                           message,
652                                           &offer_hello_done,
653                                           &p2);
654   if ((p == &p2) && (NULL == p1.oh))
655     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
656                                           &offer_hello_done,
657                                           &p1);
658
659   if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh))
660     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
661                                           p2.hello,
662                                           &offer_hello_done,
663                                           &p1);
664   if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
665     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
666                                           p1.hello,
667                                           &offer_hello_done,
668                                           &p2);
669 }
670
671
672 static void
673 setup_peer (struct PeerContext *p,
674             const char *cfgname)
675 {
676   char *binary;
677
678   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
679   p->cfg = GNUNET_CONFIGURATION_create ();
680   p->arm_proc =
681     GNUNET_OS_start_process (GNUNET_YES,
682                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
683                              NULL, NULL, NULL,
684                              binary,
685                              "gnunet-service-arm",
686                              "-c",
687                              cfgname,
688                              NULL);
689   GNUNET_assert (GNUNET_OK ==
690                  GNUNET_CONFIGURATION_load (p->cfg,
691                                             cfgname));
692   p->stats = GNUNET_STATISTICS_create ("core",
693                                        p->cfg);
694   GNUNET_assert (NULL != p->stats);
695   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
696   GNUNET_assert (NULL != p->ats);
697   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
698                                        GNUNET_TRANSPORT_AC_ANY,
699                                        &process_hello,
700                                        p);
701   GNUNET_free (binary);
702 }
703
704
705 static void
706 run (void *cls,
707      char *const *args,
708      const char *cfgfile,
709      const struct GNUNET_CONFIGURATION_Handle *cfg)
710 {
711   GNUNET_assert (ok == 1);
712   OKPP;
713   err_task =
714       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
715                                     &terminate_task_error,
716                                     NULL);
717   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
718                                  NULL);
719   if (test == SYMMETRIC)
720   {
721     setup_peer (&p1,
722                 "test_core_quota_peer1.conf");
723     setup_peer (&p2,
724                 "test_core_quota_peer2.conf");
725   }
726   else if (test == ASYMMETRIC_SEND_LIMITED)
727   {
728     setup_peer (&p1,
729                 "test_core_quota_asymmetric_send_limit_peer1.conf");
730     setup_peer (&p2,
731                 "test_core_quota_asymmetric_send_limit_peer2.conf");
732   }
733   else if (test == ASYMMETRIC_RECV_LIMITED)
734   {
735     setup_peer (&p1,
736                 "test_core_quota_asymmetric_recv_limited_peer1.conf");
737     setup_peer (&p2,
738                 "test_core_quota_asymmetric_recv_limited_peer2.conf");
739   }
740
741   GNUNET_assert (test != -1);
742   GNUNET_assert (GNUNET_SYSERR !=
743                  GNUNET_CONFIGURATION_get_value_size (p1.cfg,
744                                                       "ATS",
745                                                       "WAN_QUOTA_IN",
746                                                       &current_quota_p1_in));
747   GNUNET_assert (GNUNET_SYSERR !=
748                  GNUNET_CONFIGURATION_get_value_size (p2.cfg,
749                                                       "ATS",
750                                                       "WAN_QUOTA_IN",
751                                                       &current_quota_p2_in));
752   GNUNET_assert (GNUNET_SYSERR !=
753                  GNUNET_CONFIGURATION_get_value_size (p1.cfg,
754                                                       "ATS",
755                                                       "WAN_QUOTA_OUT",
756                                                       &current_quota_p1_out));
757   GNUNET_assert (GNUNET_SYSERR !=
758                  GNUNET_CONFIGURATION_get_value_size (p2.cfg,
759                                                       "ATS",
760                                                       "WAN_QUOTA_OUT",
761                                                       &current_quota_p2_out));
762
763   p1.ch =
764       GNUNET_CORE_connect (p1.cfg,
765                            &p1,
766                            &init_notify,
767                            &connect_notify,
768                            &disconnect_notify,
769                            &inbound_notify,
770                            GNUNET_YES,
771                            &outbound_notify,
772                            GNUNET_YES,
773                            handlers);
774 }
775
776
777 static void
778 stop_arm (struct PeerContext *p)
779 {
780   if (0 != GNUNET_OS_process_kill (p->arm_proc,
781                                    GNUNET_TERM_SIG))
782     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
783                          "kill");
784   if (GNUNET_OK !=
785       GNUNET_OS_process_wait (p->arm_proc))
786     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
787                          "waitpid");
788   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789               "ARM process %u stopped\n",
790               GNUNET_OS_process_get_pid (p->arm_proc));
791   GNUNET_OS_process_destroy (p->arm_proc);
792   p->arm_proc = NULL;
793   GNUNET_CONFIGURATION_destroy (p->cfg);
794 }
795
796
797 static int
798 check ()
799 {
800   char *const argv[] = {
801     "test-core-quota-compliance",
802     "-c",
803     "test_core_api_data.conf",
804     NULL
805   };
806   struct GNUNET_GETOPT_CommandLineOption options[] = {
807     GNUNET_GETOPT_OPTION_END
808   };
809   ok = 1;
810   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
811                       argv,
812                       "test-core-quota-compliance",
813                       "nohelp",
814                       options,
815                       &run,
816                       &ok);
817   stop_arm (&p1);
818   stop_arm (&p2);
819   return ok;
820 }
821
822
823 int
824 main (int argc,
825       char *argv[])
826 {
827   int ret;
828
829   test = -1;
830   if (strstr (argv[0], "_symmetric") != NULL)
831   {
832     test = SYMMETRIC;
833   }
834   else if (strstr (argv[0], "_asymmetric_send") != NULL)
835   {
836     test = ASYMMETRIC_SEND_LIMITED;
837   }
838   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
839   {
840     test = ASYMMETRIC_RECV_LIMITED;
841   }
842   GNUNET_assert (test != -1);
843   if (test == SYMMETRIC)
844   {
845     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
846     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
847   }
848   else if (test == ASYMMETRIC_SEND_LIMITED)
849   {
850     GNUNET_DISK_directory_remove
851         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
852     GNUNET_DISK_directory_remove
853         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
854   }
855   else if (test == ASYMMETRIC_RECV_LIMITED)
856   {
857     GNUNET_DISK_directory_remove
858         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
859     GNUNET_DISK_directory_remove
860         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
861   }
862
863   GNUNET_log_setup ("test-core-quota-compliance",
864                     "WARNING",
865                     NULL);
866   ret = check ();
867   if (test == SYMMETRIC)
868   {
869     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
870     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
871   }
872   else if (test == ASYMMETRIC_SEND_LIMITED)
873   {
874     GNUNET_DISK_directory_remove
875         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
876     GNUNET_DISK_directory_remove
877         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
878   }
879   else if (test == ASYMMETRIC_RECV_LIMITED)
880   {
881     GNUNET_DISK_directory_remove
882         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
883     GNUNET_DISK_directory_remove
884         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
885   }
886   return ret;
887 }
888
889 /* end of test_core_quota_compliance.c */