13a837cebdef95e69646f5f5ff9eedcc0e747e00
[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 #define VERBOSE GNUNET_NO
37
38 #define SYMMETRIC 0
39 #define ASYMMETRIC_SEND_LIMITED 1
40 #define ASYMMETRIC_RECV_LIMITED 2
41
42 #define START_ARM GNUNET_YES
43
44 /**
45  * Note that this value must not significantly exceed
46  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
47  * messages may be dropped even for a reliable transport.
48  */
49 #define TOTAL_MSGS (60000 * 10)
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
55
56 /**
57  * What delay do we request from the core service for transmission?
58  */
59 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
60
61 #define MTYPE 12345
62 #define MESSAGESIZE 1024
63 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
64
65 static unsigned long long total_bytes_sent;
66 static unsigned long long total_bytes_recv;
67
68 static struct GNUNET_TIME_Absolute start_time;
69
70 static GNUNET_SCHEDULER_TaskIdentifier err_task;
71
72 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
73
74 static GNUNET_SCHEDULER_TaskIdentifier connect_task;
75
76
77 struct PeerContext
78 {
79   struct GNUNET_CONFIGURATION_Handle *cfg;
80   struct GNUNET_CORE_Handle *ch;
81   struct GNUNET_CORE_TransmitHandle *nth;
82   struct GNUNET_PeerIdentity id;
83   struct GNUNET_TRANSPORT_Handle *th;
84   struct GNUNET_MessageHeader *hello;
85   struct GNUNET_STATISTICS_Handle *stats;
86   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
87   int connect_status;
88 #if START_ARM
89   struct GNUNET_OS_Process *arm_proc;
90 #endif
91 };
92
93 static struct PeerContext p1;
94 static struct PeerContext p2;
95
96 static unsigned long long current_quota_p1_in;
97 static unsigned long long current_quota_p1_out;
98 static unsigned long long current_quota_p2_in;
99 static unsigned long long current_quota_p2_out;
100
101 static int ok;
102 static int test;
103 static int32_t tr_n;
104
105 static int running;
106
107
108 #if VERBOSE
109 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
110 #else
111 #define OKPP do { ok++; } while (0)
112 #endif
113
114 struct TestMessage
115 {
116   struct GNUNET_MessageHeader header;
117   uint32_t num;
118 };
119
120 static void
121 process_hello (void *cls, const struct GNUNET_MessageHeader *message);
122
123 static void
124 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 {
126   struct GNUNET_CORE_Handle *ch;
127
128   err_task = GNUNET_SCHEDULER_NO_TASK;
129   GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
130   GNUNET_STATISTICS_destroy (p2.stats, GNUNET_NO);
131   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
132   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
133   if (p1.nth != NULL)
134   {
135     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
136     p1.nth = NULL;
137   }
138   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
139     GNUNET_SCHEDULER_cancel (connect_task);
140   ch = p1.ch;
141   p1.ch = NULL;
142   GNUNET_CORE_disconnect (ch);
143   ch = p2.ch;
144   p2.ch = NULL;
145   GNUNET_CORE_disconnect (ch);
146   GNUNET_TRANSPORT_disconnect (p1.th);
147   p1.th = NULL;
148   GNUNET_TRANSPORT_disconnect (p2.th);
149   p2.th = NULL;
150 }
151
152
153 static void
154 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 {
156   err_task = GNUNET_SCHEDULER_NO_TASK;
157
158   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
160   //GNUNET_break (0);
161   if (p1.nth != NULL)
162   {
163     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
164     p1.nth = NULL;
165   }
166   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
167     GNUNET_SCHEDULER_cancel (measure_task);
168   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
169     GNUNET_SCHEDULER_cancel (connect_task);
170
171   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
172   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
173
174   GNUNET_CORE_disconnect (p1.ch);
175   p1.ch = NULL;
176   GNUNET_CORE_disconnect (p2.ch);
177   p2.ch = NULL;
178   GNUNET_TRANSPORT_disconnect (p1.th);
179   p1.th = NULL;
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);
193   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
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;
235
236   throughput_out = total_bytes_sent * 1000 / delta;     /* convert to bytes/s */
237   throughput_in = total_bytes_recv * 1000 / 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 / 5;
243   else
244     quota_delta = max_quota_out / 5;
245
246   if ((throughput_out > (max_quota_out + quota_delta)) ||
247       (throughput_in > (max_quota_in + quota_delta)))
248     ok = 1;
249   else
250     ok = 0;
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                 (ok != 0) ? "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                 (ok != 0) ? "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                 (ok != 0) ? "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                 const struct GNUNET_ATS_Information *atsi,
365                 unsigned int atsi_count)
366 {
367   struct PeerContext *pc = cls;
368
369   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
370     return;                     /* loopback */
371   GNUNET_assert (pc->connect_status == 0);
372   pc->connect_status = 1;
373   if (pc == &p1)
374   {
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Encrypted connection established to peer `%4s'\n",
377                 GNUNET_i2s (peer));
378     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379                 "Asking core (1) for transmission to peer `%4s'\n",
380                 GNUNET_i2s (&p2.id));
381     if (err_task != GNUNET_SCHEDULER_NO_TASK)
382       GNUNET_SCHEDULER_cancel (err_task);
383     err_task =
384         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
385     start_time = GNUNET_TIME_absolute_get ();
386     running = GNUNET_YES;
387     measure_task =
388         GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH, &measurement_stop,
389                                       NULL);
390
391     GNUNET_break (NULL !=
392                   (p1.nth =
393                    GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
394                                                       TIMEOUT, &p2.id,
395                                                       MESSAGESIZE,
396                                                       &transmit_ready, &p1)));
397   }
398 }
399
400
401 static void
402 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
403 {
404   struct PeerContext *pc = cls;
405
406   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
407     return;                     /* loopback */
408   pc->connect_status = 0;
409   if (GNUNET_SCHEDULER_NO_TASK != measure_task)
410   {
411     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412                 "Measurement aborted due to disconnect!\n");
413     GNUNET_SCHEDULER_cancel (measure_task);
414     measure_task = GNUNET_SCHEDULER_NO_TASK;
415   }
416   if (pc->nth != NULL)
417   {
418     GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
419     pc->nth = NULL;
420   }
421   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
422               GNUNET_i2s (peer));
423 }
424
425
426 static int
427 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
428                 const struct GNUNET_MessageHeader *message,
429                 const struct GNUNET_ATS_Information *atsi,
430                 unsigned int atsi_count)
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                  const struct GNUNET_ATS_Information *atsi,
444                  unsigned int atsi_count)
445 {
446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447               "Core notifies about outbound data for `%4s'.\n",
448               GNUNET_i2s (other));
449   return GNUNET_OK;
450 }
451
452
453 static size_t
454 transmit_ready (void *cls, size_t size, void *buf);
455
456 static int
457 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
458                const struct GNUNET_MessageHeader *message,
459                const struct GNUNET_ATS_Information *atsi,
460                unsigned int atsi_count)
461 {
462   static int n;
463   const struct TestMessage *hdr;
464
465   hdr = (const struct TestMessage *) message;
466   if (MTYPE != ntohs (message->type))
467     return GNUNET_SYSERR;
468   if (ntohs (message->size) != MESSAGESIZE)
469   {
470     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
471                 "Expected message %u of size %u, got %u bytes of message %u\n",
472                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
473     GNUNET_SCHEDULER_cancel (err_task);
474     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
475     return GNUNET_SYSERR;
476   }
477   if (ntohl (hdr->num) != n)
478   {
479     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480                 "Expected message %u of size %u, got %u bytes of message %u\n",
481                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
482     GNUNET_SCHEDULER_cancel (err_task);
483     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
484     return GNUNET_SYSERR;
485   }
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
487               ntohl (hdr->num), ntohs (message->size));
488   n++;
489   if (0 == (n % 10))
490     FPRINTF (stderr, "%s",  ".");
491
492
493   if (running == GNUNET_YES)
494     GNUNET_break (NULL !=
495                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
496                                                      FAST_TIMEOUT, &p2.id,
497                                                      MESSAGESIZE,
498                                                      &transmit_ready, &p1));
499   return GNUNET_OK;
500 }
501
502
503 static struct GNUNET_CORE_MessageHandler handlers[] = {
504   {&process_mtype, MTYPE, 0},
505   {NULL, 0, 0}
506 };
507
508
509
510 static void
511 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
512              const struct GNUNET_PeerIdentity *my_identity)
513 {
514   struct PeerContext *p = cls;
515
516   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517               "Connection to CORE service of `%4s' established\n",
518               GNUNET_i2s (my_identity));
519   GNUNET_assert (server != NULL);
520   p->id = *my_identity;
521   GNUNET_assert (p->ch == server);
522   if (cls == &p1)
523   {
524     GNUNET_assert (ok == 2);
525     OKPP;
526     /* connect p2 */
527     p2.ch =
528         GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
529                              &disconnect_notify, &inbound_notify, GNUNET_YES,
530                              &outbound_notify, GNUNET_YES, handlers);
531   }
532   else
533   {
534     GNUNET_assert (ok == 3);
535     OKPP;
536     GNUNET_assert (cls == &p2);
537     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538                 "Asking core (1) to connect to peer `%4s'\n",
539                 GNUNET_i2s (&p2.id));
540     connect_task = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
541   }
542 }
543
544
545 static void
546 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
547 {
548   struct PeerContext *p = cls;
549
550   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551               "Received (my) `%s' from transport service\n", "HELLO");
552   GNUNET_assert (message != NULL);
553   p->hello = GNUNET_malloc (ntohs (message->size));
554   memcpy (p->hello, message, ntohs (message->size));
555   if ((p == &p1) && (p2.th != NULL))
556     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
557   if ((p == &p2) && (p1.th != NULL))
558     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
559
560   if ((p == &p1) && (p2.hello != NULL))
561     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
562   if ((p == &p2) && (p1.hello != NULL))
563     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
564 }
565
566
567
568 static void
569 setup_peer (struct PeerContext *p, const char *cfgname)
570 {
571   p->cfg = GNUNET_CONFIGURATION_create ();
572 #if START_ARM
573   p->arm_proc =
574     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
575                                "gnunet-service-arm",
576 #if VERBOSE
577                                "-L", "DEBUG",
578 #endif
579                                "-c", cfgname, NULL);
580 #endif
581   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
582   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
583   GNUNET_assert (p->stats != NULL);
584   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
585   GNUNET_assert (p->th != NULL);
586   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
587 }
588
589
590 static void
591 run (void *cls, char *const *args, const char *cfgfile,
592      const struct GNUNET_CONFIGURATION_Handle *cfg)
593 {
594   GNUNET_assert (ok == 1);
595   OKPP;
596   err_task =
597       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
598   if (test == SYMMETRIC)
599   {
600     setup_peer (&p1, "test_core_quota_peer1.conf");
601     setup_peer (&p2, "test_core_quota_peer2.conf");
602   }
603   else if (test == ASYMMETRIC_SEND_LIMITED)
604   {
605     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
606     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
607   }
608   else if (test == ASYMMETRIC_RECV_LIMITED)
609   {
610     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
611     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
612   }
613
614   GNUNET_assert (test != -1);
615   GNUNET_assert (GNUNET_SYSERR !=
616                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
617                                                       "WAN_QUOTA_IN",
618                                                       &current_quota_p1_in));
619   GNUNET_assert (GNUNET_SYSERR !=
620                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
621                                                       "WAN_QUOTA_IN",
622                                                       &current_quota_p2_in));
623   GNUNET_assert (GNUNET_SYSERR !=
624                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
625                                                       "WAN_QUOTA_OUT",
626                                                       &current_quota_p1_out));
627   GNUNET_assert (GNUNET_SYSERR !=
628                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
629                                                       "WAN_QUOTA_OUT",
630                                                       &current_quota_p2_out));
631
632   p1.ch =
633       GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
634                            &disconnect_notify, &inbound_notify, GNUNET_YES,
635                            &outbound_notify, GNUNET_YES, handlers);
636 }
637
638
639 static void
640 stop_arm (struct PeerContext *p)
641 {
642 #if START_ARM
643   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
644     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
645   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
646     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
647   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
648               GNUNET_OS_process_get_pid (p->arm_proc));
649   GNUNET_OS_process_close (p->arm_proc);
650   p->arm_proc = NULL;
651 #endif
652   GNUNET_CONFIGURATION_destroy (p->cfg);
653 }
654
655 static int
656 check ()
657 {
658
659
660   char *const argv[] = { "test-core-quota-compliance",
661     "-c",
662     "test_core_api_data.conf",
663 #if VERBOSE
664     "-L", "DEBUG",
665 #endif
666     NULL
667   };
668   struct GNUNET_GETOPT_CommandLineOption options[] = {
669     GNUNET_GETOPT_OPTION_END
670   };
671   ok = 1;
672   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
673                       "test-core-quota-compliance", "nohelp", options, &run,
674                       &ok);
675   stop_arm (&p1);
676   stop_arm (&p2);
677   return ok;
678 }
679
680 int
681 main (int argc, char *argv[])
682 {
683   int ret;
684
685   test = -1;
686   if (strstr (argv[0], "_symmetric") != NULL)
687   {
688     test = SYMMETRIC;
689   }
690   else if (strstr (argv[0], "_asymmetric_send") != NULL)
691   {
692     test = ASYMMETRIC_SEND_LIMITED;
693   }
694   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
695   {
696     test = ASYMMETRIC_RECV_LIMITED;
697   }
698   GNUNET_assert (test != -1);
699   if (test == SYMMETRIC)
700   {
701     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
702     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
703   }
704   else if (test == ASYMMETRIC_SEND_LIMITED)
705   {
706     GNUNET_DISK_directory_remove
707         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
708     GNUNET_DISK_directory_remove
709         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
710   }
711   else if (test == ASYMMETRIC_RECV_LIMITED)
712   {
713     GNUNET_DISK_directory_remove
714         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
715     GNUNET_DISK_directory_remove
716         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
717   }
718
719   GNUNET_log_setup ("test-core-quota-compliance",
720 #if VERBOSE
721                     "DEBUG",
722 #else
723                     "WARNING",
724 #endif
725                     NULL);
726   ret = check ();
727   if (test == SYMMETRIC)
728   {
729     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
730     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
731   }
732   else if (test == ASYMMETRIC_SEND_LIMITED)
733   {
734     GNUNET_DISK_directory_remove
735         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
736     GNUNET_DISK_directory_remove
737         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
738   }
739   else if (test == ASYMMETRIC_RECV_LIMITED)
740   {
741     GNUNET_DISK_directory_remove
742         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
743     GNUNET_DISK_directory_remove
744         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
745   }
746
747
748
749   return ret;
750 }
751
752 /* end of test_core_api_reliability.c */