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);
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);
111 GNUNET_free((void *)s_rd_2->data);
116 GNUNET_free((void *)s_rd_3->data);
121 GNUNET_NAMESTORE_disconnect(nsh);
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(GNUNET_TIME_UNIT_HOURS).abs_value_us;
304 rd[c].record_type = TEST_RECORD_TYPE;
305 rd[c].data_size = 50;
306 rd[c].data = GNUNET_malloc(50);
308 memset((char *)rd[c].data, 'a', 50);
315 * Callback called from the zone iterator when we iterate over
316 * the empty zone. Check that we got no records and then
317 * start the actual tests by filling the zone.
320 empty_zone_proc(void *cls,
321 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
323 unsigned int rd_count,
324 const struct GNUNET_GNSRECORD_Data *rd)
326 GNUNET_assert(nsh == cls);
329 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
330 _("Expected empty zone but received zone private key\n"));
333 GNUNET_SCHEDULER_shutdown();
336 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
338 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
339 _("Expected no zone content but received data\n"));
342 GNUNET_SCHEDULER_shutdown();
350 empty_zone_proc_end(void *cls)
353 privkey = GNUNET_CRYPTO_ecdsa_key_create();
354 GNUNET_assert(privkey != NULL);
355 privkey2 = GNUNET_CRYPTO_ecdsa_key_create();
356 GNUNET_assert(privkey2 != NULL);
358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
359 "Created record 1\n");
360 GNUNET_asprintf(&s_name_1,
362 s_rd_1 = create_record(1);
363 GNUNET_NAMESTORE_records_store(nsh,
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
372 "Created record 2 \n");
373 GNUNET_asprintf(&s_name_2,
375 s_rd_2 = create_record(1);
376 GNUNET_NAMESTORE_records_store(nsh,
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
385 "Created record 3\n");
387 /* name in different zone */
388 GNUNET_asprintf(&s_name_3,
390 s_rd_3 = create_record(1);
391 GNUNET_NAMESTORE_records_store(nsh,
402 const struct GNUNET_CONFIGURATION_Handle *cfg,
403 struct GNUNET_TESTING_Peer *peer)
405 GNUNET_SCHEDULER_add_shutdown(&end,
407 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,
410 nsh = GNUNET_NAMESTORE_connect(cfg);
411 GNUNET_break(NULL != nsh);
412 /* first, iterate over empty namestore */
413 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
419 &empty_zone_proc_end,
423 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
424 "Failed to create zone iterator\n");
426 GNUNET_SCHEDULER_shutdown();
431 #include "test_common.c"
435 main(int argc, char *argv[])
437 const char *plugin_name;
440 SETUP_CFG(plugin_name, cfg_name);
443 GNUNET_TESTING_peer_run("test-namestore-api-zone-iteration-specific-zone",
450 GNUNET_DISK_purge_cfg_dir(cfg_name,
452 GNUNET_free(cfg_name);
456 /* end of test_namestore_api_zone_iteration_specific_zone.c */