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