- fix
[oweals/gnunet.git] / src / util / test_pseudonym.c
index 4ce8b385370729aede3428fa432838d299141301..4ca293a9ab070030c1962bafe62e39f4da14801d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
+     (C) 2005--2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
  * @brief testcase for pseudonym.c
  * @author Christian Grothoff
  */
-
 #include "platform.h"
 #include "gnunet_common.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_crypto_lib.h"
-#include "gnunet_disk_lib.h"
-#include "gnunet_pseudonym_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_signatures.h"
 
 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
 
 static struct GNUNET_CONTAINER_MetaData *meta;
 
-static GNUNET_HashCode id1;
+static struct GNUNET_PseudonymIdentifier id1;
+
 
 static int
-iter (void *cls, const GNUNET_HashCode * pseudonym,
+iter (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
       const char *name, const char *unique_name,
-      const struct GNUNET_CONTAINER_MetaData *md, int rating)
+      const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
 {
   int *ok = cls;
 
-  if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) &&
+  if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_PseudonymIdentifier))) &&
       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
   {
     *ok = GNUNET_NO;
@@ -53,10 +51,11 @@ iter (void *cls, const GNUNET_HashCode * pseudonym,
   return GNUNET_OK;
 }
 
+
 static int
-noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
+noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
                const char *name, const char *unique_name,
-               const struct GNUNET_CONTAINER_MetaData *md, int rating)
+               const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
 {
   int *ret = cls;
 
@@ -64,10 +63,11 @@ noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
   return GNUNET_OK;
 }
 
+
 static int
-fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
+fake_noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
                     const char *name, const char *unique_name,
-                    const struct GNUNET_CONTAINER_MetaData *md, int rating)
+                    const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
 {
   int *ret = cls;
 
@@ -75,24 +75,30 @@ fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
   return GNUNET_OK;
 }
 
-static int
-false_callback (void *cls, const GNUNET_HashCode * pseudonym,
-                const char *name, const char *unique_name,
-                const struct GNUNET_CONTAINER_MetaData *md, int rating)
+
+static void
+create_pseu (struct GNUNET_PseudonymIdentifier *pseu)
 {
-  return GNUNET_OK;
+  struct GNUNET_PseudonymHandle *ph;
+
+  ph = GNUNET_PSEUDONYM_create (NULL);
+  GNUNET_PSEUDONYM_get_identifier (ph, pseu);
+  GNUNET_PSEUDONYM_destroy (ph);
 }
 
-int
-main (int argc, char *argv[])
+
+/**
+ * Testcase for meta data / ranking IO routines.
+ */
+static int
+test_io ()
 {
   int ok;
-  GNUNET_HashCode rid1;
-  GNUNET_HashCode id2;
-  GNUNET_HashCode rid2;
-  GNUNET_HashCode fid;
-  GNUNET_HashCode id3;
-
+  struct GNUNET_PseudonymIdentifier rid1;
+  struct GNUNET_PseudonymIdentifier id2;
+  struct GNUNET_PseudonymIdentifier rid2;
+  struct GNUNET_PseudonymIdentifier fid;
+  struct GNUNET_PseudonymIdentifier id3;
   int old;
   int newVal;
   struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -104,15 +110,15 @@ main (int argc, char *argv[])
   char *noname;
   int noname_is_a_dup;
   int notiCount, fakenotiCount;
-  int count;
   static char m[1024 * 1024 * 10];
+  struct GNUNET_PSEUDONYM_DiscoveryHandle *dh1;
+  struct GNUNET_PSEUDONYM_DiscoveryHandle *dh2;
 
   memset (m, 'b', sizeof (m));
   m[sizeof (m) - 1] = '\0';
 
   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
   ok = GNUNET_YES;
-  GNUNET_CRYPTO_random_disable_entropy_gathering ();
   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
   cfg = GNUNET_CONFIGURATION_create ();
   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
@@ -123,14 +129,11 @@ main (int argc, char *argv[])
   }
   notiCount = 0;
   fakenotiCount = 0;
-  count = 0;
-  GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
-                                                &fakenotiCount);
-  GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
-                                                &notiCount);
-  GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
-  GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
-                                                  &fakenotiCount);
+  dh1 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
+                                                     &fakenotiCount);
+  dh2 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
+                                                     &notiCount);
+  GNUNET_PSEUDONYM_discovery_callback_unregister (dh1);
 
   /* ACTUAL TEST CODE */
   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
@@ -138,7 +141,7 @@ main (int argc, char *argv[])
   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
                                      "test", strlen ("test") + 1);
-  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
+  create_pseu (&id1);
   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
   CHECK (notiCount == 1);
   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
@@ -146,7 +149,7 @@ main (int argc, char *argv[])
   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
   CHECK (old < newVal);
   old = newVal;
-  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id2);
+  create_pseu (&id2);
   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
   CHECK (notiCount == 3);
   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
@@ -157,7 +160,7 @@ main (int argc, char *argv[])
                                                     EXTRACTOR_METAFORMAT_UTF8,
                                                     "text/plain", m,
                                                     strlen (m) + 1));
-  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
+  create_pseu (&id3);
   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
   GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL);
   CHECK (name3 != NULL);
@@ -174,13 +177,13 @@ main (int argc, char *argv[])
   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2_unique, &rid2));
   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1_unique, &rid1));
-  CHECK (0 == memcmp (&id1, &rid1, sizeof (GNUNET_HashCode)));
-  CHECK (0 == memcmp (&id2, &rid2, sizeof (GNUNET_HashCode)));
+  CHECK (0 == memcmp (&id1, &rid1, sizeof (struct GNUNET_PseudonymIdentifier)));
+  CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_PseudonymIdentifier)));
 
-  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
+  create_pseu (&fid);
   GNUNET_log_skip (1, GNUNET_NO);
   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
-  GNUNET_log_skip (0, GNUNET_YES);
+  GNUNET_log_skip (0, GNUNET_NO);
   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_get_info (cfg, &fid, NULL, NULL, &noname, &noname_is_a_dup));
   CHECK (noname != NULL);
   CHECK (noname_is_a_dup == GNUNET_YES);
@@ -196,12 +199,125 @@ main (int argc, char *argv[])
   GNUNET_free (noname);
   /* END OF TEST CODE */
 FAILURE:
-  GNUNET_PSEUDONYM_discovery_callback_unregister (&noti_callback, &notiCount);
+  GNUNET_PSEUDONYM_discovery_callback_unregister (dh2);
   GNUNET_CONTAINER_meta_data_destroy (meta);
   GNUNET_CONFIGURATION_destroy (cfg);
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
   return (ok == GNUNET_YES) ? 0 : 1;
 }
 
+
+/**
+ * Use the given input to sign and check the resulting signature.
+ */
+static void
+test_signature (struct GNUNET_PseudonymHandle *ph,
+               struct GNUNET_PseudonymSignaturePurpose *purpose,
+               struct GNUNET_HashCode *seed,
+               struct GNUNET_HashCode *signing_key,
+               char *bit)
+{
+  struct GNUNET_PseudonymSignature signature;
+  struct GNUNET_PseudonymSignature signature2;
+  struct GNUNET_PseudonymIdentifier pseudonym;
+  struct GNUNET_PseudonymIdentifier verification_key;
+
+  GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature);
+  GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature2);
+  /* with seed, two sigs must be identical, without, they must be different! */
+  if (NULL != seed)
+    GNUNET_assert (0 == memcmp (&signature, &signature2, sizeof (signature)));
+  else /* crypto not implemented, thus for now 'break' */
+    GNUNET_break (0 != memcmp (&signature, &signature2, sizeof (signature)));
+  GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym);
+  GNUNET_PSEUDONYM_derive_verification_key (&pseudonym,
+                                           signing_key,
+                                           &verification_key);
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key));
+  /* also check that if the data is changed, the signature no longer matches */
+  (*bit)++;
+  /* crypto not implemented, thus for now 'break' */
+  GNUNET_break (GNUNET_OK !=
+                GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key));
+  (*bit)--;
+}
+
+
+/**
+ * Test cryptographic operations for a given private key.
+ *
+ * @param ph private key to test
+ */
+static void
+test_crypto_ops (struct GNUNET_PseudonymHandle *ph)
+{
+  char data[16];
+  struct GNUNET_PseudonymSignaturePurpose *purpose;
+  struct GNUNET_HashCode seed;
+  struct GNUNET_HashCode signing_key;
+
+  memset (data, 42, sizeof (data));
+  purpose = (struct GNUNET_PseudonymSignaturePurpose *) data;
+  purpose->size = htonl (sizeof (data));
+  purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
+  memset (&seed, 41, sizeof (seed));
+  memset (&signing_key, 40, sizeof (signing_key));
+  test_signature (ph, purpose, &seed, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]);
+  test_signature (ph, purpose, NULL, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]);
+}
+
+
+/**
+ * Test cryptographic operations.
+ */
+static int
+test_crypto ()
+{
+  struct GNUNET_PseudonymHandle *ph;
+  struct GNUNET_PseudonymIdentifier pseudonym;
+  struct GNUNET_PseudonymIdentifier pseudonym2;
+
+  /* check writing to and reading from disk */
+  ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa");
+  GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym);
+  GNUNET_PSEUDONYM_destroy (ph);
+  ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa");
+  GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
+  test_crypto_ops (ph);
+  GNUNET_PSEUDONYM_destroy (ph);
+  if (0 != memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
+    return 1;
+  
+  /* check in-memory generation */
+  ph = GNUNET_PSEUDONYM_create (NULL);
+  GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
+  if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
+    return 1;
+  test_crypto_ops (ph);
+  GNUNET_PSEUDONYM_destroy (ph);  
+
+  /* check anonymous pseudonym operations generation */
+  ph = GNUNET_PSEUDONYM_get_anonymous_pseudonym_handle ();
+  GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
+  if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
+    return 1;
+  test_crypto_ops (ph);
+  GNUNET_PSEUDONYM_destroy (ph);  
+  return 0;
+}
+
+
+int
+main (int argc, char *argv[])
+{
+  if (0 != test_io ())
+    return 1;
+  if (0 != test_crypto ())
+    return 1;
+  GNUNET_break (GNUNET_OK ==
+                GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));  
+  return 0;
+}
+
+
 /* end of test_pseudoynm.c */