dd8f16a2ce558c9923fd2fc4e2f1d3da5b9b7640
[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, 30)
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 static GNUNET_SCHEDULER_TaskIdentifier connect_task;
76
77
78 struct PeerContext
79 {
80   struct GNUNET_CONFIGURATION_Handle *cfg;
81   struct GNUNET_CORE_Handle *ch;
82   struct GNUNET_CORE_TransmitHandle *nth;
83   struct GNUNET_PeerIdentity id;
84   struct GNUNET_TRANSPORT_Handle *th;
85   struct GNUNET_MessageHeader *hello;
86   struct GNUNET_STATISTICS_Handle *stats;
87   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
88   int connect_status;
89 #if START_ARM
90   struct GNUNET_OS_Process *arm_proc;
91 #endif
92 };
93
94 static struct PeerContext p1;
95 static struct PeerContext p2;
96
97 static unsigned long long current_quota_p1_in;
98 static unsigned long long current_quota_p1_out;
99 static unsigned long long current_quota_p2_in;
100 static unsigned long long current_quota_p2_out;
101
102 static int ok;
103 static int test;
104 static int32_t tr_n;
105
106 static int running;
107
108
109 #if VERBOSE
110 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
111 #else
112 #define OKPP do { ok++; } while (0)
113 #endif
114
115 struct TestMessage
116 {
117   struct GNUNET_MessageHeader header;
118   uint32_t num;
119 };
120
121 static void
122 process_hello (void *cls, const struct GNUNET_MessageHeader *message);
123
124 static void
125 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   struct GNUNET_CORE_Handle *ch;
128
129   err_task = GNUNET_SCHEDULER_NO_TASK;
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, "\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 / 5;
242   else
243     quota_delta = max_quota_out / 5;
244
245   if ((throughput_out > (max_quota_out + quota_delta)) ||
246       (throughput_in > (max_quota_in + quota_delta)))
247     ok = 1;
248   else
249     ok = 0;
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                 (ok != 0) ? "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                 (ok != 0) ? "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                 (ok != 0) ? "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 #if DEBUG_TRANSMISSION
337     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338                 "Sending message %u of size %u at offset %u\n", tr_n,
339                 MESSAGESIZE, ret);
340 #endif
341     hdr.header.size = htons (MESSAGESIZE);
342     hdr.header.type = htons (MTYPE);
343     hdr.num = htonl (tr_n);
344     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
345     ret += sizeof (struct TestMessage);
346     memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
347     ret += MESSAGESIZE - sizeof (struct TestMessage);
348     tr_n++;
349     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
350       break;                    /* sometimes pack buffer full, sometimes not */
351   }
352   while (size - ret >= MESSAGESIZE);
353   GNUNET_SCHEDULER_cancel (err_task);
354   err_task =
355       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
356
357   total_bytes_sent += ret;
358   return ret;
359 }
360
361
362
363 static void
364 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
365                 const struct GNUNET_ATS_Information *atsi,
366                 unsigned int atsi_count)
367 {
368   struct PeerContext *pc = cls;
369
370   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
371     return;                     /* loopback */
372   GNUNET_assert (pc->connect_status == 0);
373   pc->connect_status = 1;
374   if (pc == &p1)
375   {
376 #if DEBUG_TRANSMISSION
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 #endif
384     if (err_task != GNUNET_SCHEDULER_NO_TASK)
385       GNUNET_SCHEDULER_cancel (err_task);
386     err_task =
387         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
388     start_time = GNUNET_TIME_absolute_get ();
389     running = GNUNET_YES;
390     measure_task =
391         GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH, &measurement_stop,
392                                       NULL);
393
394     GNUNET_break (NULL !=
395                   (p1.nth =
396                    GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
397                                                       TIMEOUT, &p2.id,
398                                                       MESSAGESIZE,
399                                                       &transmit_ready, &p1)));
400   }
401 }
402
403
404 static void
405 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
406 {
407   struct PeerContext *pc = cls;
408
409   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
410     return;                     /* loopback */
411   pc->connect_status = 0;
412   if (GNUNET_SCHEDULER_NO_TASK != measure_task)
413   {
414     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
415                 "Measurement aborted due to disconnect!\n");
416     GNUNET_SCHEDULER_cancel (measure_task);
417     measure_task = GNUNET_SCHEDULER_NO_TASK;
418   }
419   if (pc->nth != NULL)
420   {
421     GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
422     pc->nth = NULL;
423   }
424 #if DEBUG_TRANSMISSION
425   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
426               GNUNET_i2s (peer));
427 #endif
428 }
429
430
431 static int
432 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
433                 const struct GNUNET_MessageHeader *message,
434                 const struct GNUNET_ATS_Information *atsi,
435                 unsigned int atsi_count)
436 {
437 #if DEBUG_TRANSMISSION
438   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439               "Core provides inbound data from `%4s' %llu.\n",
440               GNUNET_i2s (other), ntohs (message->size));
441 #endif
442   total_bytes_recv += ntohs (message->size);
443   return GNUNET_OK;
444 }
445
446
447 static int
448 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
449                  const struct GNUNET_MessageHeader *message,
450                  const struct GNUNET_ATS_Information *atsi,
451                  unsigned int atsi_count)
452 {
453 #if DEBUG_TRANSMISSION
454   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455               "Core notifies about outbound data for `%4s'.\n",
456               GNUNET_i2s (other));
457 #endif
458   return GNUNET_OK;
459 }
460
461
462 static size_t
463 transmit_ready (void *cls, size_t size, void *buf);
464
465 static int
466 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
467                const struct GNUNET_MessageHeader *message,
468                const struct GNUNET_ATS_Information *atsi,
469                unsigned int atsi_count)
470 {
471   static int n;
472   const struct TestMessage *hdr;
473
474   hdr = (const struct TestMessage *) message;
475   if (MTYPE != ntohs (message->type))
476     return GNUNET_SYSERR;
477   if (ntohs (message->size) != MESSAGESIZE)
478   {
479     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480                 "Expected message %u of size %u, got %u bytes of message %u\n",
481                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
482     GNUNET_SCHEDULER_cancel (err_task);
483     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
484     return GNUNET_SYSERR;
485   }
486   if (ntohl (hdr->num) != n)
487   {
488     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
489                 "Expected message %u of size %u, got %u bytes of message %u\n",
490                 n, MESSAGESIZE, ntohs (message->size), ntohl (hdr->num));
491     GNUNET_SCHEDULER_cancel (err_task);
492     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
493     return GNUNET_SYSERR;
494   }
495 #if DEBUG_TRANSMISSION
496   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
497               ntohl (hdr->num), ntohs (message->size));
498 #endif
499   n++;
500   if (0 == (n % 10))
501     fprintf (stderr, ".");
502
503
504   if (running == GNUNET_YES)
505     GNUNET_break (NULL !=
506                   GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_NO, 0,
507                                                      FAST_TIMEOUT, &p2.id,
508                                                      MESSAGESIZE,
509                                                      &transmit_ready, &p1));
510   return GNUNET_OK;
511 }
512
513
514 static struct GNUNET_CORE_MessageHandler handlers[] = {
515   {&process_mtype, MTYPE, 0},
516   {NULL, 0, 0}
517 };
518
519
520
521 static void
522 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
523              const struct GNUNET_PeerIdentity *my_identity)
524 {
525   struct PeerContext *p = cls;
526
527   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528               "Connection to CORE service of `%4s' established\n",
529               GNUNET_i2s (my_identity));
530   GNUNET_assert (server != NULL);
531   p->id = *my_identity;
532   GNUNET_assert (p->ch == server);
533   if (cls == &p1)
534   {
535     GNUNET_assert (ok == 2);
536     OKPP;
537     /* connect p2 */
538     p2.ch =
539         GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
540                              &disconnect_notify, &inbound_notify, GNUNET_YES,
541                              &outbound_notify, GNUNET_YES, handlers);
542   }
543   else
544   {
545     GNUNET_assert (ok == 3);
546     OKPP;
547     GNUNET_assert (cls == &p2);
548 #if DEBUG_TRANSMISSION
549     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550                 "Asking core (1) to connect to peer `%4s'\n",
551                 GNUNET_i2s (&p2.id));
552 #endif
553     connect_task = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
554   }
555 }
556
557
558 static void
559 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
560 {
561   struct PeerContext *p = cls;
562
563
564 #if DEBUG_TRANSMISSION
565   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566               "Received (my) `%s' from transport service\n", "HELLO");
567 #endif
568   GNUNET_assert (message != NULL);
569   p->hello = GNUNET_malloc (ntohs (message->size));
570   memcpy (p->hello, message, ntohs (message->size));
571   if ((p == &p1) && (p2.th != NULL))
572     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
573   if ((p == &p2) && (p1.th != NULL))
574     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
575
576   if ((p == &p1) && (p2.hello != NULL))
577     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
578   if ((p == &p2) && (p1.hello != NULL))
579     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
580 }
581
582
583
584 static void
585 setup_peer (struct PeerContext *p, const char *cfgname)
586 {
587   p->cfg = GNUNET_CONFIGURATION_create ();
588 #if START_ARM
589   p->arm_proc =
590       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
591                                "gnunet-service-arm",
592 #if VERBOSE
593                                "-L", "DEBUG",
594 #endif
595                                "-c", cfgname, NULL);
596 #endif
597   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
598   p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
599   GNUNET_assert (p->stats != NULL);
600   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
601   GNUNET_assert (p->th != NULL);
602   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
603 }
604
605
606 static void
607 run (void *cls, char *const *args, const char *cfgfile,
608      const struct GNUNET_CONFIGURATION_Handle *cfg)
609 {
610   GNUNET_assert (ok == 1);
611   OKPP;
612   err_task =
613       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &terminate_task_error, NULL);
614   if (test == SYMMETRIC)
615   {
616     setup_peer (&p1, "test_core_quota_peer1.conf");
617     setup_peer (&p2, "test_core_quota_peer2.conf");
618   }
619   else if (test == ASYMMETRIC_SEND_LIMITED)
620   {
621     setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
622     setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
623   }
624   else if (test == ASYMMETRIC_RECV_LIMITED)
625   {
626     setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
627     setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
628   }
629
630   GNUNET_assert (test != -1);
631   GNUNET_assert (GNUNET_SYSERR !=
632                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "CORE",
633                                                       "TOTAL_QUOTA_IN",
634                                                       &current_quota_p1_in));
635   GNUNET_assert (GNUNET_SYSERR !=
636                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "CORE",
637                                                       "TOTAL_QUOTA_IN",
638                                                       &current_quota_p2_in));
639   GNUNET_assert (GNUNET_SYSERR !=
640                  GNUNET_CONFIGURATION_get_value_size (p1.cfg, "CORE",
641                                                       "TOTAL_QUOTA_OUT",
642                                                       &current_quota_p1_out));
643   GNUNET_assert (GNUNET_SYSERR !=
644                  GNUNET_CONFIGURATION_get_value_size (p2.cfg, "CORE",
645                                                       "TOTAL_QUOTA_OUT",
646                                                       &current_quota_p2_out));
647
648   p1.ch =
649       GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
650                            &disconnect_notify, &inbound_notify, GNUNET_YES,
651                            &outbound_notify, GNUNET_YES, handlers);
652 }
653
654
655 static void
656 stop_arm (struct PeerContext *p)
657 {
658 #if START_ARM
659   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
660     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
661   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
662     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
663   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
664               GNUNET_OS_process_get_pid (p->arm_proc));
665   GNUNET_OS_process_close (p->arm_proc);
666   p->arm_proc = NULL;
667 #endif
668   GNUNET_CONFIGURATION_destroy (p->cfg);
669 }
670
671 static int
672 check ()
673 {
674
675
676   char *const argv[] = { "test-core-quota-compliance",
677     "-c",
678     "test_core_api_data.conf",
679 #if VERBOSE
680     "-L", "DEBUG",
681 #endif
682     NULL
683   };
684   struct GNUNET_GETOPT_CommandLineOption options[] = {
685     GNUNET_GETOPT_OPTION_END
686   };
687   ok = 1;
688   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
689                       "test-core-quota-compliance", "nohelp", options, &run,
690                       &ok);
691   stop_arm (&p1);
692   stop_arm (&p2);
693   return ok;
694 }
695
696 int
697 main (int argc, char *argv[])
698 {
699   int ret;
700
701   test = -1;
702   if (strstr (argv[0], "_symmetric") != NULL)
703   {
704     test = SYMMETRIC;
705   }
706   else if (strstr (argv[0], "_asymmetric_send") != NULL)
707   {
708     test = ASYMMETRIC_SEND_LIMITED;
709   }
710   else if (strstr (argv[0], "_asymmetric_recv") != NULL)
711   {
712     test = ASYMMETRIC_RECV_LIMITED;
713   }
714   GNUNET_assert (test != -1);
715   if (test == SYMMETRIC)
716   {
717     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
718     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
719   }
720   else if (test == ASYMMETRIC_SEND_LIMITED)
721   {
722     GNUNET_DISK_directory_remove
723         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
724     GNUNET_DISK_directory_remove
725         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
726   }
727   else if (test == ASYMMETRIC_RECV_LIMITED)
728   {
729     GNUNET_DISK_directory_remove
730         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
731     GNUNET_DISK_directory_remove
732         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
733   }
734
735   GNUNET_log_setup ("test-core-quota-compliance",
736 #if VERBOSE
737                     "DEBUG",
738 #else
739                     "WARNING",
740 #endif
741                     NULL);
742   ret = check ();
743   if (test == SYMMETRIC)
744   {
745     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
746     GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
747   }
748   else if (test == ASYMMETRIC_SEND_LIMITED)
749   {
750     GNUNET_DISK_directory_remove
751         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
752     GNUNET_DISK_directory_remove
753         ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
754   }
755   else if (test == ASYMMETRIC_RECV_LIMITED)
756   {
757     GNUNET_DISK_directory_remove
758         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
759     GNUNET_DISK_directory_remove
760         ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
761   }
762
763
764
765   return ret;
766 }
767
768 /* end of test_core_api_reliability.c */