wip
[oweals/gnunet.git] / src / transport / test_quota_compliance.c
index 1c435151aa447f01520f2d6b25d9d449ee316de6..219971725578acc47f2ec042c5019f49ef73da5a 100644 (file)
@@ -42,7 +42,7 @@
 #define DEBUG_MEASUREMENT GNUNET_NO
 #define DEBUG_CONNECTIONS GNUNET_NO
 
-#define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
+#define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
 #define MEASUREMENT_MSG_SIZE 1024
 #define MEASUREMENT_MSG_SIZE_BIG 32768
 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
@@ -65,7 +65,7 @@ struct PeerContext
   struct GNUNET_TRANSPORT_Handle *th;
   struct GNUNET_PeerIdentity id;
 #if START_ARM
-  pid_t arm_pid;
+  struct GNUNET_OS_Process *arm_proc;
 #endif
 };
 
@@ -121,8 +121,6 @@ static struct PeerContext p1;
 
 static struct PeerContext p2;
 
-static struct GNUNET_SCHEDULER_Handle *sched;
-
 static int ok;
 
 static int connected;
@@ -141,6 +139,7 @@ static int is_tcp_nat;
 static int is_http;
 static int is_https;
 static int is_udp;
+static int is_unix;
 static int is_asymmetric_send_constant;
 static int is_asymmetric_recv_constant;
 
@@ -150,7 +149,7 @@ static GNUNET_SCHEDULER_TaskIdentifier die_task;
 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
 
-struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
+static struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
 
 #define OKPP do { ok++; } while (0)
 
@@ -165,20 +164,21 @@ end_send ()
 static void
 end ()
 {
-  GNUNET_SCHEDULER_cancel (sched, die_task);
+  GNUNET_SCHEDULER_cancel (die_task);
   die_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
   {
-           GNUNET_SCHEDULER_cancel (sched, measurement_task);
+           GNUNET_SCHEDULER_cancel (measurement_task);
            measurement_task = GNUNET_SCHEDULER_NO_TASK;
   }
   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
   {
-           GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
+           GNUNET_SCHEDULER_cancel (measurement_counter_task);
            measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  GNUNET_SCHEDULER_shutdown (sched);
+  fprintf(stderr,"\n");
+  GNUNET_SCHEDULER_shutdown ();
 #if DEBUG_CONNECTIONS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
 #endif
@@ -188,7 +188,7 @@ end ()
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Transports disconnected, returning success!\n");
 #endif
-  GNUNET_SCHEDULER_shutdown (sched);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -197,9 +197,11 @@ static void
 stop_arm (struct PeerContext *p)
 {
 #if START_ARM
-  if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
+  if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
-  GNUNET_OS_process_wait (p->arm_pid);
+  GNUNET_OS_process_wait (p->arm_proc);
+  GNUNET_OS_process_close (p->arm_proc);
+  p->arm_proc = NULL;
 #endif
   GNUNET_CONFIGURATION_destroy (p->cfg);
 }
@@ -211,12 +213,12 @@ end_badly (void *cls,
 {
   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
   {
-           GNUNET_SCHEDULER_cancel (sched, measurement_task);
+           GNUNET_SCHEDULER_cancel (measurement_task);
            measurement_task = GNUNET_SCHEDULER_NO_TASK;
   }
   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
   {
-           GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
+           GNUNET_SCHEDULER_cancel (measurement_counter_task);
            measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
   }
   GNUNET_break (0);
@@ -243,8 +245,7 @@ static void
 notify_receive_new (void *cls,
                 const struct GNUNET_PeerIdentity *peer,
                 const struct GNUNET_MessageHeader *message,
-                struct GNUNET_TIME_Relative latency,
-               uint32_t distance)
+                const struct GNUNET_TRANSPORT_ATS_Information *ats, uint32_t ats_count)
 {
   unsigned int s;
   const struct TestMessage *hdr;
@@ -350,11 +351,8 @@ static void measurement_counter
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
        return;
 
-#if VERBOSE
   fprintf(stderr,".");
-#endif
-  measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
-                                                          GNUNET_TIME_UNIT_SECONDS,
+  measurement_counter_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                                           &measurement_counter,
                                                           NULL);
 }
@@ -364,6 +362,7 @@ measurement_end (void *cls,
           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static int strike_counter;
+  static int failed_measurement_counter = 1;
   unsigned long long  quota_allowed = 0;
   int delta = 0;
 
@@ -377,12 +376,9 @@ measurement_end (void *cls,
 
   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
   {
-    GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
+    GNUNET_SCHEDULER_cancel (measurement_counter_task);
     measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
   }
-#if VERBOSE
-  fprintf(stderr,"\n");
-#endif
 
   if (transmit_handle != NULL)
   {
@@ -396,21 +392,61 @@ measurement_end (void *cls,
          quota_allowed = current_quota_p2;
 
 
-  if (MEASUREMENT_SOFT_LIMIT > (quota_allowed/10))
+  if (MEASUREMENT_SOFT_LIMIT > (quota_allowed/3))
          delta = MEASUREMENT_SOFT_LIMIT;
   else
-         delta = (quota_allowed/10);
+         delta = (quota_allowed/3);
 
+  /* Throughput is far too slow. This is to prevent the test to exit with success when throughput is 0 */
+  if ((total_bytes_sent/(duration.rel_value / 1000)) < 100)
+  {
+         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                         "\nQuota compliance failed: \n"\
+                         "Hard quota limit allowed: %10llu kB/s (%llu B/s)\n"\
+                         "Soft quota limit allowed: %10llu kB/s (%llu B/s)\n"\
+                         "Throughput              : %10llu kB/s (%llu B/s)\n",
+                         (quota_allowed / (1024)), quota_allowed,
+                         ((quota_allowed+delta) / (1024)),  quota_allowed+delta,
+                         (total_bytes_sent/(duration.rel_value / 1000)/1024),
+                         total_bytes_sent/(duration.rel_value / 1000));
+         ok = 1;
+         failed_measurement_counter--;
+         if (failed_measurement_counter < 0)
+         {
+                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                 "\nQuota measurement failed and no free strike: %i\n",failed_measurement_counter);
+                 end();
+                 return;
+         }
+         else
+                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                 "\nQuota measurement failed and %i free strikes\n",failed_measurement_counter);
+  }
+
+  /* Throughput is bigger than allowed quota + some extra*/
   if ((total_bytes_sent/(duration.rel_value / 1000)) > (quota_allowed + delta))
   {
          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                          "\nQuota compliance failed: \n"\
                          "Hard quota limit allowed: %10llu kB/s (%llu B/s)\n"\
                          "Soft quota limit allowed: %10llu kB/s (%llu B/s)\n"\
-                         "Throughput   : %10llu kB/s (%llu B/s)\n", (quota_allowed / (1024)), quota_allowed, ((quota_allowed+delta) / (1024)), quota_allowed+delta, (total_bytes_sent/(duration.rel_value / 1000)/1024), total_bytes_sent/(duration.rel_value / 1000));
+                         "Throughput              : %10llu kB/s (%llu B/s)\n", 
+                         (quota_allowed / (1024)), quota_allowed, 
+                         ((quota_allowed+delta) / (1024)),  quota_allowed+delta, 
+                         (total_bytes_sent/(duration.rel_value / 1000)/1024), 
+                         total_bytes_sent/(duration.rel_value / 1000));
          ok = 1;
-         end();
-         return;
+         failed_measurement_counter--;
+         if (failed_measurement_counter < 0)
+         {
+                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                 "\nQuota measurement failed and no free strike: %i\n",failed_measurement_counter);
+                 end();
+                 return;
+         }
+         else
+                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                 "\nQuota measurement failed and %i free strikes\n",failed_measurement_counter);
   }
   else
   {
@@ -418,12 +454,15 @@ measurement_end (void *cls,
                          "\nQuota compliance ok: \n"\
                          "Quota allowed: %10llu kB/s\n"\
                          "Throughput   : %10llu kB/s\n", (quota_allowed / (1024)) , (total_bytes_sent/(duration.rel_value / 1000)/1024));
+         if (failed_measurement_counter < 2)
+                 failed_measurement_counter++;
          ok = 0;
   }
 
   if ((quota_allowed) > (2 *(total_bytes_sent/(duration.rel_value / 1000))))
   {
-         strike_counter++;
+         if (failed_measurement_counter < 2)
+                 failed_measurement_counter++;
          if (strike_counter == 2)
          {
                  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -442,7 +481,6 @@ measurement_end (void *cls,
          end();
          return;
   }
-
   if (is_asymmetric_send_constant == GNUNET_YES)
   {
    if ((quota_allowed * 2) < MEASUREMENT_MAX_QUOTA)
@@ -492,19 +530,16 @@ static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
                          GNUNET_TIME_UNIT_FOREVER_REL,
                          NULL, NULL);
 
-               GNUNET_SCHEDULER_cancel (sched, die_task);
-               die_task = GNUNET_SCHEDULER_add_delayed (sched,
-                                                  TIMEOUT,
+               GNUNET_SCHEDULER_cancel (die_task);
+               die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
                                                   &end_badly,
                                                   NULL);
                if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
-                 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
-               measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
-                                                                  GNUNET_TIME_UNIT_SECONDS,
+                 GNUNET_SCHEDULER_cancel (measurement_counter_task);
+               measurement_counter_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                                                   &measurement_counter,
                                                                   NULL);
-               measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
-                                                  MEASUREMENT_INTERVALL,
+               measurement_task = GNUNET_SCHEDULER_add_delayed (MEASUREMENT_INTERVALL,
                                                   &measurement_end,
                                                   NULL);
                total_bytes_sent = 0;
@@ -525,8 +560,7 @@ static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
 static void
 notify_connect (void *cls,
                 const struct GNUNET_PeerIdentity *peer,
-                struct GNUNET_TIME_Relative latency,
-               uint32_t distance)
+                const struct GNUNET_TRANSPORT_ATS_Information *ats, uint32_t ats_count)
 {
   if (cls == &p1)
     {
@@ -572,7 +606,7 @@ setup_peer (struct PeerContext *p, const char *cfgname)
 {
   p->cfg = GNUNET_CONFIGURATION_create ();
 #if START_ARM
-  p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
+  p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
                                        "gnunet-service-arm",
                                         "gnunet-service-arm",
 #if VERBOSE_ARM
@@ -582,7 +616,7 @@ setup_peer (struct PeerContext *p, const char *cfgname)
 #endif
 
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
-  p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
+  p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
                                     p,
                                     &notify_receive_new,
                                     &notify_connect,
@@ -590,6 +624,11 @@ setup_peer (struct PeerContext *p, const char *cfgname)
   GNUNET_assert (p->th != NULL);
 }
 
+static size_t
+notify_ready_connect (void *cls, size_t size, void *buf)
+{
+  return 0;
+}
 
 static void
 exchange_hello_last (void *cls,
@@ -605,6 +644,14 @@ exchange_hello_last (void *cls,
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
                                       message, &me->id));
+
+  GNUNET_assert(NULL != (transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
+                                          &p1.id,
+                                          sizeof (struct GNUNET_MessageHeader), 0,
+                                          TIMEOUT,
+                                          &notify_ready_connect,
+                                          NULL)));
+
   /* both HELLOs exchanged, get ready to test transmission! */
 }
 
@@ -622,22 +669,19 @@ exchange_hello (void *cls,
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
                                       message, &me->id));
-  GNUNET_TRANSPORT_offer_hello (p2.th, message);
+  GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
 }
 
 static void
 run (void *cls,
-     struct GNUNET_SCHEDULER_Handle *s,
      char *const *args,
      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   GNUNET_assert (ok == 1);
   OKPP;
-  sched = s;
 
-  die_task = GNUNET_SCHEDULER_add_delayed (sched,
-                                          TIMEOUT,
+  die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
                                           &end_badly,
                                           NULL);
   measurement_running = GNUNET_NO;
@@ -688,6 +732,17 @@ run (void *cls,
       setup_peer (&p1, "test_quota_compliance_udp_peer1.conf");
       setup_peer (&p2, "test_quota_compliance_udp_peer2.conf");
     }
+  else if (is_unix)
+    {
+          if (is_asymmetric_recv_constant == GNUNET_YES)
+                  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (receiver quota constant) for UNIX transport plugin\n");
+          else if (is_asymmetric_send_constant == GNUNET_YES)
+                  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing asymmetric quota compliance (sender quota constant) for UNIX transport plugin\n");
+          else
+                  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing symmetric quota compliance for UNIX transport plugin\n");
+      setup_peer (&p1, "test_quota_compliance_unix_peer1.conf");
+      setup_peer (&p2, "test_quota_compliance_unix_peer2.conf");
+    }
   else if (is_tcp_nat)
     {
          if (is_asymmetric_recv_constant == GNUNET_YES)
@@ -734,6 +789,10 @@ main (int argc, char *argv[])
     {
       is_udp = GNUNET_YES;
     }
+  else if (strstr(argv[0], "unix") != NULL)
+    {
+      is_unix = GNUNET_YES;
+    }
 
   if (strstr(argv[0], "asymmetric_recv") != NULL)
   {
@@ -758,7 +817,7 @@ main (int argc, char *argv[])
          else
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","tcp","symmetric");
   }
-  if (is_udp == GNUNET_YES)
+  else if (is_udp == GNUNET_YES)
   {
          if (is_asymmetric_recv_constant == GNUNET_YES)
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","udp","asymmetric_recv_constant");
@@ -767,7 +826,16 @@ main (int argc, char *argv[])
          else
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","udp","symmetric");
   }
-  if (is_http == GNUNET_YES)
+  else if (is_unix == GNUNET_YES)
+  {
+          if (is_asymmetric_recv_constant == GNUNET_YES)
+                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","unix","asymmetric_recv_constant");
+          else if (is_asymmetric_send_constant == GNUNET_YES)
+                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","unix","asymmetric_send_constant");
+          else
+                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","unix","symmetric");
+  }
+  else if (is_http == GNUNET_YES)
   {
          if (is_asymmetric_recv_constant == GNUNET_YES)
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","http","asymmetric_recv_constant");
@@ -776,7 +844,7 @@ main (int argc, char *argv[])
          else
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","http","symmetric");
   }
-  if (is_https == GNUNET_YES)
+  else if (is_https == GNUNET_YES)
   {
          if (is_asymmetric_recv_constant == GNUNET_YES)
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","https","asymmetric_recv_constant");
@@ -785,6 +853,14 @@ main (int argc, char *argv[])
          else
                  GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","https","symmetric");
   }
+  else
+  {
+         GNUNET_asprintf(&logger, "test-quota-compliance-%s-%s","noplugin","none");
+  }
+
+  GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
+  GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
+
   fprintf(stderr,  "Running `%s'\n", logger);
   GNUNET_log_setup ("test-quota-compliance",
 #if VERBOSE