fix break
[oweals/gnunet.git] / src / ats / test_ats_api_performance.c
index 4d736cec58b85e16ab0a46e817aff6f674096e98..99231aa89e4869b509e23b84ac619138369037da 100644 (file)
@@ -34,7 +34,7 @@ static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 struct GNUNET_CONFIGURATION_Handle *cfg;
 
-static struct GNUNET_ATS_SchedulingHandle *ats;
+static struct GNUNET_ATS_SchedulingHandle *atsh;
 static struct GNUNET_ATS_PerformanceHandle *ph;
 struct GNUNET_ATS_AddressListHandle* phal;
 
@@ -70,6 +70,7 @@ static struct Address p1_addresses[2];
 
 struct GNUNET_HELLO_Address p0_ha[2];
 struct GNUNET_HELLO_Address p1_ha[2];
+struct GNUNET_HELLO_Address *s_ha[2];
 
 static unsigned int stage = 0;
 
@@ -80,8 +81,8 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout in stage %u\n", stage);
 
-  if (NULL != ats)
-  GNUNET_ATS_scheduling_done (ats);
+  if (NULL != atsh)
+  GNUNET_ATS_scheduling_done (atsh);
   if (phal != NULL)
     GNUNET_ATS_performance_list_addresses_cancel (phal);
   phal = NULL;
@@ -107,8 +108,8 @@ end ()
     GNUNET_SCHEDULER_cancel (die_task);
     die_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != ats)
-  GNUNET_ATS_scheduling_done (ats);
+  if (NULL != atsh)
+  GNUNET_ATS_scheduling_done (atsh);
   if (phal != NULL)
     GNUNET_ATS_performance_list_addresses_cancel (phal);
   phal = NULL;
@@ -127,6 +128,156 @@ end ()
 static void
 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+void all_active_addresses_peer_cb (void *cls,
+                      const struct
+                      GNUNET_HELLO_Address *
+                      address,
+                      struct
+                      GNUNET_BANDWIDTH_Value32NBO
+                      bandwidth_out,
+                      struct
+                      GNUNET_BANDWIDTH_Value32NBO
+                      bandwidth_in,
+                      const struct
+                      GNUNET_ATS_Information *
+                      ats, uint32_t ats_count)
+{
+  static int cb = 0;
+  int fail = GNUNET_NO;
+
+  if (address != NULL)
+  {
+    if (0 == memcmp (&address->peer, &p[0].id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+            "Did not expected callback for peer 0 address `%s', got address `%s'!\n",
+            s_ha[0]->address, address->address);
+        GNUNET_ATS_performance_list_addresses_cancel (phal);
+        fail = GNUNET_YES;
+    }
+
+    if (0 == memcmp (&address->peer, &p[1].id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+        if (0 == strcmp(address->address, s_ha[1]->address))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Callback for peer 1 suggested address %s\n",
+              s_ha[1]->address);
+          cb ++;
+        }
+        else
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Expected callback for peer 1 address `%s', got address `%s'!\n",
+              s_ha[1]->address, address->address);
+          GNUNET_ATS_performance_list_addresses_cancel (phal);
+          fail = GNUNET_YES;
+        }
+    }
+  }
+  if ((address == NULL) || (GNUNET_YES == fail))
+  {
+      phal = NULL;
+      if ((1 == cb) && (GNUNET_NO == fail))
+      {
+        /* Received all addresses + terminator cb, next stage */
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
+        GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
+        return;
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
+        GNUNET_SCHEDULER_add_now (&end, NULL);
+        ret = 5;
+        return;
+      }
+  }
+}
+
+void all_active_addresses_cb (void *cls,
+                      const struct
+                      GNUNET_HELLO_Address *
+                      address,
+                      struct
+                      GNUNET_BANDWIDTH_Value32NBO
+                      bandwidth_out,
+                      struct
+                      GNUNET_BANDWIDTH_Value32NBO
+                      bandwidth_in,
+                      const struct
+                      GNUNET_ATS_Information *
+                      ats, uint32_t ats_count)
+{
+  static int cb = 0;
+  int fail = GNUNET_NO;
+
+  if (address != NULL)
+  {
+    if (0 == memcmp (&address->peer, &p[0].id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+        if (0 == strcmp(address->address, s_ha[0]->address))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Callback for peer 0 suggested address %s\n",
+              s_ha[0]->address);
+          cb ++;
+        }
+        else
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Expected callback for peer 0 address `%s', got address `%s'!\n",
+              s_ha[0]->address, address->address);
+          GNUNET_ATS_performance_list_addresses_cancel (phal);
+          fail = GNUNET_YES;
+        }
+    }
+
+    if (0 == memcmp (&address->peer, &p[1].id,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+        if (0 == strcmp(address->address, s_ha[1]->address))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Callback for peer 1 suggested address %s\n",
+              s_ha[1]->address);
+          cb ++;
+        }
+        else
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Expected callback for peer 1 address `%s', got address `%s'!\n",
+              s_ha[1]->address, address->address);
+          GNUNET_ATS_performance_list_addresses_cancel (phal);
+          fail = GNUNET_YES;
+        }
+    }
+  }
+  if ((address == NULL) || (GNUNET_YES == fail))
+  {
+      phal = NULL;
+      if ((2 == cb) && (GNUNET_NO == fail))
+      {
+        /* Received all addresses + terminator cb, next stage */
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
+        GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
+        return;
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  FAIL\n", stage);
+        GNUNET_SCHEDULER_add_now (&end, NULL);
+        ret = 5;
+        return;
+      }
+  }
+}
+
+
+
 void all_addresses_peer_cb (void *cls,
                       const struct
                       GNUNET_HELLO_Address *
@@ -142,28 +293,30 @@ void all_addresses_peer_cb (void *cls,
                       ats, uint32_t ats_count)
 {
   static int cb = 0;
+  int fail = GNUNET_NO;
 
   if (address != NULL)
   {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'  address `%s'\n",
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          "Callback for peer `%s'  address `%s'\n",
            GNUNET_i2s (&address->peer), address->address);
 
     if (0 != memcmp (&address->peer, &p[1].id,
                      sizeof (struct GNUNET_PeerIdentity)))
     {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i:  Received address for wrong peer\n", stage);
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+            "Stage %i:  Received address for wrong peer\n", stage);
         GNUNET_ATS_performance_list_addresses_cancel (phal);
-        phal = NULL;
-        GNUNET_SCHEDULER_add_now (&end, NULL);
+        fail = GNUNET_YES;
         ret = 4;
-        return;
     }
     cb ++;
   }
-  else
+
+  if ((NULL == address) || (fail))
   {
       phal = NULL;
-      if (2 == cb)
+      if ((2 == cb) && (GNUNET_NO == fail))
       {
         /* Received all addresses + terminator cb, next stage */
         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i:  SUCCESS\n", stage);
@@ -198,13 +351,9 @@ void all_addresses_cb (void *cls,
 
   if (address != NULL)
   {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'  address `%s'\n",
-           GNUNET_i2s (&address->peer), address->address);
-
     if (0 == memcmp (&address->peer, &p[0].id,
                      sizeof (struct GNUNET_PeerIdentity)))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 `%s'\n", GNUNET_i2s (&address->peer));
       if (0 == strcmp(address->address, p0_addresses[0].addr))
       {
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
@@ -217,9 +366,8 @@ void all_addresses_cb (void *cls,
       }
     }
     if (0 == memcmp (&address->peer, &p[1].id,
-                     sizeof (struct GNUNET_PeerIdentity)));
+                     sizeof (struct GNUNET_PeerIdentity)))
     {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 `%s'\n", GNUNET_i2s (&address->peer));
         if (0 == strcmp(address->address, p1_addresses[0].addr))
         {
           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
@@ -264,22 +412,38 @@ test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   stage++;
   switch (stage) {
-    case 1:
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage 1: \n");
+    case 1: /* Get all peers, all addresses */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 1: \n");
       phal = GNUNET_ATS_performance_list_addresses (ph,
                                              NULL,
                                              GNUNET_YES,
                                              &all_addresses_cb, NULL);
       GNUNET_assert (NULL != phal);
       break;
-    case 2:
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage 2: \n");
+    case 2: /* Get specific peer, all addresses */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 2: \n");
       phal = GNUNET_ATS_performance_list_addresses (ph,
                                              &p[1].id,
                                              GNUNET_YES,
                                              &all_addresses_peer_cb, NULL);
       GNUNET_assert (NULL != phal);
       break;
+    case 3: /* Get all peers, active addresses */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 3: \n");
+      phal = GNUNET_ATS_performance_list_addresses (ph,
+                                             NULL,
+                                             GNUNET_NO,
+                                             &all_active_addresses_cb, NULL);
+      GNUNET_assert (NULL != phal);
+      break;
+    case 4: /* Get specific peers, active addresses */
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 4: \n");
+      phal = GNUNET_ATS_performance_list_addresses (ph,
+                                             &p[1].id,
+                                             GNUNET_NO,
+                                             &all_active_addresses_peer_cb, NULL);
+      GNUNET_assert (NULL != phal);
+      break;
     default:
       /* done */
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
@@ -297,22 +461,39 @@ address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
                     const struct GNUNET_ATS_Information *ats,
                     uint32_t ats_count)
 {
-  static int suggest_p0;
-  static int suggest_p1;
+  static int suggest_p0 = GNUNET_NO;
+  static int suggest_p1 = GNUNET_NO;
+  static int running = GNUNET_NO;
+
+  if ((GNUNET_NO == suggest_p0) && (0 == memcmp (&address->peer, &p[0].id,
+                   sizeof (struct GNUNET_PeerIdentity))))
+  {
+    suggest_p0 = GNUNET_YES;;
+
+    if (s_ha[0] != NULL)
+      GNUNET_free (s_ha[0]);
+    s_ha[0] = GNUNET_HELLO_address_copy (address);
+    GNUNET_ATS_suggest_address_cancel (atsh, &p[0].id);
+  }
+  if ((GNUNET_NO == suggest_p1) && (0 == memcmp (&address->peer, &p[1].id,
+                   sizeof (struct GNUNET_PeerIdentity))))
+  {
+    suggest_p1 = GNUNET_YES;
+
+    if (s_ha[1] != NULL)
+      GNUNET_free (s_ha[1]);
+    s_ha[1] = GNUNET_HELLO_address_copy (address);
+    GNUNET_ATS_suggest_address_cancel (atsh, &p[1].id);
+  }
 
-  if (0 == memcmp (&address->peer, &p[0].id,
-                   sizeof (struct GNUNET_PeerIdentity)));
-    suggest_p0++;
-  if (0 == memcmp (&address->peer, &p[1].id,
-                   sizeof (struct GNUNET_PeerIdentity)));
-    suggest_p1++;
 
-  if ((GNUNET_YES >= suggest_p0) && (GNUNET_YES >= suggest_p1))
+  if ((GNUNET_NO == running) && (GNUNET_YES == suggest_p0) && (GNUNET_YES == suggest_p1))
   {
+      running = GNUNET_YES;
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
-      test_performance_api(NULL, NULL);
-      //GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
+      GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
   }
+
 }
 
 
@@ -382,26 +563,23 @@ run (void *cls,
 
 
   /* Add addresses */
-  ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
-  if (ats == NULL)
+  atsh = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
+  if (atsh == NULL)
   {
     ret = GNUNET_SYSERR;
     end ();
     return;
   }
 
-  GNUNET_ATS_address_add (ats, &p0_ha[0], NULL, NULL, 0);
-
-  GNUNET_ATS_address_add (ats, &p0_ha[1], NULL, NULL, 0);
-  GNUNET_ATS_address_in_use (ats, &p0_ha[1], NULL, GNUNET_YES);
+  GNUNET_ATS_address_add (atsh, &p0_ha[0], NULL, NULL, 0);
+  GNUNET_ATS_address_add (atsh, &p0_ha[1], NULL, NULL, 0);
 
-  GNUNET_ATS_address_add (ats, &p1_ha[0], NULL, NULL, 0);
-  GNUNET_ATS_address_in_use (ats, &p1_ha[0], NULL, GNUNET_YES);
+  GNUNET_ATS_address_add (atsh, &p1_ha[0], NULL, NULL, 0);
+  GNUNET_ATS_address_add (atsh, &p1_ha[1], NULL, NULL, 0);
 
-  GNUNET_ATS_address_add (ats, &p1_ha[1], NULL, NULL, 0);
 
-  GNUNET_ATS_suggest_address (ats, &p[0].id);
-  GNUNET_ATS_suggest_address (ats, &p[1].id);
+  GNUNET_ATS_suggest_address (atsh, &p[0].id);
+  GNUNET_ATS_suggest_address (atsh, &p[1].id);
 }