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