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