2 This file is part of GNUnet.
3 (C) 2005--2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/test_pseudonym.c
23 * @brief testcase for fs_pseudonym.c
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_signatures.h"
32 #define CHECK(a) do { if (!(a)) { ok = GNUNET_NO; GNUNET_break(0); goto FAILURE; } } while (0)
34 static struct GNUNET_CONTAINER_MetaData *meta;
36 static struct GNUNET_FS_PseudonymIdentifier id1;
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)
46 if ((0 == memcmp (pseudonym, &id1, sizeof (struct GNUNET_FS_PseudonymIdentifier))) &&
47 (!GNUNET_CONTAINER_meta_data_test_equal (md, meta)))
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)
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)
81 create_pseu (struct GNUNET_FS_PseudonymIdentifier *pseu)
83 struct GNUNET_FS_PseudonymHandle *ph;
85 ph = GNUNET_FS_pseudonym_create (NULL);
86 GNUNET_FS_pseudonym_get_identifier (ph, pseu);
87 GNUNET_FS_pseudonym_destroy (ph);
92 * Testcase for meta data / ranking IO routines.
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;
105 struct GNUNET_CONFIGURATION_Handle *cfg;
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;
118 memset (m, 'b', sizeof (m));
119 m[sizeof (m) - 1] = '\0';
121 GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
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"))
127 GNUNET_CONFIGURATION_destroy (cfg);
133 dh1 = GNUNET_FS_pseudonym_discovery_callback_register (cfg, &fake_noti_callback,
135 dh2 = GNUNET_FS_pseudonym_discovery_callback_register (cfg, ¬i_callback,
137 GNUNET_FS_pseudonym_discovery_callback_unregister (dh1);
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);
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);
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,
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)));
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));
197 GNUNET_free (name1_unique);
198 GNUNET_free (name2_unique);
200 GNUNET_free (noname);
201 /* END OF TEST CODE */
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;
211 * Use the given input to sign and check the resulting signature.
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,
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;
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! */
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,
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 */
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));
249 * Test cryptographic operations for a given private key.
251 * @param ph private key to test
254 test_crypto_ops (struct GNUNET_FS_PseudonymHandle *ph)
257 struct GNUNET_FS_PseudonymSignaturePurpose *purpose;
258 struct GNUNET_HashCode seed;
259 struct GNUNET_HashCode signing_key;
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)]);
275 * Test cryptographic operations.
280 struct GNUNET_FS_PseudonymHandle *ph;
281 struct GNUNET_FS_PseudonymIdentifier pseudonym;
282 struct GNUNET_FS_PseudonymIdentifier pseudonym2;
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)))
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)))
300 test_crypto_ops (ph);
301 GNUNET_FS_pseudonym_destroy (ph);
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)))
309 test_crypto_ops (ph);
310 GNUNET_FS_pseudonym_destroy (ph);
316 main (int argc, char *argv[])
318 GNUNET_log_setup ("test-pseudonym", "WARNING", NULL);
321 if (0 != test_crypto ())
323 GNUNET_break (GNUNET_OK ==
324 GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test"));
329 /* end of test_pseudoynm.c */