2 This file is part of GNUnet.
3 Copyright (C) 2013 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_specific_zone.c
22 * @brief testcase for zone iteration functionality: iterate over a specific zone
23 * @author Matthias Wachs
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29 #include "gnunet_dnsparser_lib.h"
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
37 static struct GNUNET_NAMESTORE_Handle *nsh;
39 static struct GNUNET_SCHEDULER_Task *endbadly_task;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey2;
45 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
49 static int returned_records;
51 static char *s_name_1;
53 static struct GNUNET_GNSRECORD_Data *s_rd_1;
55 static char *s_name_2;
57 static struct GNUNET_GNSRECORD_Data *s_rd_2;
59 static char *s_name_3;
61 static struct GNUNET_GNSRECORD_Data *s_rd_3;
67 * @param cls handle to use to re-connect.
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);
136 zone_proc (void *cls,
137 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
139 unsigned int rd_count,
140 const struct GNUNET_GNSRECORD_Data *rd)
142 int failed = GNUNET_NO;
144 GNUNET_assert (NULL != zone);
145 if (0 == GNUNET_memcmp (zone,
148 if (0 == strcmp (label, s_name_1))
152 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
164 else if (0 == strcmp (label, s_name_2))
168 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 "Received invalid record count\n");
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Comparing result failed: got name `%s' for first zone\n",
191 else if (0 == GNUNET_memcmp (zone, privkey2))
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 "Received data for not requested zone\n");
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 "Received invalid zone\n");
205 if (failed == GNUNET_NO)
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Telling namestore to send the next result\n");
210 GNUNET_NAMESTORE_zone_iterator_next (zi,
217 GNUNET_SCHEDULER_shutdown ();
223 zone_proc_end (void *cls)
226 GNUNET_break (2 == returned_records);
227 if (2 == returned_records)
229 res = 0; /* Last iteraterator callback, we are done */
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Received last result, iteration done after receing %u results\n",
239 GNUNET_SCHEDULER_shutdown ();
250 if (success == GNUNET_OK)
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Created record %u \n", c);
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Failed to created records: `%s'\n",
263 GNUNET_SCHEDULER_shutdown ();
270 returned_records = 0;
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "All records created, starting iteration over all zones \n");
273 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "Failed to create zone iterator\n");
287 GNUNET_SCHEDULER_shutdown ();
294 static struct GNUNET_GNSRECORD_Data *
295 create_record (unsigned int count)
297 struct GNUNET_GNSRECORD_Data *rd;
299 rd = GNUNET_new_array (count,
300 struct GNUNET_GNSRECORD_Data);
301 for (unsigned int c = 0; c < count; c++)
303 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
304 GNUNET_TIME_UNIT_HOURS).abs_value_us;
305 rd[c].record_type = TEST_RECORD_TYPE;
306 rd[c].data_size = 50;
307 rd[c].data = GNUNET_malloc (50);
309 memset ((char *) rd[c].data, 'a', 50);
316 * Callback called from the zone iterator when we iterate over
317 * the empty zone. Check that we got no records and then
318 * start the actual tests by filling the zone.
321 empty_zone_proc (void *cls,
322 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
324 unsigned int rd_count,
325 const struct GNUNET_GNSRECORD_Data *rd)
327 GNUNET_assert (nsh == cls);
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331 _ ("Expected empty zone but received zone private key\n"));
334 GNUNET_SCHEDULER_shutdown ();
337 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 _ ("Expected no zone content but received data\n"));
343 GNUNET_SCHEDULER_shutdown ();
351 empty_zone_proc_end (void *cls)
354 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
355 GNUNET_assert (privkey != NULL);
356 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
357 GNUNET_assert (privkey2 != NULL);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Created record 1\n");
361 GNUNET_asprintf (&s_name_1,
363 s_rd_1 = create_record (1);
364 GNUNET_NAMESTORE_records_store (nsh,
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Created record 2 \n");
374 GNUNET_asprintf (&s_name_2,
376 s_rd_2 = create_record (1);
377 GNUNET_NAMESTORE_records_store (nsh,
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Created record 3\n");
388 /* name in different zone */
389 GNUNET_asprintf (&s_name_3,
391 s_rd_3 = create_record (1);
392 GNUNET_NAMESTORE_records_store (nsh,
403 const struct GNUNET_CONFIGURATION_Handle *cfg,
404 struct GNUNET_TESTING_Peer *peer)
406 GNUNET_SCHEDULER_add_shutdown (&end,
408 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
411 nsh = GNUNET_NAMESTORE_connect (cfg);
412 GNUNET_break (NULL != nsh);
413 /* first, iterate over empty namestore */
414 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
420 &empty_zone_proc_end,
424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
425 "Failed to create zone iterator\n");
427 GNUNET_SCHEDULER_shutdown ();
432 #include "test_common.c"
436 main (int argc, char *argv[])
438 const char *plugin_name;
441 SETUP_CFG (plugin_name, cfg_name);
444 GNUNET_TESTING_peer_run (
445 "test-namestore-api-zone-iteration-specific-zone",
452 GNUNET_DISK_purge_cfg_dir (cfg_name,
454 GNUNET_free (cfg_name);
459 /* end of test_namestore_api_zone_iteration_specific_zone.c */