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