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