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