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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
28 #include "gnunet_dnsparser_lib.h"
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36 static struct GNUNET_NAMESTORE_Handle *nsh;
38 static struct GNUNET_SCHEDULER_Task *endbadly_task;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey2;
44 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
48 static int returned_records;
50 static char *s_name_1;
52 static struct GNUNET_GNSRECORD_Data *s_rd_1;
54 static char *s_name_2;
56 static struct GNUNET_GNSRECORD_Data *s_rd_2;
58 static char *s_name_3;
60 static struct GNUNET_GNSRECORD_Data *s_rd_3;
64 * Re-establish the connection to the service.
66 * @param cls handle to use to re-connect.
67 * @param tc scheduler context
73 GNUNET_SCHEDULER_shutdown ();
83 GNUNET_NAMESTORE_zone_iteration_stop (zi);
86 if (NULL != endbadly_task)
88 GNUNET_SCHEDULER_cancel (endbadly_task);
93 GNUNET_free (privkey);
98 GNUNET_free (privkey2);
101 GNUNET_free_non_null (s_name_1);
102 GNUNET_free_non_null (s_name_2);
103 GNUNET_free_non_null (s_name_3);
106 GNUNET_free ((void *) s_rd_1->data);
107 GNUNET_free (s_rd_1);
111 GNUNET_free ((void *) s_rd_2->data);
112 GNUNET_free (s_rd_2);
116 GNUNET_free ((void *) s_rd_3->data);
117 GNUNET_free (s_rd_3);
121 GNUNET_NAMESTORE_disconnect (nsh);
130 GNUNET_break (3 == returned_records);
131 if (3 == returned_records)
133 res = 0; /* Last iteraterator callback, we are done */
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Received last result, iteration done after receing %u results\n",
142 GNUNET_SCHEDULER_shutdown ();
154 zone_proc (void *cls,
155 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
157 unsigned int rd_count,
158 const struct GNUNET_GNSRECORD_Data *rd)
160 int failed = GNUNET_NO;
162 GNUNET_assert (NULL != zone);
163 if (0 == GNUNET_memcmp (zone,
166 if (0 == strcmp (label, s_name_1))
170 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
182 else if (0 == strcmp (label, s_name_2))
186 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 "Received invalid record count\n");
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Comparing result failed: got name `%s' for first zone\n",
209 else if (0 == GNUNET_memcmp (zone,
212 if (0 == strcmp (label, s_name_3))
216 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_3))
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",
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Received invalid zone\n");
247 if (failed == GNUNET_NO)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Telling namestore to send the next result\n");
252 GNUNET_NAMESTORE_zone_iterator_next (zi,
258 GNUNET_SCHEDULER_shutdown ();
271 if (success == GNUNET_OK)
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Created record %u \n",
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 "Failed to created records: `%s'\n",
284 GNUNET_SCHEDULER_shutdown ();
292 returned_records = 0;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "All records created, starting iteration over all zones \n");
295 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 "Failed to create zone iterator\n");
308 GNUNET_SCHEDULER_shutdown ();
316 static struct GNUNET_GNSRECORD_Data *
317 create_record (unsigned int count)
319 struct GNUNET_GNSRECORD_Data *rd;
321 rd = GNUNET_new_array (count,
322 struct GNUNET_GNSRECORD_Data);
323 for (unsigned int c = 0; c < count; c++)
325 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
326 GNUNET_TIME_UNIT_HOURS).abs_value_us;
327 rd[c].record_type = TEST_RECORD_TYPE;
328 rd[c].data_size = 50;
329 rd[c].data = GNUNET_malloc (50);
331 memset ((char *) rd[c].data, 'a', 50);
338 * Callback called from the zone iterator when we iterate over
339 * the empty zone. Check that we got no records and then
340 * start the actual tests by filling the zone.
343 empty_zone_proc (void *cls,
344 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
346 unsigned int rd_count,
347 const struct GNUNET_GNSRECORD_Data *rd)
349 GNUNET_assert (nsh == cls);
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 _ ("Expected empty zone but received zone private key\n"));
355 GNUNET_SCHEDULER_shutdown ();
359 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
362 _ ("Expected no zone content but received data\n"));
364 GNUNET_SCHEDULER_shutdown ();
373 empty_zone_end (void *cls)
378 GNUNET_asprintf (&hostkey_file,
381 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Using zonekey file `%s' \n",
385 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
386 GNUNET_free (hostkey_file);
387 GNUNET_assert (privkey != NULL);
389 GNUNET_asprintf (&hostkey_file,
392 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Using zonekey file `%s' \n",
396 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
397 GNUNET_free (hostkey_file);
398 GNUNET_assert (privkey2 != NULL);
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
402 GNUNET_asprintf (&s_name_1, "dummy1");
403 s_rd_1 = create_record (1);
404 GNUNET_NAMESTORE_records_store (nsh,
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Created record 2 \n");
412 GNUNET_asprintf (&s_name_2, "dummy2");
413 s_rd_2 = create_record (1);
414 GNUNET_NAMESTORE_records_store (nsh,
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Created record 3\n");
422 /* name in different zone */
423 GNUNET_asprintf (&s_name_3, "dummy3");
424 s_rd_3 = create_record (1);
425 GNUNET_NAMESTORE_records_store (nsh,
437 const struct GNUNET_CONFIGURATION_Handle *cfg,
438 struct GNUNET_TESTING_Peer *peer)
440 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
443 GNUNET_SCHEDULER_add_shutdown (&end,
446 nsh = GNUNET_NAMESTORE_connect (cfg);
447 GNUNET_break (NULL != nsh);
448 /* first, iterate over empty namestore */
449 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 "Failed to create zone iterator\n");
463 GNUNET_SCHEDULER_shutdown ();
468 #include "test_common.c"
472 main (int argc, char *argv[])
474 const char *plugin_name;
477 SETUP_CFG (plugin_name, 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 */