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