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_testing_lib.h"
27 #include "namestore.h"
28 #include "gnunet_dnsparser_lib.h"
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
32 #define ZONE_NICK_1 "nick1"
33 #define ZONE_NICK_2 "nick2"
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
38 static struct GNUNET_NAMESTORE_Handle * nsh;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
44 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
48 static int returned_records;
50 static char * s_name_1;
52 static struct GNUNET_GNSRECORD_Data *s_rd_1;
54 static char * s_name_2;
56 static struct GNUNET_GNSRECORD_Data *s_rd_2;
58 static char * s_name_3;
60 static struct GNUNET_GNSRECORD_Data *s_rd_3;
62 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
66 * Re-establish the connection to the service.
68 * @param cls handle to use to re-connect.
69 * @param tc scheduler context
76 GNUNET_NAMESTORE_zone_iteration_stop (zi);
81 GNUNET_NAMESTORE_disconnect (nsh);
84 GNUNET_free_non_null (s_name_1);
85 GNUNET_free_non_null (s_name_2);
86 GNUNET_free_non_null (s_name_3);
90 GNUNET_free ((void *)s_rd_1->data);
95 GNUNET_free ((void *)s_rd_2->data);
100 GNUNET_free ((void *)s_rd_3->data);
101 GNUNET_free (s_rd_3);
106 GNUNET_free (privkey);
109 if (privkey2 != NULL)
111 GNUNET_free (privkey2);
118 check_zone_1 (const char *label, unsigned int rd_count,
119 const struct GNUNET_GNSRECORD_Data *rd)
121 for (unsigned int c = 0; c< rd_count ; c++)
123 if ( (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
124 (0 != strcmp (rd[c].data, ZONE_NICK_1)) )
135 check_zone_2 (const char *label,
136 unsigned int rd_count,
137 const struct GNUNET_GNSRECORD_Data *rd)
139 for (unsigned int c = 0; c< rd_count ; c++)
141 if ( (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
142 (0 != strcmp (rd[c].data, ZONE_NICK_2)) )
153 zone_proc_end (void *cls)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Received last result, iteration done after receing %u results\n",
160 GNUNET_SCHEDULER_shutdown ();
165 zone_proc (void *cls,
166 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
168 unsigned int rd_count,
169 const struct GNUNET_GNSRECORD_Data *rd)
171 int failed = GNUNET_NO;
173 GNUNET_assert (NULL != zone);
174 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
176 failed = check_zone_1 (label, rd_count, rd);
177 if (GNUNET_YES == failed)
180 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
182 failed = check_zone_2 (label, rd_count, rd);
183 if (GNUNET_YES == failed)
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 "Received invalid zone\n");
194 if (failed == GNUNET_NO)
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Telling namestore to send the next result\n");
199 GNUNET_NAMESTORE_zone_iterator_next (zi,
206 GNUNET_SCHEDULER_shutdown ();
225 if (success == GNUNET_OK)
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
235 GNUNET_SCHEDULER_shutdown ();
242 returned_records = 0;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
244 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
256 GNUNET_SCHEDULER_shutdown ();
263 static struct GNUNET_GNSRECORD_Data *
264 create_record (unsigned int count)
266 struct GNUNET_GNSRECORD_Data * rd;
268 rd = GNUNET_new_array (count,
269 struct GNUNET_GNSRECORD_Data);
270 for (unsigned int c = 0; c < count; c++)
272 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
273 rd[c].record_type = TEST_RECORD_TYPE;
274 rd[c].data_size = 50;
275 rd[c].data = GNUNET_malloc(50);
277 memset ((char *) rd[c].data, 'a', 50);
284 nick_2_cont (void *cls,
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
294 GNUNET_asprintf(&s_name_1, "dummy1");
295 s_rd_1 = create_record(1);
296 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Created record 2 \n");
302 GNUNET_asprintf(&s_name_2, "dummy2");
303 s_rd_2 = create_record(1);
304 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
305 1, s_rd_2, &put_cont, NULL);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Created record 3\n");
310 /* name in different zone */
311 GNUNET_asprintf(&s_name_3, "dummy3");
312 s_rd_3 = create_record(1);
313 GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
320 nick_1_cont (void *cls, int32_t success, const char *emsg)
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Nick 1 added : %s\n",
324 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
326 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330 _("Namestore cannot store no block\n"));
336 * Callback called from the zone iterator when we iterate over
337 * the empty zone. Check that we got no records and then
338 * start the actual tests by filling the zone.
341 empty_zone_proc (void *cls,
342 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
344 unsigned int rd_count,
345 const struct GNUNET_GNSRECORD_Data *rd)
347 GNUNET_assert (nsh == cls);
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("Expected empty zone but received zone private key\n"));
354 GNUNET_SCHEDULER_shutdown ();
357 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Expected no zone content but received data\n"));
362 GNUNET_SCHEDULER_shutdown ();
370 empty_zone_end (void *cls)
372 GNUNET_assert (nsh == cls);
374 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
375 GNUNET_assert (privkey != NULL);
376 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
377 GNUNET_assert (privkey2 != NULL);
379 nsqe = GNUNET_NAMESTORE_set_nick (nsh,
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387 _("Namestore cannot store no block\n"));
395 const struct GNUNET_CONFIGURATION_Handle *cfg,
396 struct GNUNET_TESTING_Peer *peer)
398 nsh = GNUNET_NAMESTORE_connect (cfg);
399 GNUNET_break (NULL != nsh);
400 GNUNET_SCHEDULER_add_shutdown (&end,
402 /* first, iterate over empty namestore */
403 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 "Failed to create zone iterator\n");
416 GNUNET_SCHEDULER_shutdown ();
421 #include "test_common.c"
425 main (int argc, char *argv[])
427 const char *plugin_name;
430 SETUP_CFG (plugin_name, cfg_name);
433 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
440 GNUNET_DISK_purge_cfg_dir (cfg_name,
442 GNUNET_free (cfg_name);
447 /* end of test_namestore_api_zone_iteration.c */