-fix
[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_TRANSPORT_get_hello_cancel (p2.ghh);
130   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
131   if (p1.nth != NULL)
132   {
133     GNUNET_CORE_notify_transmit_ready_cancel (p1.nth);
134     p1.nth = NULL;
135   }
136   if (connect_task != GNUNET_SCHEDULER_NO_TASK)
137     GNUNET_SCHEDULER_cancel (connect_task);
138   ch = p1.ch;
139   p1.ch = NULL;
140   GNUNET_CORE_disconnect (ch);
141   ch = p2.ch;
142   p2.ch = NULL;
143   GNUNET_CORE_disconnect (ch);
144   GNUNET_TRANSPORT_disconnect (p1.th);
145   p1.th = NULL;
146   GNUNET_TRANSPORT_disconnect (p2.th);
147   p2.th = NULL;
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     GNUNET_SCHEDULER_cancel (connect_task);
168
169   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
170   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
171
172   GNUNET_CORE_disconnect (p1.ch);
173   p1.ch = NULL;
174   GNUNET_CORE_disconnect (p2.ch);
175   p2.ch = NULL;
176   GNUNET_TRANSPORT_disconnect (p1.th);
177   p1.th = NULL;
178   GNUNET_TRANSPORT_disconnect (p2.th);
179   p2.th = NULL;
180   ok = 42;
181 }
182
183
184 static void
185 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
186 {
187   connect_task =
188       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect,
189                                     NULL);
190   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
191   GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
192 }
193
194 /**
195  * Callback function to process statistic values.
196  *
197  * @param cls closure
198  * @param subsystem name of subsystem that created the statistic
199  * @param name the name of the datum
200  * @param value the current value
201  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
202  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
203  */
204 static int
205 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
206             int is_persistent)
207 {
208   if (cls == &p1)
209     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 %50s = %12llu\n", name,
210                 (unsigned long long) value);
211   if (cls == &p2)
212     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 %50s = %12llu\n", name,
213                 (unsigned long long) value);
214   return GNUNET_OK;
215 }
216
217 static void
218 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220   unsigned long long delta;
221   unsigned long long throughput_out;
222   unsigned long long throughput_in;
223   unsigned long long max_quota_in;
224   unsigned long long max_quota_out;
225   unsigned long long quota_delta;
226   enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
227
228   measure_task = GNUNET_SCHEDULER_NO_TASK;
229   FPRINTF (stdout, "%s",  "\n");
230   running = GNUNET_NO;
231
232   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
233
234   throughput_out = total_bytes_sent * 1000 / delta;     /* convert to bytes/s */
235   throughput_in = total_bytes_recv * 1000 / delta;      /* convert to bytes/s */
236
237   max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
238   max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
239   if (max_quota_out < max_quota_in)
240     quota_delta = max_quota_in / 5;
241   else
242     quota_delta = max_quota_out / 5;
243
244   if ((throughput_out > (max_quota_out + quota_delta)) ||
245       (throughput_in > (max_quota_in + quota_delta)))
246     ok = 1;
247   else
248     ok = 0;
249   GNUNET_STATISTICS_get (p1.stats, "core", "# discarded CORE_SEND requests",
250                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
251
252   GNUNET_STATISTICS_get (p1.stats, "core",
253                          "# discarded CORE_SEND request bytes",
254                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
255   GNUNET_STATISTICS_get (p1.stats, "core",
256                          "# discarded lower priority CORE_SEND requests",
257                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, NULL);
258   GNUNET_STATISTICS_get (p1.stats, "core",
259                          "# discarded lower priority CORE_SEND request bytes",
260                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
261   GNUNET_STATISTICS_get (p2.stats, "core", "# discarded CORE_SEND requests",
262                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
263
264   GNUNET_STATISTICS_get (p2.stats, "core",
265                          "# discarded CORE_SEND request bytes",
266                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
267   GNUNET_STATISTICS_get (p2.stats, "core",
268                          "# discarded lower priority CORE_SEND requests",
269                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
270   GNUNET_STATISTICS_get (p2.stats, "core",
271                          "# discarded lower priority CORE_SEND request bytes",
272                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
273
274   if (ok != 0)
275     kind = GNUNET_ERROR_TYPE_ERROR;
276   switch (test)
277   {
278   case SYMMETRIC:
279     GNUNET_log (kind, "Core quota compliance test with symmetric quotas: %s\n",
280                 (ok != 0) ? "PASSED" : "FAILED");
281     break;
282   case ASYMMETRIC_SEND_LIMITED:
283     GNUNET_log (kind,
284                 "Core quota compliance test with limited sender quota: %s\n",
285                 (ok != 0) ? "PASSED" : "FAILED");
286     break;
287   case ASYMMETRIC_RECV_LIMITED:
288     GNUNET_log (kind,
289                 "Core quota compliance test with limited receiver quota: %s\n",
290                 (ok != 0) ? "PASSED" : "FAILED");
291     break;
292   };
293   GNUNET_log (kind, "Peer 1 send  rate: %llu b/s (%llu bytes in %llu ms)\n",
294               throughput_out, total_bytes_sent, delta);
295   GNUNET_log (kind, "Peer 1 send quota: %llu b/s\n", current_quota_p1_out);
296   GNUNET_log (kind, "Peer 2 receive  rate: %llu b/s (%llu bytes in %llu ms)\n",
297               throughput_in, total_bytes_recv, delta);
298   GNUNET_log (kind, "Peer 2 receive quota: %llu b/s\n", current_quota_p2_in);
299 /*
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound  quota allowed: %llu b/s\n",max_quota_in );
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
302 */
303   GNUNET_SCHEDULER_cancel (err_task);
304   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
305
306 }
307
308 static size_t
309 transmit_ready (void *cls, size_t size, void *buf)
310 {
311   char *cbuf = buf;
312   struct TestMessage hdr;
313   unsigned int ret;
314
315   p1.nth = NULL;
316   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
317   if (buf == NULL)
318   {
319     if ((p1.ch != NULL) && (p1.connect_status == 1))
320       GNUNET_break (NULL !=
321                     (p1.nth =
322                      GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
323                                                         FAST_TIMEOUT, &p2.id,
324                                                         MESSAGESIZE,
325                                                         &transmit_ready, &p1)));
326     return 0;
327   }
328   GNUNET_assert (tr_n < TOTAL_MSGS);
329   ret = 0;
330   GNUNET_assert (size >= MESSAGESIZE);
331   GNUNET_assert (buf != NULL);
332   cbuf = buf;
333   do
334   {
335     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336                 "Sending message %u of size %u at offset %u\n", tr_n,
337                 MESSAGESIZE, ret);
338     hdr.header.size = htons (MESSAGESIZE);
339     hdr.header.type = htons (MTYPE);
340     hdr.num = htonl (tr_n);
341     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
342     ret += sizeof (struct TestMessage);
343     memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
344     ret += MESSAGESIZE - sizeof (struct TestMessage);
345     tr_n++;
346     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
347       break;                    /* sometimes pack buffer full, sometimes not */
348   }
349   while (size - ret >= MESSAGESIZE);
350   GNUNET_SCHEDULER_cancel (err_task);
351   err_task =
352       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
353
354   total_bytes_sent += ret;
355   return ret;
356 }
357
358
359
360 static void
361 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
362                 const struct GNUNET_ATS_Information *atsi,
363                 unsigned int atsi_count)
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                 const struct GNUNET_ATS_Information *atsi,
428                 unsigned int atsi_count)
429 {
430   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431               "Core provides inbound data from `%4s' %llu.\n",
432               GNUNET_i2s (other), ntohs (message->size));
433   total_bytes_recv += ntohs (message->size);
434   return GNUNET_OK;
435 }
436
437
438 static int
439 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
440                  const struct GNUNET_MessageHeader *message,
441                  const struct GNUNET_ATS_Information *atsi,
442                  unsigned int atsi_count)
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                const struct GNUNET_ATS_Information *atsi,
458                unsigned int atsi_count)
459 {
460   static int n;
461   const struct TestMessage *hdr;
462
463   hdr = (const struct TestMessage *) message;
464   if (MTYPE != ntohs (message->type))
465     return GNUNET_SYSERR;
466   if (ntohs (message->size) != MESSAGESIZE)
467   {
468     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469                 "Expected message %u of size %u, got %u bytes of message %u\n",
470                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
471     GNUNET_SCHEDULER_cancel (err_task);
472     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
473     return GNUNET_SYSERR;
474   }
475   if (ntohl (hdr->num) != n)
476   {
477     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
478                 "Expected message %u of size %u, got %u bytes of message %u\n",
479                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
480     GNUNET_SCHEDULER_cancel (err_task);
481     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
482     return GNUNET_SYSERR;
483   }
484   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
485               ntohl (hdr->num), ntohs (message->size));
486   n++;
487   if (0 == (n % 10))
488     FPRINTF (stderr, "%s",  ".");
489
490
491   if (running == GNUNET_YES)
492     GNUNET_break (NULL !=
493                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
494                                                      FAST_TIMEOUT, &p2.id,
495                                                      MESSAGESIZE,
496                                                      &transmit_ready, &p1));
497   return GNUNET_OK;
498 }
499
500
501 static struct GNUNET_CORE_MessageHandler handlers[] = {
502   {&process_mtype, MTYPE, 0},
503   {NULL, 0, 0}
504 };
505
506
507
508 static void
509 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
510              const struct GNUNET_PeerIdentity *my_identity)
511 {
512   struct PeerContext *p = cls;
513
514   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515               "Connection to CORE service of `%4s' established\n",
516               GNUNET_i2s (my_identity));
517   GNUNET_assert (server != NULL);
518   p->id = *my_identity;
519   GNUNET_assert (p->ch == server);
520   if (cls == &p1)
521   {
522     GNUNET_assert (ok == 2);
523     OKPP;
524     /* connect p2 */
525     p2.ch =
526         GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
527                              &disconnect_notify, &inbound_notify, GNUNET_YES,
528                              &outbound_notify, GNUNET_YES, handlers);
529   }
530   else
531   {
532     GNUNET_assert (ok == 3);
533     OKPP;
534     GNUNET_assert (cls == &p2);
535     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536                 "Asking core (1) to connect to peer `%4s'\n",
537                 GNUNET_i2s (&p2.id));
538     connect_task = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
539   }
540 }
541
542
543 static void
544 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
545 {
546   struct PeerContext *p = cls;
547
548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549               "Received (my) `%s' from transport service\n", "HELLO");
550   GNUNET_assert (message != NULL);
551   p->hello = GNUNET_malloc (ntohs (message->size));
552   memcpy (p->hello, message, ntohs (message->size));
553   if ((p == &p1) && (p2.th != NULL))
554     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
555   if ((p == &p2) && (p1.th != NULL))
556     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
557
558   if ((p == &p1) && (p2.hello != NULL))
559     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
560   if ((p == &p2) && (p1.hello != NULL))
561     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
562 }
563
564
565
566 static void
567 setup_peer (struct PeerContext *p, const char *cfgname)
568 {
569   p->cfg = GNUNET_CONFIGURATION_create ();
570 #if START_ARM
571   p->arm_proc =
572     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
573                                "gnunet-service-arm",
574 #if VERBOSE
575                                "-L", "DEBUG",
576 #endif
577                                "-c", cfgname, NULL);
578 #endif
579   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
580   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
581   GNUNET_assert (p->stats != NULL);
582   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
583   GNUNET_assert (p->th != NULL);
584   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
585 }
586
587
588 static void
589 run (void *cls, char *const *args, const char *cfgfile,
590      const struct GNUNET_CONFIGURATION_Handle *cfg)
591 {
592   GNUNET_assert (ok == 1);
593   OKPP;
594   err_task =
595       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
596   if (test == SYMMETRIC)
597   {
598     setup_peer (&p1, "test_core_quota_peer1.conf");
599     setup_peer (&p2, "test_core_quota_peer2.conf");
600   }
601   else if (test == ASYMMETRIC_SEND_LIMITED)
602   {
603     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
604     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
605   }
606   else if (test == ASYMMETRIC_RECV_LIMITED)
607   {
608     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
609     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
610   }
611
612   GNUNET_assert (test != -1);
613   GNUNET_assert (GNUNET_SYSERR !=
614                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
615                                                       "WAN_QUOTA_IN",
616                                                       &current_quota_p1_in));
617   GNUNET_assert (GNUNET_SYSERR !=
618                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
619                                                       "WAN_QUOTA_IN",
620                                                       &current_quota_p2_in));
621   GNUNET_assert (GNUNET_SYSERR !=
622                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "ATS",
623                                                       "WAN_QUOTA_OUT",
624                                                       &current_quota_p1_out));
625   GNUNET_assert (GNUNET_SYSERR !=
626                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "ATS",
627                                                       "WAN_QUOTA_OUT",
628                                                       &current_quota_p2_out));
629
630   p1.ch =
631       GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
632                            &disconnect_notify, &inbound_notify, GNUNET_YES,
633                            &outbound_notify, GNUNET_YES, handlers);
634 }
635
636
637 static void
638 stop_arm (struct PeerContext *p)
639 {
640 #if START_ARM
641   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
642     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
643   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
644     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
645   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
646               GNUNET_OS_process_get_pid (p->arm_proc));
647   GNUNET_OS_process_close (p->arm_proc);
648   p->arm_proc = NULL;
649 #endif
650   GNUNET_CONFIGURATION_destroy (p->cfg);
651 }
652
653 static int
654 check ()
655 {
656
657
658   char *const argv[] = { "test-core-quota-compliance",
659     "-c",
660     "test_core_api_data.conf",
661 #if VERBOSE
662     "-L", "DEBUG",
663 #endif
664     NULL
665   };
666   struct GNUNET_GETOPT_CommandLineOption options[] = {
667     GNUNET_GETOPT_OPTION_END
668   };
669   ok = 1;
670   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
671                       "test-core-quota-compliance", "nohelp", options, &run,
672                       &ok);
673   stop_arm (&p1);
674   stop_arm (&p2);
675   return ok;
676 }
677
678 int
679 main (int argc, char *argv[])
680 {
681   int ret;
682
683   test = -1;
684   if (strstr (argv[0], "_symmetric") != NULL)
685   {
686     test = SYMMETRIC;
687   }
688   else if (strstr (argv[0], "_asymmetric_send") != NULL)
689   {
690     test = ASYMMETRIC_SEND_LIMITED;
691   }
692   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
693   {
694     test = ASYMMETRIC_RECV_LIMITED;
695   }
696   GNUNET_assert (test != -1);
697   if (test == SYMMETRIC)
698   {
699     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
700     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
701   }
702   else if (test == ASYMMETRIC_SEND_LIMITED)
703   {
704     GNUNET_DISK_directory_remove
705         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
706     GNUNET_DISK_directory_remove
707         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
708   }
709   else if (test == ASYMMETRIC_RECV_LIMITED)
710   {
711     GNUNET_DISK_directory_remove
712         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
713     GNUNET_DISK_directory_remove
714         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
715   }
716
717   GNUNET_log_setup ("test-core-quota-compliance",
718 #if VERBOSE
719                     "DEBUG",
720 #else
721                     "WARNING",
722 #endif
723                     NULL);
724   ret = check ();
725   if (test == SYMMETRIC)
726   {
727     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
728     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
729   }
730   else if (test == ASYMMETRIC_SEND_LIMITED)
731   {
732     GNUNET_DISK_directory_remove
733         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
734     GNUNET_DISK_directory_remove
735         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
736   }
737   else if (test == ASYMMETRIC_RECV_LIMITED)
738   {
739     GNUNET_DISK_directory_remove
740         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
741     GNUNET_DISK_directory_remove
742         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
743   }
744
745
746
747   return ret;
748 }
749
750 /* end of test_core_api_reliability.c */