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_specific_zone.c
22 * @brief testcase for zone iteration functionality: iterate over a specific zone
23 * @author Matthias Wachs
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34 static struct GNUNET_NAMESTORE_Handle * nsh;
36 static struct GNUNET_SCHEDULER_Task * endbadly_task;
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
42 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
46 static int returned_records;
48 static char * s_name_1;
50 static struct GNUNET_GNSRECORD_Data *s_rd_1;
52 static char * s_name_2;
54 static struct GNUNET_GNSRECORD_Data *s_rd_2;
56 static char * s_name_3;
58 static struct GNUNET_GNSRECORD_Data *s_rd_3;
60 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);
77 GNUNET_NAMESTORE_disconnect (nsh);
80 GNUNET_free_non_null(s_name_1);
81 GNUNET_free_non_null(s_name_2);
82 GNUNET_free_non_null(s_name_3);
86 GNUNET_free ((void *)s_rd_1->data);
91 GNUNET_free ((void *)s_rd_2->data);
96 GNUNET_free ((void *)s_rd_3->data);
101 GNUNET_free (privkey);
104 if (privkey2 != NULL)
105 GNUNET_free (privkey2);
116 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);
166 zone_proc (void *cls,
167 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
169 unsigned int rd_count,
170 const struct GNUNET_GNSRECORD_Data *rd)
172 int failed = GNUNET_NO;
174 GNUNET_assert (NULL != zone);
175 if (0 == memcmp (zone,
177 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
179 if (0 == strcmp (label, s_name_1))
183 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
195 else if (0 == strcmp (label, s_name_2))
199 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 "Received invalid record count\n");
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Comparing result failed: got name `%s' for first zone\n",
222 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Received data for not requested zone\n");
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Received invalid zone\n");
236 if (failed == GNUNET_NO)
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Telling namestore to send the next result\n");
241 GNUNET_NAMESTORE_zone_iterator_next (zi);
246 GNUNET_SCHEDULER_add_now (&end, NULL);
252 zone_proc_end (void *cls)
254 GNUNET_break (2 == returned_records);
255 if (2 == returned_records)
257 res = 0; /* Last iteraterator callback, we are done */
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Received last result, iteration done after receing %u results\n",
266 GNUNET_SCHEDULER_add_now (&end, NULL);
271 put_cont (void *cls, int32_t success, const char *emsg)
275 if (success == GNUNET_OK)
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Created record %u \n", c);
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "Failed to created records: `%s'\n",
287 if (NULL != endbadly_task)
288 GNUNET_SCHEDULER_cancel (endbadly_task);
289 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
296 returned_records = 0;
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "All records created, starting iteration over all zones \n");
299 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "Failed to create zone iterator\n");
312 if (NULL != endbadly_task)
313 GNUNET_SCHEDULER_cancel (endbadly_task);
314 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
321 static struct GNUNET_GNSRECORD_Data *
322 create_record (unsigned int count)
325 struct GNUNET_GNSRECORD_Data * rd;
327 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
328 for (c = 0; c < count; c++)
330 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
331 rd[c].record_type = 1111;
332 rd[c].data_size = 50;
333 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_EcdsaPrivateKey *zone,
350 unsigned int rd_count,
351 const struct GNUNET_GNSRECORD_Data *rd)
353 GNUNET_assert (nsh == cls);
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357 _("Expected empty zone but received zone private key\n"));
359 if (endbadly_task != NULL)
360 GNUNET_SCHEDULER_cancel (endbadly_task);
361 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
364 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 _("Expected no zone content but received data\n"));
369 if (endbadly_task != NULL)
370 GNUNET_SCHEDULER_cancel (endbadly_task);
371 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
379 empty_zone_proc_end (void *cls)
384 GNUNET_asprintf (&hostkey_file,
387 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Using zonekey file `%s'\n",
391 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
392 GNUNET_free (hostkey_file);
393 GNUNET_assert (privkey != NULL);
395 GNUNET_asprintf(&hostkey_file,
398 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Using zonekey file `%s' \n",
402 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
403 GNUNET_free (hostkey_file);
404 GNUNET_assert (privkey2 != NULL);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Created record 1\n");
409 GNUNET_asprintf(&s_name_1, "dummy1");
410 s_rd_1 = create_record (1);
411 GNUNET_NAMESTORE_records_store (nsh,
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Created record 2 \n");
421 GNUNET_asprintf(&s_name_2, "dummy2");
422 s_rd_2 = create_record (1);
423 GNUNET_NAMESTORE_records_store (nsh,
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Created record 3\n");
434 /* name in different zone */
435 GNUNET_asprintf(&s_name_3, "dummy3");
436 s_rd_3 = create_record (1);
437 GNUNET_NAMESTORE_records_store (nsh,
448 const struct GNUNET_CONFIGURATION_Handle *cfg,
449 struct GNUNET_TESTING_Peer *peer)
452 GNUNET_assert (GNUNET_OK ==
453 GNUNET_CONFIGURATION_get_value_string (cfg,
457 GNUNET_DISK_directory_remove (directory);
459 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
462 nsh = GNUNET_NAMESTORE_connect (cfg);
463 GNUNET_break (NULL != nsh);
464 /* first, iterate over empty namestore */
465 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
471 &empty_zone_proc_end,
475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
476 "Failed to create zone iterator\n");
478 GNUNET_SCHEDULER_cancel (endbadly_task);
479 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
485 main (int argc, char *argv[])
489 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
490 "test_namestore_api.conf",
496 if (NULL != directory)
498 GNUNET_DISK_directory_remove (directory);
499 GNUNET_free (directory);
504 /* end of test_namestore_api_zone_iteration_specific_zone.c */