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