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