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