2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * endbadly_task;
37 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
41 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
45 static int returned_records;
47 static char * s_name_1;
49 static struct GNUNET_GNSRECORD_Data *s_rd_1;
51 static char * s_name_2;
53 static struct GNUNET_GNSRECORD_Data *s_rd_2;
55 static char * s_name_3;
57 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59 static char *directory;
63 * Re-establish the connection to the service.
65 * @param cls handle to use to re-connect.
66 * @param tc scheduler context
73 GNUNET_NAMESTORE_zone_iteration_stop (zi);
78 GNUNET_NAMESTORE_disconnect (nsh);
81 GNUNET_free_non_null(s_name_1);
82 GNUNET_free_non_null(s_name_2);
83 GNUNET_free_non_null(s_name_3);
87 GNUNET_free ((void *)s_rd_1->data);
92 GNUNET_free ((void *)s_rd_2->data);
97 GNUNET_free ((void *)s_rd_3->data);
102 GNUNET_free (privkey);
105 if (privkey2 != NULL)
106 GNUNET_free (privkey2);
117 GNUNET_NAMESTORE_zone_iteration_stop (zi);
120 if (endbadly_task != NULL)
122 GNUNET_SCHEDULER_cancel (endbadly_task);
123 endbadly_task = NULL;
127 GNUNET_free (privkey);
130 if (privkey2 != NULL)
131 GNUNET_free (privkey2);
134 GNUNET_free (s_name_1);
135 GNUNET_free (s_name_2);
136 GNUNET_free (s_name_3);
139 GNUNET_free ((void *)s_rd_1->data);
140 GNUNET_free (s_rd_1);
144 GNUNET_free ((void *)s_rd_2->data);
145 GNUNET_free (s_rd_2);
149 GNUNET_free ((void *)s_rd_3->data);
150 GNUNET_free (s_rd_3);
153 GNUNET_NAMESTORE_disconnect (nsh);
161 GNUNET_break (3 == returned_records);
162 if (3 == returned_records)
164 res = 0; /* Last iteraterator callback, we are done */
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Received last result, iteration done after receing %u results\n",
173 GNUNET_SCHEDULER_add_now (&end, NULL);
185 zone_proc (void *cls,
186 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
188 unsigned int rd_count,
189 const struct GNUNET_GNSRECORD_Data *rd)
191 int failed = GNUNET_NO;
193 GNUNET_assert (NULL != zone);
194 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
196 if (0 == strcmp (label, s_name_1))
200 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
212 else if (0 == strcmp (label, s_name_2))
216 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Received invalid record count\n");
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Comparing result failed: got name `%s' for first zone\n", label);
238 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
240 if (0 == strcmp (label, s_name_3))
244 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "Received invalid record count\n");
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Comparing result failed: got name `%s' for first zone\n", label);
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "Received invalid zone\n");
274 if (failed == GNUNET_NO)
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Telling namestore to send the next result\n");
279 GNUNET_NAMESTORE_zone_iterator_next (zi);
284 GNUNET_SCHEDULER_add_now (&end, NULL);
290 put_cont (void *cls, int32_t success, const char *emsg)
294 if (success == GNUNET_OK)
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
304 if (NULL != endbadly_task)
305 GNUNET_SCHEDULER_cancel (endbadly_task);
306 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
313 returned_records = 0;
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
315 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
327 if (NULL != endbadly_task)
328 GNUNET_SCHEDULER_cancel (endbadly_task);
329 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
336 static struct GNUNET_GNSRECORD_Data *
337 create_record (unsigned int count)
340 struct GNUNET_GNSRECORD_Data * rd;
342 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
343 for (c = 0; c < count; c++)
345 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
346 rd[c].record_type = 1111;
347 rd[c].data_size = 50;
348 rd[c].data = GNUNET_malloc(50);
350 memset ((char *) rd[c].data, 'a', 50);
357 * Callback called from the zone iterator when we iterate over
358 * the empty zone. Check that we got no records and then
359 * start the actual tests by filling the zone.
362 empty_zone_proc (void *cls,
363 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
365 unsigned int rd_count,
366 const struct GNUNET_GNSRECORD_Data *rd)
368 GNUNET_assert (nsh == cls);
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _("Expected empty zone but received zone private key\n"));
374 if (endbadly_task != NULL)
375 GNUNET_SCHEDULER_cancel (endbadly_task);
376 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
379 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Expected no zone content but received data\n"));
384 if (endbadly_task != NULL)
385 GNUNET_SCHEDULER_cancel (endbadly_task);
386 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
394 empty_zone_end (void *cls)
399 GNUNET_asprintf (&hostkey_file,
402 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Using zonekey file `%s' \n",
406 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
407 GNUNET_free (hostkey_file);
408 GNUNET_assert (privkey != NULL);
410 GNUNET_asprintf (&hostkey_file,
413 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
415 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
416 GNUNET_free (hostkey_file);
417 GNUNET_assert (privkey2 != NULL);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
421 GNUNET_asprintf(&s_name_1, "dummy1");
422 s_rd_1 = create_record(1);
423 GNUNET_NAMESTORE_records_store (nsh,
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Created record 2 \n");
431 GNUNET_asprintf(&s_name_2, "dummy2");
432 s_rd_2 = create_record(1);
433 GNUNET_NAMESTORE_records_store (nsh,
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Created record 3\n");
443 /* name in different zone */
444 GNUNET_asprintf(&s_name_3, "dummy3");
445 s_rd_3 = create_record(1);
446 GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
454 const struct GNUNET_CONFIGURATION_Handle *cfg,
455 struct GNUNET_TESTING_Peer *peer)
458 GNUNET_assert (GNUNET_OK ==
459 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS",
462 GNUNET_DISK_directory_remove (directory);
464 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
465 nsh = GNUNET_NAMESTORE_connect (cfg);
466 GNUNET_break (NULL != nsh);
467 /* first, iterate over empty namestore */
468 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
480 GNUNET_SCHEDULER_cancel (endbadly_task);
481 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
487 main (int argc, char *argv[])
491 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
492 "test_namestore_api.conf",
498 if (NULL != directory)
500 GNUNET_DISK_directory_remove (directory);
501 GNUNET_free (directory);
507 /* end of test_namestore_api_zone_iteration.c */