88ba2c0f205754a50206f201fd0ca6cf0e430997
[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, 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 transmit_ready (void *cls, size_t size, void *buf);
428
429 static int
430 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
431                const struct GNUNET_MessageHeader *message,
432                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
433 {
434   static int n;
435   const struct TestMessage *hdr;
436
437   hdr = (const struct TestMessage *) message;
438   if (MTYPE != ntohs (message->type))
439     return GNUNET_SYSERR;
440   if (ntohs (message->size) != MESSAGESIZE)
441   {
442     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443                 "Expected message %u of size %u, got %u bytes of message %u\n",
444                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
445     GNUNET_SCHEDULER_cancel (err_task);
446     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
447     return GNUNET_SYSERR;
448   }
449   if (ntohl (hdr->num) != n)
450   {
451     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452                 "Expected message %u of size %u, got %u bytes of message %u\n",
453                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
454     GNUNET_SCHEDULER_cancel (err_task);
455     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
456     return GNUNET_SYSERR;
457   }
458 #if DEBUG_TRANSMISSION
459   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
460               ntohl (hdr->num), ntohs (message->size));
461 #endif
462   n++;
463   if (0 == (n % 10))
464     fprintf (stderr, ".");
465
466
467   if (running == GNUNET_YES)
468     GNUNET_break (NULL !=
469                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
470                                                      FAST_TIMEOUT, &p2.id,
471                                                      MESSAGESIZE,
472                                                      &transmit_ready, &p1));
473   return GNUNET_OK;
474 }
475
476
477 static struct GNUNET_CORE_MessageHandler handlers[] = {
478   {&process_mtype, MTYPE, 0},
479   {NULL, 0, 0}
480 };
481
482
483
484 static void
485 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
486              const struct GNUNET_PeerIdentity *my_identity,
487              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
488 {
489   struct PeerContext *p = cls;
490
491   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492               "Connection to CORE service of `%4s' established\n",
493               GNUNET_i2s (my_identity));
494   GNUNET_assert (server != NULL);
495   p->id = *my_identity;
496   p->ch = server;
497   if (cls == &p1)
498   {
499     GNUNET_assert (ok == 2);
500     OKPP;
501     /* connect p2 */
502     GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
503                          &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
504                          &outbound_notify, GNUNET_YES, handlers);
505   }
506   else
507   {
508     GNUNET_assert (ok == 3);
509     OKPP;
510     GNUNET_assert (cls == &p2);
511 #if DEBUG_TRANSMISSION
512     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513                 "Asking core (1) to connect to peer `%4s'\n",
514                 GNUNET_i2s (&p2.id));
515 #endif
516     GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
517   }
518 }
519
520
521 static void
522 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
523 {
524   struct PeerContext *p = cls;
525
526
527 #if DEBUG_TRANSMISSION
528   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529               "Received (my) `%s' from transport service\n", "HELLO");
530 #endif
531   GNUNET_assert (message != NULL);
532   p->hello = GNUNET_malloc (ntohs (message->size));
533   memcpy (p->hello, message, ntohs (message->size));
534   if ((p == &p1) && (p2.th != NULL))
535     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
536   if ((p == &p2) && (p1.th != NULL))
537     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
538
539   if ((p == &p1) && (p2.hello != NULL))
540     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
541   if ((p == &p2) && (p1.hello != NULL))
542     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
543 }
544
545
546
547 static void
548 setup_peer (struct PeerContext *p, const char *cfgname)
549 {
550   p->cfg = GNUNET_CONFIGURATION_create ();
551 #if START_ARM
552   p->arm_proc =
553       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
554                                "gnunet-service-arm",
555 #if VERBOSE
556                                "-L", "DEBUG",
557 #endif
558                                "-c", cfgname, NULL);
559 #endif
560   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
561   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
562   GNUNET_assert (p->stats != NULL);
563   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
564   GNUNET_assert (p->th != NULL);
565   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
566 }
567
568
569 static void
570 run (void *cls, char *const *args, const char *cfgfile,
571      const struct GNUNET_CONFIGURATION_Handle *cfg)
572 {
573   GNUNET_assert (ok == 1);
574   OKPP;
575   err_task =
576       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
577   if (test == SYMMETRIC)
578   {
579     setup_peer (&p1, "test_core_quota_peer1.conf");
580     setup_peer (&p2, "test_core_quota_peer2.conf");
581   }
582   else if (test == ASYMMETRIC_SEND_LIMITED)
583   {
584     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
585     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
586   }
587   else if (test == ASYMMETRIC_RECV_LIMITED)
588   {
589     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
590     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
591   }
592
593   GNUNET_assert (test != -1);
594   GNUNET_assert (GNUNET_SYSERR !=
595                  GNUNET_CONFIGURATION_get_value_number (p1.cfg, "CORE",
596                                                         "TOTAL_QUOTA_IN",
597                                                         &current_quota_p1_in));
598   GNUNET_assert (GNUNET_SYSERR !=
599                  GNUNET_CONFIGURATION_get_value_number (p2.cfg, "CORE",
600                                                         "TOTAL_QUOTA_IN",
601                                                         &current_quota_p2_in));
602   GNUNET_assert (GNUNET_SYSERR !=
603                  GNUNET_CONFIGURATION_get_value_number (p1.cfg, "CORE",
604                                                         "TOTAL_QUOTA_OUT",
605                                                         &current_quota_p1_out));
606   GNUNET_assert (GNUNET_SYSERR !=
607                  GNUNET_CONFIGURATION_get_value_number (p2.cfg, "CORE",
608                                                         "TOTAL_QUOTA_OUT",
609                                                         &current_quota_p2_out));
610
611   GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
612                        &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
613                        &outbound_notify, GNUNET_YES, handlers);
614 }
615
616
617 static void
618 stop_arm (struct PeerContext *p)
619 {
620 #if START_ARM
621   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
622     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
623   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
624     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
625   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
626               GNUNET_OS_process_get_pid (p->arm_proc));
627   GNUNET_OS_process_close (p->arm_proc);
628   p->arm_proc = NULL;
629 #endif
630   GNUNET_CONFIGURATION_destroy (p->cfg);
631 }
632
633 static int
634 check ()
635 {
636
637
638   char *const argv[] = { "test-core-quota-compliance",
639     "-c",
640     "test_core_api_data.conf",
641 #if VERBOSE
642     "-L", "DEBUG",
643 #endif
644     NULL
645   };
646   struct GNUNET_GETOPT_CommandLineOption options[] = {
647     GNUNET_GETOPT_OPTION_END
648   };
649   ok = 1;
650   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
651                       "test-core-quota-compliance", "nohelp", options, &run,
652                       &ok);
653   stop_arm (&p1);
654   stop_arm (&p2);
655   return ok;
656 }
657
658 int
659 main (int argc, char *argv[])
660 {
661   int ret;
662
663   test = -1;
664   if (strstr (argv[0], "_symmetric") != NULL)
665   {
666     test = SYMMETRIC;
667   }
668   else if (strstr (argv[0], "_asymmetric_send") != NULL)
669   {
670     test = ASYMMETRIC_SEND_LIMITED;
671   }
672   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
673   {
674     test = ASYMMETRIC_RECV_LIMITED;
675   }
676   GNUNET_assert (test != -1);
677   if (test == SYMMETRIC)
678   {
679     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
680     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
681   }
682   else if (test == ASYMMETRIC_SEND_LIMITED)
683   {
684     GNUNET_DISK_directory_remove
685         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
686     GNUNET_DISK_directory_remove
687         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
688   }
689   else if (test == ASYMMETRIC_RECV_LIMITED)
690   {
691     GNUNET_DISK_directory_remove
692         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
693     GNUNET_DISK_directory_remove
694         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
695   }
696
697   GNUNET_log_setup ("test-core-quota-compliance",
698 #if VERBOSE
699                     "DEBUG",
700 #else
701                     "WARNING",
702 #endif
703                     NULL);
704   ret = check ();
705   if (test == SYMMETRIC)
706   {
707     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
708     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
709   }
710   else if (test == ASYMMETRIC_SEND_LIMITED)
711   {
712     GNUNET_DISK_directory_remove
713         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
714     GNUNET_DISK_directory_remove
715         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
716   }
717   else if (test == ASYMMETRIC_RECV_LIMITED)
718   {
719     GNUNET_DISK_directory_remove
720         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
721     GNUNET_DISK_directory_remove
722         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
723   }
724
725
726
727   return ret;
728 }
729
730 /* end of test_core_api_reliability.c */