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