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 "namestore.h"
29 #define VERBOSE GNUNET_NO
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
33 static struct GNUNET_NAMESTORE_Handle * nsh;
35 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
36 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
37 static struct GNUNET_OS_Process *arm;
39 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
41 static GNUNET_HashCode zone;
43 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey2;
44 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey2;
45 static GNUNET_HashCode zone2;
47 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
49 static int returned_records;
51 struct GNUNET_CRYPTO_RsaSignature *sig_1;
53 struct GNUNET_NAMESTORE_RecordData *s_rd_1;
55 struct GNUNET_CRYPTO_RsaSignature *sig_2;
57 struct GNUNET_NAMESTORE_RecordData *s_rd_2;
59 struct GNUNET_CRYPTO_RsaSignature *sig_3;
61 struct GNUNET_NAMESTORE_RecordData *s_rd_3;
64 start_arm (const char *cfgname)
66 arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
67 "gnunet-service-arm", "-c", cfgname,
81 if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
82 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
83 GNUNET_OS_process_wait (arm);
84 GNUNET_OS_process_close (arm);
90 * Re-establish the connection to the service.
92 * @param cls handle to use to re-connect.
93 * @param tc scheduler context
96 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (stopiteration_task);
101 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
105 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
108 GNUNET_free_non_null(sig_1);
109 GNUNET_free_non_null(sig_2);
110 GNUNET_free_non_null(sig_3);
111 GNUNET_free_non_null(s_name_1);
112 GNUNET_free_non_null(s_name_2);
113 GNUNET_free_non_null(s_name_3);
117 GNUNET_free ((void *)s_rd_1->data);
118 GNUNET_free (s_rd_1);
122 GNUNET_free ((void *)s_rd_2->data);
123 GNUNET_free (s_rd_2);
127 GNUNET_free ((void *)s_rd_3->data);
128 GNUNET_free (s_rd_3);
132 GNUNET_CRYPTO_rsa_key_free (privkey);
135 if (privkey2 != NULL)
136 GNUNET_CRYPTO_rsa_key_free (privkey2);
147 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel (stopiteration_task);
152 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
155 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
157 GNUNET_SCHEDULER_cancel (endbadly_task);
158 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
162 GNUNET_CRYPTO_rsa_key_free (privkey);
165 if (privkey2 != NULL)
166 GNUNET_CRYPTO_rsa_key_free (privkey2);
172 GNUNET_free (s_name_1);
173 GNUNET_free (s_name_2);
174 GNUNET_free (s_name_3);
177 GNUNET_free ((void *)s_rd_1->data);
178 GNUNET_free (s_rd_1);
182 GNUNET_free ((void *)s_rd_2->data);
183 GNUNET_free (s_rd_2);
187 GNUNET_free ((void *)s_rd_3->data);
188 GNUNET_free (s_rd_3);
192 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
197 if (returned_records == 1)
204 void zone_proc (void *cls,
205 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
206 struct GNUNET_TIME_Absolute expire,
208 unsigned int rd_count,
209 const struct GNUNET_NAMESTORE_RecordData *rd,
210 const struct GNUNET_CRYPTO_RsaSignature *signature)
212 int failed = GNUNET_NO;
214 if ((zone_key == NULL) && (name == NULL))
216 GNUNET_break (3 == returned_records);
217 if (3 == returned_records)
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after receing %u results\n",returned_records );
223 GNUNET_SCHEDULER_add_now (&end, NULL);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
228 if (0 == strcmp (name, s_name_1))
232 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
243 if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
249 else if (0 == strcmp (name, s_name_2))
253 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
264 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_verify_signature(&pubkey2, s_name_2, rd_count, rd, sig_2));
265 if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
271 else if (0 == strcmp (name, s_name_3))
275 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
286 if (0 != memcmp (signature, sig_3, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
297 GNUNET_SCHEDULER_add_now (&end, NULL);
300 if (failed == GNUNET_NO)
304 if (1 == returned_records)
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping zone iteration after %u received record \n",returned_records );
307 GNUNET_NAMESTORE_zone_iteration_stop (zi);
308 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
310 GNUNET_SCHEDULER_cancel (endbadly_task);
311 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end , NULL);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
319 GNUNET_NAMESTORE_zone_iterator_next (zi);
325 GNUNET_SCHEDULER_add_now (&end, NULL);
331 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
336 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
337 "FILENAME", &afsdir))
339 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
340 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
341 if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
343 GNUNET_free (afsdir);
349 put_cont (void *cls, int32_t success, const char *emsg)
353 if (success == GNUNET_OK)
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
362 GNUNET_SCHEDULER_cancel (endbadly_task);
363 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
369 returned_records = 0;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
371 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
373 GNUNET_NAMESTORE_RF_NONE,
374 GNUNET_NAMESTORE_RF_NONE,
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
381 GNUNET_SCHEDULER_cancel (endbadly_task);
382 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
387 static struct GNUNET_NAMESTORE_RecordData *
388 create_record (int count)
391 struct GNUNET_NAMESTORE_RecordData * rd;
392 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
394 for (c = 0; c < count; c++)
396 rd[c].expiration = GNUNET_TIME_absolute_get();
397 rd[c].record_type = 1111;
398 rd[c].data_size = 50;
399 rd[c].data = GNUNET_malloc(50);
400 memset ((char *) rd[c].data, 'a', 50);
406 run (void *cls, char *const *args, const char *cfgfile,
407 const struct GNUNET_CONFIGURATION_Handle *cfg)
409 delete_existing_db(cfg);
410 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
413 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, "4UCICULTINKC87UO4326KEEDQ9MTEP2AJT88MJFVGTGNK12QNGMQI2S41VI07UUU6EO19BTB06PDL0HE6VP1OM50HOJEI75RHP4JP80.zone");
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
415 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
416 GNUNET_free (hostkey_file);
417 GNUNET_assert (privkey != NULL);
418 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
419 GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
421 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, "KJI3AL00K91EDPFJF58DAJM7H61D189TLP70N56JL8SVDCJE1SJ3SNNBOQPPONTL37FMHPS39SMK2NMVC0GQMGA6QCMHITT78O8GF80.zone");
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
423 privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
424 GNUNET_free (hostkey_file);
425 GNUNET_assert (privkey2 != NULL);
426 GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
427 GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
431 GNUNET_assert (arm != NULL);
433 nsh = GNUNET_NAMESTORE_connect (cfg);
434 GNUNET_break (NULL != nsh);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
438 GNUNET_asprintf(&s_name_1, "dummy1");
439 s_rd_1 = create_record(1);
440 sig_1 = GNUNET_NAMESTORE_create_signature(privkey, s_name_1, s_rd_1, 1);
441 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
445 GNUNET_asprintf(&s_name_2, "dummy2");
446 s_rd_2 = create_record(1);
448 sig_2 = GNUNET_NAMESTORE_create_signature(privkey, s_name_2, s_rd_2, 1);
449 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
452 /* name in different zone */
453 GNUNET_asprintf(&s_name_3, "dummy3");
454 s_rd_3 = create_record(1);
455 sig_3 = GNUNET_NAMESTORE_create_signature(privkey, s_name_3, s_rd_3, 1);
456 GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_absolute_get_forever(), 1, s_rd_3, sig_3, &put_cont, NULL);
462 static char *const argv[] = { "test_namestore_api_zone_iteration",
464 "test_namestore_api.conf",
470 static struct GNUNET_GETOPT_CommandLineOption options[] = {
471 GNUNET_GETOPT_OPTION_END
475 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_api_zone_iteration",
476 "nohelp", options, &run, &res);
481 main (int argc, char *argv[])
490 /* end of test_namestore_api_zone_iteration.c */