2 This file is part of GNUnet.
3 (C) 2009 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.
21 * @file namestore/test_namestore_api_zone_iteration.c
22 * @brief testcase for zone iteration functionality: iterate of a specific zone
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib-new.h"
28 #include "namestore.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
34 static struct GNUNET_NAMESTORE_Handle * nsh;
36 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
38 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
40 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
42 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
44 static struct GNUNET_HashCode zone;
46 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey2;
48 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey2;
50 static struct GNUNET_HashCode zone2;
52 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
56 static int returned_records;
58 static struct GNUNET_CRYPTO_RsaSignature *sig_1;
60 static char * s_name_1;
62 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
64 static struct GNUNET_CRYPTO_RsaSignature *sig_2;
66 static char * s_name_2;
68 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
70 static struct GNUNET_CRYPTO_RsaSignature *sig_3;
72 static char * s_name_3;
74 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
78 * Re-establish the connection to the service.
80 * @param cls handle to use to re-connect.
81 * @param tc scheduler context
84 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
88 GNUNET_SCHEDULER_cancel (stopiteration_task);
89 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
93 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
96 GNUNET_free_non_null(sig_1);
97 GNUNET_free_non_null(sig_2);
98 GNUNET_free_non_null(sig_3);
99 GNUNET_free_non_null(s_name_1);
100 GNUNET_free_non_null(s_name_2);
101 GNUNET_free_non_null(s_name_3);
105 GNUNET_free ((void *)s_rd_1->data);
106 GNUNET_free (s_rd_1);
110 GNUNET_free ((void *)s_rd_2->data);
111 GNUNET_free (s_rd_2);
115 GNUNET_free ((void *)s_rd_3->data);
116 GNUNET_free (s_rd_3);
120 GNUNET_CRYPTO_rsa_key_free (privkey);
123 if (privkey2 != NULL)
124 GNUNET_CRYPTO_rsa_key_free (privkey2);
131 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
135 GNUNET_SCHEDULER_cancel (stopiteration_task);
136 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
139 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
141 GNUNET_SCHEDULER_cancel (endbadly_task);
142 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
146 GNUNET_CRYPTO_rsa_key_free (privkey);
149 if (privkey2 != NULL)
150 GNUNET_CRYPTO_rsa_key_free (privkey2);
156 GNUNET_free (s_name_1);
157 GNUNET_free (s_name_2);
158 GNUNET_free (s_name_3);
161 GNUNET_free ((void *)s_rd_1->data);
162 GNUNET_free (s_rd_1);
166 GNUNET_free ((void *)s_rd_2->data);
167 GNUNET_free (s_rd_2);
171 GNUNET_free ((void *)s_rd_3->data);
172 GNUNET_free (s_rd_3);
175 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
181 zone_proc (void *cls,
182 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
183 struct GNUNET_TIME_Absolute expire,
185 unsigned int rd_count,
186 const struct GNUNET_NAMESTORE_RecordData *rd,
187 const struct GNUNET_CRYPTO_RsaSignature *signature)
189 int failed = GNUNET_NO;
190 if ((zone_key == NULL) && (name == NULL))
192 GNUNET_break (3 == returned_records);
193 if (3 == returned_records)
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after receing %u results\n",returned_records );
199 GNUNET_SCHEDULER_add_now (&end, NULL);
203 /* verify signature returned from name store */
204 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (zone_key, expire, name, rd_count, rd, signature))
206 struct GNUNET_HashCode zone_key_hash;
207 GNUNET_CRYPTO_hash (zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone_key_hash);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Verifying signature for `%s' in zone `%s' with %u records and expiration %llu failed\n", name, GNUNET_h2s(&zone_key_hash), rd_count, expire.abs_value);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
215 if (0 == strcmp (name, s_name_1))
219 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
230 if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
236 else if (0 == strcmp (name, s_name_2))
240 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
251 if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
257 else if (0 == strcmp (name, s_name_3))
261 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
272 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(zone_key, expire, name, rd_count, rd, signature))
278 if (0 != memcmp (signature, sig_3, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
289 GNUNET_SCHEDULER_add_now (&end, NULL);
292 if (failed == GNUNET_NO)
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
296 GNUNET_NAMESTORE_zone_iterator_next (zi);
301 GNUNET_SCHEDULER_add_now (&end, NULL);
308 put_cont (void *cls, int32_t success, const char *emsg)
312 if (success == GNUNET_OK)
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
321 GNUNET_SCHEDULER_cancel (endbadly_task);
322 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
328 returned_records = 0;
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
330 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
332 GNUNET_NAMESTORE_RF_NONE,
333 GNUNET_NAMESTORE_RF_NONE,
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
340 GNUNET_SCHEDULER_cancel (endbadly_task);
341 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
347 static struct GNUNET_NAMESTORE_RecordData *
348 create_record (unsigned int count)
351 struct GNUNET_NAMESTORE_RecordData * rd;
353 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
354 for (c = 0; c < count; c++)
356 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value;
357 rd[c].record_type = 1111;
358 rd[c].data_size = 50;
359 rd[c].data = GNUNET_malloc(50);
360 memset ((char *) rd[c].data, 'a', 50);
367 * Callback called from the zone iterator when we iterate over
368 * the empty zone. Check that we got no records and then
369 * start the actual tests by filling the zone.
372 empty_zone_proc (void *cls,
373 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
374 struct GNUNET_TIME_Absolute expire,
376 unsigned int rd_count,
377 const struct GNUNET_NAMESTORE_RecordData *rd,
378 const struct GNUNET_CRYPTO_RsaSignature *signature)
381 struct GNUNET_TIME_Absolute et;
383 GNUNET_assert (nsh == cls);
384 GNUNET_assert (NULL == name);
385 GNUNET_assert (NULL == zone_key);
387 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
388 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
390 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
391 GNUNET_free (hostkey_file);
392 GNUNET_assert (privkey != NULL);
393 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
394 GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
397 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
398 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
400 privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
401 GNUNET_free (hostkey_file);
403 GNUNET_assert (privkey2 != NULL);
404 GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
405 GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
409 GNUNET_asprintf(&s_name_1, "dummy1");
410 s_rd_1 = create_record(1);
411 et.abs_value = s_rd_1->expiration_time;
412 sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
413 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
417 GNUNET_asprintf(&s_name_2, "dummy2");
418 s_rd_2 = create_record(1);
420 et.abs_value = s_rd_2->expiration_time;
421 sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
422 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
425 /* name in different zone */
426 GNUNET_asprintf(&s_name_3, "dummy3");
427 s_rd_3 = create_record(1);
428 et.abs_value = s_rd_3->expiration_time;
429 sig_3 = GNUNET_NAMESTORE_create_signature(privkey2, et, s_name_3, s_rd_3, 1);
430 GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
436 const struct GNUNET_CONFIGURATION_Handle *cfg,
437 struct GNUNET_TESTING_Peer *peer)
439 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
440 nsh = GNUNET_NAMESTORE_connect (cfg);
441 GNUNET_break (NULL != nsh);
442 /* first, iterate over empty namestore */
443 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
445 GNUNET_NAMESTORE_RF_NONE,
446 GNUNET_NAMESTORE_RF_NONE,
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
453 GNUNET_SCHEDULER_cancel (endbadly_task);
454 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
460 main (int argc, char *argv[])
464 GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration",
466 "test_namestore_api.conf",
474 /* end of test_namestore_api_zone_iteration.c */