-remove trailing whitespace
[oweals/gnunet.git] / src / core / test_core_quota_compliance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_statistics_service.h"
33
34
35 #define SYMMETRIC 0
36 #define ASYMMETRIC_SEND_LIMITED 1
37 #define ASYMMETRIC_RECV_LIMITED 2
38
39 /**
40  * Note that this value must not significantly exceed
41  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
42  * messages may be dropped even for a reliable transport.
43  */
44 #define TOTAL_MSGS (60000 * 10)
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
50
51 /**
52  * What delay do we request from the core service for transmission?
53  */
54 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
55
56 #define MTYPE 12345
57 #define MESSAGESIZE 1024
58 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
59
60 static unsigned long long total_bytes_sent;
61 static unsigned long long total_bytes_recv;
62
63 static struct GNUNET_TIME_Absolute start_time;
64
65 static GNUNET_SCHEDULER_TaskIdentifier err_task;
66
67 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
68
69 static GNUNET_SCHEDULER_TaskIdentifier connect_task;
70
71
72 struct PeerContext
73 {
74   struct GNUNET_CONFIGURATION_Handle *cfg;
75   struct GNUNET_CORE_Handle *ch;
76   struct GNUNET_CORE_TransmitHandle *nth;
77   struct GNUNET_PeerIdentity id;
78   struct GNUNET_TRANSPORT_Handle *th;
79   struct GNUNET_MessageHeader *hello;
80   struct GNUNET_STATISTICS_Handle *stats;
81   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
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 static void
114 process_hello (void *cls, const struct GNUNET_MessageHeader *message);
115
116 static void
117 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   struct GNUNET_CORE_Handle *ch;
120
121   err_task = GNUNET_SCHEDULER_NO_TASK;
122   GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
123   GNUNET_STATISTICS_destroy (p2.stats, GNUNET_NO);
124   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
125   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
126   if (p1.nth != NULL)
127   {
128     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
129     p1.nth = NULL;
130   }
131   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
132   {
133     GNUNET_SCHEDULER_cancel (connect_task);
134     connect_task = GNUNET_SCHEDULER_NO_TASK;
135   }
136   ch = p1.ch;
137   p1.ch = NULL;
138   GNUNET_CORE_disconnect (ch);
139   ch = p2.ch;
140   p2.ch = NULL;
141   GNUNET_CORE_disconnect (ch);
142   GNUNET_TRANSPORT_disconnect (p1.th);
143   p1.th = NULL;
144   GNUNET_TRANSPORT_disconnect (p2.th);
145   p2.th = NULL;
146   GNUNET_free_non_null (p1.hello);
147   GNUNET_free_non_null (p2.hello);
148 }
149
150
151 static void
152 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 {
154   err_task = GNUNET_SCHEDULER_NO_TASK;
155
156   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
157     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
158   //GNUNET_break (0);
159   if (p1.nth != NULL)
160   {
161     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
162     p1.nth = NULL;
163   }
164   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
165     GNUNET_SCHEDULER_cancel (measure_task);
166   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
167   {
168     GNUNET_SCHEDULER_cancel (connect_task);
169     connect_task = GNUNET_SCHEDULER_NO_TASK;
170   }
171
172   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
173   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
174   if (NULL != p1.ch)
175     GNUNET_CORE_disconnect (p1.ch);
176   p1.ch = NULL;
177   if (NULL != p2.ch)
178     GNUNET_CORE_disconnect (p2.ch);
179   p2.ch = NULL;
180   if (NULL != p1.th)
181     GNUNET_TRANSPORT_disconnect (p1.th);
182   p1.th = NULL;
183   if (NULL != p2.th)
184     GNUNET_TRANSPORT_disconnect (p2.th);
185   p2.th = NULL;
186   ok = 42;
187 }
188
189
190 static void
191 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193   connect_task =
194       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect,
195                                     NULL);
196   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id, NULL, NULL); /*FIXME TRY_CONNECT change */
197   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id, NULL, NULL); /*FIXME TRY_CONNECT change */
198 }
199
200 /**
201  * Callback function to process statistic values.
202  *
203  * @param cls closure
204  * @param subsystem name of subsystem that created the statistic
205  * @param name the name of the datum
206  * @param value the current value
207  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
208  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
209  */
210 static int
211 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
212             int is_persistent)
213 {
214   if (cls == &p1)
215     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 %50s = %12llu\n", name,
216                 (unsigned long long) value);
217   if (cls == &p2)
218     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 %50s = %12llu\n", name,
219                 (unsigned long long) value);
220   return GNUNET_OK;
221 }
222
223 static void
224 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 {
226   unsigned long long delta;
227   unsigned long long throughput_out;
228   unsigned long long throughput_in;
229   unsigned long long max_quota_in;
230   unsigned long long max_quota_out;
231   unsigned long long quota_delta;
232   enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
233
234   measure_task = GNUNET_SCHEDULER_NO_TASK;
235   FPRINTF (stdout, "%s",  "\n");
236   running = GNUNET_NO;
237
238   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
239
240   throughput_out = total_bytes_sent * 1000000LL / delta;     /* convert to bytes/s */
241   throughput_in = total_bytes_recv * 1000000LL / delta;      /* convert to bytes/s */
242
243   max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
244   max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
245   if (max_quota_out < max_quota_in)
246     quota_delta = max_quota_in / 3;
247   else
248     quota_delta = max_quota_out / 3;
249
250   if ((throughput_out > (max_quota_out + quota_delta)) ||
251       (throughput_in > (max_quota_in + quota_delta)))
252     ok = 1; /* fail */
253   else
254     ok = 0; /* pass */
255   GNUNET_STATISTICS_get (p1.stats, "core", "# discarded CORE_SEND requests",
256                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
257
258   GNUNET_STATISTICS_get (p1.stats, "core",
259                          "# discarded CORE_SEND request bytes",
260                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
261   GNUNET_STATISTICS_get (p1.stats, "core",
262                          "# discarded lower priority CORE_SEND requests",
263                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, NULL);
264   GNUNET_STATISTICS_get (p1.stats, "core",
265                          "# discarded lower priority CORE_SEND request bytes",
266                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
267   GNUNET_STATISTICS_get (p2.stats, "core", "# discarded CORE_SEND requests",
268                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
269
270   GNUNET_STATISTICS_get (p2.stats, "core",
271                          "# discarded CORE_SEND request bytes",
272                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
273   GNUNET_STATISTICS_get (p2.stats, "core",
274                          "# discarded lower priority CORE_SEND requests",
275                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
276   GNUNET_STATISTICS_get (p2.stats, "core",
277                          "# discarded lower priority CORE_SEND request bytes",
278                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
279
280   if (ok != 0)
281     kind = GNUNET_ERROR_TYPE_ERROR;
282   switch (test)
283   {
284   case SYMMETRIC:
285     GNUNET_log (kind, "Core quota compliance test with symmetric quotas: %s\n",
286                 (0 == ok) ? "PASSED" : "FAILED");
287     break;
288   case ASYMMETRIC_SEND_LIMITED:
289     GNUNET_log (kind,
290                 "Core quota compliance test with limited sender quota: %s\n",
291                 (0 == ok) ? "PASSED" : "FAILED");
292     break;
293   case ASYMMETRIC_RECV_LIMITED:
294     GNUNET_log (kind,
295                 "Core quota compliance test with limited receiver quota: %s\n",
296                 (0 == ok) ? "PASSED" : "FAILED");
297     break;
298   };
299   GNUNET_log (kind, "Peer 1 send  rate: %llu b/s (%llu bytes in %llu ms)\n",
300               throughput_out, total_bytes_sent, delta);
301   GNUNET_log (kind, "Peer 1 send quota: %llu b/s\n", current_quota_p1_out);
302   GNUNET_log (kind, "Peer 2 receive  rate: %llu b/s (%llu bytes in %llu ms)\n",
303               throughput_in, total_bytes_recv, delta);
304   GNUNET_log (kind, "Peer 2 receive quota: %llu b/s\n", current_quota_p2_in);
305 /*
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound  quota allowed: %llu b/s\n",max_quota_in );
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
308 */
309   GNUNET_SCHEDULER_cancel (err_task);
310   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
311
312 }
313
314 static size_t
315 transmit_ready (void *cls, size_t size, void *buf)
316 {
317   char *cbuf = buf;
318   struct TestMessage hdr;
319   unsigned int ret;
320
321   p1.nth = NULL;
322   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
323   if (buf == NULL)
324   {
325     if ((p1.ch != NULL) && (p1.connect_status == 1))
326       GNUNET_break (NULL !=
327                     (p1.nth =
328                      GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
329                                                         FAST_TIMEOUT, &p2.id,
330                                                         MESSAGESIZE,
331                                                         &transmit_ready, &p1)));
332     return 0;
333   }
334   GNUNET_assert (tr_n < TOTAL_MSGS);
335   ret = 0;
336   GNUNET_assert (size >= MESSAGESIZE);
337   GNUNET_assert (buf != NULL);
338   cbuf = buf;
339   do
340   {
341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342                 "Sending message %u of size %u at offset %u\n", tr_n,
343                 MESSAGESIZE, ret);
344     hdr.header.size = htons (MESSAGESIZE);
345     hdr.header.type = htons (MTYPE);
346     hdr.num = htonl (tr_n);
347     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
348     ret += sizeof (struct TestMessage);
349     memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
350     ret += MESSAGESIZE - sizeof (struct TestMessage);
351     tr_n++;
352     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
353       break;                    /* sometimes pack buffer full, sometimes not */
354   }
355   while (size - ret >= MESSAGESIZE);
356   GNUNET_SCHEDULER_cancel (err_task);
357   err_task =
358       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
359
360   total_bytes_sent += ret;
361   return ret;
362 }
363
364
365
366 static void
367 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
368 {
369   struct PeerContext *pc = cls;
370
371   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
372     return;                     /* loopback */
373   GNUNET_assert (pc->connect_status == 0);
374   pc->connect_status = 1;
375   if (pc == &p1)
376   {
377     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378                 "Encrypted connection established to peer `%4s'\n",
379                 GNUNET_i2s (peer));
380     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381                 "Asking core (1) for transmission to peer `%4s'\n",
382                 GNUNET_i2s (&p2.id));
383     if (err_task != GNUNET_SCHEDULER_NO_TASK)
384       GNUNET_SCHEDULER_cancel (err_task);
385     err_task =
386         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
387     start_time = GNUNET_TIME_absolute_get ();
388     running = GNUNET_YES;
389     measure_task =
390         GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH, &measurement_stop,
391                                       NULL);
392
393     GNUNET_break (NULL !=
394                   (p1.nth =
395                    GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
396                                                       TIMEOUT, &p2.id,
397                                                       MESSAGESIZE,
398                                                       &transmit_ready, &p1)));
399   }
400 }
401
402
403 static void
404 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
405 {
406   struct PeerContext *pc = cls;
407
408   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
409     return;                     /* loopback */
410   pc->connect_status = 0;
411   if (GNUNET_SCHEDULER_NO_TASK != measure_task)
412   {
413     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414                 "Measurement aborted due to disconnect!\n");
415     GNUNET_SCHEDULER_cancel (measure_task);
416     measure_task = GNUNET_SCHEDULER_NO_TASK;
417   }
418   if (pc->nth != NULL)
419   {
420     GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
421     pc->nth = NULL;
422   }
423   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
424               GNUNET_i2s (peer));
425 }
426
427
428 static int
429 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
430                 const struct GNUNET_MessageHeader *message)
431 {
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Core provides inbound data from `%4s' %llu.\n",
434               GNUNET_i2s (other), ntohs (message->size));
435   total_bytes_recv += ntohs (message->size);
436   return GNUNET_OK;
437 }
438
439
440 static int
441 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
442                  const struct GNUNET_MessageHeader *message)
443 {
444   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445               "Core notifies about outbound data for `%4s'.\n",
446               GNUNET_i2s (other));
447   return GNUNET_OK;
448 }
449
450
451 static size_t
452 transmit_ready (void *cls, size_t size, void *buf);
453
454 static int
455 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
456                const struct GNUNET_MessageHeader *message)
457 {
458   static int n;
459   const struct TestMessage *hdr;
460
461   hdr = (const struct TestMessage *) message;
462   if (MTYPE != ntohs (message->type))
463     return GNUNET_SYSERR;
464   if (ntohs (message->size) != MESSAGESIZE)
465   {
466     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
467                 "Expected message %u of size %u, got %u bytes of message %u\n",
468                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
469     GNUNET_SCHEDULER_cancel (err_task);
470     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
471     return GNUNET_SYSERR;
472   }
473   if (ntohl (hdr->num) != n)
474   {
475     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
476                 "Expected message %u of size %u, got %u bytes of message %u\n",
477                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
478     GNUNET_SCHEDULER_cancel (err_task);
479     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
480     return GNUNET_SYSERR;
481   }
482   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
483               ntohl (hdr->num), ntohs (message->size));
484   n++;
485   if (0 == (n % 10))
486     FPRINTF (stderr, "%s",  ".");
487
488
489   if (running == GNUNET_YES)
490     GNUNET_break (NULL !=
491                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
492                                                      FAST_TIMEOUT, &p2.id,
493                                                      MESSAGESIZE,
494                                                      &transmit_ready, &p1));
495   return GNUNET_OK;
496 }
497
498
499 static struct GNUNET_CORE_MessageHandler handlers[] = {
500   {&process_mtype, MTYPE, 0},
501   {NULL, 0, 0}
502 };
503
504
505
506 static void
507 init_notify (void *cls,
508              const struct GNUNET_PeerIdentity *my_identity)
509 {
510   struct PeerContext *p = cls;
511
512   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513               "Connection to CORE service of `%4s' established\n",
514               GNUNET_i2s (my_identity));
515   GNUNET_assert (NULL != my_identity);
516   p->id = *my_identity;
517   if (cls == &p1)
518   {
519     GNUNET_assert (ok == 2);
520     OKPP;
521     /* connect p2 */
522     p2.ch =
523         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
524                              &disconnect_notify, &inbound_notify, GNUNET_YES,
525                              &outbound_notify, GNUNET_YES, handlers);
526   }
527   else
528   {
529     GNUNET_assert (ok == 3);
530     OKPP;
531     GNUNET_assert (cls == &p2);
532     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533                 "Asking core (1) to connect to peer `%4s'\n",
534                 GNUNET_i2s (&p2.id));
535     connect_task = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
536   }
537 }
538
539
540 static void
541 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
542 {
543   struct PeerContext *p = cls;
544
545   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546               "Received (my) `%s' from transport service\n", "HELLO");
547   GNUNET_assert (message != NULL);
548   p->hello = GNUNET_malloc (ntohs (message->size));
549   memcpy (p->hello, message, ntohs (message->size));
550   if ((p == &p1) && (p2.th != NULL))
551     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
552   if ((p == &p2) && (p1.th != NULL))
553     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
554
555   if ((p == &p1) && (p2.hello != NULL))
556     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
557   if ((p == &p2) && (p1.hello != NULL))
558     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
559 }
560
561
562
563 static void
564 setup_peer (struct PeerContext *p, const char *cfgname)
565 {
566   char *binary;
567
568   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
569   p->cfg = GNUNET_CONFIGURATION_create ();
570   p->arm_proc =
571     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
572                                "gnunet-service-arm",
573                                "-c", cfgname, NULL);
574   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
575   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
576   GNUNET_assert (p->stats != NULL);
577   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
578   GNUNET_assert (p->th != NULL);
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 */