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