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 all zones
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
33 static struct GNUNET_NAMESTORE_Handle * nsh;
35 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
39 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
41 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
45 static int returned_records;
47 static char * s_name_1;
49 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
51 static char * s_name_2;
53 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
55 static char * s_name_3;
57 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
61 * Re-establish the connection to the service.
63 * @param cls handle to use to re-connect.
64 * @param tc scheduler context
67 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 GNUNET_NAMESTORE_zone_iteration_stop (zi);
76 GNUNET_NAMESTORE_disconnect (nsh);
79 GNUNET_free_non_null(s_name_1);
80 GNUNET_free_non_null(s_name_2);
81 GNUNET_free_non_null(s_name_3);
85 GNUNET_free ((void *)s_rd_1->data);
90 GNUNET_free ((void *)s_rd_2->data);
95 GNUNET_free ((void *)s_rd_3->data);
100 GNUNET_free (privkey);
103 if (privkey2 != NULL)
104 GNUNET_free (privkey2);
111 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 GNUNET_NAMESTORE_zone_iteration_stop (zi);
119 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
121 GNUNET_SCHEDULER_cancel (endbadly_task);
122 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
126 GNUNET_free (privkey);
129 if (privkey2 != NULL)
130 GNUNET_free (privkey2);
133 GNUNET_free (s_name_1);
134 GNUNET_free (s_name_2);
135 GNUNET_free (s_name_3);
138 GNUNET_free ((void *)s_rd_1->data);
139 GNUNET_free (s_rd_1);
143 GNUNET_free ((void *)s_rd_2->data);
144 GNUNET_free (s_rd_2);
148 GNUNET_free ((void *)s_rd_3->data);
149 GNUNET_free (s_rd_3);
152 GNUNET_NAMESTORE_disconnect (nsh);
158 zone_proc (void *cls,
159 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
161 unsigned int rd_count,
162 const struct GNUNET_NAMESTORE_RecordData *rd)
164 int failed = GNUNET_NO;
165 if ((zone == NULL) && (label == NULL))
167 GNUNET_break (3 == returned_records);
168 if (3 == returned_records)
170 res = 0; /* Last iteraterator callback, we are done */
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Received last result, iteration done after receing %u results\n",
179 GNUNET_SCHEDULER_add_now (&end, NULL);
184 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
186 if (0 == strcmp (label, s_name_1))
190 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
202 else if (0 == strcmp (label, s_name_2))
206 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 "Received invalid record count\n");
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Comparing result failed: got name `%s' for first zone\n", label);
228 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
230 if (0 == strcmp (label, s_name_3))
234 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Received invalid record count\n");
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Comparing result failed: got name `%s' for first zone\n", label);
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
264 if (failed == GNUNET_NO)
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Telling namestore to send the next result\n");
269 GNUNET_NAMESTORE_zone_iterator_next (zi);
274 GNUNET_SCHEDULER_add_now (&end, NULL);
280 put_cont (void *cls, int32_t success, const char *emsg)
284 if (success == GNUNET_OK)
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
294 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
295 GNUNET_SCHEDULER_cancel (endbadly_task);
296 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
303 returned_records = 0;
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
305 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
313 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
314 GNUNET_SCHEDULER_cancel (endbadly_task);
315 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
322 static struct GNUNET_NAMESTORE_RecordData *
323 create_record (unsigned int count)
326 struct GNUNET_NAMESTORE_RecordData * rd;
328 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
329 for (c = 0; c < count; c++)
331 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
332 rd[c].record_type = 1111;
333 rd[c].data_size = 50;
334 rd[c].data = GNUNET_malloc(50);
335 memset ((char *) rd[c].data, 'a', 50);
342 * Callback called from the zone iterator when we iterate over
343 * the empty zone. Check that we got no records and then
344 * start the actual tests by filling the zone.
347 empty_zone_proc (void *cls,
348 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
350 unsigned int rd_count,
351 const struct GNUNET_NAMESTORE_RecordData *rd)
355 GNUNET_assert (nsh == cls);
358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
359 _("Expected empty zone but received zone private key\n"));
361 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
362 GNUNET_SCHEDULER_cancel (endbadly_task);
363 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
366 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 _("Expected no zone content but received data\n"));
371 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
372 GNUNET_SCHEDULER_cancel (endbadly_task);
373 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
379 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
380 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
382 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
383 GNUNET_free (hostkey_file);
384 GNUNET_assert (privkey != NULL);
386 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
387 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
389 privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
390 GNUNET_free (hostkey_file);
391 GNUNET_assert (privkey2 != NULL);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
395 GNUNET_asprintf(&s_name_1, "dummy1");
396 s_rd_1 = create_record(1);
397 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
398 1, s_rd_1, &put_cont, NULL);
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
401 GNUNET_asprintf(&s_name_2, "dummy2");
402 s_rd_2 = create_record(1);
403 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
404 1, s_rd_2, &put_cont, NULL);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
408 /* name in different zone */
409 GNUNET_asprintf(&s_name_3, "dummy3");
410 s_rd_3 = create_record(1);
411 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
412 1, s_rd_3, &put_cont, NULL);
418 const struct GNUNET_CONFIGURATION_Handle *cfg,
419 struct GNUNET_TESTING_Peer *peer)
421 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
422 nsh = GNUNET_NAMESTORE_connect (cfg);
423 GNUNET_break (NULL != nsh);
424 /* first, iterate over empty namestore */
425 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
426 NULL, &empty_zone_proc, nsh);
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
431 GNUNET_SCHEDULER_cancel (endbadly_task);
432 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
438 main (int argc, char *argv[])
442 GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration",
444 "test_namestore_api.conf",
452 /* end of test_namestore_api_zone_iteration.c */