593ffa1202678571b002277f0e35d008b7e9a79e
[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 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 {
120   struct GNUNET_CORE_Handle *ch;
121
122   ch = p1.ch;
123   p1.ch = NULL;
124   GNUNET_CORE_disconnect (ch);
125   ch = p2.ch;
126   p2.ch = NULL;
127   GNUNET_CORE_disconnect (ch);
128   GNUNET_TRANSPORT_disconnect (p1.th);
129   p1.th = NULL;
130   GNUNET_TRANSPORT_disconnect (p2.th);
131   p2.th = NULL;
132 }
133
134
135 static void
136 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
139           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\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            (p1.connect_status == 1) )
318         GNUNET_break (NULL !=
319                       GNUNET_CORE_notify_transmit_ready (p1.ch,
320                                                          GNUNET_NO,
321                                                          0,
322                                                          FAST_TIMEOUT,
323                                                          &p2.id,
324                                                          MESSAGESIZE,
325                                                          &transmit_ready, &p1));
326       return 0;
327     }
328   GNUNET_assert (tr_n < TOTAL_MSGS);
329   ret = 0;
330   GNUNET_assert (size >= MESSAGESIZE);
331   GNUNET_assert (buf != NULL);
332   cbuf = buf;
333   do
334     {
335 #if DEBUG_TRANSMISSION
336       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337                   "Sending message %u of size %u at offset %u\n",
338                   tr_n,
339                   MESSAGESIZE,
340                   ret);
341 #endif
342       hdr.header.size = htons (MESSAGESIZE);
343       hdr.header.type = htons (MTYPE);
344       hdr.num = htonl (tr_n);
345       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
346       ret += sizeof (struct TestMessage);
347       memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
348       ret += MESSAGESIZE - sizeof (struct TestMessage);
349       tr_n++;
350       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
351         break; /* sometimes pack buffer full, sometimes not */
352     }
353   while (size - ret >= MESSAGESIZE);
354   GNUNET_SCHEDULER_cancel (err_task);
355   err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
356                                   &terminate_task_error,
357                                   NULL);
358
359   total_bytes_sent += ret;
360   return ret;
361 }
362
363
364
365 static void
366 connect_notify (void *cls,
367                 const struct GNUNET_PeerIdentity *peer,
368                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
369 {
370   struct PeerContext *pc = cls;
371
372   if (0 == memcmp (&pc->id,
373                    peer,
374                    sizeof (struct GNUNET_PeerIdentity)))
375     return;
376   GNUNET_assert (pc->connect_status == 0);
377   pc->connect_status = 1;
378   if (pc == &p1)
379     {
380 #if DEBUG_TRANSMISSION
381       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382                   "Encrypted connection established to peer `%4s'\n",
383                   GNUNET_i2s (peer));
384       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385                   "Asking core (1) for transmission to peer `%4s'\n",
386                   GNUNET_i2s (&p2.id));
387 #endif
388       if (err_task != GNUNET_SCHEDULER_NO_TASK) GNUNET_SCHEDULER_cancel (err_task);
389       err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
390                                       &terminate_task_error,
391                                       NULL);
392       start_time = GNUNET_TIME_absolute_get ();
393       running = GNUNET_YES;
394       measure_task = GNUNET_SCHEDULER_add_delayed(MEASUREMENT_LENGTH, &measurement_stop, NULL);
395
396       GNUNET_break (NULL != GNUNET_CORE_notify_transmit_ready (p1.ch,
397                                                                GNUNET_NO,
398                                                                0,
399                                                                TIMEOUT,
400                                                                &p2.id,
401                                                                MESSAGESIZE,
402                                                                &transmit_ready, &p1));
403     }
404 }
405
406
407 static void
408 disconnect_notify (void *cls,
409                    const struct GNUNET_PeerIdentity *peer)
410 {
411   struct PeerContext *pc = cls;
412
413   if (0 == memcmp (&pc->id,
414                    peer,
415                    sizeof (struct GNUNET_PeerIdentity)))
416     return;
417   pc->connect_status = 0;
418 #if DEBUG_TRANSMISSION
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
421 #endif
422 }
423
424
425 static int
426 inbound_notify (void *cls,
427                 const struct GNUNET_PeerIdentity *other,
428                 const struct GNUNET_MessageHeader *message,
429                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
430 {
431 #if DEBUG_TRANSMISSION
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Core provides inbound data from `%4s' %llu.\n", GNUNET_i2s (other), ntohs(message->size));
434 #endif
435   total_bytes_recv += ntohs(message->size);
436   return GNUNET_OK;
437 }
438
439
440 static int
441 outbound_notify (void *cls,
442                  const struct GNUNET_PeerIdentity *other,
443                  const struct GNUNET_MessageHeader *message,
444                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
445 {
446 #if DEBUG_TRANSMISSION
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448               "Core notifies about outbound data for `%4s'.\n",
449               GNUNET_i2s (other));
450 #endif
451   return GNUNET_OK;
452 }
453
454
455 static size_t
456 transmit_ready (void *cls, size_t size, void *buf);
457
458 static int
459 process_mtype (void *cls,
460                const struct GNUNET_PeerIdentity *peer,
461                const struct GNUNET_MessageHeader *message,
462                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
463 {
464   static int n;
465   const struct TestMessage *hdr;
466
467   hdr = (const struct TestMessage*) message;
468   if (MTYPE != ntohs (message->type))
469     return GNUNET_SYSERR;
470   if (ntohs (message->size) != MESSAGESIZE)
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 (ntohl (hdr->num) != n)
482     {
483       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484                   "Expected message %u of size %u, got %u bytes of message %u\n",
485                   n, MESSAGESIZE,
486                   ntohs (message->size),
487                   ntohl (hdr->num));
488       GNUNET_SCHEDULER_cancel (err_task);
489       err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
490       return GNUNET_SYSERR;
491     }
492 #if DEBUG_TRANSMISSION
493   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494               "Got message %u of size %u\n",
495               ntohl (hdr->num),
496               ntohs (message->size));         
497 #endif
498   n++;
499   if (0 == (n % 10))
500     fprintf (stderr, ".");
501
502
503   if (running == GNUNET_YES)
504         GNUNET_break (NULL !=
505                       GNUNET_CORE_notify_transmit_ready (p1.ch,
506                                                          GNUNET_NO,
507                                                          0,
508                                                          FAST_TIMEOUT,
509                                                          &p2.id,
510                                                          MESSAGESIZE,
511                                                          &transmit_ready, &p1));
512   return GNUNET_OK;
513 }
514
515
516 static struct GNUNET_CORE_MessageHandler handlers[] = {
517   {&process_mtype, MTYPE, 0},
518   {NULL, 0, 0}
519 };
520
521
522
523 static void
524 init_notify (void *cls,
525              struct GNUNET_CORE_Handle *server,
526              const struct GNUNET_PeerIdentity *my_identity,
527              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
528 {
529   struct PeerContext *p = cls;
530
531   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532               "Connection to CORE service of `%4s' established\n",
533               GNUNET_i2s (my_identity));
534   GNUNET_assert (server != NULL);
535   p->id = *my_identity;
536   p->ch = server;
537   if (cls == &p1)
538     {
539       GNUNET_assert (ok == 2);
540       OKPP;
541       /* connect p2 */
542       GNUNET_CORE_connect (p2.cfg, 1,
543                            &p2,
544                            &init_notify,                         
545                            &connect_notify,
546                            &disconnect_notify,
547                            NULL,
548                            &inbound_notify,
549                            GNUNET_YES,
550                            &outbound_notify, GNUNET_YES, handlers);
551     }
552   else
553     {
554       GNUNET_assert (ok == 3);
555       OKPP;
556       GNUNET_assert (cls == &p2);
557 #if DEBUG_TRANSMISSION
558       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559                   "Asking core (1) to connect to peer `%4s'\n",
560                   GNUNET_i2s (&p2.id));
561 #endif
562       GNUNET_CORE_peer_request_connect (p1.ch,
563                                         &p2.id,
564                                         NULL, NULL);
565     }
566 }
567
568
569 static void
570 process_hello (void *cls,
571                const struct GNUNET_MessageHeader *message)
572 {
573   struct PeerContext *p = cls;
574
575   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
576
577 #if DEBUG_TRANSMISSION
578   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579               "Received (my) `%s' from transport service\n",
580               "HELLO");
581 #endif
582   GNUNET_assert (message != NULL);
583   p->hello = GNUNET_malloc (ntohs (message->size));
584   memcpy (p->hello, message, ntohs (message->size));
585   if ((p == &p1) && (p2.th != NULL))
586     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
587   if ((p == &p2) && (p1.th != NULL))
588     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
589
590   if ((p == &p1) && (p2.hello != NULL))
591     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
592   if ((p == &p2) && (p1.hello != NULL))
593     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
594 }
595
596
597
598 static void
599 setup_peer (struct PeerContext *p, const char *cfgname)
600 {
601   p->cfg = GNUNET_CONFIGURATION_create ();
602 #if START_ARM
603   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
604                                         "gnunet-service-arm",
605 #if VERBOSE
606                                         "-L", "DEBUG",
607 #endif
608                                         "-c", cfgname, NULL);
609 #endif
610   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
611   p->stats = GNUNET_STATISTICS_create ("core",p->cfg);
612   GNUNET_assert (p->stats!=NULL);
613   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
614   GNUNET_assert (p->th != NULL);
615   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
616 }
617
618
619 static void
620 run (void *cls,
621      char *const *args,
622      const char *cfgfile,
623      const struct GNUNET_CONFIGURATION_Handle *cfg)
624 {
625   GNUNET_assert (ok == 1);
626   OKPP;
627   err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
628                               &terminate_task_error,
629                               NULL);
630   if (test == SYMMETRIC)
631     {
632       setup_peer (&p1, "test_core_quota_peer1.conf");
633       setup_peer (&p2, "test_core_quota_peer2.conf");
634     }
635   else if (test == ASYMMETRIC_SEND_LIMITED)
636     {
637       setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
638       setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
639     }
640   else if (test == ASYMMETRIC_RECV_LIMITED)
641     {
642       setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
643       setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
644     }
645
646   GNUNET_assert (test != -1);
647   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
648                                          "CORE",
649                                          "TOTAL_QUOTA_IN",
650                                          &current_quota_p1_in));
651   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
652                                          "CORE",
653                                          "TOTAL_QUOTA_IN",
654                                          &current_quota_p2_in));
655   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
656                                          "CORE",
657                                          "TOTAL_QUOTA_OUT",
658                                          &current_quota_p1_out));
659   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
660                                          "CORE",
661                                          "TOTAL_QUOTA_OUT",
662                                          &current_quota_p2_out));
663
664   GNUNET_CORE_connect (p1.cfg, 1,
665                        &p1,
666                        &init_notify,
667                        &connect_notify,
668                        &disconnect_notify,
669                        NULL,
670                        &inbound_notify,
671                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
672 }
673
674
675 static void
676 stop_arm (struct PeerContext *p)
677 {
678 #if START_ARM
679   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
680     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
681   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
682     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
683   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
685   GNUNET_OS_process_close (p->arm_proc);
686   p->arm_proc = NULL;
687 #endif
688   GNUNET_CONFIGURATION_destroy (p->cfg);
689 }
690
691 static int
692 check ()
693 {
694
695
696   char *const argv[] = { "test-core-quota-compliance",
697     "-c",
698     "test_core_api_data.conf",
699 #if VERBOSE
700     "-L", "DEBUG",
701 #endif
702     NULL
703   };
704   struct GNUNET_GETOPT_CommandLineOption options[] = {
705     GNUNET_GETOPT_OPTION_END
706   };
707   ok = 1;
708   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
709                       argv, "test-core-quota-compliance", "nohelp", options, &run, &ok);
710   stop_arm (&p1);
711   stop_arm (&p2);
712   return ok;
713 }
714
715 int
716 main (int argc, char *argv[])
717 {
718   int ret;
719
720   test = -1;
721   if (strstr(argv[0], "_symmetric") != NULL)
722     {
723       test = SYMMETRIC;
724     }
725   else if (strstr(argv[0], "_asymmetric_send") != NULL)
726     {
727           test = ASYMMETRIC_SEND_LIMITED;
728     }
729   else if (strstr(argv[0], "_asymmetric_recv") != NULL)
730     {
731           test = ASYMMETRIC_RECV_LIMITED;
732     }
733   GNUNET_assert (test != -1);
734   if (test == SYMMETRIC)
735     {
736           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
737           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
738     }
739   else if (test == ASYMMETRIC_SEND_LIMITED)
740     {
741           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
742           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
743     }
744   else if (test == ASYMMETRIC_RECV_LIMITED)
745     {
746           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
747           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
748     }
749
750   GNUNET_log_setup ("test-core-quota-compliance",
751 #if VERBOSE
752                     "DEBUG",
753 #else
754                     "WARNING",
755 #endif
756                     NULL);
757   ret = check ();
758   if (test == SYMMETRIC)
759     {
760           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
761           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
762     }
763   else if (test == ASYMMETRIC_SEND_LIMITED)
764     {
765           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
766           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
767     }
768   else if (test == ASYMMETRIC_RECV_LIMITED)
769     {
770           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
771           GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
772     }
773
774
775
776   return ret;
777 }
778 /* end of test_core_api_reliability.c */