Verify that GCD(m,n) != 1 when n is an RSA modulus
[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,
419                 const struct GNUNET_PeerIdentity *other,
420                 const struct GNUNET_MessageHeader *message)
421 {
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423               "Core provides inbound data from `%4s' %u.\n",
424               GNUNET_i2s (other),
425               (unsigned int) ntohs (message->size));
426   total_bytes_recv += ntohs (message->size);
427   return GNUNET_OK;
428 }
429
430
431 static int
432 outbound_notify (void *cls,
433                  const struct GNUNET_PeerIdentity *other,
434                  const struct GNUNET_MessageHeader *message)
435 {
436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437               "Core notifies about outbound data for `%4s'.\n",
438               GNUNET_i2s (other));
439   return GNUNET_OK;
440 }
441
442
443 static size_t
444 transmit_ready (void *cls, size_t size, void *buf);
445
446
447 static int
448 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
449                const struct GNUNET_MessageHeader *message)
450 {
451   static int n;
452   const struct TestMessage *hdr;
453
454   hdr = (const struct TestMessage *) message;
455   if (MTYPE != ntohs (message->type))
456     return GNUNET_SYSERR;
457   if (ntohs (message->size) != MESSAGESIZE)
458   {
459     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460                 "Expected message %u of size %u, got %u bytes of message %u\n",
461                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
462     GNUNET_SCHEDULER_cancel (err_task);
463     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
464     return GNUNET_SYSERR;
465   }
466   if (ntohl (hdr->num) != n)
467   {
468     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469                 "Expected message %u of size %u, got %u bytes of message %u\n",
470                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
471     GNUNET_SCHEDULER_cancel (err_task);
472     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
473     return GNUNET_SYSERR;
474   }
475   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
476               ntohl (hdr->num), ntohs (message->size));
477   n++;
478   if (0 == (n % 10))
479     FPRINTF (stderr, "%s",  ".");
480
481
482   if (running == GNUNET_YES)
483     GNUNET_break (NULL !=
484                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO,
485                                                      GNUNET_CORE_PRIO_BEST_EFFORT,
486                                                      FAST_TIMEOUT, &p2.id,
487                                                      MESSAGESIZE,
488                                                      &transmit_ready, &p1));
489   return GNUNET_OK;
490 }
491
492
493 static struct GNUNET_CORE_MessageHandler handlers[] = {
494   {&process_mtype, MTYPE, 0},
495   {NULL, 0, 0}
496 };
497
498
499
500 static void
501 init_notify (void *cls,
502              const struct GNUNET_PeerIdentity *my_identity)
503 {
504   struct PeerContext *p = cls;
505
506   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507               "Connection to CORE service of `%4s' established\n",
508               GNUNET_i2s (my_identity));
509   GNUNET_assert (NULL != my_identity);
510   p->id = *my_identity;
511   if (cls == &p1)
512   {
513     GNUNET_assert (ok == 2);
514     OKPP;
515     /* connect p2 */
516     p2.ch =
517         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
518                              &disconnect_notify, &inbound_notify, GNUNET_YES,
519                              &outbound_notify, GNUNET_YES, handlers);
520   }
521   else
522   {
523     GNUNET_assert (ok == 3);
524     OKPP;
525     GNUNET_assert (cls == &p2);
526     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527                 "Asking core (1) to connect to peer `%s' and vice-versa\n",
528                 GNUNET_i2s (&p2.id));
529     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
530                                                  &p2.id,
531                                                  1);
532     p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
533                                                  &p1.id,
534                                                  1);
535   }
536 }
537
538
539 static void
540 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
541 {
542   struct PeerContext *p = cls;
543
544   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545               "Received (my) `%s' from transport service\n", "HELLO");
546   GNUNET_assert (message != NULL);
547   p->hello = GNUNET_malloc (ntohs (message->size));
548   memcpy (p->hello, message, ntohs (message->size));
549   if ((p == &p1) && (p2.th != NULL))
550     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
551   if ((p == &p2) && (p1.th != NULL))
552     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
553
554   if ((p == &p1) && (p2.hello != NULL))
555     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
556   if ((p == &p2) && (p1.hello != NULL))
557     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
558 }
559
560
561
562 static void
563 setup_peer (struct PeerContext *p, const char *cfgname)
564 {
565   char *binary;
566
567   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
568   p->cfg = GNUNET_CONFIGURATION_create ();
569   p->arm_proc =
570     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
571                              NULL, NULL, NULL,
572                              binary,
573                              "gnunet-service-arm",
574                              "-c", cfgname, NULL);
575   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
576   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
577   GNUNET_assert (p->stats != NULL);
578   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
579   GNUNET_assert (p->th != NULL);
580   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
581   GNUNET_assert (NULL != p->ats);
582   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
583   GNUNET_free (binary);
584 }
585
586
587 static void
588 run (void *cls, char *const *args, const char *cfgfile,
589      const struct GNUNET_CONFIGURATION_Handle *cfg)
590 {
591   GNUNET_assert (ok == 1);
592   OKPP;
593   err_task =
594       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
595   if (test == SYMMETRIC)
596   {
597     setup_peer (&p1, "test_core_quota_peer1.conf");
598     setup_peer (&p2, "test_core_quota_peer2.conf");
599   }
600   else if (test == ASYMMETRIC_SEND_LIMITED)
601   {
602     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
603     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
604   }
605   else if (test == ASYMMETRIC_RECV_LIMITED)
606   {
607     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
608     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
609   }
610
611   GNUNET_assert (test != -1);
612   GNUNET_assert (GNUNET_SYSERR !=
613                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
614                                                       "WAN_QUOTA_IN",
615                                                       &current_quota_p1_in));
616   GNUNET_assert (GNUNET_SYSERR !=
617                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
618                                                       "WAN_QUOTA_IN",
619                                                       &current_quota_p2_in));
620   GNUNET_assert (GNUNET_SYSERR !=
621                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
622                                                       "WAN_QUOTA_OUT",
623                                                       &current_quota_p1_out));
624   GNUNET_assert (GNUNET_SYSERR !=
625                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
626                                                       "WAN_QUOTA_OUT",
627                                                       &current_quota_p2_out));
628
629   p1.ch =
630       GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
631                            &disconnect_notify, &inbound_notify, GNUNET_YES,
632                            &outbound_notify, GNUNET_YES, handlers);
633 }
634
635
636 static void
637 stop_arm (struct PeerContext *p)
638 {
639   if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
640     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
641   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
642     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
643   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
644               GNUNET_OS_process_get_pid (p->arm_proc));
645   GNUNET_OS_process_destroy (p->arm_proc);
646   p->arm_proc = NULL;
647   GNUNET_CONFIGURATION_destroy (p->cfg);
648 }
649
650
651 static int
652 check ()
653 {
654   char *const argv[] = { "test-core-quota-compliance",
655     "-c",
656     "test_core_api_data.conf",
657     NULL
658   };
659   struct GNUNET_GETOPT_CommandLineOption options[] = {
660     GNUNET_GETOPT_OPTION_END
661   };
662   ok = 1;
663   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
664                       "test-core-quota-compliance", "nohelp", options, &run,
665                       &ok);
666   stop_arm (&p1);
667   stop_arm (&p2);
668   return ok;
669 }
670
671
672 int
673 main (int argc, char *argv[])
674 {
675   int ret;
676
677   test = -1;
678   if (strstr (argv[0], "_symmetric") != NULL)
679   {
680     test = SYMMETRIC;
681   }
682   else if (strstr (argv[0], "_asymmetric_send") != NULL)
683   {
684     test = ASYMMETRIC_SEND_LIMITED;
685   }
686   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
687   {
688     test = ASYMMETRIC_RECV_LIMITED;
689   }
690   GNUNET_assert (test != -1);
691   if (test == SYMMETRIC)
692   {
693     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
694     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
695   }
696   else if (test == ASYMMETRIC_SEND_LIMITED)
697   {
698     GNUNET_DISK_directory_remove
699         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
700     GNUNET_DISK_directory_remove
701         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
702   }
703   else if (test == ASYMMETRIC_RECV_LIMITED)
704   {
705     GNUNET_DISK_directory_remove
706         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
707     GNUNET_DISK_directory_remove
708         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
709   }
710
711   GNUNET_log_setup ("test-core-quota-compliance",
712                     "WARNING",
713                     NULL);
714   ret = check ();
715   if (test == SYMMETRIC)
716   {
717     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
718     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
719   }
720   else if (test == ASYMMETRIC_SEND_LIMITED)
721   {
722     GNUNET_DISK_directory_remove
723         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
724     GNUNET_DISK_directory_remove
725         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
726   }
727   else if (test == ASYMMETRIC_RECV_LIMITED)
728   {
729     GNUNET_DISK_directory_remove
730         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
731     GNUNET_DISK_directory_remove
732         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
733   }
734   return ret;
735 }
736
737 /* end of test_core_quota_compliance.c */