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