2 This file is part of GNUnet.
3 Copyright (C) 2009 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: stop iterating of 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
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
33 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
35 static struct GNUNET_NAMESTORE_Handle * nsh;
37 static struct GNUNET_SCHEDULER_Task * endbadly_task;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
43 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
47 static int returned_records;
49 static char * s_name_1;
51 static struct GNUNET_GNSRECORD_Data *s_rd_1;
53 static char * s_name_2;
55 static struct GNUNET_GNSRECORD_Data *s_rd_2;
57 static char * s_name_3;
59 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.
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);
121 if (endbadly_task != NULL)
123 GNUNET_SCHEDULER_cancel (endbadly_task);
124 endbadly_task = NULL;
128 GNUNET_free (privkey);
131 if (privkey2 != NULL)
132 GNUNET_free (privkey2);
135 GNUNET_free (s_name_1);
136 GNUNET_free (s_name_2);
137 GNUNET_free (s_name_3);
140 GNUNET_free ((void *)s_rd_1->data);
141 GNUNET_free (s_rd_1);
145 GNUNET_free ((void *)s_rd_2->data);
146 GNUNET_free (s_rd_2);
150 GNUNET_free ((void *)s_rd_3->data);
151 GNUNET_free (s_rd_3);
154 GNUNET_NAMESTORE_disconnect (nsh);
167 zone_proc (void *cls,
168 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
170 unsigned int rd_count,
171 const struct GNUNET_GNSRECORD_Data *rd)
173 int failed = GNUNET_NO;
175 GNUNET_assert (NULL != zone);
176 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
178 if (0 == strcmp (label, s_name_1))
182 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
194 else if (0 == strcmp (label, s_name_2))
198 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 "Received invalid record count\n");
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Comparing result failed: got name `%s' for first zone\n", label);
220 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
222 if (0 == strcmp (label, s_name_3))
226 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Received invalid record count\n");
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Comparing result failed: got name `%s' for first zone\n", label);
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 "Received invalid zone\n");
255 if (failed == GNUNET_NO)
257 if (1 == returned_records)
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Telling namestore to stop zone iteration\n");
261 GNUNET_NAMESTORE_zone_iteration_stop (zi);
263 if (NULL != endbadly_task)
265 GNUNET_SCHEDULER_cancel (endbadly_task);
266 endbadly_task = NULL;
269 GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Telling namestore to send the next result\n");
275 GNUNET_NAMESTORE_zone_iterator_next (zi,
281 GNUNET_SCHEDULER_add_now (&end, NULL);
287 zone_proc_end (void *cls)
289 GNUNET_break (1 <= returned_records);
290 if (1 >= returned_records)
291 res = 1; /* Last iteraterator callback, we are done */
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Received last result, iteration done after receing %u results\n",
298 GNUNET_SCHEDULER_add_now (&end, NULL);
303 put_cont (void *cls, int32_t success, const char *emsg)
307 if (success == GNUNET_OK)
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
317 if (NULL != endbadly_task)
318 GNUNET_SCHEDULER_cancel (endbadly_task);
319 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
326 returned_records = 0;
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "All records created, starting iteration over all zones \n");
329 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 "Failed to create zone iterator\n");
342 if (NULL != endbadly_task)
343 GNUNET_SCHEDULER_cancel (endbadly_task);
344 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
351 static struct GNUNET_GNSRECORD_Data *
352 create_record (unsigned int count)
354 struct GNUNET_GNSRECORD_Data *rd;
356 rd = GNUNET_new_array (count,
357 struct GNUNET_GNSRECORD_Data);
358 for (unsigned int c = 0; c < count; c++)
360 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
361 rd[c].record_type = TEST_RECORD_TYPE;
362 rd[c].data_size = 50;
363 rd[c].data = GNUNET_malloc(50);
365 memset ((char *) rd[c].data, 'a', 50);
372 * Callback called from the zone iterator when we iterate over
373 * the empty zone. Check that we got no records and then
374 * start the actual tests by filling the zone.
377 empty_zone_proc (void *cls,
378 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
380 unsigned int rd_count,
381 const struct GNUNET_GNSRECORD_Data *rd)
384 GNUNET_assert (nsh == cls);
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
388 _("Expected empty zone but received zone private key\n"));
390 if (endbadly_task != NULL)
391 GNUNET_SCHEDULER_cancel (endbadly_task);
392 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
395 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398 _("Expected no zone content but received data\n"));
400 if (endbadly_task != NULL)
401 GNUNET_SCHEDULER_cancel (endbadly_task);
402 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
410 empty_zone_proc_end (void *cls)
414 GNUNET_assert (nsh == cls);
416 GNUNET_asprintf(&hostkey_file,
419 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Using zonekey file `%s' \n",
423 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
424 GNUNET_free (hostkey_file);
425 GNUNET_assert (privkey != NULL);
427 GNUNET_asprintf (&hostkey_file,
430 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Using zonekey file `%s'\n",
434 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
435 GNUNET_free (hostkey_file);
436 GNUNET_assert (privkey2 != NULL);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Created record 1\n");
441 GNUNET_asprintf(&s_name_1,
443 s_rd_1 = create_record(1);
444 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
445 1, s_rd_1, &put_cont, NULL);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Created record 2 \n");
449 GNUNET_asprintf(&s_name_2,
451 s_rd_2 = create_record(1);
452 GNUNET_NAMESTORE_records_store (nsh,
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Created record 3\n");
462 /* name in different zone */
463 GNUNET_asprintf(&s_name_3, "dummy3");
464 s_rd_3 = create_record(1);
465 GNUNET_NAMESTORE_records_store(nsh,
476 const struct GNUNET_CONFIGURATION_Handle *cfg,
477 struct GNUNET_TESTING_Peer *peer)
479 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
482 nsh = GNUNET_NAMESTORE_connect (cfg);
483 GNUNET_break (NULL != nsh);
484 /* first, iterate over empty namestore */
485 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
491 &empty_zone_proc_end,
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
496 "Failed to create zone iterator\n");
498 GNUNET_SCHEDULER_cancel (endbadly_task);
499 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
505 main (int argc, char *argv[])
507 const char *plugin_name;
510 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
511 GNUNET_asprintf (&cfg_name,
512 "test_namestore_api_%s.conf",
514 GNUNET_DISK_purge_cfg_dir (cfg_name,
518 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
525 GNUNET_DISK_purge_cfg_dir (cfg_name,
527 GNUNET_free (cfg_name);
532 /* end of test_namestore_api_zone_iteration_stop.c */