removing deprecated, dead test-connected API
[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_common.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_statistics_service.h"
35
36
37 #define SYMMETRIC 0
38 #define ASYMMETRIC_SEND_LIMITED 1
39 #define ASYMMETRIC_RECV_LIMITED 2
40
41 /**
42  * Note that this value must not significantly exceed
43  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
44  * messages may be dropped even for a reliable transport.
45  */
46 #define TOTAL_MSGS (60000 * 10)
47
48 /**
49  * How long until we give up on transmitting the message?
50  */
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
52
53 /**
54  * What delay do we request from the core service for transmission?
55  */
56 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
57
58 #define MTYPE 12345
59 #define MESSAGESIZE 1024
60 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
61
62 static unsigned long long total_bytes_sent;
63 static unsigned long long total_bytes_recv;
64
65 static struct GNUNET_TIME_Absolute start_time;
66
67 static GNUNET_SCHEDULER_TaskIdentifier err_task;
68
69 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
70
71 static GNUNET_SCHEDULER_TaskIdentifier connect_task;
72
73
74 struct PeerContext
75 {
76   struct GNUNET_CONFIGURATION_Handle *cfg;
77   struct GNUNET_CORE_Handle *ch;
78   struct GNUNET_CORE_TransmitHandle *nth;
79   struct GNUNET_PeerIdentity id;
80   struct GNUNET_TRANSPORT_Handle *th;
81   struct GNUNET_MessageHeader *hello;
82   struct GNUNET_STATISTICS_Handle *stats;
83   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
84   int connect_status;
85   struct GNUNET_OS_Process *arm_proc;
86 };
87
88 static struct PeerContext p1;
89 static struct PeerContext p2;
90
91 static unsigned long long current_quota_p1_in;
92 static unsigned long long current_quota_p1_out;
93 static unsigned long long current_quota_p2_in;
94 static unsigned long long current_quota_p2_out;
95
96 static int ok;
97 static int test;
98 static int32_t tr_n;
99
100 static int running;
101
102
103 #if VERBOSE
104 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
105 #else
106 #define OKPP do { ok++; } while (0)
107 #endif
108
109 struct TestMessage
110 {
111   struct GNUNET_MessageHeader header;
112   uint32_t num;
113 };
114
115 static void
116 process_hello (void *cls, const struct GNUNET_MessageHeader *message);
117
118 static void
119 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   struct GNUNET_CORE_Handle *ch;
122
123   err_task = GNUNET_SCHEDULER_NO_TASK;
124   GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
125   GNUNET_STATISTICS_destroy (p2.stats, GNUNET_NO);
126   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
127   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
128   if (p1.nth != NULL)
129   {
130     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
131     p1.nth = NULL;
132   }
133   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
134     GNUNET_SCHEDULER_cancel (connect_task);
135   ch = p1.ch;
136   p1.ch = NULL;
137   GNUNET_CORE_disconnect (ch);
138   ch = p2.ch;
139   p2.ch = NULL;
140   GNUNET_CORE_disconnect (ch);
141   GNUNET_TRANSPORT_disconnect (p1.th);
142   p1.th = NULL;
143   GNUNET_TRANSPORT_disconnect (p2.th);
144   p2.th = NULL;
145   GNUNET_free_non_null (p1.hello);
146   GNUNET_free_non_null (p2.hello);
147 }
148
149
150 static void
151 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 {
153   err_task = GNUNET_SCHEDULER_NO_TASK;
154
155   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
157   //GNUNET_break (0);
158   if (p1.nth != NULL)
159   {
160     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
161     p1.nth = NULL;
162   }
163   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
164     GNUNET_SCHEDULER_cancel (measure_task);
165   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
166     GNUNET_SCHEDULER_cancel (connect_task);
167
168   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
169   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
170   if (NULL != p1.ch)
171     GNUNET_CORE_disconnect (p1.ch);
172   p1.ch = NULL;
173   if (NULL != p2.ch)
174     GNUNET_CORE_disconnect (p2.ch);
175   p2.ch = NULL;
176   if (NULL != p1.th)
177     GNUNET_TRANSPORT_disconnect (p1.th);
178   p1.th = NULL;
179   if (NULL != p2.th)
180     GNUNET_TRANSPORT_disconnect (p2.th);
181   p2.th = NULL;
182   ok = 42;
183 }
184
185
186 static void
187 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 {
189   connect_task =
190       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect,
191                                     NULL);
192   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id, NULL, NULL); /*FIXME TRY_CONNECT change */
193   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id, NULL, NULL); /*FIXME TRY_CONNECT change */
194 }
195
196 /**
197  * Callback function to process statistic values.
198  *
199  * @param cls closure
200  * @param subsystem name of subsystem that created the statistic
201  * @param name the name of the datum
202  * @param value the current value
203  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
204  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
205  */
206 static int
207 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
208             int is_persistent)
209 {
210   if (cls == &p1)
211     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 %50s = %12llu\n", name,
212                 (unsigned long long) value);
213   if (cls == &p2)
214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 %50s = %12llu\n", name,
215                 (unsigned long long) value);
216   return GNUNET_OK;
217 }
218
219 static void
220 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 {
222   unsigned long long delta;
223   unsigned long long throughput_out;
224   unsigned long long throughput_in;
225   unsigned long long max_quota_in;
226   unsigned long long max_quota_out;
227   unsigned long long quota_delta;
228   enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
229
230   measure_task = GNUNET_SCHEDULER_NO_TASK;
231   FPRINTF (stdout, "%s",  "\n");
232   running = GNUNET_NO;
233
234   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
235
236   throughput_out = total_bytes_sent * 1000000LL / delta;     /* convert to bytes/s */
237   throughput_in = total_bytes_recv * 1000000LL / delta;      /* convert to bytes/s */
238
239   max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
240   max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
241   if (max_quota_out < max_quota_in)
242     quota_delta = max_quota_in / 3;
243   else
244     quota_delta = max_quota_out / 3;
245
246   if ((throughput_out > (max_quota_out + quota_delta)) ||
247       (throughput_in > (max_quota_in + quota_delta)))
248     ok = 1; /* fail */
249   else
250     ok = 0; /* pass */
251   GNUNET_STATISTICS_get (p1.stats, "core", "# discarded CORE_SEND requests",
252                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
253
254   GNUNET_STATISTICS_get (p1.stats, "core",
255                          "# discarded CORE_SEND request bytes",
256                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
257   GNUNET_STATISTICS_get (p1.stats, "core",
258                          "# discarded lower priority CORE_SEND requests",
259                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, NULL);
260   GNUNET_STATISTICS_get (p1.stats, "core",
261                          "# discarded lower priority CORE_SEND request bytes",
262                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
263   GNUNET_STATISTICS_get (p2.stats, "core", "# discarded CORE_SEND requests",
264                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
265
266   GNUNET_STATISTICS_get (p2.stats, "core",
267                          "# discarded CORE_SEND request bytes",
268                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
269   GNUNET_STATISTICS_get (p2.stats, "core",
270                          "# discarded lower priority CORE_SEND requests",
271                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
272   GNUNET_STATISTICS_get (p2.stats, "core",
273                          "# discarded lower priority CORE_SEND request bytes",
274                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
275
276   if (ok != 0)
277     kind = GNUNET_ERROR_TYPE_ERROR;
278   switch (test)
279   {
280   case SYMMETRIC:
281     GNUNET_log (kind, "Core quota compliance test with symmetric quotas: %s\n",
282                 (0 == ok) ? "PASSED" : "FAILED");
283     break;
284   case ASYMMETRIC_SEND_LIMITED:
285     GNUNET_log (kind,
286                 "Core quota compliance test with limited sender quota: %s\n",
287                 (0 == ok) ? "PASSED" : "FAILED");
288     break;
289   case ASYMMETRIC_RECV_LIMITED:
290     GNUNET_log (kind,
291                 "Core quota compliance test with limited receiver quota: %s\n",
292                 (0 == ok) ? "PASSED" : "FAILED");
293     break;
294   };
295   GNUNET_log (kind, "Peer 1 send  rate: %llu b/s (%llu bytes in %llu ms)\n",
296               throughput_out, total_bytes_sent, delta);
297   GNUNET_log (kind, "Peer 1 send quota: %llu b/s\n", current_quota_p1_out);
298   GNUNET_log (kind, "Peer 2 receive  rate: %llu b/s (%llu bytes in %llu ms)\n",
299               throughput_in, total_bytes_recv, delta);
300   GNUNET_log (kind, "Peer 2 receive quota: %llu b/s\n", current_quota_p2_in);
301 /*
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound  quota allowed: %llu b/s\n",max_quota_in );
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
304 */
305   GNUNET_SCHEDULER_cancel (err_task);
306   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
307
308 }
309
310 static size_t
311 transmit_ready (void *cls, size_t size, void *buf)
312 {
313   char *cbuf = buf;
314   struct TestMessage hdr;
315   unsigned int ret;
316
317   p1.nth = NULL;
318   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
319   if (buf == NULL)
320   {
321     if ((p1.ch != NULL) && (p1.connect_status == 1))
322       GNUNET_break (NULL !=
323                     (p1.nth =
324                      GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
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 != GNUNET_SCHEDULER_NO_TASK)
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, 0,
392                                                       TIMEOUT, &p2.id,
393                                                       MESSAGESIZE,
394                                                       &transmit_ready, &p1)));
395   }
396 }
397
398
399 static void
400 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
401 {
402   struct PeerContext *pc = cls;
403
404   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
405     return;                     /* loopback */
406   pc->connect_status = 0;
407   if (GNUNET_SCHEDULER_NO_TASK != measure_task)
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410                 "Measurement aborted due to disconnect!\n");
411     GNUNET_SCHEDULER_cancel (measure_task);
412     measure_task = GNUNET_SCHEDULER_NO_TASK;
413   }
414   if (pc->nth != NULL)
415   {
416     GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
417     pc->nth = NULL;
418   }
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
420               GNUNET_i2s (peer));
421 }
422
423
424 static int
425 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
426                 const struct GNUNET_MessageHeader *message)
427 {
428   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429               "Core provides inbound data from `%4s' %llu.\n",
430               GNUNET_i2s (other), ntohs (message->size));
431   total_bytes_recv += ntohs (message->size);
432   return GNUNET_OK;
433 }
434
435
436 static int
437 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
438                  const struct GNUNET_MessageHeader *message)
439 {
440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441               "Core notifies about outbound data for `%4s'.\n",
442               GNUNET_i2s (other));
443   return GNUNET_OK;
444 }
445
446
447 static size_t
448 transmit_ready (void *cls, size_t size, void *buf);
449
450 static int
451 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
452                const struct GNUNET_MessageHeader *message)
453 {
454   static int n;
455   const struct TestMessage *hdr;
456
457   hdr = (const struct TestMessage *) message;
458   if (MTYPE != ntohs (message->type))
459     return GNUNET_SYSERR;
460   if (ntohs (message->size) != MESSAGESIZE)
461   {
462     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
463                 "Expected message %u of size %u, got %u bytes of message %u\n",
464                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
465     GNUNET_SCHEDULER_cancel (err_task);
466     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
467     return GNUNET_SYSERR;
468   }
469   if (ntohl (hdr->num) != n)
470   {
471     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472                 "Expected message %u of size %u, got %u bytes of message %u\n",
473                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
474     GNUNET_SCHEDULER_cancel (err_task);
475     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
476     return GNUNET_SYSERR;
477   }
478   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
479               ntohl (hdr->num), ntohs (message->size));
480   n++;
481   if (0 == (n % 10))
482     FPRINTF (stderr, "%s",  ".");
483
484
485   if (running == GNUNET_YES)
486     GNUNET_break (NULL !=
487                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
488                                                      FAST_TIMEOUT, &p2.id,
489                                                      MESSAGESIZE,
490                                                      &transmit_ready, &p1));
491   return GNUNET_OK;
492 }
493
494
495 static struct GNUNET_CORE_MessageHandler handlers[] = {
496   {&process_mtype, MTYPE, 0},
497   {NULL, 0, 0}
498 };
499
500
501
502 static void
503 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
504              const struct GNUNET_PeerIdentity *my_identity)
505 {
506   struct PeerContext *p = cls;
507
508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509               "Connection to CORE service of `%4s' established\n",
510               GNUNET_i2s (my_identity));
511   GNUNET_assert (server != NULL);
512   p->id = *my_identity;
513   GNUNET_assert (p->ch == server);
514   if (cls == &p1)
515   {
516     GNUNET_assert (ok == 2);
517     OKPP;
518     /* connect p2 */
519     p2.ch =
520         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
521                              &disconnect_notify, &inbound_notify, GNUNET_YES,
522                              &outbound_notify, GNUNET_YES, handlers);
523   }
524   else
525   {
526     GNUNET_assert (ok == 3);
527     OKPP;
528     GNUNET_assert (cls == &p2);
529     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530                 "Asking core (1) to connect to peer `%4s'\n",
531                 GNUNET_i2s (&p2.id));
532     connect_task = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
533   }
534 }
535
536
537 static void
538 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
539 {
540   struct PeerContext *p = cls;
541
542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543               "Received (my) `%s' from transport service\n", "HELLO");
544   GNUNET_assert (message != NULL);
545   p->hello = GNUNET_malloc (ntohs (message->size));
546   memcpy (p->hello, message, ntohs (message->size));
547   if ((p == &p1) && (p2.th != NULL))
548     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
549   if ((p == &p2) && (p1.th != NULL))
550     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
551
552   if ((p == &p1) && (p2.hello != NULL))
553     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
554   if ((p == &p2) && (p1.hello != NULL))
555     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
556 }
557
558
559
560 static void
561 setup_peer (struct PeerContext *p, const char *cfgname)
562 {
563   char *binary;
564
565   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
566   p->cfg = GNUNET_CONFIGURATION_create ();
567   p->arm_proc =
568     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
569                                "gnunet-service-arm",
570                                "-c", cfgname, NULL);
571   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
572   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
573   GNUNET_assert (p->stats != NULL);
574   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
575   GNUNET_assert (p->th != NULL);
576   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
577   GNUNET_free (binary);
578 }
579
580
581 static void
582 run (void *cls, char *const *args, const char *cfgfile,
583      const struct GNUNET_CONFIGURATION_Handle *cfg)
584 {
585   GNUNET_assert (ok == 1);
586   OKPP;
587   err_task =
588       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
589   if (test == SYMMETRIC)
590   {
591     setup_peer (&p1, "test_core_quota_peer1.conf");
592     setup_peer (&p2, "test_core_quota_peer2.conf");
593   }
594   else if (test == ASYMMETRIC_SEND_LIMITED)
595   {
596     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
597     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
598   }
599   else if (test == ASYMMETRIC_RECV_LIMITED)
600   {
601     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
602     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
603   }
604
605   GNUNET_assert (test != -1);
606   GNUNET_assert (GNUNET_SYSERR !=
607                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
608                                                       "WAN_QUOTA_IN",
609                                                       &current_quota_p1_in));
610   GNUNET_assert (GNUNET_SYSERR !=
611                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
612                                                       "WAN_QUOTA_IN",
613                                                       &current_quota_p2_in));
614   GNUNET_assert (GNUNET_SYSERR !=
615                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
616                                                       "WAN_QUOTA_OUT",
617                                                       &current_quota_p1_out));
618   GNUNET_assert (GNUNET_SYSERR !=
619                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
620                                                       "WAN_QUOTA_OUT",
621                                                       &current_quota_p2_out));
622
623   p1.ch =
624       GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
625                            &disconnect_notify, &inbound_notify, GNUNET_YES,
626                            &outbound_notify, GNUNET_YES, handlers);
627 }
628
629
630 static void
631 stop_arm (struct PeerContext *p)
632 {
633   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
634     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
635   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
636     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
637   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
638               GNUNET_OS_process_get_pid (p->arm_proc));
639   GNUNET_OS_process_destroy (p->arm_proc);
640   p->arm_proc = NULL;
641   GNUNET_CONFIGURATION_destroy (p->cfg);
642 }
643
644
645 static int
646 check ()
647 {
648   char *const argv[] = { "test-core-quota-compliance",
649     "-c",
650     "test_core_api_data.conf",
651     NULL
652   };
653   struct GNUNET_GETOPT_CommandLineOption options[] = {
654     GNUNET_GETOPT_OPTION_END
655   };
656   ok = 1;
657   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
658                       "test-core-quota-compliance", "nohelp", options, &run,
659                       &ok);
660   stop_arm (&p1);
661   stop_arm (&p2);
662   return ok;
663 }
664
665
666 int
667 main (int argc, char *argv[])
668 {
669   int ret;
670
671   test = -1;
672   if (strstr (argv[0], "_symmetric") != NULL)
673   {
674     test = SYMMETRIC;
675   }
676   else if (strstr (argv[0], "_asymmetric_send") != NULL)
677   {
678     test = ASYMMETRIC_SEND_LIMITED;
679   }
680   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
681   {
682     test = ASYMMETRIC_RECV_LIMITED;
683   }
684   GNUNET_assert (test != -1);
685   if (test == SYMMETRIC)
686   {
687     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
688     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
689   }
690   else if (test == ASYMMETRIC_SEND_LIMITED)
691   {
692     GNUNET_DISK_directory_remove
693         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
694     GNUNET_DISK_directory_remove
695         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
696   }
697   else if (test == ASYMMETRIC_RECV_LIMITED)
698   {
699     GNUNET_DISK_directory_remove
700         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
701     GNUNET_DISK_directory_remove
702         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
703   }
704
705   GNUNET_log_setup ("test-core-quota-compliance",
706                     "WARNING",
707                     NULL);
708   ret = check ();
709   if (test == SYMMETRIC)
710   {
711     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
712     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
713   }
714   else if (test == ASYMMETRIC_SEND_LIMITED)
715   {
716     GNUNET_DISK_directory_remove
717         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
718     GNUNET_DISK_directory_remove
719         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
720   }
721   else if (test == ASYMMETRIC_RECV_LIMITED)
722   {
723     GNUNET_DISK_directory_remove
724         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
725     GNUNET_DISK_directory_remove
726         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
727   }
728   return ret;
729 }
730
731 /* end of test_core_quota_compliance.c */