added fix
[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 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46 #define MEASUREMENT_MSG_SIZE 1024
47 #define MEASUREMENT_MSG_SIZE_BIG 32768
48 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
49 #define MEASUREMENT_MIN_QUOTA 1024
50 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
51 #define MEASUREMENT_SOFT_LIMIT 1024
52
53 /**
54  * Testcase timeout
55  */
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
57
58
59
60 #define MTYPE 11111
61
62 struct PeerContext
63 {
64   struct GNUNET_CONFIGURATION_Handle *cfg;
65   struct GNUNET_TRANSPORT_Handle *th;
66   struct GNUNET_PeerIdentity id;
67 #if START_ARM
68   struct GNUNET_OS_Process *arm_proc;
69 #endif
70 };
71
72 /**
73  * Handle for a transmission-ready request.
74  */
75 struct GNUNET_TRANSPORT_TransmitHandle
76 {
77
78   /**
79    * Neighbour for this handle, NULL for control-traffic.
80    */
81   struct NeighbourList *neighbour;
82
83   /**
84    * Function to call when notify_size bytes are available
85    * for transmission.
86    */
87   GNUNET_CONNECTION_TransmitReadyNotify notify;
88
89   /**
90    * Closure for notify.
91    */
92   void *notify_cls;
93
94   /**
95    * transmit_ready task Id.  The task is used to introduce the
96    * artificial delay that may be required to maintain the bandwidth
97    * limits.  Later, this will be the ID of the "transmit_timeout"
98    * task which is used to signal a timeout if the transmission could
99    * not be done in a timely fashion.
100    */
101   GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
102
103   /**
104    * Timeout for this request.
105    */
106   struct GNUNET_TIME_Absolute timeout;
107
108   /**
109    * How many bytes is our notify callback waiting for?
110    */
111   size_t notify_size;
112
113   /**
114    * How important is this message?
115    */
116   unsigned int priority;
117
118 };
119
120 static struct PeerContext p1;
121
122 static struct PeerContext p2;
123
124 static int ok;
125
126 static int connected;
127 static int measurement_running;
128 static int send_running;
129 static int recv_running;
130
131 static unsigned long long total_bytes_sent;
132 static unsigned long long last_msg_sent;
133 static unsigned long long last_msg_recv;
134 static unsigned long long current_quota_p1;
135 static unsigned long long current_quota_p2;
136
137 static int is_tcp;
138 static int is_tcp_nat;
139 static int is_http;
140 static int is_https;
141 static int is_udp;
142 static int is_asymmetric_send_constant;
143 static int is_asymmetric_recv_constant;
144
145 static struct GNUNET_TIME_Absolute start_time;
146
147 static GNUNET_SCHEDULER_TaskIdentifier die_task;
148 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
149 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
150
151 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
152
153 #define OKPP do { ok++; } while (0)
154
155
156
157 static void
158 end_send ()
159 {
160
161 }
162
163 static void
164 end ()
165 {
166   GNUNET_SCHEDULER_cancel (die_task);
167   die_task = GNUNET_SCHEDULER_NO_TASK;
168
169   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
170   {
171             GNUNET_SCHEDULER_cancel (measurement_task);
172             measurement_task = GNUNET_SCHEDULER_NO_TASK;
173   }
174   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
175   {
176             GNUNET_SCHEDULER_cancel (measurement_counter_task);
177             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
178   }
179   fprintf(stderr,"\n");
180   GNUNET_SCHEDULER_shutdown ();
181 #if DEBUG_CONNECTIONS
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
183 #endif
184   GNUNET_TRANSPORT_disconnect (p1.th);
185   GNUNET_TRANSPORT_disconnect (p2.th);
186 #if DEBUG_CONNECTIONS
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188               "Transports disconnected, returning success!\n");
189 #endif
190   GNUNET_SCHEDULER_shutdown ();
191 }
192
193
194
195 static void
196 stop_arm (struct PeerContext *p)
197 {
198 #if START_ARM
199   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
200     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
201   GNUNET_OS_process_wait (p->arm_proc);
202   GNUNET_OS_process_close (p->arm_proc);
203   p->arm_proc = NULL;
204 #endif
205   GNUNET_CONFIGURATION_destroy (p->cfg);
206 }
207
208
209 static void
210 end_badly (void *cls,
211            const struct GNUNET_SCHEDULER_TaskContext *tc)
212 {
213   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
214   {
215             GNUNET_SCHEDULER_cancel (measurement_task);
216             measurement_task = GNUNET_SCHEDULER_NO_TASK;
217   }
218   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
219   {
220             GNUNET_SCHEDULER_cancel (measurement_counter_task);
221             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
222   }
223   GNUNET_break (0);
224   if (p1.th != NULL)
225           GNUNET_TRANSPORT_disconnect (p1.th);
226   if (p2.th != NULL)
227           GNUNET_TRANSPORT_disconnect (p2.th);
228   ok = 1;
229 }
230
231 struct TestMessage
232 {
233   struct GNUNET_MessageHeader header;
234   uint32_t num;
235 };
236
237 static unsigned int
238 get_size (void)
239 {
240   return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
241 }
242
243 static void
244 notify_receive_new (void *cls,
245                 const struct GNUNET_PeerIdentity *peer,
246                 const struct GNUNET_MessageHeader *message,
247                 const struct GNUNET_TRANSPORT_ATS_Information *ats, uint32_t ats_count)
248 {
249   unsigned int s;
250   const struct TestMessage *hdr;
251
252   hdr = (const struct TestMessage*) message;
253   s = get_size ();
254   if (measurement_running == GNUNET_NO)
255           return;
256   if (MTYPE != ntohs (message->type))
257     return;
258
259 #if DEBUG_MEASUREMENT
260   if (ntohl(hdr->num) % 5000 == 0)
261     {
262       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263                   "Got message %u of size %u\n",
264                   ntohl (hdr->num),
265                   ntohs (message->size));
266     }
267 #endif
268   /*
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270               "Got message %u\n",
271               ntohl (hdr->num));*/
272   last_msg_recv = ntohl (hdr->num);
273 }
274
275 static size_t
276 notify_ready (void *cls, size_t size, void *buf)
277 {
278   char *cbuf = buf;
279   struct TestMessage hdr;
280   unsigned int s;
281   unsigned int ret;
282
283   transmit_handle = NULL;
284
285   if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
286           return 0;
287
288   if (buf == NULL)
289     {
290       ok = 42;
291       return 0;
292     }
293
294   if (measurement_running != GNUNET_YES)
295   {
296           send_running = GNUNET_NO;
297           end_send();
298           return 0;
299   }
300
301   send_running = GNUNET_YES;
302   ret = 0;
303   s = get_size ();
304   GNUNET_assert (size >= s);
305   GNUNET_assert (buf != NULL);
306   last_msg_sent++;
307   cbuf = buf;
308   do
309     {
310       hdr.header.size = htons (s);
311       hdr.header.type = htons (MTYPE);
312       hdr.num = htonl (last_msg_sent);
313       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
314       ret += sizeof (struct TestMessage);
315       memset (&cbuf[ret], last_msg_sent, s - sizeof (struct TestMessage));
316       ret += s - sizeof (struct TestMessage);
317 #if DEBUG_MEASUREMENT
318       if (n % 5000 == 0)
319        {
320           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321                       "Sending message %u\n",n);
322        }
323 #endif
324
325     /*      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326                       "Sending message %u\n",last_msg_sent);*/
327
328       s = get_size ();
329       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
330         break; /* sometimes pack buffer full, sometimes not */
331     }
332   while (size - ret >= s);
333   transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
334                                             &p1.id,
335                                             s, 0, SEND_TIMEOUT,
336                                             &notify_ready,
337                                             NULL);
338   total_bytes_sent += s;
339   return ret;
340 }
341
342 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
343
344 static void measurement_counter
345  (void *cls,
346            const struct GNUNET_SCHEDULER_TaskContext *tc)
347 {
348   measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
349
350   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
351         return;
352
353   fprintf(stderr,".");
354   measurement_counter_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
355                                                            &measurement_counter,
356                                                            NULL);
357 }
358
359 static void
360 measurement_end (void *cls,
361            const struct GNUNET_SCHEDULER_TaskContext *tc)
362 {
363   static int strike_counter;
364   static int failed_measurement_counter = 1;
365   unsigned long long  quota_allowed = 0;
366   int delta = 0;
367
368   measurement_task  = GNUNET_SCHEDULER_NO_TASK;
369   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
370         return;
371
372   measurement_running = GNUNET_NO;
373   struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
374
375
376   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
377   {
378     GNUNET_SCHEDULER_cancel (measurement_counter_task);
379     measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
380   }
381
382   if (transmit_handle != NULL)
383   {
384           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
385           transmit_handle = NULL;
386   }
387
388   if (current_quota_p1 < current_quota_p2)
389           quota_allowed = current_quota_p1;
390   else
391           quota_allowed = current_quota_p2;
392
393
394   if (MEASUREMENT_SOFT_LIMIT > (quota_allowed/3))
395           delta = MEASUREMENT_SOFT_LIMIT;
396   else
397           delta = (quota_allowed/3);
398
399   /* Throughput is far too slow. This is to prevent the test to exit with success when throughput is 0 */
400   if ((total_bytes_sent/(duration.rel_value / 1000)) < 100)
401   {
402           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
403                           "\nQuota compliance failed: \n"\
404                           "Hard quota limit allowed: %10llu kB/s (%llu B/s)\n"\
405                           "Soft quota limit allowed: %10llu kB/s (%llu B/s)\n"\
406                           "Throughput              : %10llu kB/s (%llu B/s)\n",
407                           (quota_allowed / (1024)), quota_allowed,
408                           ((quota_allowed+delta) / (1024)),  quota_allowed+delta,
409                           (total_bytes_sent/(duration.rel_value / 1000)/1024),
410                           total_bytes_sent/(duration.rel_value / 1000));
411           ok = 1;
412           failed_measurement_counter--;
413           if (failed_measurement_counter < 0)
414           {
415                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416                                   "\nQuota measurement failed and no free strike: %i\n",failed_measurement_counter);
417                   end();
418                   return;
419           }
420           else
421                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422                                   "\nQuota measurement failed and %i free strikes\n",failed_measurement_counter);
423   }
424
425   /* Throughput is bigger than allowed quota + some extra*/
426   if ((total_bytes_sent/(duration.rel_value / 1000)) > (quota_allowed + delta))
427   {
428           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429                           "\nQuota compliance failed: \n"\
430                           "Hard quota limit allowed: %10llu kB/s (%llu B/s)\n"\
431                           "Soft quota limit allowed: %10llu kB/s (%llu B/s)\n"\
432                           "Throughput              : %10llu kB/s (%llu B/s)\n", 
433                           (quota_allowed / (1024)), quota_allowed, 
434                           ((quota_allowed+delta) / (1024)),  quota_allowed+delta, 
435                           (total_bytes_sent/(duration.rel_value / 1000)/1024), 
436                           total_bytes_sent/(duration.rel_value / 1000));
437           ok = 1;
438           failed_measurement_counter--;
439           if (failed_measurement_counter < 0)
440           {
441                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442                                   "\nQuota measurement failed and no free strike: %i\n",failed_measurement_counter);
443                   end();
444                   return;
445           }
446           else
447                   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448                                   "\nQuota measurement failed and %i free strikes\n",failed_measurement_counter);
449   }
450   else
451   {
452           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
453                           "\nQuota compliance ok: \n"\
454                           "Quota allowed: %10llu kB/s\n"\
455                           "Throughput   : %10llu kB/s\n", (quota_allowed / (1024)) , (total_bytes_sent/(duration.rel_value / 1000)/1024));
456           if (failed_measurement_counter < 2)
457                   failed_measurement_counter++;
458           ok = 0;
459   }
460
461   if ((quota_allowed) > (2 *(total_bytes_sent/(duration.rel_value / 1000))))
462   {
463           if (failed_measurement_counter < 2)
464                   failed_measurement_counter++;
465           if (strike_counter == 2)
466           {
467                   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468                                   "Maximum transmission rate reached, stopping test\n");
469                   end();
470                   return;
471           }
472   }
473   else
474   {
475           strike_counter = 0;
476   }
477
478   if (quota_allowed == MEASUREMENT_MAX_QUOTA)
479   {
480           end();
481           return;
482   }
483   if (is_asymmetric_send_constant == GNUNET_YES)
484   {
485    if ((quota_allowed * 2) < MEASUREMENT_MAX_QUOTA)
486           measure (current_quota_p1 * 2, MEASUREMENT_MAX_QUOTA);
487    else
488            measure (MEASUREMENT_MAX_QUOTA, MEASUREMENT_MAX_QUOTA);
489   }
490   else if (is_asymmetric_recv_constant == GNUNET_YES)
491   {
492    if ((quota_allowed * 2) < MEASUREMENT_MAX_QUOTA)
493           measure (MEASUREMENT_MAX_QUOTA, current_quota_p2 * 2);
494    else
495            measure (MEASUREMENT_MAX_QUOTA, MEASUREMENT_MAX_QUOTA);
496   }
497   else
498   {
499    if ((quota_allowed * 2) < MEASUREMENT_MAX_QUOTA)
500           measure ((current_quota_p1) * 2, (current_quota_p2) * 2);
501    else
502            measure (MEASUREMENT_MAX_QUOTA, MEASUREMENT_MAX_QUOTA);
503   }
504 }
505
506 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
507 {
508           current_quota_p1 = quota_p1;
509           current_quota_p2 = quota_p2;
510 #if VERBOSE
511   if ((is_asymmetric_send_constant == GNUNET_YES) || (is_asymmetric_recv_constant == GNUNET_YES))
512           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513               "Starting transport level measurement for %u seconds, receiving peer quota %llu kB/s, sending peer quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1 / 1024, current_quota_p2 / 1024);
514   else
515           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516               "Starting transport level measurement for %u seconds, symmetric quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p2 / 1024);
517
518 #endif
519                 GNUNET_TRANSPORT_set_quota (p1.th,
520                           &p2.id,
521                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
522                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
523                           GNUNET_TIME_UNIT_FOREVER_REL,
524                           NULL, NULL);
525                 GNUNET_TRANSPORT_set_quota (p2.th,
526                           &p1.id,
527                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
528                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
529                           GNUNET_TIME_UNIT_FOREVER_REL,
530                           NULL, NULL);
531
532                 GNUNET_SCHEDULER_cancel (die_task);
533                 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
534                                                    &end_badly,
535                                                    NULL);
536                 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
537                   GNUNET_SCHEDULER_cancel (measurement_counter_task);
538                 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
539                                                                    &measurement_counter,
540                                                                    NULL);
541                 measurement_task = GNUNET_SCHEDULER_add_delayed (MEASUREMENT_INTERVALL,
542                                                    &measurement_end,
543                                                    NULL);
544                 total_bytes_sent = 0;
545                 last_msg_sent = 0;
546                 last_msg_recv = 0;
547                 measurement_running = GNUNET_YES;
548                 start_time = GNUNET_TIME_absolute_get ();
549
550                 if (transmit_handle != NULL)
551                           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
552                 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
553                                                                                           &p1.id,
554                                                                                           get_size (), 0, SEND_TIMEOUT,
555                                                                                           &notify_ready,
556                                                                                           NULL);
557 }
558
559 static void
560 notify_connect (void *cls,
561                 const struct GNUNET_PeerIdentity *peer,
562                 const struct GNUNET_TRANSPORT_ATS_Information *ats, uint32_t ats_count)
563 {
564   if (cls == &p1)
565     {
566 #if DEBUG_CONNECTIONS
567   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568               "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
569 #endif
570           connected++;
571     }
572   else
573     {
574 #if DEBUG_CONNECTIONS
575   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576               "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
577 #endif
578       connected++;
579     }
580   if (connected == 2)
581     {
582            if (is_asymmetric_send_constant == GNUNET_YES)
583                    measure (MEASUREMENT_MIN_QUOTA, MEASUREMENT_MAX_QUOTA);
584            else if (is_asymmetric_recv_constant == GNUNET_YES)
585                    measure (MEASUREMENT_MAX_QUOTA, MEASUREMENT_MIN_QUOTA);
586            else
587                    measure (MEASUREMENT_MIN_QUOTA, MEASUREMENT_MIN_QUOTA);
588     }
589 }
590
591
592 static void
593 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
594 {
595 #if DEBUG_CONNECTIONS
596   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597               "Peer `%4s' disconnected (%p)!\n",
598               GNUNET_i2s (peer), cls);
599 #endif
600 }
601
602
603 static void
604 setup_peer (struct PeerContext *p, const char *cfgname)
605 {
606   p->cfg = GNUNET_CONFIGURATION_create ();
607 #if START_ARM
608   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
609                                         "gnunet-service-arm",
610                                         "gnunet-service-arm",
611 #if VERBOSE_ARM
612                                         "-L", "DEBUG",
613 #endif
614                                         "-c", cfgname, NULL);
615 #endif
616
617   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
618   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
619                                     p,
620                                     &notify_receive_new,
621                                     &notify_connect,
622                                     &notify_disconnect);
623   GNUNET_assert (p->th != NULL);
624 }
625
626 static size_t
627 notify_ready_connect (void *cls, size_t size, void *buf)
628 {
629   return 0;
630 }
631
632 static void
633 exchange_hello_last (void *cls,
634                      const struct GNUNET_MessageHeader *message)
635 {
636   struct PeerContext *me = cls;
637
638   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
639
640   GNUNET_assert (ok >= 3);
641   OKPP;
642   GNUNET_assert (message != NULL);
643   GNUNET_assert (GNUNET_OK ==
644                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
645                                       message, &me->id));
646
647   GNUNET_assert(NULL != (transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
648                                           &p1.id,
649                                           sizeof (struct GNUNET_MessageHeader), 0,
650                                           TIMEOUT,
651                                           &notify_ready_connect,
652                                           NULL)));
653
654   /* both HELLOs exchanged, get ready to test transmission! */
655 }
656
657
658 static void
659 exchange_hello (void *cls,
660                 const struct GNUNET_MessageHeader *message)
661 {
662   struct PeerContext *me = cls;
663
664   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
665   GNUNET_assert (ok >= 2);
666   OKPP;
667   GNUNET_assert (message != NULL);
668   GNUNET_assert (GNUNET_OK ==
669                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
670                                       message, &me->id));
671   GNUNET_TRANSPORT_offer_hello (p2.th, message);
672   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
673 }
674
675 static void
676 run (void *cls,
677      char *const *args,
678      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
679 {
680   GNUNET_assert (ok == 1);
681   OKPP;
682
683   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
684                                            &end_badly,
685                                            NULL);
686   measurement_running = GNUNET_NO;
687   send_running = GNUNET_NO;
688   recv_running = GNUNET_NO;
689
690   if (is_tcp)
691     {
692           if (is_asymmetric_recv_constant == GNUNET_YES)
693                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for TCP transport plugin\n");
694           else if (is_asymmetric_send_constant == GNUNET_YES)
695                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for TCP transport plugin\n");
696           else
697                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for TCP transport plugin\n");
698       setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
699       setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
700     }
701   else if (is_http)
702     {
703           if (is_asymmetric_recv_constant == GNUNET_YES)
704                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for HTTP transport plugin\n");
705           else if (is_asymmetric_send_constant == GNUNET_YES)
706                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for HTTP transport plugin\n");
707           else
708                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for HTTP transport plugin\n");
709       setup_peer (&p1, "test_quota_compliance_http_peer1.conf");
710       setup_peer (&p2, "test_quota_compliance_http_peer2.conf");
711     }
712   else if (is_https)
713     {
714           if (is_asymmetric_recv_constant == GNUNET_YES)
715                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for HTTPS transport plugin\n");
716           else if (is_asymmetric_send_constant == GNUNET_YES)
717                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for HTTPS transport plugin\n");
718           else
719                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for HTTPS transport plugin\n");
720       setup_peer (&p1, "test_quota_compliance_https_peer1.conf");
721       setup_peer (&p2, "test_quota_compliance_https_peer2.conf");
722     }
723   else if (is_udp)
724     {
725           if (is_asymmetric_recv_constant == GNUNET_YES)
726                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for UDP transport plugin\n");
727           else if (is_asymmetric_send_constant == GNUNET_YES)
728                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for UDP transport plugin\n");
729           else
730                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for UDP transport plugin\n");
731       setup_peer (&p1, "test_quota_compliance_udp_peer1.conf");
732       setup_peer (&p2, "test_quota_compliance_udp_peer2.conf");
733     }
734   else if (is_tcp_nat)
735     {
736           if (is_asymmetric_recv_constant == GNUNET_YES)
737                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for TCP NAT transport plugin\n");
738           else if (is_asymmetric_send_constant == GNUNET_YES)
739                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for TCP NAT transport plugin\n");
740           else
741                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for TCP NAT transport plugin\n");
742       setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
743       setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
744     }
745   else
746     GNUNET_assert (0);
747
748   GNUNET_assert(p1.th != NULL);
749   GNUNET_assert(p2.th != NULL);
750   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
751 }
752
753 int
754 main (int argc, char *argv[])
755 {
756   int ret = 0;
757 #ifdef MINGW
758   return GNUNET_SYSERR;
759 #endif
760   if (strstr(argv[0], "tcp_nat") != NULL)
761     {
762       is_tcp_nat = GNUNET_YES;
763     }
764   else if (strstr(argv[0], "tcp") != NULL)
765     {
766       is_tcp = GNUNET_YES;
767     }
768   else if (strstr(argv[0], "https") != NULL)
769     {
770       is_https = GNUNET_YES;
771     }
772   else if (strstr(argv[0], "http") != NULL)
773     {
774       is_http = GNUNET_YES;
775     }
776   else if (strstr(argv[0], "udp") != NULL)
777     {
778       is_udp = GNUNET_YES;
779     }
780
781   if (strstr(argv[0], "asymmetric_recv") != NULL)
782   {
783       is_asymmetric_recv_constant = GNUNET_YES;
784   }
785   else
786           is_asymmetric_recv_constant = GNUNET_NO;
787   if (strstr(argv[0], "asymmetric_send") != NULL)
788   {
789       is_asymmetric_send_constant = GNUNET_YES;
790   }
791   else
792           is_asymmetric_send_constant = GNUNET_NO;
793
794   char * logger;
795   if (is_tcp == GNUNET_YES)
796   {
797           if (is_asymmetric_recv_constant == GNUNET_YES)
798                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","tcp","asymmetric_recv_constant");
799           else if (is_asymmetric_send_constant == GNUNET_YES)
800                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","tcp","asymmetric_send_constant");
801           else
802                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","tcp","symmetric");
803   }
804   if (is_udp == GNUNET_YES)
805   {
806           if (is_asymmetric_recv_constant == GNUNET_YES)
807                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","udp","asymmetric_recv_constant");
808           else if (is_asymmetric_send_constant == GNUNET_YES)
809                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","udp","asymmetric_send_constant");
810           else
811                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","udp","symmetric");
812   }
813   if (is_http == GNUNET_YES)
814   {
815           if (is_asymmetric_recv_constant == GNUNET_YES)
816                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","http","asymmetric_recv_constant");
817           else if (is_asymmetric_send_constant == GNUNET_YES)
818                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","http","asymmetric_send_constant");
819           else
820                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","http","symmetric");
821   }
822   if (is_https == GNUNET_YES)
823   {
824           if (is_asymmetric_recv_constant == GNUNET_YES)
825                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","https","asymmetric_recv_constant");
826           else if (is_asymmetric_send_constant == GNUNET_YES)
827                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","https","asymmetric_send_constant");
828           else
829                   GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","https","symmetric");
830   }
831
832   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
833   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
834
835   fprintf(stderr,  "Running `%s'\n", logger);
836   GNUNET_log_setup ("test-quota-compliance",
837 #if VERBOSE
838                     "DEBUG",
839 #else
840                     "WARNING",
841 #endif
842                     NULL);
843   char *const argv1[] = { "test-quota-compliance",
844     "-c",
845     "test_quota_compliance_data.conf",
846 #if VERBOSE
847     "-L", "DEBUG",
848 #endif
849     NULL
850   };
851   struct GNUNET_GETOPT_CommandLineOption options[] = {
852     GNUNET_GETOPT_OPTION_END
853   };
854   ok = 1;
855   GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
856                       argv1, logger , "nohelp",
857                       options, &run, &ok);
858   ret = ok;
859   stop_arm (&p1);
860   stop_arm (&p2);
861   GNUNET_free(logger);
862   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
863   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
864   return ret;
865 }
866
867 /* end of test_quota_compliance.c */