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