regex profiler fixes
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling.c
index 892186cd1238e1f0bcf9bbd1d63d903f343b498d..750bae6c2f31174671c4d8c38d52808b4aef144d 100644 (file)
  */
 #include "platform.h"
 #include "gnunet_ats_service.h"
+#include "gnunet_testing_lib-new.h"
 #include "ats.h"
 
-#define VERBOSE GNUNET_NO
-
-#define VERBOSE_ARM GNUNET_NO
-
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
 
 static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 static struct GNUNET_ATS_SchedulingHandle *ats;
 
-struct GNUNET_OS_Process *arm_proc;
-
-
-
 static int ret;
 
 struct Address
@@ -71,19 +64,11 @@ struct PeerContext
   struct Address *addr;
 };
 
-struct Address addr[2];
-struct PeerContext p[2];
-struct GNUNET_ATS_Information atsi[2];
+static struct Address test_addr[2];
 
-static void
-stop_arm ()
-{
-  if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
-  GNUNET_OS_process_wait (arm_proc);
-  GNUNET_OS_process_close (arm_proc);
-  arm_proc = NULL;
-}
+static struct PeerContext p[2];
+
+static struct GNUNET_ATS_Information atsi[2];
 
 
 static void
@@ -92,10 +77,7 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   die_task = GNUNET_SCHEDULER_NO_TASK;
   if (ats != NULL)
     GNUNET_ATS_scheduling_done (ats);
-
   ret = GNUNET_SYSERR;
-
-  stop_arm ();
 }
 
 
@@ -110,10 +92,7 @@ end ()
   }
 
   GNUNET_ATS_scheduling_done (ats);
-
   ret = 0;
-
-  stop_arm ();
 }
 
 
@@ -131,59 +110,36 @@ address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
   GNUNET_assert (0 ==
                  memcmp (&address->peer, &p[0].id,
                          sizeof (struct GNUNET_PeerIdentity)));
-  GNUNET_assert (0 == strcmp (address->transport_name, addr[0].plugin));
-  GNUNET_assert (address->address_length == addr[0].addr_len);
+  GNUNET_assert (0 == strcmp (address->transport_name, test_addr[0].plugin));
+  GNUNET_assert (address->address_length == test_addr[0].addr_len);
   GNUNET_assert (0 ==
-                 memcmp (address->address, addr[0].plugin,
+                 memcmp (address->address, test_addr[0].plugin,
                          address->address_length));
-  GNUNET_assert (addr[0].session == session);
-
-
-  /* TODO ats merge
-   * GNUNET_assert (ats_count == 2);
-   * GNUNET_assert (atsi[0].type == htons (1));
-   * GNUNET_assert (atsi[0].type == htons (2));
-   * GNUNET_assert (atsi[1].type == htons (2));
-   * GNUNET_assert (atsi[1].type == htons (2));
-   */
+  GNUNET_assert (test_addr[0].session == session);
 
   ret = 0;
 
   GNUNET_SCHEDULER_add_now (&end, NULL);
 }
 
-void
-start_arm (const char *cfgname)
-{
-  arm_proc =
-    GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
-                               "gnunet-service-arm",
-#if VERBOSE_ARM
-                               "-L", "DEBUG",
-#endif
-                               "-c", cfgname, NULL);
-}
 
 static void
-check (void *cls, char *const *args, const char *cfgfile,
-       const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls, 
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_TESTING_Peer *peer)
 {
   struct GNUNET_HELLO_Address address0;
 
   ret = GNUNET_SYSERR;
 
   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
-  start_arm (cfgfile);
-
   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
-
   if (ats == NULL)
   {
     ret = GNUNET_SYSERR;
     end ();
     return;
   }
-
   /* set up peer */
   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
                                     &p[0].id.hashPubKey);
@@ -195,25 +151,26 @@ check (void *cls, char *const *args, const char *cfgfile,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
               GNUNET_i2s (&p[1].id));
 
-  addr[0].plugin = "test";
-  addr[0].session = NULL;
-  addr[0].addr = GNUNET_strdup ("test");
-  addr[0].addr_len = 4;
+  test_addr[0].plugin = "test";
+  test_addr[0].session = NULL;
+  test_addr[0].addr = GNUNET_strdup ("test");
+  test_addr[0].addr_len = 4;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing address creation\n");
 
   address0.peer = p[0].id;
-  address0.transport_name = addr[0].plugin;
-  address0.address = addr[0].addr;
-  address0.address_length = addr[0].addr_len;
-  GNUNET_ATS_address_update (ats, &address0, addr[0].session, NULL, 0);
+  address0.transport_name = test_addr[0].plugin;
+  address0.address = test_addr[0].addr;
+  address0.address_length = test_addr[0].addr_len;
+  GNUNET_ATS_address_add (ats, &address0, test_addr[0].session, NULL, 0);
+  GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, NULL, 0);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info creation\n");
 
   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
   atsi[0].value = htonl (1024);
 
-  GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 1);
+  GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, atsi, 1);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info update\n");
 
@@ -223,41 +180,21 @@ check (void *cls, char *const *args, const char *cfgfile,
   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
   atsi[1].value = htonl (1024);
 
-  GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 2);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing manual address deletion \n");
-  address0.peer = p[1].id;      // FIXME: why? typo in old code?
-  GNUNET_ATS_address_update (ats, &address0, addr[0].session, NULL, 0);
-  GNUNET_ATS_address_destroyed (ats, &address0, addr[0].session);
+  GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, atsi, 2);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
               GNUNET_i2s (&p[0].id));
   GNUNET_ATS_suggest_address (ats, &p[0].id);
 }
 
+
 int
 main (int argc, char *argv[])
 {
-  static char *const argv2[] = { "test_ats_api_scheduling",
-    "-c",
-    "test_ats_api.conf",
-#if VERBOSE
-    "-L", "DEBUG",
-#else
-    "-L", "WARNING",
-#endif
-    NULL
-  };
-
-  static struct GNUNET_GETOPT_CommandLineOption options[] = {
-    GNUNET_GETOPT_OPTION_END
-  };
-
-  GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
-                      "test_ats_api_scheduling", "nohelp", options, &check,
-                      NULL);
-
-
+  if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling",
+                                   "test_ats_api.conf",
+                                   &run, NULL))
+    return 1;
   return ret;
 }