-dce
[oweals/gnunet.git] / src / util / test_pseudonym.c
index 30a04598c748c3c028046af89a4a0dca6b51183f..f0233771a691d6a07109dd21f7ebebe4fef28c16 100644 (file)
 
 static struct GNUNET_CONTAINER_MetaData *meta;
 
-static GNUNET_HashCode id1;
+static struct GNUNET_HashCode id1;
 
 static int
-iter (void *cls, const GNUNET_HashCode * pseudonym,
+iter (void *cls, const struct GNUNET_HashCode * pseudonym,
+      const char *name, const char *unique_name,
       const struct GNUNET_CONTAINER_MetaData *md, int rating)
 {
   int *ok = cls;
 
-  if ((0 == memcmp (pseudonym, &id1, sizeof (GNUNET_HashCode))) &&
+  if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_HashCode))) &&
       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
-    {
-      *ok = GNUNET_NO;
-      GNUNET_break (0);
-    }
+  {
+    *ok = GNUNET_NO;
+    GNUNET_break (0);
+  }
   return GNUNET_OK;
 }
 
 static int
-noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
-              const struct GNUNET_CONTAINER_MetaData *md, int rating)
+noti_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
+               const char *name, const char *unique_name,
+               const struct GNUNET_CONTAINER_MetaData *md, int rating)
 {
   int *ret = cls;
 
@@ -63,8 +65,9 @@ noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
 }
 
 static int
-fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
-                   const struct GNUNET_CONTAINER_MetaData *md, int rating)
+fake_noti_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
+                    const char *name, const char *unique_name,
+                    const struct GNUNET_CONTAINER_MetaData *md, int rating)
 {
   int *ret = cls;
 
@@ -73,8 +76,9 @@ fake_noti_callback (void *cls, const GNUNET_HashCode * pseudonym,
 }
 
 static int
-false_callback (void *cls, const GNUNET_HashCode * pseudonym,
-               const struct GNUNET_CONTAINER_MetaData *md, int rating)
+false_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
+                const char *name, const char *unique_name,
+                const struct GNUNET_CONTAINER_MetaData *md, int rating)
 {
   return GNUNET_OK;
 }
@@ -83,11 +87,11 @@ int
 main (int argc, char *argv[])
 {
   int ok;
-  GNUNET_HashCode rid1;
-  GNUNET_HashCode id2;
-  GNUNET_HashCode rid2;
-  GNUNET_HashCode fid;
-  GNUNET_HashCode id3;
+  struct GNUNET_HashCode rid1;
+  struct GNUNET_HashCode id2;
+  struct GNUNET_HashCode rid2;
+  struct GNUNET_HashCode fid;
+  struct GNUNET_HashCode id3;
 
   int old;
   int newVal;
@@ -95,7 +99,10 @@ main (int argc, char *argv[])
   char *name1;
   char *name2;
   char *name3;
+  char *name1_unique;
+  char *name2_unique;
   char *noname;
+  int noname_is_a_dup;
   int notiCount, fakenotiCount;
   int count;
   static char m[1024 * 1024 * 10];
@@ -109,28 +116,28 @@ main (int argc, char *argv[])
   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
   cfg = GNUNET_CONFIGURATION_create ();
   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
-    {
-      GNUNET_CONFIGURATION_destroy (cfg);
-      GNUNET_break (0);
-      return -1;
-    }
+  {
+    GNUNET_CONFIGURATION_destroy (cfg);
+    GNUNET_break (0);
+    return -1;
+  }
   notiCount = 0;
   fakenotiCount = 0;
   count = 0;
   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
-                                               &fakenotiCount);
+                                                &fakenotiCount);
   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
-                                               &notiCount);
+                                                &notiCount);
   GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
   GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
-                                                 &fakenotiCount);
+                                                  &fakenotiCount);
 
   /* ACTUAL TEST CODE */
   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
   meta = GNUNET_CONTAINER_meta_data_create ();
   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
-                                    EXTRACTOR_METAFORMAT_UTF8, "text/plain",
-                                    "test", strlen ("test") + 1);
+                                     EXTRACTOR_METAFORMAT_UTF8, "text/plain",
+                                     "test", strlen ("test") + 1);
   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
   CHECK (notiCount == 1);
@@ -145,37 +152,46 @@ main (int argc, char *argv[])
   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
   CHECK (old < newVal);
   GNUNET_assert (GNUNET_OK ==
-                GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
-                                                   EXTRACTOR_METATYPE_COMMENT,
-                                                   EXTRACTOR_METAFORMAT_UTF8,
-                                                   "text/plain", m,
-                                                   strlen (m) + 1));
+                 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
+                                                    EXTRACTOR_METATYPE_COMMENT,
+                                                    EXTRACTOR_METAFORMAT_UTF8,
+                                                    "text/plain", m,
+                                                    strlen (m) + 1));
   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
-  name3 = GNUNET_PSEUDONYM_id_to_name (cfg, &id3);
-  name2 = GNUNET_PSEUDONYM_id_to_name (cfg, &id2);
+  GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL);
+  CHECK (name3 != NULL);
+  GNUNET_PSEUDONYM_get_info (cfg, &id2, NULL, NULL, &name2, NULL);
   CHECK (name2 != NULL);
-  name1 = GNUNET_PSEUDONYM_id_to_name (cfg, &id1);
+  GNUNET_PSEUDONYM_get_info (cfg, &id1, NULL, NULL, &name1, NULL);
   CHECK (name1 != NULL);
-  CHECK (0 != strcmp (name1, name2));
+  CHECK (0 == strcmp (name1, name2));
+  name1_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id1, name1, NULL);
+  name2_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id2, name2, NULL);
+  CHECK (0 != strcmp (name1_unique, name2_unique));
   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
-  CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
-  CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
-  CHECK (0 == memcmp (&id1, &rid1, sizeof (GNUNET_HashCode)));
-  CHECK (0 == memcmp (&id2, &rid2, sizeof (GNUNET_HashCode)));
+  CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
+  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 (struct GNUNET_HashCode)));
+  CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_HashCode)));
 
   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
   GNUNET_log_skip (1, GNUNET_NO);
   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
   GNUNET_log_skip (0, GNUNET_YES);
-  noname = GNUNET_PSEUDONYM_id_to_name (cfg, &fid);
+  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);
   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
   GNUNET_free (name1);
   GNUNET_free (name2);
+  GNUNET_free (name1_unique);
+  GNUNET_free (name2_unique);
   GNUNET_free (name3);
   GNUNET_free (noname);
   /* END OF TEST CODE */
@@ -184,7 +200,7 @@ FAILURE:
   GNUNET_CONTAINER_meta_data_destroy (meta);
   GNUNET_CONFIGURATION_destroy (cfg);
   GNUNET_break (GNUNET_OK ==
-               GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
+                GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
   return (ok == GNUNET_YES) ? 0 : 1;
 }