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_stop.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_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.
70 GNUNET_NAMESTORE_zone_iteration_stop (zi);
75 GNUNET_NAMESTORE_disconnect (nsh);
78 GNUNET_free_non_null (s_name_1);
79 GNUNET_free_non_null (s_name_2);
80 GNUNET_free_non_null (s_name_3);
83 GNUNET_free ((void *) s_rd_1->data);
88 GNUNET_free ((void *) s_rd_2->data);
93 GNUNET_free ((void *) s_rd_3->data);
98 GNUNET_free (privkey);
101 if (privkey2 != NULL)
103 GNUNET_free (privkey2);
110 delayed_end (void *cls)
112 GNUNET_SCHEDULER_shutdown ();
124 zone_proc (void *cls,
125 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
127 unsigned int rd_count,
128 const struct GNUNET_GNSRECORD_Data *rd)
130 int failed = GNUNET_NO;
132 GNUNET_assert (NULL != zone);
133 if (0 == GNUNET_memcmp (zone, privkey))
135 if (0 == strcmp (label, s_name_1))
139 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
151 else if (0 == strcmp (label, s_name_2))
155 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 "Received invalid record count\n");
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Comparing result failed: got name `%s' for first zone\n",
178 else if (0 == GNUNET_memcmp (zone, privkey2))
180 if (0 == strcmp (label, s_name_3))
184 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_3))
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",
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Received invalid zone\n");
214 if (failed == GNUNET_NO)
216 if (1 == returned_records)
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Telling namestore to stop zone iteration\n");
220 GNUNET_NAMESTORE_zone_iteration_stop (zi);
223 GNUNET_SCHEDULER_add_delayed (WAIT,
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Telling namestore to send the next result\n");
231 GNUNET_NAMESTORE_zone_iterator_next (zi,
237 GNUNET_SCHEDULER_shutdown ();
243 zone_proc_end (void *cls)
245 GNUNET_break (1 <= returned_records);
246 if (1 >= returned_records)
247 res = 1; /* Last iteraterator callback, we are done */
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Received last result, iteration done after receing %u results\n",
254 GNUNET_SCHEDULER_add_now (&end, NULL);
259 put_cont (void *cls, int32_t success, const char *emsg)
263 if (success == GNUNET_OK)
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
273 GNUNET_SCHEDULER_shutdown ();
280 returned_records = 0;
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "All records created, starting iteration over all zones \n");
283 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 "Failed to create zone iterator\n");
296 GNUNET_SCHEDULER_shutdown ();
303 static struct GNUNET_GNSRECORD_Data *
304 create_record (unsigned int count)
306 struct GNUNET_GNSRECORD_Data *rd;
308 rd = GNUNET_new_array (count,
309 struct GNUNET_GNSRECORD_Data);
310 for (unsigned int c = 0; c < count; c++)
312 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
313 GNUNET_TIME_UNIT_HOURS).abs_value_us;
314 rd[c].record_type = TEST_RECORD_TYPE;
315 rd[c].data_size = 50;
316 rd[c].data = GNUNET_malloc (50);
318 memset ((char *) rd[c].data, 'a', 50);
325 * Callback called from the zone iterator when we iterate over
326 * the empty zone. Check that we got no records and then
327 * start the actual tests by filling the zone.
330 empty_zone_proc (void *cls,
331 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
333 unsigned int rd_count,
334 const struct GNUNET_GNSRECORD_Data *rd)
336 GNUNET_assert (nsh == cls);
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 _ ("Expected empty zone but received zone private key\n"));
342 GNUNET_SCHEDULER_shutdown ();
345 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 _ ("Expected no zone content but received data\n"));
350 GNUNET_SCHEDULER_shutdown ();
358 empty_zone_proc_end (void *cls)
362 GNUNET_assert (nsh == cls);
364 GNUNET_asprintf (&hostkey_file,
367 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Using zonekey file `%s' \n",
371 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
372 GNUNET_free (hostkey_file);
373 GNUNET_assert (privkey != NULL);
375 GNUNET_asprintf (&hostkey_file,
378 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Using zonekey file `%s'\n",
382 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
383 GNUNET_free (hostkey_file);
384 GNUNET_assert (privkey2 != NULL);
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Created record 1\n");
389 GNUNET_asprintf (&s_name_1,
391 s_rd_1 = create_record (1);
392 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
393 1, s_rd_1, &put_cont, NULL);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Created record 2 \n");
397 GNUNET_asprintf (&s_name_2,
399 s_rd_2 = create_record (1);
400 GNUNET_NAMESTORE_records_store (nsh,
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Created record 3\n");
410 /* name in different zone */
411 GNUNET_asprintf (&s_name_3, "dummy3");
412 s_rd_3 = create_record (1);
413 GNUNET_NAMESTORE_records_store (nsh,
424 const struct GNUNET_CONFIGURATION_Handle *cfg,
425 struct GNUNET_TESTING_Peer *peer)
427 nsh = GNUNET_NAMESTORE_connect (cfg);
428 GNUNET_break (NULL != nsh);
429 GNUNET_SCHEDULER_add_shutdown (&end,
431 /* first, iterate over empty namestore */
432 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
438 &empty_zone_proc_end,
442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443 "Failed to create zone iterator\n");
445 GNUNET_SCHEDULER_shutdown ();
450 #include "test_common.c"
454 main (int argc, char *argv[])
456 const char *plugin_name;
459 SETUP_CFG (plugin_name, cfg_name);
462 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
469 GNUNET_DISK_purge_cfg_dir (cfg_name,
471 GNUNET_free (cfg_name);
477 /* end of test_namestore_api_zone_iteration_stop.c */