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