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