diff options
Diffstat (limited to 'src/fs/test_pseudonym.c')
-rw-r--r-- | src/fs/test_pseudonym.c | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/src/fs/test_pseudonym.c b/src/fs/test_pseudonym.c new file mode 100644 index 000000000..5247d6dcc --- /dev/null +++ b/src/fs/test_pseudonym.c | |||
@@ -0,0 +1,327 @@ | |||
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_PseudonymIdentifier id1; | ||
37 | |||
38 | |||
39 | static int | ||
40 | iter (void *cls, const struct GNUNET_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_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_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_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_PseudonymIdentifier *pseu) | ||
82 | { | ||
83 | struct GNUNET_PseudonymHandle *ph; | ||
84 | |||
85 | ph = GNUNET_PSEUDONYM_create (NULL); | ||
86 | GNUNET_PSEUDONYM_get_identifier (ph, pseu); | ||
87 | GNUNET_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_PseudonymIdentifier rid1; | ||
99 | struct GNUNET_PseudonymIdentifier id2; | ||
100 | struct GNUNET_PseudonymIdentifier rid2; | ||
101 | struct GNUNET_PseudonymIdentifier fid; | ||
102 | struct GNUNET_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_PSEUDONYM_DiscoveryHandle *dh1; | ||
116 | struct GNUNET_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_PSEUDONYM_discovery_callback_register (cfg, &fake_noti_callback, | ||
134 | &fakenotiCount); | ||
135 | dh2 = GNUNET_PSEUDONYM_discovery_callback_register (cfg, ¬i_callback, | ||
136 | ¬iCount); | ||
137 | GNUNET_PSEUDONYM_discovery_callback_unregister (dh1); | ||
138 | |||
139 | /* ACTUAL TEST CODE */ | ||
140 | old = GNUNET_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_PSEUDONYM_add (cfg, &id1, meta); | ||
147 | CHECK (notiCount == 1); | ||
148 | GNUNET_PSEUDONYM_add (cfg, &id1, meta); | ||
149 | CHECK (notiCount == 2); | ||
150 | newVal = GNUNET_PSEUDONYM_list_all (cfg, &iter, &ok); | ||
151 | CHECK (old < newVal); | ||
152 | old = newVal; | ||
153 | create_pseu (&id2); | ||
154 | GNUNET_PSEUDONYM_add (cfg, &id2, meta); | ||
155 | CHECK (notiCount == 3); | ||
156 | newVal = GNUNET_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_PSEUDONYM_add (cfg, &id3, meta); | ||
166 | GNUNET_PSEUDONYM_get_info (cfg, &id3, NULL, NULL, &name3, NULL); | ||
167 | CHECK (name3 != NULL); | ||
168 | GNUNET_PSEUDONYM_get_info (cfg, &id2, NULL, NULL, &name2, NULL); | ||
169 | CHECK (name2 != NULL); | ||
170 | GNUNET_PSEUDONYM_get_info (cfg, &id1, NULL, NULL, &name1, NULL); | ||
171 | CHECK (name1 != NULL); | ||
172 | CHECK (0 == strcmp (name1, name2)); | ||
173 | name1_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id1, name1, NULL); | ||
174 | name2_unique = GNUNET_PSEUDONYM_name_uniquify (cfg, &id2, name2, NULL); | ||
175 | CHECK (0 != strcmp (name1_unique, name2_unique)); | ||
176 | CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, "fake", &rid2)); | ||
177 | CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name2, &rid2)); | ||
178 | CHECK (GNUNET_SYSERR == GNUNET_PSEUDONYM_name_to_id (cfg, name1, &rid1)); | ||
179 | CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name2_unique, &rid2)); | ||
180 | CHECK (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, name1_unique, &rid1)); | ||
181 | CHECK (0 == memcmp (&id1, &rid1, sizeof (struct GNUNET_PseudonymIdentifier))); | ||
182 | CHECK (0 == memcmp (&id2, &rid2, sizeof (struct GNUNET_PseudonymIdentifier))); | ||
183 | |||
184 | create_pseu (&fid); | ||
185 | GNUNET_log_skip (1, GNUNET_NO); | ||
186 | CHECK (0 == GNUNET_PSEUDONYM_rank (cfg, &fid, 0)); | ||
187 | GNUNET_log_skip (0, GNUNET_NO); | ||
188 | CHECK (GNUNET_OK == GNUNET_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_PSEUDONYM_rank (cfg, &id1, 0)); | ||
192 | CHECK (5 == GNUNET_PSEUDONYM_rank (cfg, &id1, 5)); | ||
193 | CHECK (-5 == GNUNET_PSEUDONYM_rank (cfg, &id1, -10)); | ||
194 | CHECK (0 == GNUNET_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_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_PseudonymHandle *ph, | ||
215 | struct GNUNET_PseudonymSignaturePurpose *purpose, | ||
216 | struct GNUNET_HashCode *seed, | ||
217 | struct GNUNET_HashCode *signing_key, | ||
218 | char *bit) | ||
219 | { | ||
220 | struct GNUNET_PseudonymSignature signature; | ||
221 | struct GNUNET_PseudonymSignature signature2; | ||
222 | struct GNUNET_PseudonymIdentifier pseudonym; | ||
223 | struct GNUNET_PseudonymIdentifier verification_key; | ||
224 | |||
225 | GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature); | ||
226 | if (0) | ||
227 | { | ||
228 | GNUNET_PSEUDONYM_sign (ph, purpose, seed, signing_key, &signature2); | ||
229 | /* with seed, two sigs must be identical, without, they must be different! */ | ||
230 | if (NULL != seed) | ||
231 | GNUNET_break (0 == memcmp (&signature, &signature2, sizeof (signature))); | ||
232 | else /* crypto not implemented, thus for now 'break' */ | ||
233 | GNUNET_break (0 != memcmp (&signature, &signature2, sizeof (signature))); | ||
234 | } | ||
235 | GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym); | ||
236 | GNUNET_PSEUDONYM_derive_verification_key (&pseudonym, | ||
237 | signing_key, | ||
238 | &verification_key); | ||
239 | GNUNET_break (GNUNET_OK == | ||
240 | GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key)); | ||
241 | /* also check that if the data is changed, the signature no longer matches */ | ||
242 | (*bit)++; | ||
243 | /* crypto not implemented, thus for now 'break' */ | ||
244 | GNUNET_break (GNUNET_OK != | ||
245 | GNUNET_PSEUDONYM_verify (purpose, &signature, &verification_key)); | ||
246 | (*bit)--; | ||
247 | } | ||
248 | |||
249 | |||
250 | /** | ||
251 | * Test cryptographic operations for a given private key. | ||
252 | * | ||
253 | * @param ph private key to test | ||
254 | */ | ||
255 | static void | ||
256 | test_crypto_ops (struct GNUNET_PseudonymHandle *ph) | ||
257 | { | ||
258 | char data[16]; | ||
259 | struct GNUNET_PseudonymSignaturePurpose *purpose; | ||
260 | struct GNUNET_HashCode seed; | ||
261 | struct GNUNET_HashCode signing_key; | ||
262 | |||
263 | memset (data, 42, sizeof (data)); | ||
264 | purpose = (struct GNUNET_PseudonymSignaturePurpose *) data; | ||
265 | purpose->size = htonl (sizeof (data)); | ||
266 | purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
267 | memset (&seed, 41, sizeof (seed)); | ||
268 | memset (&signing_key, 40, sizeof (signing_key)); | ||
269 | test_signature (ph, purpose, &seed, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]); | ||
270 | test_signature (ph, purpose, NULL, &signing_key, &data[sizeof (struct GNUNET_PseudonymSignaturePurpose)]); | ||
271 | } | ||
272 | |||
273 | |||
274 | /** | ||
275 | * Test cryptographic operations. | ||
276 | */ | ||
277 | static int | ||
278 | test_crypto () | ||
279 | { | ||
280 | struct GNUNET_PseudonymHandle *ph; | ||
281 | struct GNUNET_PseudonymIdentifier pseudonym; | ||
282 | struct GNUNET_PseudonymIdentifier pseudonym2; | ||
283 | |||
284 | /* check writing to and reading from disk */ | ||
285 | ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa"); | ||
286 | GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym); | ||
287 | GNUNET_PSEUDONYM_destroy (ph); | ||
288 | ph = GNUNET_PSEUDONYM_create ("/tmp/gnunet-pseudonym-test/pseu.dsa"); | ||
289 | GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2); | ||
290 | test_crypto_ops (ph); | ||
291 | GNUNET_PSEUDONYM_destroy (ph); | ||
292 | if (0 != memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym))) | ||
293 | return 1; | ||
294 | |||
295 | /* check in-memory generation */ | ||
296 | ph = GNUNET_PSEUDONYM_create (NULL); | ||
297 | GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2); | ||
298 | if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym))) | ||
299 | return 1; | ||
300 | test_crypto_ops (ph); | ||
301 | GNUNET_PSEUDONYM_destroy (ph); | ||
302 | |||
303 | /* check anonymous pseudonym operations generation */ | ||
304 | ph = GNUNET_PSEUDONYM_get_anonymous_pseudonym_handle (); | ||
305 | GNUNET_PSEUDONYM_get_identifier (ph, &pseudonym2); | ||
306 | if (0 == memcmp (&pseudonym, &pseudonym2, sizeof (pseudonym))) | ||
307 | return 1; | ||
308 | test_crypto_ops (ph); | ||
309 | GNUNET_PSEUDONYM_destroy (ph); | ||
310 | return 0; | ||
311 | } | ||
312 | |||
313 | |||
314 | int | ||
315 | main (int argc, char *argv[]) | ||
316 | { | ||
317 | if (0 != test_io ()) | ||
318 | return 1; | ||
319 | if (0 != test_crypto ()) | ||
320 | return 1; | ||
321 | GNUNET_break (GNUNET_OK == | ||
322 | GNUNET_DISK_directory_remove ("/tmp/gnunet-pseudonym-test")); | ||
323 | return 0; | ||
324 | } | ||
325 | |||
326 | |||
327 | /* end of test_pseudoynm.c */ | ||