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 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
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", label);
177 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
179 if (0 == strcmp (label, s_name_3))
183 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 "Received invalid record count\n");
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Comparing result failed: got name `%s' for first zone\n", label);
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 "Received invalid zone\n");
212 if (failed == GNUNET_NO)
214 if (1 == returned_records)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Telling namestore to stop zone iteration\n");
218 GNUNET_NAMESTORE_zone_iteration_stop (zi);
221 GNUNET_SCHEDULER_add_delayed (WAIT,
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Telling namestore to send the next result\n");
229 GNUNET_NAMESTORE_zone_iterator_next (zi,
235 GNUNET_SCHEDULER_shutdown ();
241 zone_proc_end (void *cls)
243 GNUNET_break (1 <= returned_records);
244 if (1 >= returned_records)
245 res = 1; /* Last iteraterator callback, we are done */
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Received last result, iteration done after receing %u results\n",
252 GNUNET_SCHEDULER_add_now (&end, NULL);
257 put_cont (void *cls, int32_t success, const char *emsg)
261 if (success == GNUNET_OK)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
271 GNUNET_SCHEDULER_shutdown ();
278 returned_records = 0;
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "All records created, starting iteration over all zones \n");
281 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "Failed to create zone iterator\n");
294 GNUNET_SCHEDULER_shutdown ();
301 static struct GNUNET_GNSRECORD_Data *
302 create_record (unsigned int count)
304 struct GNUNET_GNSRECORD_Data *rd;
306 rd = GNUNET_new_array (count,
307 struct GNUNET_GNSRECORD_Data);
308 for (unsigned int c = 0; c < count; c++)
310 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
311 rd[c].record_type = TEST_RECORD_TYPE;
312 rd[c].data_size = 50;
313 rd[c].data = GNUNET_malloc(50);
315 memset ((char *) rd[c].data, 'a', 50);
322 * Callback called from the zone iterator when we iterate over
323 * the empty zone. Check that we got no records and then
324 * start the actual tests by filling the zone.
327 empty_zone_proc (void *cls,
328 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
330 unsigned int rd_count,
331 const struct GNUNET_GNSRECORD_Data *rd)
334 GNUNET_assert (nsh == cls);
337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
338 _("Expected empty zone but received zone private key\n"));
340 GNUNET_SCHEDULER_shutdown ();
343 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Expected no zone content but received data\n"));
348 GNUNET_SCHEDULER_shutdown ();
356 empty_zone_proc_end (void *cls)
360 GNUNET_assert (nsh == cls);
362 GNUNET_asprintf(&hostkey_file,
365 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Using zonekey file `%s' \n",
369 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
370 GNUNET_free (hostkey_file);
371 GNUNET_assert (privkey != NULL);
373 GNUNET_asprintf (&hostkey_file,
376 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Using zonekey file `%s'\n",
380 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
381 GNUNET_free (hostkey_file);
382 GNUNET_assert (privkey2 != NULL);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Created record 1\n");
387 GNUNET_asprintf(&s_name_1,
389 s_rd_1 = create_record(1);
390 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
391 1, s_rd_1, &put_cont, NULL);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Created record 2 \n");
395 GNUNET_asprintf(&s_name_2,
397 s_rd_2 = create_record(1);
398 GNUNET_NAMESTORE_records_store (nsh,
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Created record 3\n");
408 /* name in different zone */
409 GNUNET_asprintf(&s_name_3, "dummy3");
410 s_rd_3 = create_record(1);
411 GNUNET_NAMESTORE_records_store(nsh,
422 const struct GNUNET_CONFIGURATION_Handle *cfg,
423 struct GNUNET_TESTING_Peer *peer)
425 nsh = GNUNET_NAMESTORE_connect (cfg);
426 GNUNET_break (NULL != nsh);
427 GNUNET_SCHEDULER_add_shutdown (&end,
429 /* first, iterate over empty namestore */
430 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
436 &empty_zone_proc_end,
440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 "Failed to create zone iterator\n");
443 GNUNET_SCHEDULER_shutdown ();
448 #include "test_common.c"
452 main (int argc, char *argv[])
454 const char *plugin_name;
457 SETUP_CFG (plugin_name, cfg_name);
460 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
467 GNUNET_DISK_purge_cfg_dir (cfg_name,
469 GNUNET_free (cfg_name);
474 /* end of test_namestore_api_zone_iteration_stop.c */