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