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