fix for size
[oweals/gnunet.git] / src / namestore / test_plugin_namestore.c
index 6b606e62bc68cd77d844b1b8b6bc3f3778158370..21aaa0f7d932327e3356db53ee57e9e0fbb2f1cf 100644 (file)
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_namestore_plugin.h"
+#include "gnunet_testing_lib.h"
 
-#define VERBOSE GNUNET_EXTRA_LOGGING
-
-#define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
 
 static int ok;
 
@@ -73,6 +71,7 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
   {
     FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
+    GNUNET_free (libname);
     return NULL;
   }
   GNUNET_free (libname);
@@ -80,286 +79,70 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
 }
 
 
-/**
- * Function called by for each matching record.
- *
- * @param cls closure
- * @param zone hash of the public key of the zone
- * @param loc location of the signature for this record
- * @param name name that is being mapped (at most 255 characters long)
- * @param record_type type of the record (A, AAAA, PKEY, etc.)
- * @param expiration expiration time for the content
- * @param flags flags for the content
- * @param data_size number of bytes in data
- * @param data value, semantics depend on 'record_type' (see RFCs for DNS and 
- *             GNS specification for GNS extensions) 
- */
-static void 
-test_record (void *cls,
-            const GNUNET_HashCode *zone,
-            const struct GNUNET_NAMESTORE_SignatureLocation *loc,
-            const char *name,
-            uint32_t record_type,
-            struct GNUNET_TIME_Absolute expiration,
-            enum GNUNET_NAMESTORE_RecordFlags flags,
-            size_t data_size,
-            const void *data)
-{
-  int *idp = cls;
-  int id = *idp;
-  size_t tdata_size = id * 17;
-  char tdata[tdata_size];
-  GNUNET_HashCode tzone;
-  char tname[32];
-  uint32_t trecord_type;
-  struct GNUNET_NAMESTORE_SignatureLocation tloc;
-  enum GNUNET_NAMESTORE_RecordFlags tflags;               
-  
-  memset (&tzone, 42, sizeof (tzone));
-  memset (tdata, id % 255, sizeof (tdata));
-  GNUNET_snprintf (tname, sizeof (tname),
-                  "aa%u", (unsigned int) id);
-  trecord_type = id % 4;
-  tloc.depth = (id % 10);
-  tloc.offset = (id % 3);
-  tloc.revision = id % 1024;
-  tflags = GNUNET_NAMESTORE_RF_AUTHORITY;
-  
-  GNUNET_assert (0 == memcmp (&tzone, zone, sizeof (GNUNET_HashCode)));
-  GNUNET_assert (trecord_type == record_type);
-  GNUNET_assert (0 == strcmp (tname, name));
-  GNUNET_assert (tdata_size == data_size);
-  GNUNET_assert (0 == memcmp (data, tdata, data_size));
-  GNUNET_assert (flags == tflags);
-  GNUNET_assert (0 == memcmp (loc, &tloc, sizeof (struct GNUNET_NAMESTORE_SignatureLocation)));
-}
-
-
-static void
-get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
-{
-  GNUNET_HashCode zone;
-  GNUNET_HashCode nh;
-  char name[32];
-  
-  memset (&zone, 42, sizeof (zone));
-  GNUNET_snprintf (name, sizeof (name),
-                  "aa%u", (unsigned int) id);
-  GNUNET_CRYPTO_hash (name, strlen (name), &nh);
-  GNUNET_assert (1 == nsp->iterate_records (nsp->cls,
-                                           &zone,
-                                           &nh,
-                                           &test_record, &id));
-}
-
-
-/**
- * Function called with the matching node.
- *
- * @param cls closure
- * @param zone hash of public key of the zone
- * @param loc location in the B-tree
- * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root
- * @param num_entries number of entries at this node in the B-tree
- * @param entries the 'num_entries' entries to store (hashes over the
- *                records)
- */
 static void
-test_node (void *cls,
-          const GNUNET_HashCode *zone,
-          const struct GNUNET_NAMESTORE_SignatureLocation *loc,
-          const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
-          unsigned int num_entries,
-          const GNUNET_HashCode *entries)
+test_record (void *cls,
+                                                const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
+                                                const char *label,
+                                                unsigned int rd_count,
+                                                const struct GNUNET_GNSRECORD_Data *rd)
 {
   int *idp = cls;
   int id = *idp;
-  struct GNUNET_NAMESTORE_SignatureLocation tloc;
-  struct GNUNET_NAMESTORE_SignatureLocation tploc;
-  unsigned int tnum_entries = 1 + (id % 15);
-  GNUNET_HashCode tentries[num_entries];
+  struct GNUNET_CRYPTO_EcdsaPrivateKey tzone_private_key;
+  char tname[64];
+  unsigned int trd_count = 1 + (id % 1024);
   unsigned int i;
 
-  tloc.depth = (id % 10);
-  tloc.offset = (id % 3);
-  tloc.revision = id % 1024;
-  tploc.depth = tloc.depth + 1;
-  tploc.offset = (id % 5);
-  tploc.revision = tloc.revision;
-  for (i=0;i<tnum_entries;i++)
-    memset (&tentries[i], (id+i) % 255, sizeof (GNUNET_HashCode));
-  GNUNET_assert (0 == memcmp (&tloc, loc, 
-                             sizeof (const struct GNUNET_NAMESTORE_SignatureLocation)));
-  GNUNET_assert (0 == memcmp (&tploc, ploc, 
-                             sizeof (const struct GNUNET_NAMESTORE_SignatureLocation)));
-  GNUNET_assert (num_entries == tnum_entries);
-  GNUNET_assert (0 == memcmp (entries, tentries, sizeof (tentries)));
-}
-
-
-static void
-get_node (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
-{
-  GNUNET_HashCode zone;
-  struct GNUNET_NAMESTORE_SignatureLocation loc;
-
-  memset (&zone, 42, sizeof (zone));
-  loc.depth = (id % 10);
-  loc.offset = (id % 3);
-  loc.revision = id % 1024;
-  GNUNET_assert (GNUNET_OK ==
-                nsp->get_node (nsp->cls,
-                               &zone,
-                               &loc,
-                               &test_node,
-                               &id));
-}
-
-
-/**
- * Function called with the matching signature.
- *
- * @param cls closure
- * @param zone public key of the zone
- * @param loc location of the root in the B-tree (depth, revision)
- * @param top_sig signature signing the zone
- * @param zone_time time the signature was created
- * @param root_hash top level hash that is being signed
- */
-static void
-test_signature (void *cls,
-               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
-               const struct GNUNET_NAMESTORE_SignatureLocation *loc,
-               const struct GNUNET_CRYPTO_RsaSignature *top_sig,
-               struct GNUNET_TIME_Absolute zone_time,
-               const GNUNET_HashCode *root_hash)
-{
-  int *idp = cls;
-  int id = *idp;
-  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded tzone_key;
-  struct GNUNET_NAMESTORE_SignatureLocation tloc;
-  struct GNUNET_CRYPTO_RsaSignature ttop_sig;
-  GNUNET_HashCode troot_hash;
-
-  memset (&tzone_key, 13, sizeof (tzone_key));
-  tloc.depth = (id % 10);
-  tloc.offset = (id % 3);
-  tloc.revision = id % 1024;
-  memset (&ttop_sig, 24, sizeof (ttop_sig));
-  memset (&troot_hash, 42, sizeof (troot_hash));
-
-  GNUNET_assert (0 == memcmp (&tzone_key, zone_key, sizeof (GNUNET_HashCode)));
-  GNUNET_assert (0 == memcmp (&tloc, loc, sizeof (struct GNUNET_NAMESTORE_SignatureLocation)));
-  GNUNET_assert (0 == memcmp (&ttop_sig, top_sig, sizeof (struct GNUNET_CRYPTO_RsaSignature)));
-  GNUNET_assert (0 == memcmp (&troot_hash, root_hash, sizeof (GNUNET_HashCode)));
+  GNUNET_snprintf (tname, sizeof (tname),
+                  "a%u", (unsigned int ) id);
+  for (i=0;i<trd_count;i++)
+  {
+    GNUNET_assert (rd[i].data_size == id % 10);
+    GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
+    GNUNET_assert (rd[i].record_type == 1 + (id % 13));
+    GNUNET_assert (rd[i].flags == 0);
+  }
+  memset (&tzone_private_key, (id % 241), sizeof (tzone_private_key));
+  GNUNET_assert (0 == strcmp (label, tname));
+  GNUNET_assert (0 == memcmp (&tzone_private_key, private_key, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)));
 }
 
 
 static void
-get_signature (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
+get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
 {
-  GNUNET_HashCode root_hash;
-
-  memset (&root_hash, 42, sizeof (root_hash));
-  GNUNET_assert (GNUNET_OK ==
-                nsp->get_signature (nsp->cls,
-                                    &root_hash,
-                                    test_signature,
-                                    &id));
+  GNUNET_assert (GNUNET_OK == nsp->iterate_records (nsp->cls,
+                                           NULL, 0, &test_record, &id));
 }
 
 
 static void
 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
 {
-  size_t data_size = id * 17;
-  char data[data_size];
-  GNUNET_HashCode zone;
-  char name[32];
-  uint32_t record_type;
-  struct GNUNET_NAMESTORE_SignatureLocation loc;
-  struct GNUNET_TIME_Absolute expiration;
-  enum GNUNET_NAMESTORE_RecordFlags flags;                
-  
-  memset (&zone, 42, sizeof (zone));
-  memset (data, id % 255, sizeof (data));
-  GNUNET_snprintf (name, sizeof (name),
-                  "aa%u", (unsigned int) id);
-  record_type = id % 4;
-  loc.depth = (id % 10);
-  loc.offset = (id % 3);
-  loc.revision = id % 1024;
-  expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
-  flags = GNUNET_NAMESTORE_RF_AUTHORITY;
-
-  GNUNET_assert (GNUNET_OK ==
-                nsp->put_record (nsp->cls,
-                                 &zone,
-                                 name,
-                                 record_type,
-                                 &loc,
-                                 expiration,
-                                 flags,
-                                 data_size,
-                                 data));
-}
-
-
-static void
-put_node (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
-         int id)
-{
-  GNUNET_HashCode zone;
-  struct GNUNET_NAMESTORE_SignatureLocation loc;
-  struct GNUNET_NAMESTORE_SignatureLocation ploc;
-  unsigned int num_entries = 1 + (id % 15);
-  GNUNET_HashCode entries[num_entries];
+  struct GNUNET_CRYPTO_EcdsaPrivateKey zone_private_key;
+  char label[64];
+  unsigned int rd_count = 1 + (id % 1024);
+  struct GNUNET_GNSRECORD_Data rd[rd_count];
+  struct GNUNET_CRYPTO_EcdsaSignature signature;
   unsigned int i;
 
-  memset (&zone, 42, sizeof (zone));
-  loc.depth = (id % 10);
-  loc.offset = (id % 3);
-  loc.revision = id % 1024;
-  ploc.depth = loc.depth + 1;
-  ploc.offset = (id % 5);
-  ploc.revision = loc.revision;
-  for (i=0;i<num_entries;i++)
-    memset (&entries[i], (id+i) % 255, sizeof (GNUNET_HashCode));
-  GNUNET_assert (GNUNET_OK ==
-                nsp->put_node (nsp->cls,
-                               &zone,
-                               &loc,
-                               &ploc,
-                               num_entries,
-                               entries));
-}
-
-
-static void
-put_signature (struct GNUNET_NAMESTORE_PluginFunctions *nsp,
-              int id)
-{
-  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key;
-  struct GNUNET_NAMESTORE_SignatureLocation loc;
-  struct GNUNET_CRYPTO_RsaSignature top_sig;
-  GNUNET_HashCode root_hash;
-  struct GNUNET_TIME_Absolute zone_time;
-
-  memset (&zone_key, 13, sizeof (zone_key));
-  loc.depth = (id % 10);
-  loc.offset = (id % 3);
-  loc.revision = id % 1024;
-  memset (&top_sig, 24, sizeof (top_sig));
-  memset (&root_hash, 42, sizeof (root_hash));
-  zone_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
-  
-  GNUNET_assert (GNUNET_OK ==
-                nsp->put_signature (nsp->cls,
-                                    &zone_key,
-                                    &loc,
-                                    &top_sig,
-                                    &root_hash,
-                                    zone_time));
+  GNUNET_snprintf (label, sizeof (label),
+                  "a%u", (unsigned int ) id);
+  for (i=0;i<rd_count;i++)
+  {
+    rd[i].data = "Hello World";
+    rd[i].data_size = id % 10;
+    rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
+    rd[i].record_type = 1 + (id % 13);
+    rd[i].flags = 0;
+  }
+  memset (&zone_private_key, (id % 241), sizeof (zone_private_key));
+  memset (&signature, (id % 243), sizeof (signature));
+  GNUNET_assert (GNUNET_OK == nsp->store_records (nsp->cls,
+                                               &zone_private_key,
+                                               label,
+                                               rd_count,
+                                               rd));
 }
 
 
@@ -367,24 +150,20 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  struct GNUNET_NAMESTORE_PluginFunctions *nsp;  
-  
+  struct GNUNET_NAMESTORE_PluginFunctions *nsp;
+
   ok = 0;
   nsp = load_plugin (cfg);
   if (NULL == nsp)
   {
     FPRINTF (stderr,
-             "%s", 
+             "%s",
             "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
     return;
   }
   put_record (nsp, 1);
   get_record (nsp, 1);
-  put_node (nsp, 1);
-  get_node (nsp, 1);
-  put_signature (nsp, 1);
-  get_signature (nsp, 1);
-  
+
   unload_plugin (nsp);
 }
 
@@ -392,46 +171,29 @@ run (void *cls, char *const *args, const char *cfgfile,
 int
 main (int argc, char *argv[])
 {
-  char *pos;
   char cfg_name[128];
-
   char *const xargv[] = {
     "test-plugin-namestore",
     "-c",
     cfg_name,
-#if VERBOSE
-    "-L", "DEBUG",
-#endif
     NULL
   };
   struct GNUNET_GETOPT_CommandLineOption options[] = {
     GNUNET_GETOPT_OPTION_END
   };
 
+  GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
   GNUNET_log_setup ("test-plugin-namestore",
-#if VERBOSE
-                    "DEBUG",
-#else
                     "WARNING",
-#endif
                     NULL);
-  /* 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 (cfg_name, sizeof (cfg_name), "test_plugin_namestore_%s.conf",
                    plugin_name);
-  if (pos != plugin_name)
-    pos[0] = '.';
   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
                       "test-plugin-namestore", "nohelp", options, &run, NULL);
   if (ok != 0)
     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
+  GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namestore-sqlite");
   return ok;
 }