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.c
22 * @brief testcase for zone iteration functionality: iterate all zones
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_gns_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
33 #define ZONE_NICK_1 "nick1"
34 #define ZONE_NICK_2 "nick2"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
39 static struct GNUNET_NAMESTORE_Handle *nsh;
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;
63 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
67 * Re-establish the connection to the service.
69 * @param cls handle to use to re-connect.
70 * @param tc scheduler context
77 GNUNET_NAMESTORE_zone_iteration_stop (zi);
82 GNUNET_NAMESTORE_disconnect (nsh);
85 GNUNET_free_non_null (s_name_1);
86 GNUNET_free_non_null (s_name_2);
87 GNUNET_free_non_null (s_name_3);
91 GNUNET_free_nz ((void *) s_rd_1->data);
96 GNUNET_free_nz ((void *) s_rd_2->data);
101 GNUNET_free_nz ((void *) s_rd_3->data);
102 GNUNET_free (s_rd_3);
108 check_zone_1 (const char *label, unsigned int rd_count,
109 const struct GNUNET_GNSRECORD_Data *rd)
111 for (unsigned int c = 0; c < rd_count; c++)
113 if ((rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
114 (0 != strcmp (rd[c].data, ZONE_NICK_1)))
125 check_zone_2 (const char *label,
126 unsigned int rd_count,
127 const struct GNUNET_GNSRECORD_Data *rd)
129 for (unsigned int c = 0; c < rd_count; c++)
131 if ((rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
132 (0 != strcmp (rd[c].data, ZONE_NICK_2)))
143 zone_proc_end (void *cls)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received last result, iteration done after receing %u results\n",
150 GNUNET_SCHEDULER_shutdown ();
155 zone_proc (void *cls,
156 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
158 unsigned int rd_count,
159 const struct GNUNET_GNSRECORD_Data *rd)
161 int failed = GNUNET_NO;
163 GNUNET_assert (NULL != zone);
164 if (0 == GNUNET_memcmp (zone, &privkey))
166 failed = check_zone_1 (label, rd_count, rd);
167 if (GNUNET_YES == failed)
170 else if (0 == GNUNET_memcmp (zone, &privkey2))
172 failed = check_zone_2 (label, rd_count, rd);
173 if (GNUNET_YES == failed)
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Received invalid zone\n");
184 if (failed == GNUNET_NO)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "Telling namestore to send the next result\n");
189 GNUNET_NAMESTORE_zone_iterator_next (zi,
196 GNUNET_SCHEDULER_shutdown ();
215 if (success == GNUNET_OK)
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
225 GNUNET_SCHEDULER_shutdown ();
232 returned_records = 0;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "All records created, starting iteration over all zones \n");
235 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
247 GNUNET_SCHEDULER_shutdown ();
254 static struct GNUNET_GNSRECORD_Data *
255 create_record (unsigned int count)
257 struct GNUNET_GNSRECORD_Data *rd;
259 rd = GNUNET_new_array (count,
260 struct GNUNET_GNSRECORD_Data);
261 for (unsigned int c = 0; c < count; c++)
263 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
264 GNUNET_TIME_UNIT_HOURS).abs_value_us;
265 rd[c].record_type = TEST_RECORD_TYPE;
266 rd[c].data_size = 50;
267 rd[c].data = GNUNET_malloc (50);
269 memset ((char *) rd[c].data, 'a', 50);
276 nick_2_cont (void *cls,
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
286 GNUNET_asprintf (&s_name_1, "dummy1");
287 s_rd_1 = create_record (1);
288 GNUNET_NAMESTORE_records_store (nsh, &privkey, s_name_1,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Created record 2 \n");
294 GNUNET_asprintf (&s_name_2, "dummy2");
295 s_rd_2 = create_record (1);
296 GNUNET_NAMESTORE_records_store (nsh, &privkey, s_name_2,
297 1, s_rd_2, &put_cont, NULL);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Created record 3\n");
302 /* name in different zone */
303 GNUNET_asprintf (&s_name_3, "dummy3");
304 s_rd_3 = create_record (1);
305 GNUNET_NAMESTORE_records_store (nsh, &privkey2, s_name_3,
312 nick_1_cont (void *cls, int32_t success, const char *emsg)
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Nick 1 added : %s\n",
316 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
317 struct GNUNET_GNSRECORD_Data rd;
319 memset (&rd, 0, sizeof(rd));
320 rd.data = ZONE_NICK_2;
321 rd.data_size = strlen (ZONE_NICK_2) + 1;
322 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
323 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
324 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
325 nsqe = GNUNET_NAMESTORE_records_store (nsh,
327 GNUNET_GNS_EMPTY_LABEL_AT,
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _ ("Namestore cannot store no block\n"));
342 * Callback called from the zone iterator when we iterate over
343 * the empty zone. Check that we got no records and then
344 * start the actual tests by filling the zone.
347 empty_zone_proc (void *cls,
348 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
350 unsigned int rd_count,
351 const struct GNUNET_GNSRECORD_Data *rd)
353 GNUNET_assert (nsh == cls);
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 _ ("Expected empty zone but received zone private key\n"));
360 GNUNET_SCHEDULER_shutdown ();
363 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
366 _ ("Expected no zone content but received data\n"));
368 GNUNET_SCHEDULER_shutdown ();
376 empty_zone_end (void *cls)
378 GNUNET_assert (nsh == cls);
379 struct GNUNET_GNSRECORD_Data rd;
382 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
383 GNUNET_CRYPTO_ecdsa_key_create (&privkey2);
385 memset (&rd, 0, sizeof(rd));
386 rd.data = ZONE_NICK_1;
387 rd.data_size = strlen (ZONE_NICK_1) + 1;
388 rd.record_type = GNUNET_GNSRECORD_TYPE_NICK;
389 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
390 rd.flags |= GNUNET_GNSRECORD_RF_PRIVATE;
391 nsqe = GNUNET_NAMESTORE_records_store (nsh,
393 GNUNET_GNS_EMPTY_LABEL_AT,
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
401 _ ("Namestore cannot store no block\n"));
408 const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_TESTING_Peer *peer)
411 nsh = GNUNET_NAMESTORE_connect (cfg);
412 GNUNET_break (NULL != nsh);
413 GNUNET_SCHEDULER_add_shutdown (&end,
415 /* first, iterate over empty namestore */
416 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
427 "Failed to create zone iterator\n");
429 GNUNET_SCHEDULER_shutdown ();
434 #include "test_common.c"
438 main (int argc, char *argv[])
440 const char *plugin_name;
443 SETUP_CFG (plugin_name, cfg_name);
446 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
453 GNUNET_DISK_purge_cfg_dir (cfg_name,
455 GNUNET_free (cfg_name);
460 /* end of test_namestore_api_zone_iteration.c */