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