test now works
[oweals/gnunet.git] / src / transport / test_quota_compliance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 core and 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 "transport.h"
36
37 #define VERBOSE GNUNET_NO
38
39 #define VERBOSE_ARM GNUNET_NO
40
41 #define START_ARM GNUNET_YES
42 #define DEBUG_MEASUREMENT GNUNET_NO
43 #define DEBUG_CONNECTIONS GNUNET_NO
44
45 /**
46  * Note that this value must not significantly exceed
47  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48  * messages may be dropped even for a reliable transport.
49  */
50 #define TOTAL_MSGS (10000 * 2)
51
52 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
53 #define MEASUREMENT_MSG_SIZE 1024
54 #define MEASUREMENT_MSG_SIZE_BIG 32768
55 #define MEASUREMENT_MAX_QUOTA 1024*1024*1024
56 #define MEASUREMENT_MIN_QUOTA 1024
57
58 /**
59  * Testcase timeout
60  */
61 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
62
63
64 #define MTYPE 11111
65
66 struct PeerContext
67 {
68   struct GNUNET_CONFIGURATION_Handle *cfg;
69   struct GNUNET_TRANSPORT_Handle *th;
70   struct GNUNET_PeerIdentity id;
71 #if START_ARM
72   pid_t arm_pid;
73 #endif
74 };
75
76 static struct PeerContext p1;
77
78 static struct PeerContext p2;
79
80 static struct GNUNET_SCHEDULER_Handle *sched;
81
82 static int ok;
83
84 static int connected;
85
86 static unsigned long long total_bytes;
87 static unsigned long long current_quota_p1;
88 static unsigned long long current_quota_p2;
89
90 static struct GNUNET_TIME_Absolute start_time;
91
92 static GNUNET_SCHEDULER_TaskIdentifier die_task;
93 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
94 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
95
96 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
97
98 #if VERBOSE
99 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
100 #else
101 #define OKPP do { ok++; } while (0)
102 #endif
103
104
105 static void
106 end ()
107 {
108   GNUNET_SCHEDULER_cancel (sched, die_task);
109   die_task = GNUNET_SCHEDULER_NO_TASK;
110
111   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
112   {
113             GNUNET_SCHEDULER_cancel (sched, measurement_task);
114             measurement_task = GNUNET_SCHEDULER_NO_TASK;
115   }
116   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
117   {
118             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
119             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
120   }
121   GNUNET_SCHEDULER_shutdown (sched);
122 #if VERBOSE
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
124 #endif
125   GNUNET_TRANSPORT_disconnect (p1.th);
126   GNUNET_TRANSPORT_disconnect (p2.th);
127 #if VERBOSE
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Transports disconnected, returning success!\n");
130 #endif
131   GNUNET_SCHEDULER_shutdown (sched);
132 }
133
134
135
136 static void
137 stop_arm (struct PeerContext *p)
138 {
139 #if START_ARM
140   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
141     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
142   GNUNET_OS_process_wait (p->arm_pid);
143 #endif
144   GNUNET_CONFIGURATION_destroy (p->cfg);
145 }
146
147
148 static void
149 end_badly (void *cls,
150            const struct GNUNET_SCHEDULER_TaskContext *tc)
151 {
152   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
153   {
154             GNUNET_SCHEDULER_cancel (sched, measurement_task);
155             measurement_task = GNUNET_SCHEDULER_NO_TASK;
156   }
157   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
158   {
159             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
160             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
161   }
162   GNUNET_break (0);
163   if (p1.th != NULL)
164           GNUNET_TRANSPORT_disconnect (p1.th);
165   if (p2.th != NULL)
166           GNUNET_TRANSPORT_disconnect (p2.th);
167   ok = 1;
168 }
169
170 struct TestMessage
171 {
172   struct GNUNET_MessageHeader header;
173   uint32_t num;
174 };
175
176 static unsigned int
177 get_size_new (unsigned int iter)
178 {
179   return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
180 }
181
182 static void
183 notify_receive_new (void *cls,
184                 const struct GNUNET_PeerIdentity *peer,
185                 const struct GNUNET_MessageHeader *message,
186                 struct GNUNET_TIME_Relative latency,
187                 uint32_t distance)
188 {
189   static int n;
190   unsigned int s;
191   const struct TestMessage *hdr;
192
193   hdr = (const struct TestMessage*) message;
194   s = get_size_new (n);
195   if (MTYPE != ntohs (message->type))
196     return;
197 #if DEBUG_MEASUREMENT
198   if (ntohl(hdr->num) % 5000 == 0)
199     {
200       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201                   "Got message %u of size %u\n",
202                   ntohl (hdr->num),
203                   ntohs (message->size));
204     }
205 #endif
206   n++;
207 }
208
209 static size_t
210 notify_ready_new (void *cls, size_t size, void *buf)
211 {
212   static int n;
213   char *cbuf = buf;
214   struct TestMessage hdr;
215   unsigned int s;
216   unsigned int ret;
217
218   transmit_handle = NULL;
219
220   if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
221           return 0;
222
223   if (buf == NULL)
224     {
225       GNUNET_break (0);
226       ok = 42;
227       return 0;
228     }
229   ret = 0;
230   s = get_size_new (n);
231   GNUNET_assert (size >= s);
232   GNUNET_assert (buf != NULL);
233   cbuf = buf;
234   do
235     {
236       hdr.header.size = htons (s);
237       hdr.header.type = htons (MTYPE);
238       hdr.num = htonl (n);
239       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
240       ret += sizeof (struct TestMessage);
241       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
242       ret += s - sizeof (struct TestMessage);
243 #if DEBUG_MEASUREMENT
244       if (n % 5000 == 0)
245        {
246           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247                       "Sending message %u\n",n);
248        }
249 #endif
250       n++;
251       s = get_size_new (n);
252       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
253         break; /* sometimes pack buffer full, sometimes not */
254     }
255   while (size - ret >= s);
256   transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
257                                             &p1.id,
258                                             s, 0, TIMEOUT,
259                                             &notify_ready_new,
260                                             NULL);
261   total_bytes += s;
262   return ret;
263 }
264
265 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
266
267 static void measurement_counter
268  (void *cls,
269            const struct GNUNET_SCHEDULER_TaskContext *tc)
270 {
271   measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
272
273   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
274         return;
275
276 #if VERBOSE
277   fprintf(stderr,".");
278 #endif
279   measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
280                                                            GNUNET_TIME_UNIT_SECONDS,
281                                                            &measurement_counter,
282                                                            NULL);
283 }
284
285 static void
286 measurement_end (void *cls,
287            const struct GNUNET_SCHEDULER_TaskContext *tc)
288 {
289   measurement_task  = GNUNET_SCHEDULER_NO_TASK;
290   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
291         return;
292
293   struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
294
295   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
296   {
297     GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
298     measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
299   }
300
301   if (transmit_handle != NULL)
302   {
303           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
304           transmit_handle = NULL;
305   }
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307                   "\n Measurement finished: \n Quota allowed: %llu kb/s\n Throughput: %llu kb/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.value / 1000)/1024));
308
309   if (current_quota_p1 < total_bytes/(duration.value / 1000))
310   {
311           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312                           "\nQuota compliance failed: \n"\
313                           "Quota allowed: %llu kb/s\n"\
314                           "Throughput   : %llu kb/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.value / 1000)/1024));
315           ok = 1;
316           end();
317           return;
318   }
319   else
320   {
321           ok = 0;
322   }
323   if (current_quota_p1 < (MEASUREMENT_MIN_QUOTA))
324           end();
325   else
326         measure (current_quota_p1/100, current_quota_p2/100);
327 }
328
329 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
330 {
331           current_quota_p1 = quota_p1;
332           current_quota_p2 = quota_p2;
333 #if VERBOSE
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Starting measurement: Duration: %u Quota: %u\n", MEASUREMENT_INTERVALL, current_quota_p1);
336 #endif
337                 GNUNET_TRANSPORT_set_quota (p1.th,
338                           &p2.id,
339                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
340                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
341                           GNUNET_TIME_UNIT_FOREVER_REL,
342                           NULL, NULL);
343                 GNUNET_TRANSPORT_set_quota (p2.th,
344                           &p1.id,
345                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
346                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
347                           GNUNET_TIME_UNIT_FOREVER_REL,
348                           NULL, NULL);
349
350                 if (transmit_handle != NULL)
351                           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
352
353                 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
354                                                                                           &p1.id,
355                                                                                           get_size_new (0), 0, TIMEOUT,
356                                                                                           &notify_ready_new,
357                                                                                           NULL);
358
359                 GNUNET_SCHEDULER_cancel (sched, die_task);
360                 die_task = GNUNET_SCHEDULER_add_delayed (sched,
361                                                    TIMEOUT,
362                                                    &end_badly,
363                                                    NULL);
364                 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
365                   GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
366                 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
367                                                                    GNUNET_TIME_UNIT_SECONDS,
368                                                                    &measurement_counter,
369                                                                    NULL);
370                 measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
371                                                    MEASUREMENT_INTERVALL,
372                                                    &measurement_end,
373                                                    NULL);
374                 total_bytes = 0;
375                 start_time = GNUNET_TIME_absolute_get ();
376 }
377
378 static void
379 notify_connect (void *cls,
380                 const struct GNUNET_PeerIdentity *peer,
381                 struct GNUNET_TIME_Relative latency,
382                 uint32_t distance)
383 {
384   if (cls == &p1)
385     {
386 #if DEBUG_CONNECTIONS
387   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388               "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
389 #endif
390           connected++;
391     }
392   else
393     {
394 #if DEBUG_CONNECTIONS
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396               "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
397 #endif
398       connected++;
399     }
400   if (connected == 2)
401     {
402           measure(MEASUREMENT_MAX_QUOTA,MEASUREMENT_MAX_QUOTA);
403     }
404 }
405
406
407 static void
408 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
409 {
410 #if DEBUG_CONNECTIONS
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412               "Peer `%4s' disconnected (%p)!\n",
413               GNUNET_i2s (peer), cls);
414 #endif
415 }
416
417
418 static void
419 setup_peer (struct PeerContext *p, const char *cfgname)
420 {
421   p->cfg = GNUNET_CONFIGURATION_create ();
422 #if START_ARM
423   p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
424                                         "gnunet-service-arm",
425                                         "gnunet-service-arm",
426 #if VERBOSE_ARM
427                                         "-L", "DEBUG",
428 #endif
429                                         "-c", cfgname, NULL);
430 #endif
431
432   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
433   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
434                                     p,
435                                     &notify_receive_new,
436                                     &notify_connect,
437                                     &notify_disconnect);
438   GNUNET_assert (p->th != NULL);
439 }
440
441
442 static void
443 exchange_hello_last (void *cls,
444                      const struct GNUNET_MessageHeader *message)
445 {
446   struct PeerContext *me = cls;
447
448   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
449
450   GNUNET_assert (ok >= 3);
451   OKPP;
452   GNUNET_assert (message != NULL);
453   GNUNET_assert (GNUNET_OK ==
454                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
455                                       message, &me->id));
456   /* both HELLOs exchanged, get ready to test transmission! */
457 }
458
459
460 static void
461 exchange_hello (void *cls,
462                 const struct GNUNET_MessageHeader *message)
463 {
464   struct PeerContext *me = cls;
465
466   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
467   GNUNET_assert (ok >= 2);
468   OKPP;
469   GNUNET_assert (message != NULL);
470   GNUNET_assert (GNUNET_OK ==
471                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
472                                       message, &me->id));
473   GNUNET_TRANSPORT_offer_hello (p2.th, message);
474   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
475 }
476
477 static void
478 run (void *cls,
479      struct GNUNET_SCHEDULER_Handle *s,
480      char *const *args,
481      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
482 {
483   GNUNET_assert (ok == 1);
484   OKPP;
485   sched = s;
486
487   die_task = GNUNET_SCHEDULER_add_delayed (sched,
488                                            TIMEOUT,
489                                            &end_badly,
490                                            NULL);
491
492   /* Setting initial quota for both peers */
493 //  current_quota_p1 = 1024 * 1024 * 1024;
494 //  current_quota_p2 = 1024 * 1024 * 1024;
495
496   setup_peer (&p1, "test_quota_compliance_peer1.conf");
497   setup_peer (&p2, "test_quota_compliance_peer2.conf");
498
499   GNUNET_assert(p1.th != NULL);
500   GNUNET_assert(p2.th != NULL);
501   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
502 }
503
504 int
505 main (int argc, char *argv[])
506 {
507   int ret = 0;
508 #ifdef MINGW
509   return GNUNET_SYSERR;
510 #endif
511   GNUNET_log_setup ("test-quota-compliance",
512 #if VERBOSE
513                     "DEBUG",
514 #else
515                     "WARNING",
516 #endif
517                     NULL);
518   char *const argv1[] = { "test-quota-compliance",
519     "-c",
520     "test_quota_compliance_data.conf",
521 #if VERBOSE
522     "-L", "DEBUG",
523 #endif
524     NULL
525   };
526   struct GNUNET_GETOPT_CommandLineOption options[] = {
527     GNUNET_GETOPT_OPTION_END
528   };
529
530   ok = 1;
531   GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
532                       argv1, "test-quota-compliance", "nohelp",
533                       options, &run, &ok);
534   ret = ok;
535   stop_arm (&p1);
536   stop_arm (&p2);
537   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
538   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
539   return ret;
540 }
541
542 /* end of test_quota_compliance.c */