even nicer indentation, thanks to LRN's indent patch
[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
118 process_hello (void *cls, 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, const char *subsystem, const char *name, uint64_t value,
177             int is_persistent)
178 {
179   if (cls == &p1)
180     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer1 %50s = %12llu\n", name,
181                 (unsigned long long) value);
182   if (cls == &p2)
183     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer2 %50s = %12llu\n", name,
184                 (unsigned long long) value);
185   return GNUNET_OK;
186 }
187
188 static void
189 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191   unsigned long long int delta;
192   unsigned long long int throughput_out;
193   unsigned long long int throughput_in;
194   unsigned long long int max_quota_in;
195   unsigned long long int max_quota_out;
196   unsigned long long int quota_delta;
197
198   measure_task = GNUNET_SCHEDULER_NO_TASK;
199   fprintf (stdout, "\n");
200   running = GNUNET_NO;
201
202   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
203
204   throughput_out = total_bytes_sent * 1000 / 1024 / delta;
205   throughput_in = total_bytes_recv * 1000 / 1024 / delta;
206
207   if (current_quota_p1_in < current_quota_p2_in)
208     max_quota_in = current_quota_p1_in;
209   else
210     max_quota_in = current_quota_p2_in;
211   if (current_quota_p1_out < current_quota_p2_out)
212     max_quota_out = current_quota_p1_out;
213   else
214     max_quota_out = current_quota_p2_out;
215
216   if (max_quota_out < max_quota_in)
217     quota_delta = max_quota_in / 10;
218   else
219     quota_delta = max_quota_out / 10;
220
221   if ((throughput_out > (max_quota_out + quota_delta) / 1024) ||
222       (throughput_in > (max_quota_in + quota_delta) / 1024))
223     ok = 1;
224   else
225     ok = 0;
226
227   GNUNET_STATISTICS_get (p1.stats, "core", "# discarded CORE_SEND requests",
228                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
229
230   GNUNET_STATISTICS_get (p1.stats, "core",
231                          "# discarded CORE_SEND request bytes",
232                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
233   GNUNET_STATISTICS_get (p1.stats, "core",
234                          "# discarded lower priority CORE_SEND requests",
235                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, NULL);
236   GNUNET_STATISTICS_get (p1.stats, "core",
237                          "# discarded lower priority CORE_SEND request bytes",
238                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p1);
239   GNUNET_STATISTICS_get (p2.stats, "core", "# discarded CORE_SEND requests",
240                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
241
242   GNUNET_STATISTICS_get (p2.stats, "core",
243                          "# discarded CORE_SEND request bytes",
244                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
245   GNUNET_STATISTICS_get (p2.stats, "core",
246                          "# discarded lower priority CORE_SEND requests",
247                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
248   GNUNET_STATISTICS_get (p2.stats, "core",
249                          "# discarded lower priority CORE_SEND request bytes",
250                          GNUNET_TIME_UNIT_FOREVER_REL, NULL, &print_stat, &p2);
251
252   enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
253
254   if (ok == 1)
255   {
256     kind = GNUNET_ERROR_TYPE_ERROR;
257   }
258   switch (test)
259   {
260   case SYMMETRIC:
261     GNUNET_log (kind, "Core quota compliance test with symmetric quotas: %s\n",
262                 (ok == 0) ? "PASSED" : "FAILED");
263     break;
264   case ASYMMETRIC_SEND_LIMITED:
265     GNUNET_log (kind,
266                 "Core quota compliance test with limited sender quota: %s\n",
267                 (ok == 0) ? "PASSED" : "FAILED");
268     break;
269   case ASYMMETRIC_RECV_LIMITED:
270     GNUNET_log (kind,
271                 "Core quota compliance test with limited receiver quota: %s\n",
272                 (ok == 0) ? "PASSED" : "FAILED");
273     break;
274   };
275   GNUNET_log (kind, "Peer 1 send  rate: %llu kB/s (%llu Bytes in %u sec.)\n",
276               throughput_out, total_bytes_sent, delta / 1000);
277   GNUNET_log (kind, "Peer 1 send quota: %llu kB/s\n",
278               current_quota_p1_out / 1024);
279   GNUNET_log (kind, "Peer 2 receive  rate: %llu kB/s (%llu Bytes in %u sec.)\n",
280               throughput_in, total_bytes_recv, delta / 1000);
281   GNUNET_log (kind, "Peer 2 receive quota: %llu kB/s\n",
282               current_quota_p2_in / 1024);
283 /*
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound  quota allowed: %llu kB/s\n",max_quota_in /1024);
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu kB/s\n",max_quota_out/1024);
286 */
287   GNUNET_SCHEDULER_cancel (err_task);
288   GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
289
290 }
291
292 static size_t
293 transmit_ready (void *cls, size_t size, void *buf)
294 {
295   char *cbuf = buf;
296   struct TestMessage hdr;
297   unsigned int ret;
298
299   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
300   if (buf == NULL)
301   {
302     if ((p1.ch != NULL) && (p1.connect_status == 1))
303       GNUNET_break (NULL !=
304                     GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
305                                                        FAST_TIMEOUT, &p2.id,
306                                                        MESSAGESIZE,
307                                                        &transmit_ready, &p1));
308     return 0;
309   }
310   GNUNET_assert (tr_n < TOTAL_MSGS);
311   ret = 0;
312   GNUNET_assert (size >= MESSAGESIZE);
313   GNUNET_assert (buf != NULL);
314   cbuf = buf;
315   do
316   {
317 #if DEBUG_TRANSMISSION
318     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319                 "Sending message %u of size %u at offset %u\n", tr_n,
320                 MESSAGESIZE, ret);
321 #endif
322     hdr.header.size = htons (MESSAGESIZE);
323     hdr.header.type = htons (MTYPE);
324     hdr.num = htonl (tr_n);
325     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
326     ret += sizeof (struct TestMessage);
327     memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
328     ret += MESSAGESIZE - sizeof (struct TestMessage);
329     tr_n++;
330     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
331       break;                    /* sometimes pack buffer full, sometimes not */
332   }
333   while (size - ret >= MESSAGESIZE);
334   GNUNET_SCHEDULER_cancel (err_task);
335   err_task =
336       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
337
338   total_bytes_sent += ret;
339   return ret;
340 }
341
342
343
344 static void
345 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
346                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
347 {
348   struct PeerContext *pc = cls;
349
350   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
351     return;
352   GNUNET_assert (pc->connect_status == 0);
353   pc->connect_status = 1;
354   if (pc == &p1)
355   {
356 #if DEBUG_TRANSMISSION
357     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358                 "Encrypted connection established to peer `%4s'\n",
359                 GNUNET_i2s (peer));
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361                 "Asking core (1) for transmission to peer `%4s'\n",
362                 GNUNET_i2s (&p2.id));
363 #endif
364     if (err_task != GNUNET_SCHEDULER_NO_TASK)
365       GNUNET_SCHEDULER_cancel (err_task);
366     err_task =
367         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
368     start_time = GNUNET_TIME_absolute_get ();
369     running = GNUNET_YES;
370     measure_task =
371         GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH, &measurement_stop,
372                                       NULL);
373
374     GNUNET_break (NULL !=
375                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
376                                                      TIMEOUT, &p2.id,
377                                                      MESSAGESIZE,
378                                                      &transmit_ready, &p1));
379   }
380 }
381
382
383 static void
384 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
385 {
386   struct PeerContext *pc = cls;
387
388   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
389     return;
390   pc->connect_status = 0;
391 #if DEBUG_TRANSMISSION
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
393               GNUNET_i2s (peer));
394 #endif
395 }
396
397
398 static int
399 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
400                 const struct GNUNET_MessageHeader *message,
401                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
402 {
403 #if DEBUG_TRANSMISSION
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405               "Core provides inbound data from `%4s' %llu.\n",
406               GNUNET_i2s (other), ntohs (message->size));
407 #endif
408   total_bytes_recv += ntohs (message->size);
409   return GNUNET_OK;
410 }
411
412
413 static int
414 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
415                  const struct GNUNET_MessageHeader *message,
416                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
417 {
418 #if DEBUG_TRANSMISSION
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420               "Core notifies about outbound data for `%4s'.\n",
421               GNUNET_i2s (other));
422 #endif
423   return GNUNET_OK;
424 }
425
426
427 static size_t
428 transmit_ready (void *cls, size_t size, void *buf);
429
430 static int
431 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
432                const struct GNUNET_MessageHeader *message,
433                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
434 {
435   static int n;
436   const struct TestMessage *hdr;
437
438   hdr = (const struct TestMessage *) message;
439   if (MTYPE != ntohs (message->type))
440     return GNUNET_SYSERR;
441   if (ntohs (message->size) != MESSAGESIZE)
442   {
443     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444                 "Expected message %u of size %u, got %u bytes of message %u\n",
445                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
446     GNUNET_SCHEDULER_cancel (err_task);
447     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
448     return GNUNET_SYSERR;
449   }
450   if (ntohl (hdr->num) != n)
451   {
452     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453                 "Expected message %u of size %u, got %u bytes of message %u\n",
454                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
455     GNUNET_SCHEDULER_cancel (err_task);
456     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
457     return GNUNET_SYSERR;
458   }
459 #if DEBUG_TRANSMISSION
460   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
461               ntohl (hdr->num), ntohs (message->size));
462 #endif
463   n++;
464   if (0 == (n % 10))
465     fprintf (stderr, ".");
466
467
468   if (running == GNUNET_YES)
469     GNUNET_break (NULL !=
470                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
471                                                      FAST_TIMEOUT, &p2.id,
472                                                      MESSAGESIZE,
473                                                      &transmit_ready, &p1));
474   return GNUNET_OK;
475 }
476
477
478 static struct GNUNET_CORE_MessageHandler handlers[] = {
479   {&process_mtype, MTYPE, 0},
480   {NULL, 0, 0}
481 };
482
483
484
485 static void
486 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
487              const struct GNUNET_PeerIdentity *my_identity,
488              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
489 {
490   struct PeerContext *p = cls;
491
492   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493               "Connection to CORE service of `%4s' established\n",
494               GNUNET_i2s (my_identity));
495   GNUNET_assert (server != NULL);
496   p->id = *my_identity;
497   p->ch = server;
498   if (cls == &p1)
499   {
500     GNUNET_assert (ok == 2);
501     OKPP;
502     /* connect p2 */
503     GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
504                          &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
505                          &outbound_notify, GNUNET_YES, handlers);
506   }
507   else
508   {
509     GNUNET_assert (ok == 3);
510     OKPP;
511     GNUNET_assert (cls == &p2);
512 #if DEBUG_TRANSMISSION
513     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514                 "Asking core (1) to connect to peer `%4s'\n",
515                 GNUNET_i2s (&p2.id));
516 #endif
517     GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
518   }
519 }
520
521
522 static void
523 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
524 {
525   struct PeerContext *p = cls;
526
527
528 #if DEBUG_TRANSMISSION
529   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530               "Received (my) `%s' from transport service\n", "HELLO");
531 #endif
532   GNUNET_assert (message != NULL);
533   p->hello = GNUNET_malloc (ntohs (message->size));
534   memcpy (p->hello, message, ntohs (message->size));
535   if ((p == &p1) && (p2.th != NULL))
536     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
537   if ((p == &p2) && (p1.th != NULL))
538     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
539
540   if ((p == &p1) && (p2.hello != NULL))
541     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
542   if ((p == &p2) && (p1.hello != NULL))
543     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
544 }
545
546
547
548 static void
549 setup_peer (struct PeerContext *p, const char *cfgname)
550 {
551   p->cfg = GNUNET_CONFIGURATION_create ();
552 #if START_ARM
553   p->arm_proc =
554       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
555                                "gnunet-service-arm",
556 #if VERBOSE
557                                "-L", "DEBUG",
558 #endif
559                                "-c", cfgname, NULL);
560 #endif
561   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
562   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
563   GNUNET_assert (p->stats != NULL);
564   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
565   GNUNET_assert (p->th != NULL);
566   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
567 }
568
569
570 static void
571 run (void *cls, char *const *args, const char *cfgfile,
572      const struct GNUNET_CONFIGURATION_Handle *cfg)
573 {
574   GNUNET_assert (ok == 1);
575   OKPP;
576   err_task =
577       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
578   if (test == SYMMETRIC)
579   {
580     setup_peer (&p1, "test_core_quota_peer1.conf");
581     setup_peer (&p2, "test_core_quota_peer2.conf");
582   }
583   else if (test == ASYMMETRIC_SEND_LIMITED)
584   {
585     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
586     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
587   }
588   else if (test == ASYMMETRIC_RECV_LIMITED)
589   {
590     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
591     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
592   }
593
594   GNUNET_assert (test != -1);
595   GNUNET_assert (GNUNET_SYSERR !=
596                  GNUNET_CONFIGURATION_get_value_number (p1.cfg, "CORE",
597                                                         "TOTAL_QUOTA_IN",
598                                                         &current_quota_p1_in));
599   GNUNET_assert (GNUNET_SYSERR !=
600                  GNUNET_CONFIGURATION_get_value_number (p2.cfg, "CORE",
601                                                         "TOTAL_QUOTA_IN",
602                                                         &current_quota_p2_in));
603   GNUNET_assert (GNUNET_SYSERR !=
604                  GNUNET_CONFIGURATION_get_value_number (p1.cfg, "CORE",
605                                                         "TOTAL_QUOTA_OUT",
606                                                         &current_quota_p1_out));
607   GNUNET_assert (GNUNET_SYSERR !=
608                  GNUNET_CONFIGURATION_get_value_number (p2.cfg, "CORE",
609                                                         "TOTAL_QUOTA_OUT",
610                                                         &current_quota_p2_out));
611
612   GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
613                        &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
614                        &outbound_notify, GNUNET_YES, handlers);
615 }
616
617
618 static void
619 stop_arm (struct PeerContext *p)
620 {
621 #if START_ARM
622   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
623     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
624   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
625     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
626   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
627               GNUNET_OS_process_get_pid (p->arm_proc));
628   GNUNET_OS_process_close (p->arm_proc);
629   p->arm_proc = NULL;
630 #endif
631   GNUNET_CONFIGURATION_destroy (p->cfg);
632 }
633
634 static int
635 check ()
636 {
637
638
639   char *const argv[] = { "test-core-quota-compliance",
640     "-c",
641     "test_core_api_data.conf",
642 #if VERBOSE
643     "-L", "DEBUG",
644 #endif
645     NULL
646   };
647   struct GNUNET_GETOPT_CommandLineOption options[] = {
648     GNUNET_GETOPT_OPTION_END
649   };
650   ok = 1;
651   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
652                       "test-core-quota-compliance", "nohelp", options, &run,
653                       &ok);
654   stop_arm (&p1);
655   stop_arm (&p2);
656   return ok;
657 }
658
659 int
660 main (int argc, char *argv[])
661 {
662   int ret;
663
664   test = -1;
665   if (strstr (argv[0], "_symmetric") != NULL)
666   {
667     test = SYMMETRIC;
668   }
669   else if (strstr (argv[0], "_asymmetric_send") != NULL)
670   {
671     test = ASYMMETRIC_SEND_LIMITED;
672   }
673   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
674   {
675     test = ASYMMETRIC_RECV_LIMITED;
676   }
677   GNUNET_assert (test != -1);
678   if (test == SYMMETRIC)
679   {
680     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
681     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
682   }
683   else if (test == ASYMMETRIC_SEND_LIMITED)
684   {
685     GNUNET_DISK_directory_remove
686         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
687     GNUNET_DISK_directory_remove
688         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
689   }
690   else if (test == ASYMMETRIC_RECV_LIMITED)
691   {
692     GNUNET_DISK_directory_remove
693         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
694     GNUNET_DISK_directory_remove
695         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
696   }
697
698   GNUNET_log_setup ("test-core-quota-compliance",
699 #if VERBOSE
700                     "DEBUG",
701 #else
702                     "WARNING",
703 #endif
704                     NULL);
705   ret = check ();
706   if (test == SYMMETRIC)
707   {
708     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
709     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
710   }
711   else if (test == ASYMMETRIC_SEND_LIMITED)
712   {
713     GNUNET_DISK_directory_remove
714         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
715     GNUNET_DISK_directory_remove
716         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
717   }
718   else if (test == ASYMMETRIC_RECV_LIMITED)
719   {
720     GNUNET_DISK_directory_remove
721         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
722     GNUNET_DISK_directory_remove
723         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
724   }
725
726
727
728   return ret;
729 }
730
731 /* end of test_core_api_reliability.c */