utility functions
authorMatthias Wachs <wachs@net.in.tum.de>
Wed, 31 Aug 2011 15:45:07 +0000 (15:45 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Wed, 31 Aug 2011 15:45:07 +0000 (15:45 +0000)
src/transport/test_transport_api_reliability.c
src/transport/transport-testing.c
src/transport/transport-testing.h

index 55db67877dc2dd5fab383649cdd9d16f14cd4fe1..09d23b78987100f93e4e2d28e4dc445b00c632ab 100644 (file)
@@ -83,6 +83,7 @@ struct TestMessage
 };
 
 static char *test_name;
+static char *test_plugin;
 
 static int msg_scheduled;
 static int msg_sent;
@@ -118,7 +119,7 @@ end ()
   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
   fprintf (stderr, "\nThroughput was %llu kb/s\n",
            total_bytes * 1000 / 1024 / delta);
-  GNUNET_asprintf (&value_name, "reliable_%s", test_name);
+  GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
   GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
           "kb/s");
   GNUNET_free (value_name);
@@ -379,7 +380,7 @@ run (void *cls, char *const *args, const char *cfgfile,
 static int
 check ()
 {
-  static char *const argv[] = { "test-transport-api-reliability",
+  static char * argv[] = { "SDSD",
     "-c",
     "test_transport_api_data.conf",
 #if VERBOSE
@@ -396,7 +397,7 @@ check ()
 #endif
   ok = 1;
   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
-                      "test-transport-api-reliability", "nohelp", options, &run,
+                      test_name, "nohelp", options, &run,
                       &ok);
 
   return ok;
@@ -407,8 +408,12 @@ main (int argc, char *argv[])
 {
   int ret;
   int nat_res;
+  //char * test_exec;
 
-  GNUNET_log_setup ("test-transport-api-reliability",
+  GNUNET_TRANSPORT_TESTING_get_test_sourcename (__FILE__, &test_name);
+
+
+  GNUNET_log_setup (test_name,
 #if VERBOSE
                     "DEBUG",
 #else
@@ -416,37 +421,7 @@ main (int argc, char *argv[])
 #endif
                     NULL);
 
-  char *pch = strdup (argv[0]);
-  char *backup = pch;
-  char *filename = NULL;
-  char *dotexe;
-  char *src_name = strdup (__FILE__);
-  char *split = NULL;
-
-  /* get executable filename */
-  pch = strtok (pch, "/");
-  while (pch != NULL)
-  {
-    pch = strtok (NULL, "/");
-    if (pch != NULL)
-      filename = pch;
-  }
-  /* remove "lt-" */
-  filename = strstr (filename, "tes");
-  if (NULL != (dotexe = strstr (filename, ".exe")))
-    dotexe[0] = '\0';
-
-  split = strstr (src_name, ".");
-  if (split != NULL)
-  {
-    split[0] = '\0';
-    test_name = strdup (&filename[strlen (src_name) + 1]);
-  }
-  else
-    test_name = NULL;
-
-  GNUNET_free (src_name);
-  GNUNET_free (backup);
+  GNUNET_TRANSPORT_TESTING_get_test_plugin (argv[0], test_name, &test_plugin);
 
   if ((strstr (argv[0], "tcp_nat") != NULL) ||
       (strstr (argv[0], "udp_nat") != NULL))
@@ -466,6 +441,7 @@ main (int argc, char *argv[])
     }
   }
 
+
   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
 
@@ -474,7 +450,8 @@ main (int argc, char *argv[])
   GNUNET_free (cfg_file_p1);
   GNUNET_free (cfg_file_p2);
 
-  GNUNET_free_non_null (test_name);
+  GNUNET_free (test_name);
+  GNUNET_free (test_plugin);
 
   return ret;
 }
index 3ee471453ce3ec603c096042b9ecc550fab557cc..f71d7b78bd23e2e2c184c69df21b924326149c61 100644 (file)
@@ -345,6 +345,92 @@ void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
  * Some utility functions
  */
 
+char *
+extract_filename (const char * file)
+{
+  char *pch = strdup (file);
+  char *backup = pch;
+  char *filename = NULL;
+  char *res;
+
+
+  /* get executable filename */
+  if (NULL != strstr (pch, "/"))
+  {
+    pch = strtok (pch, "/");
+    while (pch != NULL)
+    {
+      pch = strtok (NULL, "/");
+      if (pch != NULL)
+        {
+        filename = pch;
+        }
+    }
+  }
+  else
+    filename = pch;
+  res = strdup(filename);
+  GNUNET_free (backup);
+
+  return res;
+}
+
+void
+GNUNET_TRANSPORT_TESTING_get_test_sourcename (const char * file, char **testname)
+{
+  char * src = extract_filename (file);
+  char * split;
+
+  split = strstr (src, ".");
+  if (split != NULL)
+  {
+    split[0] = '\0';
+  }
+  GNUNET_asprintf(testname, "%s", src);
+  GNUNET_free (src);
+}
+
+void
+GNUNET_TRANSPORT_TESTING_get_test_plugin (const char * executable, const char * testname, char **pluginname)
+{
+  char *exec = extract_filename (executable);
+  char *test = extract_filename (testname);
+
+  char *backup_t = test;
+  char *filename = NULL;
+  char *dotexe;
+
+  if (exec == NULL)
+    goto fail;
+
+  /* remove "lt-" */
+  filename = strstr (exec, "tes");
+  if (filename == NULL)
+    goto fail;
+
+  /* remove ".exe" */
+  if (NULL != (dotexe = strstr (filename, ".exe")))
+      dotexe[0] = '\0';
+
+  /* find last _ */
+  filename = strstr (filename, test);
+  if (filename == NULL)
+    goto fail;
+
+  /* copy plugin */
+  filename += strlen (test);
+  filename++;
+  GNUNET_asprintf (pluginname, "%s", filename);
+  goto suc;
+
+fail:
+  (*pluginname) = NULL;
+suc:
+  GNUNET_free (backup_t);
+  GNUNET_free (exec);
+
+}
+
 /**
  * this function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
  * if existing ".exe"-prefix and adds the peer-number
@@ -356,30 +442,39 @@ void
 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **cfgname,
                                           int count)
 {
-  char *pch = strdup (file);
-  char *backup = pch;
-  char *filename = NULL;
+  char *filename = extract_filename (file);
+  char *backup = filename;
   char *dotexe;
 
 
-  /* get executable filename */
-  pch = strtok (pch, "/");
-  while (pch != NULL)
-  {
-    pch = strtok (NULL, "/");
-    if (pch != NULL)
-      filename = pch;
-  }
+  if (filename == NULL)
+    goto fail;
+
   /* remove "lt-" */
   filename = strstr (filename, "tes");
+  if (filename == NULL)
+    goto fail;
+
+  /* remove ".exe" */
   if (NULL != (dotexe = strstr (filename, ".exe")))
-    dotexe[0] = '\0';
+      dotexe[0] = '\0';
+
+  if (filename == NULL)
+    goto fail;
+
+  /* copy plugin */
+  goto suc;
 
+fail:
+   (*cfgname) = NULL;
+   return;
+
+suc:
   /* create cfg filename */
   GNUNET_asprintf (cfgname, "%s_peer%u.conf", filename, count);
-
   GNUNET_free (backup);
 }
 
 
+
 /* end of transport_testing.h */
index 7899279dcd1be338c455b2be5b92139c53bf9be6..96fe216e98dbe537a6d3997fdfeb3563082ad39b 100644 (file)
@@ -133,4 +133,10 @@ void
 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **cfgname,
                                           int count);
 
+void
+GNUNET_TRANSPORT_TESTING_get_test_plugin (const char * executable, const char * testname, char **pluginname);
+
+void
+GNUNET_TRANSPORT_TESTING_get_test_sourcename (const char * file, char **testname);
+
 /* end of transport_testing.h */