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