2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
16 * @file namestore/test_namestore_api_zone_iteration.c
17 * @brief testcase for zone iteration functionality: iterate all zones
20 #include "gnunet_namestore_service.h"
21 #include "gnunet_testing_lib.h"
22 #include "namestore.h"
23 #include "gnunet_dnsparser_lib.h"
25 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
28 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
31 static struct GNUNET_NAMESTORE_Handle * nsh;
33 static struct GNUNET_SCHEDULER_Task * endbadly_task;
35 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
37 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
39 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
43 static int returned_records;
45 static char * s_name_1;
47 static struct GNUNET_GNSRECORD_Data *s_rd_1;
49 static char * s_name_2;
51 static struct GNUNET_GNSRECORD_Data *s_rd_2;
53 static char * s_name_3;
55 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59 * Re-establish the connection to the service.
61 * @param cls handle to use to re-connect.
62 * @param tc scheduler context
68 GNUNET_SCHEDULER_shutdown ();
78 GNUNET_NAMESTORE_zone_iteration_stop (zi);
81 if (NULL != endbadly_task)
83 GNUNET_SCHEDULER_cancel (endbadly_task);
88 GNUNET_free (privkey);
93 GNUNET_free (privkey2);
96 GNUNET_free (s_name_1);
97 GNUNET_free (s_name_2);
98 GNUNET_free (s_name_3);
101 GNUNET_free ((void *)s_rd_1->data);
102 GNUNET_free (s_rd_1);
106 GNUNET_free ((void *)s_rd_2->data);
107 GNUNET_free (s_rd_2);
111 GNUNET_free ((void *)s_rd_3->data);
112 GNUNET_free (s_rd_3);
116 GNUNET_NAMESTORE_disconnect (nsh);
125 GNUNET_break (3 == returned_records);
126 if (3 == returned_records)
128 res = 0; /* Last iteraterator callback, we are done */
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Received last result, iteration done after receing %u results\n",
137 GNUNET_SCHEDULER_shutdown ();
149 zone_proc (void *cls,
150 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
152 unsigned int rd_count,
153 const struct GNUNET_GNSRECORD_Data *rd)
155 int failed = GNUNET_NO;
157 GNUNET_assert (NULL != zone);
158 if (0 == memcmp (zone,
160 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
162 if (0 == strcmp (label, s_name_1))
166 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
178 else if (0 == strcmp (label, s_name_2))
182 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Received invalid record count\n");
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Comparing result failed: got name `%s' for first zone\n",
205 else if (0 == memcmp (zone,
207 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
209 if (0 == strcmp (label, s_name_3))
213 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 "Received invalid record count\n");
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Comparing result failed: got name `%s' for first zone\n",
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Received invalid zone\n");
244 if (failed == GNUNET_NO)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Telling namestore to send the next result\n");
249 GNUNET_NAMESTORE_zone_iterator_next (zi,
255 GNUNET_SCHEDULER_shutdown ();
268 if (success == GNUNET_OK)
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Created record %u \n",
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 "Failed to created records: `%s'\n",
281 GNUNET_SCHEDULER_shutdown ();
289 returned_records = 0;
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "All records created, starting iteration over all zones \n");
292 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 "Failed to create zone iterator\n");
305 GNUNET_SCHEDULER_shutdown ();
313 static struct GNUNET_GNSRECORD_Data *
314 create_record (unsigned int count)
316 struct GNUNET_GNSRECORD_Data * rd;
318 rd = GNUNET_new_array (count,
319 struct GNUNET_GNSRECORD_Data);
320 for (unsigned int c = 0; c < count; c++)
322 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
323 rd[c].record_type = TEST_RECORD_TYPE;
324 rd[c].data_size = 50;
325 rd[c].data = GNUNET_malloc(50);
327 memset ((char *) rd[c].data, 'a', 50);
334 * Callback called from the zone iterator when we iterate over
335 * the empty zone. Check that we got no records and then
336 * start the actual tests by filling the zone.
339 empty_zone_proc (void *cls,
340 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
342 unsigned int rd_count,
343 const struct GNUNET_GNSRECORD_Data *rd)
345 GNUNET_assert (nsh == cls);
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 _("Expected empty zone but received zone private key\n"));
351 GNUNET_SCHEDULER_shutdown ();
355 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 _("Expected no zone content but received data\n"));
360 GNUNET_SCHEDULER_shutdown ();
369 empty_zone_end (void *cls)
374 GNUNET_asprintf (&hostkey_file,
377 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Using zonekey file `%s' \n",
381 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
382 GNUNET_free (hostkey_file);
383 GNUNET_assert (privkey != NULL);
385 GNUNET_asprintf (&hostkey_file,
388 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Using zonekey file `%s' \n",
392 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
393 GNUNET_free (hostkey_file);
394 GNUNET_assert (privkey2 != NULL);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
398 GNUNET_asprintf(&s_name_1, "dummy1");
399 s_rd_1 = create_record(1);
400 GNUNET_NAMESTORE_records_store (nsh,
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Created record 2 \n");
408 GNUNET_asprintf(&s_name_2, "dummy2");
409 s_rd_2 = create_record(1);
410 GNUNET_NAMESTORE_records_store (nsh,
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Created record 3\n");
418 /* name in different zone */
419 GNUNET_asprintf(&s_name_3, "dummy3");
420 s_rd_3 = create_record(1);
421 GNUNET_NAMESTORE_records_store (nsh,
433 const struct GNUNET_CONFIGURATION_Handle *cfg,
434 struct GNUNET_TESTING_Peer *peer)
436 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
439 GNUNET_SCHEDULER_add_shutdown (&end,
442 nsh = GNUNET_NAMESTORE_connect (cfg);
443 GNUNET_break (NULL != nsh);
444 /* first, iterate over empty namestore */
445 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
456 "Failed to create zone iterator\n");
459 GNUNET_SCHEDULER_shutdown ();
465 main (int argc, char *argv[])
467 const char *plugin_name;
470 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
471 GNUNET_asprintf (&cfg_name,
472 "test_namestore_api_%s.conf",
474 GNUNET_DISK_purge_cfg_dir (cfg_name,
478 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
485 GNUNET_DISK_purge_cfg_dir (cfg_name,
487 GNUNET_free (cfg_name);
492 /* end of test_namestore_api_zone_iteration.c */