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