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