7d28820cbdd6d429780a7c5ab28080b9a6bdd0e9
[oweals/gnunet.git] / src / transport / test_quota_compliance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 transport/test_quota_compliance.c
22  * @brief base test case for transport implementations
23  *
24  * This test case tests quota compliance both on transport level
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.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_server_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gauger.h"
36 #include "transport.h"
37 #include "transport-testing.h"
38
39 #define VERBOSE GNUNET_NO
40
41 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
42
43 #define START_ARM GNUNET_YES
44
45 /**
46  * Testcase timeout
47  */
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49
50 /**
51  * How long until we give up on transmitting the message?
52  */
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
54
55 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
56
57 static char *test_source;
58
59 static char *test_plugin;
60
61 static char *test_name;
62
63 static int ok;
64
65 static GNUNET_SCHEDULER_TaskIdentifier die_task;
66
67 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
68
69 struct PeerContext *p1;
70
71 struct PeerContext *p2;
72
73 struct PeerContext *sender;
74
75 struct PeerContext *receiver;
76
77 struct GNUNET_TRANSPORT_TransmitHandle *th;
78
79 char *cfg_file_p1;
80 char *gen_cfg_p2;
81 unsigned long long quota_in_p1;
82 unsigned long long quota_out_p1;
83
84 char *cfg_file_p2;
85 char *gen_cfg_p1;
86 unsigned long long quota_in_p2;
87 unsigned long long quota_out_p2;
88
89 struct GNUNET_TRANSPORT_TESTING_handle *tth;
90
91 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
92
93
94 /*
95  * Testcase specific declarations
96  */
97
98 /**
99  * Note that this value must not significantly exceed
100  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
101  * messages may be dropped even for a reliable transport.
102  */
103 #define TOTAL_MSGS (1024 * 2)
104
105 #define MTYPE 12345
106
107 struct TestMessage
108 {
109   struct GNUNET_MessageHeader header;
110   uint32_t num;
111 };
112
113 static int msg_scheduled;
114 static int msg_sent;
115 static int msg_recv_expected;
116 static int msg_recv;
117
118 static int test_failed;
119 static int test_connected;
120
121 static unsigned long long total_bytes_sent;
122
123 static struct GNUNET_TIME_Absolute start_time;
124
125 /*
126  * END Testcase specific declarations
127  */
128
129 #if VERBOSE
130 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
131 #else
132 #define OKPP do { ok++; } while (0)
133 #endif
134
135
136 static void
137 end ()
138 {
139   unsigned long long delta;
140   unsigned long long datarate;
141
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
143
144   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
145   datarate = (total_bytes_sent * 1000) / delta;
146
147   fprintf (stderr, "Throughput was %llu b/s\n", datarate);
148
149   test_failed = GNUNET_NO;
150   if (datarate > quota_in_p2)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153                 "Datarate of %llu b/s higher than allowed inbound quota of %llu b/s\n",
154                 datarate, quota_in_p2);
155     test_failed = GNUNET_YES;
156   }
157   if (datarate > quota_out_p1)
158   {
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                 "Datarate of %llu b/s higher than allowed outbound quota of %llu b/s\n",
161                 datarate, quota_out_p1);
162     test_failed = GNUNET_YES;
163   }
164   if (test_failed == GNUNET_NO)
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167                 "Datarate of %llu b/s complied to allowed outbound quota of %llu b/s and inbound quota of %llu b/s\n",
168                 datarate, quota_out_p1, quota_in_p2);
169   }
170
171   if (die_task != GNUNET_SCHEDULER_NO_TASK)
172     GNUNET_SCHEDULER_cancel (die_task);
173
174   if (th != NULL)
175     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
176   th = NULL;
177
178   if (cc != NULL)
179     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
180
181   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
182   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
183
184 }
185
186 static void
187 end_badly ()
188 {
189   die_task = GNUNET_SCHEDULER_NO_TASK;
190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
191
192   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
193     GNUNET_SCHEDULER_cancel (measure_task);
194
195   if (test_connected == GNUNET_YES)
196     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
197   else
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
199
200   if (th != NULL)
201     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
202   th = NULL;
203
204   if (cc != NULL)
205     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
206
207   if (p1 != NULL)
208     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
209   if (p2 != NULL)
210     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
211
212   ok = GNUNET_SYSERR;
213 }
214
215
216 static unsigned int
217 get_size (unsigned int iter)
218 {
219   unsigned int ret;
220
221   ret = (iter * iter * iter);
222   return sizeof (struct TestMessage) + (ret % 60000);
223 }
224
225
226 static void
227 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
228                 const struct GNUNET_MessageHeader *message,
229                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
230 {
231   static int n;
232   unsigned int s;
233   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
234   const struct TestMessage *hdr;
235
236   hdr = (const struct TestMessage *) message;
237   s = get_size (n);
238   if (MTYPE != ntohs (message->type))
239     return;
240   msg_recv_expected = n;
241   msg_recv = ntohl (hdr->num);
242   if (ntohs (message->size) != (s))
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245                 "Expected message %u of size %u, got %u bytes of message %u\n",
246                 n, s, ntohs (message->size), ntohl (hdr->num));
247     if (die_task != GNUNET_SCHEDULER_NO_TASK)
248       GNUNET_SCHEDULER_cancel (die_task);
249     test_failed = GNUNET_YES;
250     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
251     return;
252   }
253   if (ntohl (hdr->num) != n)
254   {
255     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256                 "Expected message %u of size %u, got %u bytes of message %u\n",
257                 n, s, ntohs (message->size), ntohl (hdr->num));
258     if (die_task != GNUNET_SCHEDULER_NO_TASK)
259       GNUNET_SCHEDULER_cancel (die_task);
260     test_failed = GNUNET_YES;
261     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
262     return;
263   }
264   memset (cbuf, n, s - sizeof (struct TestMessage));
265   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
266   {
267     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268                 "Expected message %u with bits %u, but body did not match\n", n,
269                 (unsigned char) n);
270     if (die_task != GNUNET_SCHEDULER_NO_TASK)
271       GNUNET_SCHEDULER_cancel (die_task);
272     test_failed = GNUNET_YES;
273     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
274     return;
275   }
276 #if VERBOSE
277   if (ntohl (hdr->num) % 5000 == 0)
278   {
279     struct PeerContext *p = cls;
280     char *ps = strdup (GNUNET_i2s (&p->id));
281
282     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283                 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
284                 p->no, ps, ntohl (hdr->num), ntohs (message->size),
285                 GNUNET_i2s (peer));
286     GNUNET_free (ps);
287   }
288 #endif
289   n++;
290 }
291
292
293 static size_t
294 notify_ready (void *cls, size_t size, void *buf)
295 {
296   static int n;
297   char *cbuf = buf;
298   struct TestMessage hdr;
299   unsigned int s;
300   unsigned int ret;
301
302   th = NULL;
303   if (buf == NULL)
304   {
305     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306                 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
307                 msg_scheduled, TOTAL_MSGS);
308     if (GNUNET_SCHEDULER_NO_TASK != die_task)
309       GNUNET_SCHEDULER_cancel (die_task);
310     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
311     ok = 42;
312     return 0;
313   }
314
315   ret = 0;
316   s = get_size (n);
317   GNUNET_assert (size >= s);
318   GNUNET_assert (buf != NULL);
319   cbuf = buf;
320   do
321   {
322     hdr.header.size = htons (s);
323     hdr.header.type = htons (MTYPE);
324     hdr.num = htonl (n);
325     msg_sent = n;
326     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
327     ret += sizeof (struct TestMessage);
328     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
329     ret += s - sizeof (struct TestMessage);
330 #if VERBOSE
331     if (n % 5000 == 0)
332     {
333
334       char *receiver_s = strdup (GNUNET_i2s (&receiver->id));
335
336       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337                   "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
338                   n, sender->no, GNUNET_i2s (&sender->id), receiver->no,
339                   receiver_s);
340       GNUNET_free (receiver_s);
341     }
342 #endif
343     n++;
344     s = get_size (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 >= s);
349   if (n < TOTAL_MSGS)
350   {
351     if (th == NULL)
352       th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
353                                                    TIMEOUT_TRANSMIT,
354                                                    &notify_ready, NULL);
355     msg_scheduled = n;
356   }
357   if (n % 5000 == 0)
358   {
359     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360                 "Returning total message block of size %u\n", ret);
361   }
362   total_bytes_sent += ret;
363   if (n == TOTAL_MSGS)
364   {
365     fprintf (stderr, "\n");
366     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
367   }
368   return ret;
369 }
370
371
372 static void
373 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
374                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
375 {
376
377   struct PeerContext *p = cls;
378
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
380               p->no, GNUNET_i2s (peer));
381 }
382
383
384 static void
385 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
386 {
387   struct PeerContext *p = cls;
388
389   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
390               GNUNET_i2s (peer));
391   if (th != NULL)
392     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
393   th = NULL;
394
395 }
396
397 static void
398 sendtask ()
399 {
400   start_time = GNUNET_TIME_absolute_get ();
401   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
402                                                TIMEOUT_TRANSMIT, &notify_ready,
403                                                NULL);
404 }
405
406
407 static void
408 measure (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
409 {
410   static int counter;
411
412   measure_task = GNUNET_SCHEDULER_NO_TASK;
413
414   counter++;
415   if ((DURATION.rel_value / 1000) < counter)
416   {
417     fprintf (stderr, ".\n");
418     GNUNET_SCHEDULER_add_now (&end, NULL);
419   }
420   else
421   {
422     fprintf (stderr, ".");
423     measure_task =
424         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
425   }
426 }
427
428
429 static void
430 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
431 {
432   char *p1_c = strdup (GNUNET_i2s (&p1->id));
433
434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
435               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
436   GNUNET_free (p1_c);
437
438   cc = NULL;
439   test_connected = GNUNET_YES;
440
441   measure_task =
442       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
443   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
444
445 }
446
447 void
448 start_cb (struct PeerContext *p, void *cls)
449 {
450   static int started;
451
452   started++;
453
454   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
455               GNUNET_i2s (&p->id));
456
457   if (started != 2)
458     return;
459
460   test_connected = GNUNET_NO;
461
462   sender = p2;
463   receiver = p1;
464
465   char *sender_c = strdup (GNUNET_i2s (&sender->id));
466
467   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468               "Test tries to send from %u (%s) -> peer %u (%s)\n", sender->no,
469               sender_c, receiver->no, GNUNET_i2s (&receiver->id));
470
471   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
472                                                NULL);
473
474 }
475
476 static char *
477 generate_config (char *cfg_file, unsigned long long quota_in,
478                  unsigned long long quota_out)
479 {
480   char *fname = NULL;
481   struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
482
483   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_file));
484   GNUNET_asprintf (&fname, "q_in_%llu_q_out_%llu_%s", quota_in, quota_out,
485                    cfg_file);
486   GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", fname);
487   GNUNET_CONFIGURATION_set_value_number (cfg, "core", "TOTAL_QUOTA_OUT",
488                                          quota_out);
489   GNUNET_CONFIGURATION_set_value_number (cfg, "core", "TOTAL_QUOTA_IN",
490                                          quota_in);
491   GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "TOTAL_QUOTA_IN",
492                                          quota_in);
493   GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "TOTAL_QUOTA_OUT",
494                                          quota_out);
495   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write (cfg, fname));
496   GNUNET_CONFIGURATION_destroy (cfg);
497   return fname;
498 }
499
500 static void
501 run_measurement (unsigned long long p1_quota_in,
502                  unsigned long long p1_quota_out,
503                  unsigned long long p2_quota_in,
504                  unsigned long long p2_quota_out)
505 {
506   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
507
508   /* setting ATS quota */
509   quota_out_p1 = p1_quota_out;
510   gen_cfg_p1 = generate_config (cfg_file_p1, p1_quota_in, p1_quota_out);
511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
512               gen_cfg_p1);
513
514   quota_in_p2 = p2_quota_in;
515   gen_cfg_p2 = generate_config (cfg_file_p2, p2_quota_in, p2_quota_out);
516   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
517               gen_cfg_p2);
518
519   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p1, 1, &notify_receive,
520                                             &notify_connect, &notify_disconnect,
521                                             &start_cb, NULL);
522
523   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p2, 2, &notify_receive,
524                                             &notify_connect, &notify_disconnect,
525                                             &start_cb, NULL);
526
527   if ((p1 == NULL) || (p2 == NULL))
528   {
529     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
530     if (die_task != GNUNET_SCHEDULER_NO_TASK)
531       GNUNET_SCHEDULER_cancel (die_task);
532     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
533     return;
534   }
535 }
536
537 static void
538 run (void *cls, char *const *args, const char *cfgfile,
539      const struct GNUNET_CONFIGURATION_Handle *cfg)
540 {
541   unsigned long long p1_quota_in = 10000;
542   unsigned long long p1_quota_out = 10000;
543   unsigned long long p2_quota_in = 10000;
544   unsigned long long p2_quota_out = 10000;
545
546   if (NULL != strstr (test_name, "asymmetric"))
547   {
548     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549                 "Running asymmetric test with sending peer unlimited, receiving peer (in/out): %llu/%llu b/s \n",
550                 p2_quota_in, p2_quota_out);
551     p1_quota_out = 1024 * 1024 * 1024;
552     p1_quota_in = 1024 * 1024 * 1024;
553   }
554   else
555   {
556     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557                 "Running symmetric test with (in/out) %llu/%llu b/s \n",
558                 p2_quota_in, p2_quota_out);
559   }
560   run_measurement (p1_quota_in, p1_quota_out, p2_quota_in, p2_quota_out);
561 }
562
563 static int
564 check ()
565 {
566   static char *argv[] = { "test_transport-quota-compliance",
567     "-c",
568     "",
569 #if VERBOSE
570     "-L", "DEBUG",
571 #endif
572     NULL
573   };
574   static struct GNUNET_GETOPT_CommandLineOption options[] = {
575     GNUNET_GETOPT_OPTION_END
576   };
577
578   ok = 1;
579   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
580                       "nohelp", options, &run, &ok);
581
582   return ok;
583 }
584
585 int
586 main (int argc, char *argv[])
587 {
588   int nat_res;
589
590   tth = GNUNET_TRANSPORT_TESTING_init ();
591
592   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
593   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
594                                                  &test_plugin);
595   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
596
597   GNUNET_log_setup (test_name,
598 #if VERBOSE
599                     "DEBUG",
600 #else
601                     "WARNING",
602 #endif
603                     NULL);
604
605   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
606       (strcmp (test_plugin, "udp_nat") == 0))
607   {
608     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
609     if (GNUNET_NO == nat_res)
610     {
611       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
612                   "gnunet-nat-server", "SUID not set");
613       return 0;
614     }
615     if (GNUNET_SYSERR == nat_res)
616     {
617       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
618                   "gnunet-nat-server", "file not found");
619       return 0;
620     }
621   }
622
623   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
624   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
625
626   check ();
627
628   GNUNET_free (cfg_file_p1);
629   GNUNET_free (cfg_file_p2);
630
631   if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p1))
632   {
633     GNUNET_DISK_directory_remove (gen_cfg_p1);
634     GNUNET_free (gen_cfg_p1);
635   }
636
637   if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p2))
638   {
639     GNUNET_DISK_directory_remove (gen_cfg_p2);
640     GNUNET_free (gen_cfg_p2);
641   }
642
643   GNUNET_free (test_source);
644   GNUNET_free (test_plugin);
645   GNUNET_free (test_name);
646
647   GNUNET_TRANSPORT_TESTING_done (tth);
648
649   return test_failed;
650 }
651
652
653 /* end of test_quota_compliance.c */