really reuse the dns-tunnel
[oweals/gnunet.git] / src / transport / test_transport_ats.c
index ac10ba1c888375c4907dd624053be3f7f822f1ab..fe8b6e87b22722b78ba4bb1bb11b1f8904d06a77 100644 (file)
 */
 /**
  * @file testing/test_transport_ats.c
- * @brief testcase for ats functionality
+ * @brief testcase for ats functionality without starting peers
  */
 #include "platform.h"
-#include "gnunet_testing_lib.h"
-#include "gnunet_scheduler_lib.h"
-#include "gauger.h"
+#include "gnunet-service-transport_ats.h"
+#include "gnunet_configuration_lib.h"
+#include "gnunet_crypto_lib.h"
 
-#define VERBOSE GNUNET_NO
+#define VERBOSE GNUNET_YES
 
-#define NUM_PEERS 11
-#define MEASUREMENTS 5
+static struct ATS_Handle * ats;
 
-#define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
+static struct GNUNET_CONFIGURATION_Handle * cfg;
 
+static struct TransportConfiguration *tc;
 
-static int ok;
 
-static int peers_left;
-
-static int failed_peers;
-
-static int measurement_started;
-
-static struct GNUNET_TESTING_PeerGroup *pg;
+static void 
+ats_result_cb ()
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "ATS Result callback\n");
+}
 
-static  GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
-static  GNUNET_SCHEDULER_TaskIdentifier stats_task;
-struct GNUNET_TESTING_Daemon * master_deamon;
+struct TransportConfiguration
+{
+  int peers;
+  int mechanisms;
 
-struct GNUNET_STATISTICS_Handle * stats;
+  struct ATS_peer * p_head;
+  struct ATS_peer * p_tail;
 
-struct TEST_result
-{
-       uint64_t timestamp;
-       uint64_t duration;
-       uint64_t mechs;
-       uint64_t peers;
-       uint64_t solution;
+  struct ATS_mechanism * m_head;
+  struct ATS_mechanism * m_tail;
 };
 
-static int r_index;
-//static int measurements;
-static int peers;
-static struct TEST_result results[MEASUREMENTS];
 
-struct GNUNET_STATISTICS_GetHandle * s_solution;
-struct GNUNET_STATISTICS_GetHandle * s_time;
-struct GNUNET_STATISTICS_GetHandle * s_peers;
-struct GNUNET_STATISTICS_GetHandle * s_mechs;
-struct GNUNET_STATISTICS_GetHandle * s_duration;
-struct GNUNET_STATISTICS_GetHandle * s_invalid;
+static void 
+create_ats_information (struct ATS_peer **p, int * c_p,
+                       struct ATS_mechanism ** m, int * c_m)
+{ 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      "ATS needs addresses\n");
 
-/**
- * Check whether peers successfully shut down.
- */
-void
-shutdown_callback (void *cls, const char *emsg)
-{
-  if (emsg != NULL)
-    {
-#if VERBOSE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
-#endif
-      if (ok == 0)
-        ok = 666;
-    }
-  else
-    {
-#if VERBOSE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "All peers successfully shut down!\n");
-       if (stats != NULL)
-               GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
-       stats = NULL;
-#endif
-    }
+ (*p) = tc->p_head;
+ (*c_p) = tc->mechanisms;
+ (*m) = tc->m_head;
+ (*c_m) = tc->mechanisms;
 }
 
-static void shutdown_peers()
-{
-       if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
-       {
-               GNUNET_SCHEDULER_cancel(shutdown_task);
-               shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-       }
-       if (stats_task != GNUNET_SCHEDULER_NO_TASK)
-       {
-               GNUNET_SCHEDULER_cancel(stats_task);
-               stats_task = GNUNET_SCHEDULER_NO_TASK;
-       }
-
-       if (s_time != NULL)
-       {
-               GNUNET_STATISTICS_get_cancel(s_time);
-               s_time = NULL;
-       }
-       if (s_peers != NULL)
-       {
-               GNUNET_STATISTICS_get_cancel(s_peers);
-               s_peers = NULL;
-       }
-       if (s_mechs != NULL)
-       {
-               GNUNET_STATISTICS_get_cancel(s_mechs);
-               s_mechs = NULL;
-       }
-       if (s_solution != NULL)
-       {
-               GNUNET_STATISTICS_get_cancel(s_solution);
-               s_solution = NULL;
-       }
-       if (s_duration != NULL)
-       {
-               GNUNET_STATISTICS_get_cancel(s_duration);
-               s_duration = NULL;
-       }
-
-    GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
-}
 
-static void evaluate_measurements()
+static
+int run_ats ()
 {
-       int c;
-       char * output = NULL;
-       char * temp;
-       double average;
-       double stddev;
-       double measure = MEASUREMENTS;
-       for (c=0; c<MEASUREMENTS;c++)
-       {
-               average += (double) results[c].duration;
-               GNUNET_asprintf(&temp, "%sm%i,%llu,%llu,%llu,%llu,", (output==NULL) ? "" : output, c, results[c].peers, results[c].mechs, results[c].duration, results[c].solution);
-               GNUNET_free_non_null (output);
-               output = temp;
-       }
-       average /= measure;
-
-       for (c=0; c<MEASUREMENTS;c++)
-       {
-               stddev += (results[c].duration - average) * (results[c].duration - average);
-       }
-       stddev /= measure;
-       stddev = sqrt (stddev);
-
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"%savg,%f,stddev,%f\n",output,average,stddev);
-       /* only log benchmark time for 10 peers */
-       if (results[MEASUREMENTS-1].peers == (10))
-               {
-                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send data to gauger: %f \n", average);
-                       GAUGER ("TRANSPORT", "ATS execution time 10 peers", average , "ms");
-               }
-       shutdown_peers();
+  int ret = 0;
+  ats_calculate_bandwidth_distribution (ats);
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Running ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
+  return ret;
 }
 
-int stats_cb (void *cls,
-                          const char *subsystem,
-                          const char *name,
-                          uint64_t value,
-                          int is_persistent)
-{
 
-       if (0 == strcmp (name,"ATS invalid solutions"))
-       {
-               if (stats_task != GNUNET_SCHEDULER_NO_TASK)
-               {
-                       GNUNET_SCHEDULER_cancel(stats_task);
-                       stats_task = GNUNET_SCHEDULER_NO_TASK;
-               }
-               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MLP produced invalid %llu result(s)!\n", value);
-               shutdown_peers();
-               return GNUNET_SYSERR;
-       }
-
-
-       if (0 == strcmp (name,"ATS solution"))
-       {
-               s_solution = NULL;
-       }
-
-       if (0 == strcmp (name,"ATS peers"))
-       {
-               s_peers = NULL;
-       }
-
-       if (0 == strcmp (name,"ATS mechanisms"))
-       {
-               s_mechs = NULL;
-       }
-
-       if (0 == strcmp (name,"ATS duration"))
-       {
-               s_duration = NULL;
-       }
-       if (0 == strcmp (name,"ATS timestamp"))
-       {
-               s_time = NULL;
-       }
-
-    if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) && (value == peers-1))
-    {
-               measurement_started = GNUNET_YES;
-               r_index = 0;
-               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
-    }
-    if (measurement_started == GNUNET_YES)
-    {
-               // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
-               if (0 == strcmp (name,"ATS timestamp"))
-               {
-                       if (results[r_index].timestamp == 0)
-                               results[r_index].timestamp = value;
-                       if (results[r_index].timestamp != value)
-                       {
-                               r_index++;
-                               fprintf(stderr, "(%i/%i)", r_index, MEASUREMENTS);
-                               if (r_index >= MEASUREMENTS)
-                               {
-                                       fprintf(stderr, "\n");
-                                       if (stats_task != GNUNET_SCHEDULER_NO_TASK)
-                                       {
-                                               GNUNET_SCHEDULER_cancel(stats_task);
-                                               stats_task = GNUNET_SCHEDULER_NO_TASK;
-                                       }
-                                       evaluate_measurements();
-                                       return GNUNET_NO;
-                               }
-                               fprintf(stderr, "..");
-
-                               results[r_index].timestamp = value;
-                               return GNUNET_SYSERR;
-                       }
-                       //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS solution: %s %llu \n", r_index, name, value);
-               }
-
-               if (0 == strcmp (name,"ATS solution"))
-               {
-                       results[r_index].solution = value;
-                       //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS solution: %s %llu \n", r_index, name, value);
-               }
-
-               if (0 == strcmp (name,"ATS peers"))
-               {
-                       results[r_index].peers = value;
-                       // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS peers: %s %llu \n", r_index, name, value);
-               }
-
-               if (0 == strcmp (name,"ATS mechanisms"))
-               {
-                       results[r_index].mechs = value;
-                       //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS mechanisms: %s %llu \n", r_index, name, value);
-               }
-
-               if (0 == strcmp (name,"ATS duration"))
-               {
-                       results[r_index].duration = value;
-                       // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS duration: %s %llu \n", r_index, name, value);
-               }
-    }
-    return GNUNET_OK;
+static int
+init_ats ()
+{
+  int ret = 0;
+  ats = ats_init(1.0, 1.0, 1.0, 50000, 5, 10, ATS_MAX_EXEC_DURATION,
+                create_ats_information,
+                ats_result_cb);
+  //GNUNET_assert (ats != NULL);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Initializing ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
+  return ret;
 }
 
 
-void
-stats_get_task (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
+static int 
+shutdown_ats ()
 {
-       stats_task = GNUNET_SCHEDULER_NO_TASK;
-       if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
-           return;
-
-       s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL, &stats_cb, NULL);
-       s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL, &stats_cb, NULL);
-       s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
-       s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
-       s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);
-       s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions", TIMEOUT, NULL, &stats_cb, NULL);
-
-
-       stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250), &stats_get_task, NULL);
+  int ret = 0;
+  ats_delete_problem (ats);
+  ats_shutdown (ats);
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Shutdown ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
+  return ret;
 }
 
-void
-delay (void *cls,
-                         const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-       shutdown_task = GNUNET_SCHEDULER_NO_TASK;
-       if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
-           return;
-
-#if VERBOSE
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
-#endif
-       shutdown_peers ();
-}
 
-static void connect_peers()
+/* To make compiler happy */
+void 
+dummy()
 {
-    shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
-
+  struct ATS_quality_metric * q = qm;
+  q = NULL;
+  struct ATS_ressource * r = ressources;
+  r = NULL;
+  q++; 
+  r++;
 }
 
-void daemon_connect_cb(void *cls,
-                                               const struct GNUNET_PeerIdentity *first,
-                                               const struct GNUNET_PeerIdentity *second,
-                                               uint32_t distance,
-                                               const struct GNUNET_CONFIGURATION_Handle *first_cfg,
-                                               const struct GNUNET_CONFIGURATION_Handle *second_cfg,
-                                               struct GNUNET_TESTING_Daemon *first_daemon,
-                                               struct GNUNET_TESTING_Daemon *second_daemon,
-                                               const char *emsg)
+
+static void 
+iterate_peer_values (void *cls,
+                    const char *section,
+                    const char *option,
+                    const char *value)
 {
-       char * firstc =  strdup(GNUNET_i2s(first));
-       char * secondc =  strdup(GNUNET_i2s(second));
-       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s'\n", firstc, secondc);
-       GNUNET_free(firstc);
-       GNUNET_free(secondc);
+  if (strcmp (option, "f") == 0)
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "\t %s %s\n", option, value);
 }
 
-void cont_cb (void *cls, int success)
+static void
+iterate_mech_values (void *cls,
+                    const char *section,
+                    const char *option,
+                    const char *value)
 {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "STATS cont_cb: %i\n", success);
+  if (strcmp (option, "f") == 0)
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "\t %s %s\n", option, value);
 }
 
-static void
-daemon_start_cb (void *cls,
-       const struct GNUNET_PeerIdentity *id,
-       const struct GNUNET_CONFIGURATION_Handle *cfg,
-       struct GNUNET_TESTING_Daemon *d, const char *emsg)
+static void 
+iterate_sections (void *cls,
+                 const char *section)
 {
-  if (id == NULL)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Start callback called with error (too long starting peers), aborting test!\n");
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
-      failed_peers++;
-      if (failed_peers == peers_left)
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "Too many peers failed, ending test!\n");
-          ok = 1;
-       shutdown_peers ();
-        }
-      return;
-    }
-  peers_left--;
-
-  if (master_deamon == NULL)
-  {
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Master peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
-
-         master_deamon = d;
-         stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
-         GNUNET_assert (stats != NULL);
-         stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
-  }
-  else
-  {
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer `%s'\n", GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
-         GNUNET_TESTING_daemons_connect(d, master_deamon, TIMEOUT, 0, GNUNET_YES,&daemon_connect_cb, NULL);
-  }
-
-  if (peers_left == 0)
+  struct TransportConfiguration * tc = cls;
+  /* Peer definition */
+  if (99 == strlen(section))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "All peers started successfully!\n");
-      connect_peers();
-      ok = 0;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer '%s`\n", section);
+      GNUNET_HashCode h;
+      int res =GNUNET_CRYPTO_hash_from_string(section, &h);
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "HASH '%s` %i\n", GNUNET_h2s (&h), res);
+      GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_peer_values, NULL);
+      tc->peers++;
     }
-  else if (failed_peers == peers_left)
+  if (10 == strlen(section))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Too many peers failed, ending test!\n");
-      shutdown_peers();
-      ok = 1;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Mechanism '%s`\n",section);
+      GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_mech_values, NULL);
+      tc->peers++;
     }
 }
 
 
-static void
-run (void *cls,
-     char *const *args,
-     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
+static struct TransportConfiguration * 
+load_transport_configuration (char * filename)
 {
-  ok = 1;
-  measurement_started = GNUNET_NO;
-#if VERBOSE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
-#endif
-  peers_left = peers;
-  pg = GNUNET_TESTING_daemons_start (cfg,
-                                     peers_left, /* Total number of peers */
-                                     peers_left, /* Number of outstanding connections */
-                                     peers_left, /* Number of parallel ssh connections, or peers being started at once */
-                                     TIMEOUT,
-                                     NULL, NULL,
-                                     &daemon_start_cb, NULL, NULL, NULL, NULL);
-  GNUNET_assert (pg != NULL);
+  struct TransportConfiguration * ret = GNUNET_malloc(sizeof (struct TransportConfiguration));
+
+  cfg = GNUNET_CONFIGURATION_create();
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CONFIGURATION_load(cfg, filename));
+  GNUNET_CONFIGURATION_iterate_sections (cfg, iterate_sections, ret);
+  GNUNET_CONFIGURATION_destroy (cfg);
+  cfg = NULL;
+  return ret;
 }
 
-static int
-check ()
-{
-  char *const argv[] = { "test-testing",
-    "-c",
-    "test_transport_ats.conf",
-#if VERBOSE
-    "-L", "DEBUG",
-#endif
-    NULL
-  };
-  struct GNUNET_GETOPT_CommandLineOption options[] = {
-    GNUNET_GETOPT_OPTION_END
-  };
-  GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
-                      argv, "test-transport-ats", "nohelp",
-                      options, &run, &ok);
-  return ok;
-}
 
 int
 main (int argc, char *argv[])
 {
-  int ret;
+  int ret = 0;
 
   GNUNET_log_setup ("test-transport-ats",
 #if VERBOSE
                     "DEBUG",
 #else
-                    "WARNING",
+                    "INFO",
 #endif
                     NULL);
-  GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
-
-  peers = NUM_PEERS;
-  if (argc >= 2)
-  {
-         peers = atoi(argv[1]);
-         peers++;
-         if(peers <1)
-                 peers = NUM_PEERS;
-  }
-  ret = check ();
-  /**
-   * Still need to remove the base testing directory here,
-   * because group starts will create subdirectories under this
-   * main dir.  However, we no longer need to sleep, as the
-   * shutdown sequence won't return until everything is cleaned
-   * up.
-   */
-  GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
+  tc = load_transport_configuration ("test.ats");
+  ats = NULL;
+  ret += init_ats ();
+  ret += run_ats ();
+  ret += shutdown_ats ();
   return ret;
+
 }
 
 /* end of test_transport_ats.c*/