some fixes to the pt/vpn testcase.
[oweals/gnunet.git] / src / util / test_pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005--2013 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 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
30
31 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
32
33 static struct GNUNET_CONTAINER_MetaData *meta;
34
35 static struct GNUNET_PseudonymIdentifier id1;
36
37
38 static int
39 iter (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
40       const char *name, const char *unique_name,
41       const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
42 {
43   int *ok = cls;
44
45   if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_PseudonymIdentifier))) &&
46       (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
47   {
48     *ok = GNUNET_NO;
49     GNUNET_break (0);
50   }
51   return GNUNET_OK;
52 }
53
54
55 static int
56 noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
57                const char *name, const char *unique_name,
58                const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
59 {
60   int *ret = cls;
61
62   (*ret)++;
63   return GNUNET_OK;
64 }
65
66
67 static int
68 fake_noti_callback (void *cls, const struct GNUNET_PseudonymIdentifier * pseudonym,
69                     const char *name, const char *unique_name,
70                     const struct GNUNET_CONTAINER_MetaData *md, int32_t rating)
71 {
72   int *ret = cls;
73
74   (*ret)++;
75   return GNUNET_OK;
76 }
77
78
79 static void
80 create_pseu (struct GNUNET_PseudonymIdentifier *pseu)
81 {
82   struct GNUNET_PseudonymHandle *ph;
83
84   ph = GNUNET_PSEUDONYM_create (NULL);
85   GNUNET_PSEUDONYM_get_identifier (ph, pseu);
86   GNUNET_PSEUDONYM_destroy (ph);
87 }
88
89
90 /**
91  * Testcase for meta data / ranking IO routines.
92  */
93 static int
94 test_io ()
95 {
96   int ok;
97   struct GNUNET_PseudonymIdentifier rid1;
98   struct GNUNET_PseudonymIdentifier id2;
99   struct GNUNET_PseudonymIdentifier rid2;
100   struct GNUNET_PseudonymIdentifier fid;
101   struct GNUNET_PseudonymIdentifier id3;
102   int old;
103   int newVal;
104   struct GNUNET_CONFIGURATION_Handle *cfg;
105   char *name1;
106   char *name2;
107   char *name3;
108   char *name1_unique;
109   char *name2_unique;
110   char *noname;
111   int noname_is_a_dup;
112   int notiCount, fakenotiCount;
113   static char m[1024 * 1024 * 10];
114   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh1;
115   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh2;
116
117   memset (m, 'b', sizeof (m));
118   m[sizeof (m) - 1] = '\0';
119
120   GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
121   ok = GNUNET_YES;
122   (void) GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test");
123   cfg = GNUNET_CONFIGURATION_create ();
124   if (-1 == GNUNET_CONFIGURATION_parse (cfg, "test_pseudonym_data.conf"))
125   {
126     GNUNET_CONFIGURATION_destroy (cfg);
127     GNUNET_break (0);
128     return -1;
129   }
130   notiCount = 0;
131   fakenotiCount = 0;
132   dh1 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback,
133                                                       &fakenotiCount);
134   dh2 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, &noti_callback,
135                                                       &notiCount);
136   GNUNET_PSEUDONYM_discovery_callback_unregister (dh1);
137
138   /* ACTUAL TEST CODE */
139   old = GNUNET_PSEUDONYM_list_all (cfg, NULL, NULL);
140   meta = GNUNET_CONTAINER_meta_data_create ();
141   GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
142                                      EXTRACTOR_METAFORMAT_UTF8, "text/plain",
143                                      "test", strlen ("test") + 1);
144   create_pseu (&id1);
145   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
146   CHECK (notiCount == 1);
147   GNUNET_PSEUDONYM_add (cfg, &id1, meta);
148   CHECK (notiCount == 2);
149   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
150   CHECK (old < newVal);
151   old = newVal;
152   create_pseu (&id2);
153   GNUNET_PSEUDONYM_add (cfg, &id2, meta);
154   CHECK (notiCount == 3);
155   newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok);
156   CHECK (old < newVal);
157   GNUNET_assert (GNUNET_OK ==
158                  GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
159                                                     EXTRACTOR_METATYPE_COMMENT,
160                                                     EXTRACTOR_METAFORMAT_UTF8,
161                                                     "text/plain", m,
162                                                     strlen (m) + 1));
163   create_pseu (&id3);
164   GNUNET_PSEUDONYM_add (cfg, &id3, meta);
165   GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL);
166   CHECK (name3 != NULL);
167   GNUNET_PSEUDONYM_get_info (cfg, &id2, NULL, NULL, &name2, NULL);
168   CHECK (name2 != NULL);
169   GNUNET_PSEUDONYM_get_info (cfg, &id1, NULL, NULL, &name1, NULL);
170   CHECK (name1 != NULL);
171   CHECK (0 == strcmp (name1, name2));
172   name1_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id1, name1, NULL);
173   name2_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id2, name2, NULL);
174   CHECK (0 != strcmp (name1_unique, name2_unique));
175   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2));
176   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2));
177   CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1));
178   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2_unique, &rid2));
179   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1_unique, &rid1));
180   CHECK (0 == memcmp (&id1, &rid1, sizeof (struct GNUNET_PseudonymIdentifier)));
181   CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_PseudonymIdentifier)));
182
183   create_pseu (&fid);
184   GNUNET_log_skip (1, GNUNET_NO);
185   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0));
186   GNUNET_log_skip (0, GNUNET_NO);
187   CHECK (GNUNET_OK == GNUNET_PSEUDONYM_get_info (cfg, &fid, NULL, NULL, &noname, &noname_is_a_dup));
188   CHECK (noname != NULL);
189   CHECK (noname_is_a_dup == GNUNET_YES);
190   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 0));
191   CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
192   CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10));
193   CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5));
194   GNUNET_free (name1);
195   GNUNET_free (name2);
196   GNUNET_free (name1_unique);
197   GNUNET_free (name2_unique);
198   GNUNET_free (name3);
199   GNUNET_free (noname);
200   /* END OF TEST CODE */
201 FAILURE:
202   GNUNET_PSEUDONYM_discovery_callback_unregister (dh2);
203   GNUNET_CONTAINER_meta_data_destroy (meta);
204   GNUNET_CONFIGURATION_destroy (cfg);
205   return (ok == GNUNET_YES) ? 0 : 1;
206 }
207
208
209 /**
210  * Use the given input to sign and check the resulting signature.
211  */
212 static void
213 test_signature (struct GNUNET_PseudonymHandle *ph,
214                 struct GNUNET_PseudonymSignaturePurpose *purpose,
215                 struct GNUNET_HashCode *seed,
216                 struct GNUNET_HashCode *signing_key,
217                 char *bit)
218 {
219   struct GNUNET_PseudonymSignature signature;
220   struct GNUNET_PseudonymSignature signature2;
221   struct GNUNET_PseudonymIdentifier pseudonym;
222   struct GNUNET_PseudonymIdentifier verification_key;
223
224   GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature);
225   GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature2);
226   /* with seed, two sigs must be identical, without, they must be different! */
227   if (NULL != seed)
228     GNUNET_assert (0 == memcmp (&signature, &signature2, sizeof (signature)));
229   else /* crypto not implemented, thus for now 'break' */
230     GNUNET_break (0 != memcmp (&signature, &signature2, sizeof (signature)));
231   GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym);
232   GNUNET_PSEUDONYM_derive_verification_key (&pseudonym,
233                                             signing_key,
234                                             &verification_key);
235   GNUNET_assert (GNUNET_OK ==
236                  GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key));
237   /* also check that if the data is changed, the signature no longer matches */
238   (*bit)++;
239   /* crypto not implemented, thus for now 'break' */
240   GNUNET_break (GNUNET_OK !=
241                  GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key));
242   (*bit)--;
243 }
244
245
246 /**
247  * Test cryptographic operations for a given private key.
248  *
249  * @param ph private key to test
250  */
251 static void
252 test_crypto_ops (struct GNUNET_PseudonymHandle *ph)
253 {
254   char data[16];
255   struct GNUNET_PseudonymSignaturePurpose *purpose;
256   struct GNUNET_HashCode seed;
257   struct GNUNET_HashCode signing_key;
258
259   memset (data, 42, sizeof (data));
260   purpose = (struct GNUNET_PseudonymSignaturePurpose *) data;
261   purpose->size = htonl (sizeof (data));
262   purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
263   memset (&seed, 41, sizeof (seed));
264   memset (&signing_key, 40, sizeof (signing_key));
265   test_signature (ph, purpose, &seed, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]);
266   test_signature (ph, purpose, NULL, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]);
267 }
268
269
270 /**
271  * Test cryptographic operations.
272  */
273 static int
274 test_crypto ()
275 {
276   struct GNUNET_PseudonymHandle *ph;
277   struct GNUNET_PseudonymIdentifier pseudonym;
278   struct GNUNET_PseudonymIdentifier pseudonym2;
279
280   /* check writing to and reading from disk */
281   ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa");
282   GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym);
283   GNUNET_PSEUDONYM_destroy (ph);
284   ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa");
285   GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
286   test_crypto_ops (ph);
287   GNUNET_PSEUDONYM_destroy (ph);
288   if (0 != memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
289     return 1;
290   
291   /* check in-memory generation */
292   ph = GNUNET_PSEUDONYM_create (NULL);
293   GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
294   if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
295     return 1;
296   test_crypto_ops (ph);
297   GNUNET_PSEUDONYM_destroy (ph);  
298
299   /* check anonymous pseudonym operations generation */
300   ph = GNUNET_PSEUDONYM_get_anonymous_pseudonym_handle ();
301   GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2);
302   if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym)))
303     return 1;
304   test_crypto_ops (ph);
305   GNUNET_PSEUDONYM_destroy (ph);  
306   return 0;
307 }
308
309
310 int
311 main (int argc, char *argv[])
312 {
313   if (0 != test_io ())
314     return 1;
315   if (0 != test_crypto ())
316     return 1;
317   GNUNET_break (GNUNET_OK ==
318                 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));  
319   return 0;
320 }
321
322
323 /* end of test_pseudoynm.c */