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/>.
19 * @file namestore/test_namestore_api_zone_iteration_specific_zone.c
20 * @brief testcase for zone iteration functionality: iterate over a specific zone
21 * @author Matthias Wachs
24 #include "gnunet_namestore_service.h"
25 #include "gnunet_testing_lib.h"
26 #include "namestore.h"
27 #include "gnunet_dnsparser_lib.h"
29 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
35 static struct GNUNET_NAMESTORE_Handle * nsh;
37 static struct GNUNET_SCHEDULER_Task * endbadly_task;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
43 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
47 static int returned_records;
49 static char * s_name_1;
51 static struct GNUNET_GNSRECORD_Data *s_rd_1;
53 static char * s_name_2;
55 static struct GNUNET_GNSRECORD_Data *s_rd_2;
57 static char * s_name_3;
59 static struct GNUNET_GNSRECORD_Data *s_rd_3;
65 * @param cls handle to use to re-connect.
71 GNUNET_SCHEDULER_shutdown ();
81 GNUNET_NAMESTORE_zone_iteration_stop (zi);
84 if (NULL != endbadly_task)
86 GNUNET_SCHEDULER_cancel (endbadly_task);
91 GNUNET_free (privkey);
96 GNUNET_free (privkey2);
99 GNUNET_free (s_name_1);
100 GNUNET_free (s_name_2);
101 GNUNET_free (s_name_3);
104 GNUNET_free ((void *)s_rd_1->data);
105 GNUNET_free (s_rd_1);
109 GNUNET_free ((void *)s_rd_2->data);
110 GNUNET_free (s_rd_2);
114 GNUNET_free ((void *)s_rd_3->data);
115 GNUNET_free (s_rd_3);
119 GNUNET_NAMESTORE_disconnect (nsh);
134 zone_proc (void *cls,
135 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
137 unsigned int rd_count,
138 const struct GNUNET_GNSRECORD_Data *rd)
140 int failed = GNUNET_NO;
142 GNUNET_assert (NULL != zone);
143 if (0 == memcmp (zone,
145 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
147 if (0 == strcmp (label, s_name_1))
151 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
163 else if (0 == strcmp (label, s_name_2))
167 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Received invalid record count\n");
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Comparing result failed: got name `%s' for first zone\n",
190 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 "Received data for not requested zone\n");
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Received invalid zone\n");
204 if (failed == GNUNET_NO)
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Telling namestore to send the next result\n");
209 GNUNET_NAMESTORE_zone_iterator_next (zi,
216 GNUNET_SCHEDULER_shutdown ();
222 zone_proc_end (void *cls)
225 GNUNET_break (2 == returned_records);
226 if (2 == returned_records)
228 res = 0; /* Last iteraterator callback, we are done */
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Received last result, iteration done after receing %u results\n",
238 GNUNET_SCHEDULER_shutdown ();
249 if (success == GNUNET_OK)
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Created record %u \n", c);
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "Failed to created records: `%s'\n",
262 GNUNET_SCHEDULER_shutdown ();
269 returned_records = 0;
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "All records created, starting iteration over all zones \n");
272 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 "Failed to create zone iterator\n");
286 GNUNET_SCHEDULER_shutdown ();
293 static struct GNUNET_GNSRECORD_Data *
294 create_record (unsigned int count)
296 struct GNUNET_GNSRECORD_Data *rd;
298 rd = GNUNET_new_array (count,
299 struct GNUNET_GNSRECORD_Data);
300 for (unsigned int c = 0; c < count; c++)
302 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
303 rd[c].record_type = TEST_RECORD_TYPE;
304 rd[c].data_size = 50;
305 rd[c].data = GNUNET_malloc(50);
307 memset ((char *) rd[c].data, 'a', 50);
314 * Callback called from the zone iterator when we iterate over
315 * the empty zone. Check that we got no records and then
316 * start the actual tests by filling the zone.
319 empty_zone_proc (void *cls,
320 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
322 unsigned int rd_count,
323 const struct GNUNET_GNSRECORD_Data *rd)
325 GNUNET_assert (nsh == cls);
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 _("Expected empty zone but received zone private key\n"));
332 GNUNET_SCHEDULER_shutdown ();
335 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
338 _("Expected no zone content but received data\n"));
341 GNUNET_SCHEDULER_shutdown ();
349 empty_zone_proc_end (void *cls)
352 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
353 GNUNET_assert (privkey != NULL);
354 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
355 GNUNET_assert (privkey2 != NULL);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Created record 1\n");
359 GNUNET_asprintf (&s_name_1,
361 s_rd_1 = create_record (1);
362 GNUNET_NAMESTORE_records_store (nsh,
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Created record 2 \n");
372 GNUNET_asprintf (&s_name_2,
374 s_rd_2 = create_record (1);
375 GNUNET_NAMESTORE_records_store (nsh,
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Created record 3\n");
386 /* name in different zone */
387 GNUNET_asprintf (&s_name_3,
389 s_rd_3 = create_record (1);
390 GNUNET_NAMESTORE_records_store (nsh,
401 const struct GNUNET_CONFIGURATION_Handle *cfg,
402 struct GNUNET_TESTING_Peer *peer)
404 GNUNET_SCHEDULER_add_shutdown (&end,
406 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
409 nsh = GNUNET_NAMESTORE_connect (cfg);
410 GNUNET_break (NULL != nsh);
411 /* first, iterate over empty namestore */
412 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
418 &empty_zone_proc_end,
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "Failed to create zone iterator\n");
425 GNUNET_SCHEDULER_shutdown ();
431 main (int argc, char *argv[])
433 const char *plugin_name;
436 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
437 GNUNET_asprintf (&cfg_name,
438 "test_namestore_api_%s.conf",
441 GNUNET_DISK_purge_cfg_dir (cfg_name,
444 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-specific-zone",
451 GNUNET_DISK_purge_cfg_dir (cfg_name,
453 GNUNET_free (cfg_name);
457 /* end of test_namestore_api_zone_iteration_specific_zone.c */