2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018 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;
61 * Re-establish the connection to the service.
63 * @param cls handle to use to re-connect.
64 * @param tc scheduler context
70 GNUNET_SCHEDULER_shutdown ();
80 GNUNET_NAMESTORE_zone_iteration_stop (zi);
83 if (NULL != endbadly_task)
85 GNUNET_SCHEDULER_cancel (endbadly_task);
90 GNUNET_free (privkey);
95 GNUNET_free (privkey2);
98 GNUNET_free (s_name_1);
99 GNUNET_free (s_name_2);
100 GNUNET_free (s_name_3);
103 GNUNET_free ((void *)s_rd_1->data);
104 GNUNET_free (s_rd_1);
108 GNUNET_free ((void *)s_rd_2->data);
109 GNUNET_free (s_rd_2);
113 GNUNET_free ((void *)s_rd_3->data);
114 GNUNET_free (s_rd_3);
118 GNUNET_NAMESTORE_disconnect (nsh);
127 GNUNET_break (3 == returned_records);
128 if (3 == returned_records)
130 res = 0; /* Last iteraterator callback, we are done */
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Received last result, iteration done after receing %u results\n",
139 GNUNET_SCHEDULER_shutdown ();
151 zone_proc (void *cls,
152 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
154 unsigned int rd_count,
155 const struct GNUNET_GNSRECORD_Data *rd)
157 int failed = GNUNET_NO;
159 GNUNET_assert (NULL != zone);
160 if (0 == memcmp (zone,
162 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
164 if (0 == strcmp (label, s_name_1))
168 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
180 else if (0 == strcmp (label, s_name_2))
184 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 "Received invalid record count\n");
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Comparing result failed: got name `%s' for first zone\n",
207 else if (0 == memcmp (zone,
209 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
211 if (0 == strcmp (label, s_name_3))
215 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 "Received invalid record count\n");
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Comparing result failed: got name `%s' for first zone\n",
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "Received invalid zone\n");
246 if (failed == GNUNET_NO)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Telling namestore to send the next result\n");
251 GNUNET_NAMESTORE_zone_iterator_next (zi,
257 GNUNET_SCHEDULER_shutdown ();
270 if (success == GNUNET_OK)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Created record %u \n",
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 "Failed to created records: `%s'\n",
283 GNUNET_SCHEDULER_shutdown ();
291 returned_records = 0;
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "All records created, starting iteration over all zones \n");
294 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "Failed to create zone iterator\n");
307 GNUNET_SCHEDULER_shutdown ();
315 static struct GNUNET_GNSRECORD_Data *
316 create_record (unsigned int count)
318 struct GNUNET_GNSRECORD_Data * rd;
320 rd = GNUNET_new_array (count,
321 struct GNUNET_GNSRECORD_Data);
322 for (unsigned int c = 0; c < count; c++)
324 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
325 rd[c].record_type = 1111;
326 rd[c].data_size = 50;
327 rd[c].data = GNUNET_malloc(50);
329 memset ((char *) rd[c].data, 'a', 50);
336 * Callback called from the zone iterator when we iterate over
337 * the empty zone. Check that we got no records and then
338 * start the actual tests by filling the zone.
341 empty_zone_proc (void *cls,
342 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
344 unsigned int rd_count,
345 const struct GNUNET_GNSRECORD_Data *rd)
347 GNUNET_assert (nsh == cls);
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 _("Expected empty zone but received zone private key\n"));
353 GNUNET_SCHEDULER_shutdown ();
357 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Expected no zone content but received data\n"));
362 GNUNET_SCHEDULER_shutdown ();
371 empty_zone_end (void *cls)
376 GNUNET_asprintf (&hostkey_file,
379 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Using zonekey file `%s' \n",
383 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
384 GNUNET_free (hostkey_file);
385 GNUNET_assert (privkey != NULL);
387 GNUNET_asprintf (&hostkey_file,
390 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Using zonekey file `%s' \n",
394 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
395 GNUNET_free (hostkey_file);
396 GNUNET_assert (privkey2 != NULL);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
400 GNUNET_asprintf(&s_name_1, "dummy1");
401 s_rd_1 = create_record(1);
402 GNUNET_NAMESTORE_records_store (nsh,
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Created record 2 \n");
410 GNUNET_asprintf(&s_name_2, "dummy2");
411 s_rd_2 = create_record(1);
412 GNUNET_NAMESTORE_records_store (nsh,
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Created record 3\n");
420 /* name in different zone */
421 GNUNET_asprintf(&s_name_3, "dummy3");
422 s_rd_3 = create_record(1);
423 GNUNET_NAMESTORE_records_store (nsh,
435 const struct GNUNET_CONFIGURATION_Handle *cfg,
436 struct GNUNET_TESTING_Peer *peer)
438 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
441 GNUNET_SCHEDULER_add_shutdown (&end,
444 nsh = GNUNET_NAMESTORE_connect (cfg);
445 GNUNET_break (NULL != nsh);
446 /* first, iterate over empty namestore */
447 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 "Failed to create zone iterator\n");
461 GNUNET_SCHEDULER_shutdown ();
467 main (int argc, char *argv[])
469 const char *plugin_name;
472 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
473 GNUNET_asprintf (&cfg_name,
474 "test_namestore_api_%s.conf",
476 GNUNET_DISK_purge_cfg_dir (cfg_name,
480 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
487 GNUNET_DISK_purge_cfg_dir (cfg_name,
489 GNUNET_free (cfg_name);
494 /* end of test_namestore_api_zone_iteration.c */