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