continue to fix extract result
[oweals/gnunet.git] / src / datastore / test_plugin_datastore.c
index de9e7c2ab6d6fcf2d62bc88d21e247db9b2a6906..9ec0c53a22033a90cc59c5b409d53c376fe0a1e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /*
  * @file test_plugin_datastore.c
@@ -27,6 +27,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_datastore_plugin.h"
+#include "gnunet_testing_lib.h"
 
 /**
  * Number of put operations to perform.
@@ -83,23 +84,71 @@ disk_utilization_change_cb (void *cls, int delta)
 
 
 static void
-gen_key (int i, GNUNET_HashCode * key)
+test (void *cls);
+
+
+/**
+ * Put continuation.
+ *
+ * @param cls closure
+ * @param key key for the item stored
+ * @param size size of the item stored
+ * @param status #GNUNET_OK or #GNUNET_SYSERROR
+ * @param msg error message on error
+ */
+static void
+put_continuation (void *cls,
+                 const struct GNUNET_HashCode *key,
+                  uint32_t size,
+                 int status,
+                 const char *msg)
 {
-  memset (key, 0, sizeof (GNUNET_HashCode));
+  struct CpsRunContext *crc = cls;
+  static unsigned long long os;
+  unsigned long long cs;
+
+  if (GNUNET_OK != status)
+  {
+    FPRINTF (stderr, "ERROR: `%s'\n", msg);
+  }
+  else
+  {
+    crc->api->estimate_size (crc->api->cls, &cs);
+    GNUNET_assert (os <= cs);
+    os = cs;
+    stored_bytes += size;
+    stored_ops++;
+    stored_entries++;
+  }
+  GNUNET_SCHEDULER_add_now (&test, crc);
+}
+
+
+static void
+gen_key (int i, struct GNUNET_HashCode * key)
+{
+  memset (key, 0, sizeof (struct GNUNET_HashCode));
   key->bits[0] = (unsigned int) i;
-  GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
+  GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), key);
 }
 
 
 static void
-put_value (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
+do_put (struct CpsRunContext *crc)
 {
   char value[65536];
   size_t size;
-  GNUNET_HashCode key;
-  char *msg;
+  struct GNUNET_HashCode key;
   unsigned int prio;
+  static int i;
 
+  if (PUT_10 == i)
+  {
+    i = 0;
+    crc->phase++;
+    GNUNET_SCHEDULER_add_now (&test, crc);
+    return;
+  }
   /* most content is 32k */
   size = 32 * 1024;
 
@@ -112,41 +161,30 @@ put_value (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
   memset (value, i, size);
   if (i > 255)
     memset (value, i - 255, size / 2);
-  value[0] = k;
-  msg = NULL;
+  value[0] = crc->i;
   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "putting type %u, anon %u under key %s\n", i + 1, i,
              GNUNET_h2s (&key));
-  if (GNUNET_OK != api->put (api->cls, &key, size, value, i + 1 /* type */ ,
-                             prio, i /* anonymity */ ,
-                             0 /* replication */ ,
-                             GNUNET_TIME_relative_to_absolute
-                             (GNUNET_TIME_relative_multiply
-                              (GNUNET_TIME_UNIT_MILLISECONDS,
-                               60 * 60 * 60 * 1000 +
-                               GNUNET_CRYPTO_random_u32
-                               (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
-  {
-    FPRINTF (stderr, "ERROR: `%s'\n", msg);
-    GNUNET_free_non_null (msg);
-    return;
-  }
-  stored_bytes += size;
-  stored_ops++;
-  stored_entries++;
+  crc->api->put (crc->api->cls, &key, size, value, i + 1 /* type */ ,
+                 prio, i /* anonymity */ ,
+                 0 /* replication */ ,
+                 GNUNET_TIME_relative_to_absolute
+                   (GNUNET_TIME_relative_multiply
+                     (GNUNET_TIME_UNIT_MILLISECONDS,
+                      60 * 60 * 60 * 1000 +
+                      GNUNET_CRYPTO_random_u32
+                      (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
+                 put_continuation, crc);
+  i++;
 }
 
 
-static void
-test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
 static uint64_t guid;
 
 
 static int
-iterate_one_shot (void *cls, const GNUNET_HashCode * key, uint32_t size,
+iterate_one_shot (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
                   const void *data, enum GNUNET_BLOCK_Type type,
                   uint32_t priority, uint32_t anonymity,
                   struct GNUNET_TIME_Absolute expiration, uint64_t uid)
@@ -157,8 +195,9 @@ iterate_one_shot (void *cls, const GNUNET_HashCode * key, uint32_t size,
   guid = uid;
   crc->phase++;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n",
-             type, priority, size, (unsigned long long) expiration.abs_value,
+             "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
+             type, priority, size,
+             GNUNET_STRINGS_absolute_time_to_string (expiration),
              GNUNET_h2s (key));
   GNUNET_SCHEDULER_add_now (&test, crc);
   return GNUNET_OK;
@@ -201,7 +240,7 @@ unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
  * the transport and core.
  */
 static void
-cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleaning_task (void *cls)
 {
   struct CpsRunContext *crc = cls;
 
@@ -211,20 +250,25 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 static void
-test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+update_continuation (void *cls,
+                    int status,
+                    const char *msg)
 {
   struct CpsRunContext *crc = cls;
-  int j;
-  unsigned long long os;
-  unsigned long long cs;
-  GNUNET_HashCode key;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test aborted.\n");
-    crc->phase = RP_ERROR;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_assert (GNUNET_OK == status);
+  crc->phase++;
+  GNUNET_SCHEDULER_add_now (&test, crc);
+}
+
+
+static void
+test (void *cls)
+{
+  struct CpsRunContext *crc = cls;
+  struct GNUNET_HashCode key;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "In phase %d, iteration %u\n", crc->phase, crc->cnt);
   switch (crc->phase)
   {
@@ -235,16 +279,7 @@ test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
     break;
   case RP_PUT:
-    os = 0;
-    for (j = 0; j < PUT_10; j++)
-    {
-      put_value (crc->api, j, crc->i);
-      cs = crc->api->estimate_size (crc->api->cls);
-      GNUNET_assert (os <= cs);
-      os = cs;
-    }
-    crc->phase++;
-    GNUNET_SCHEDULER_add_now (&test, crc);
+    do_put (crc);
     break;
   case RP_GET:
     if (crc->cnt == 1)
@@ -259,11 +294,12 @@ test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                        GNUNET_BLOCK_TYPE_ANY, &iterate_one_shot, crc);
     break;
   case RP_UPDATE:
-    GNUNET_assert (GNUNET_OK ==
-                   crc->api->update (crc->api->cls, guid, 1,
-                                     GNUNET_TIME_UNIT_ZERO_ABS, NULL));
-    crc->phase++;
-    GNUNET_SCHEDULER_add_now (&test, crc);
+    crc->api->update (crc->api->cls,
+                     guid,
+                     1,
+                     GNUNET_TIME_UNIT_ZERO_ABS,
+                      &update_continuation,
+                     crc);
     break;
 
   case RP_ITER_ZERO:
@@ -302,11 +338,14 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
   char *libname;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
+      GNUNET_CONFIGURATION_get_value_string (cfg,
+                                            "DATASTORE",
+                                            "DATABASE",
                                              &name))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
+                _("No `%s' specified for `%s' in configuration!\n"),
+               "DATABASE",
                 "DATASTORE");
     return NULL;
   }
@@ -319,6 +358,9 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
   {
     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
+    GNUNET_free (libname);
+    GNUNET_free (name);
+    ok = 77; /* mark test as skipped */
     return NULL;
   }
   GNUNET_free (libname);
@@ -341,7 +383,7 @@ run (void *cls, char *const *args, const char *cfgfile,
              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
     return;
   }
-  crc = GNUNET_malloc (sizeof (struct CpsRunContext));
+  crc = GNUNET_new (struct CpsRunContext);
   crc->api = api;
   crc->cfg = c;
   crc->phase = RP_PUT;
@@ -349,11 +391,9 @@ run (void *cls, char *const *args, const char *cfgfile,
 }
 
 
-
 int
 main (int argc, char *argv[])
 {
-  char *pos;
   char dir_name[128];
   char cfg_name[128];
   char *const xargv[] = {
@@ -367,14 +407,7 @@ main (int argc, char *argv[])
   };
 
   /* determine name of plugin to use */
-  plugin_name = argv[0];
-  while (NULL != (pos = strstr (plugin_name, "_")))
-    plugin_name = pos + 1;
-  if (NULL != (pos = strstr (plugin_name, ".")))
-    pos[0] = 0;
-  else
-    pos = (char *) plugin_name;
-
+  plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
   GNUNET_snprintf (dir_name, sizeof (dir_name),
                    "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
   GNUNET_DISK_directory_remove (dir_name);
@@ -385,10 +418,8 @@ main (int argc, char *argv[])
                    "test_plugin_datastore_data_%s.conf", plugin_name);
   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
                       "test-plugin-datastore", "nohelp", options, &run, NULL);
-  if (ok != 0)
+  if ( (0 != ok) && (77 != ok) )
     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
-  if (pos != plugin_name)
-    pos[0] = '.';
   GNUNET_DISK_directory_remove (dir_name);
   return ok;
 }