-fixing
[oweals/gnunet.git] / src / util / test_pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_pseudonym.c
23  * @brief testcase for pseudonym.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_pseudonym_lib.h"
33
34 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
35
36 static struct GNUNET_CONTAINER_MetaData *meta;
37
38 static struct GNUNET_HashCode id1;
39
40 static int
41 iter (void *cls, const struct GNUNET_HashCode * pseudonym,
42       const char *name, const char *unique_name,
43       const struct GNUNET_CONTAINER_MetaData *md, int rating)
44 {
45   int *ok = cls;
46
47   if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_HashCode))) &&
48       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
49   {
50     *ok = GNUNET_NO;
51     GNUNET_break (0);
52   }
53   return GNUNET_OK;
54 }
55
56 static int
57 noti_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
58                const char *name, const char *unique_name,
59                const struct GNUNET_CONTAINER_MetaData *md, int rating)
60 {
61   int *ret = cls;
62
63   (*ret)++;
64   return GNUNET_OK;
65 }
66
67 static int
68 fake_noti_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
69                     const char *name, const char *unique_name,
70                     const struct GNUNET_CONTAINER_MetaData *md, int rating)
71 {
72   int *ret = cls;
73
74   (*ret)++;
75   return GNUNET_OK;
76 }
77
78 static int
79 false_callback (void *cls, const struct GNUNET_HashCode * pseudonym,
80                 const char *name, const char *unique_name,
81                 const struct GNUNET_CONTAINER_MetaData *md, int rating)
82 {
83   return GNUNET_OK;
84 }
85
86 int
87 main (int argc, char *argv[])
88 {
89   int ok;
90   struct GNUNET_HashCode rid1;
91   struct GNUNET_HashCode id2;
92   struct GNUNET_HashCode rid2;
93   struct GNUNET_HashCode fid;
94   struct GNUNET_HashCode id3;
95
96   int old;
97   int newVal;
98   struct GNUNET_CONFIGURATION_Handle *cfg;
99   char *name1;
100   char *name2;
101   char *name3;
102   char *name1_unique;
103   char *name2_unique;
104   char *noname;
105   int noname_is_a_dup;
106   int notiCount, fakenotiCount;
107   int count;
108   static char m[1024 * 1024 * 10];
109
110   memset (m, 'b', sizeof (m));
111   m[sizeof (m) - 1] = '\0';
112
113   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
114   ok = GNUNET_YES;
115   GNUNET_CRYPTO_random_disable_entropy_gathering ();
116   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
117   cfg = GNUNET_CONFIGURATION_create ();
118   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
119   {
120     GNUNET_CONFIGURATION_destroy (cfg);
121     GNUNET_break (0);
122     return -1;
123   }
124   notiCount = 0;
125   fakenotiCount = 0;
126   count = 0;
127   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
128                                                 &fakenotiCount);
129   GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
130                                                 &notiCount);
131   GNUNET_PSEUDONYM_discovery_callback_unregister (&false_callback, &count);
132   GNUNET_PSEUDONYM_discovery_callback_unregister (&fake_noti_callback,
133                                                   &fakenotiCount);
134
135   /* ACTUAL TEST CODE */
136   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
137   meta = GNUNET_CONTAINER_meta_data_create ();
138   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
139                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
140                                      "test", strlen ("test") + 1);
141   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id1);
142   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
143   CHECK (notiCount == 1);
144   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
145   CHECK (notiCount == 2);
146   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
147   CHECK (old < newVal);
148   old = newVal;
149   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id2);
150   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
151   CHECK (notiCount == 3);
152   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
153   CHECK (old < newVal);
154   GNUNET_assert (GNUNET_OK ==
155                  GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
156                                                     EXTRACTOR_METATYPE_COMMENT,
157                                                     EXTRACTOR_METAFORMAT_UTF8,
158                                                     "text/plain", m,
159                                                     strlen (m) + 1));
160   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id3);
161   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
162   GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL);
163   CHECK (name3 != NULL);
164   GNUNET_PSEUDONYM_get_info (cfg, &id2, NULL, NULL, &name2, NULL);
165   CHECK (name2 != NULL);
166   GNUNET_PSEUDONYM_get_info (cfg, &id1, NULL, NULL, &name1, NULL);
167   CHECK (name1 != NULL);
168   CHECK (0 == strcmp (name1, name2));
169   name1_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id1, name1, NULL);
170   name2_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id2, name2, NULL);
171   CHECK (0 != strcmp (name1_unique, name2_unique));
172   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
173   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
174   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
175   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2_unique, &rid2));
176   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1_unique, &rid1));
177   CHECK (0 == memcmp (&id1, &rid1, sizeof (struct GNUNET_HashCode)));
178   CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_HashCode)));
179
180   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &fid);
181   GNUNET_log_skip (1, GNUNET_NO);
182   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
183   GNUNET_log_skip (0, GNUNET_YES);
184   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_get_info (cfg, &fid, NULL, NULL, &noname, &noname_is_a_dup));
185   CHECK (noname != NULL);
186   CHECK (noname_is_a_dup == GNUNET_YES);
187   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
188   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
189   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
190   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
191   GNUNET_free (name1);
192   GNUNET_free (name2);
193   GNUNET_free (name1_unique);
194   GNUNET_free (name2_unique);
195   GNUNET_free (name3);
196   GNUNET_free (noname);
197   /* END OF TEST CODE */
198 FAILURE:
199   GNUNET_PSEUDONYM_discovery_callback_unregister (&noti_callback, &notiCount);
200   GNUNET_CONTAINER_meta_data_destroy (meta);
201   GNUNET_CONFIGURATION_destroy (cfg);
202   GNUNET_break (GNUNET_OK ==
203                 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
204   return (ok == GNUNET_YES) ? 0 : 1;
205 }
206
207 /* end of test_pseudoynm.c */