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