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