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.
16 * @file namestore/test_namestore_api_zone_iteration_specific_zone.c
17 * @brief testcase for zone iteration functionality: iterate over a specific zone
18 * @author Matthias Wachs
21 #include "gnunet_namestore_service.h"
22 #include "gnunet_testing_lib.h"
23 #include "namestore.h"
24 #include "gnunet_dnsparser_lib.h"
26 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32 static struct GNUNET_NAMESTORE_Handle * nsh;
34 static struct GNUNET_SCHEDULER_Task * endbadly_task;
36 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
40 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
44 static int returned_records;
46 static char * s_name_1;
48 static struct GNUNET_GNSRECORD_Data *s_rd_1;
50 static char * s_name_2;
52 static struct GNUNET_GNSRECORD_Data *s_rd_2;
54 static char * s_name_3;
56 static struct GNUNET_GNSRECORD_Data *s_rd_3;
62 * @param cls handle to use to re-connect.
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);
131 zone_proc (void *cls,
132 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
134 unsigned int rd_count,
135 const struct GNUNET_GNSRECORD_Data *rd)
137 int failed = GNUNET_NO;
139 GNUNET_assert (NULL != zone);
140 if (0 == memcmp (zone,
142 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
144 if (0 == strcmp (label, s_name_1))
148 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
160 else if (0 == strcmp (label, s_name_2))
164 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "Received invalid record count\n");
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Comparing result failed: got name `%s' for first zone\n",
187 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "Received data for not requested zone\n");
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 "Received invalid zone\n");
201 if (failed == GNUNET_NO)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Telling namestore to send the next result\n");
206 GNUNET_NAMESTORE_zone_iterator_next (zi,
213 GNUNET_SCHEDULER_shutdown ();
219 zone_proc_end (void *cls)
222 GNUNET_break (2 == returned_records);
223 if (2 == returned_records)
225 res = 0; /* Last iteraterator callback, we are done */
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Received last result, iteration done after receing %u results\n",
235 GNUNET_SCHEDULER_shutdown ();
246 if (success == GNUNET_OK)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Created record %u \n", c);
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "Failed to created records: `%s'\n",
259 GNUNET_SCHEDULER_shutdown ();
266 returned_records = 0;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "All records created, starting iteration over all zones \n");
269 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 "Failed to create zone iterator\n");
283 GNUNET_SCHEDULER_shutdown ();
290 static struct GNUNET_GNSRECORD_Data *
291 create_record (unsigned int count)
293 struct GNUNET_GNSRECORD_Data *rd;
295 rd = GNUNET_new_array (count,
296 struct GNUNET_GNSRECORD_Data);
297 for (unsigned int c = 0; c < count; c++)
299 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
300 rd[c].record_type = TEST_RECORD_TYPE;
301 rd[c].data_size = 50;
302 rd[c].data = GNUNET_malloc(50);
304 memset ((char *) rd[c].data, 'a', 50);
311 * Callback called from the zone iterator when we iterate over
312 * the empty zone. Check that we got no records and then
313 * start the actual tests by filling the zone.
316 empty_zone_proc (void *cls,
317 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
319 unsigned int rd_count,
320 const struct GNUNET_GNSRECORD_Data *rd)
322 GNUNET_assert (nsh == cls);
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 _("Expected empty zone but received zone private key\n"));
329 GNUNET_SCHEDULER_shutdown ();
332 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 _("Expected no zone content but received data\n"));
338 GNUNET_SCHEDULER_shutdown ();
346 empty_zone_proc_end (void *cls)
349 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
350 GNUNET_assert (privkey != NULL);
351 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
352 GNUNET_assert (privkey2 != NULL);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Created record 1\n");
356 GNUNET_asprintf (&s_name_1,
358 s_rd_1 = create_record (1);
359 GNUNET_NAMESTORE_records_store (nsh,
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Created record 2 \n");
369 GNUNET_asprintf (&s_name_2,
371 s_rd_2 = create_record (1);
372 GNUNET_NAMESTORE_records_store (nsh,
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Created record 3\n");
383 /* name in different zone */
384 GNUNET_asprintf (&s_name_3,
386 s_rd_3 = create_record (1);
387 GNUNET_NAMESTORE_records_store (nsh,
398 const struct GNUNET_CONFIGURATION_Handle *cfg,
399 struct GNUNET_TESTING_Peer *peer)
401 GNUNET_SCHEDULER_add_shutdown (&end,
403 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
406 nsh = GNUNET_NAMESTORE_connect (cfg);
407 GNUNET_break (NULL != nsh);
408 /* first, iterate over empty namestore */
409 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
415 &empty_zone_proc_end,
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "Failed to create zone iterator\n");
422 GNUNET_SCHEDULER_shutdown ();
428 main (int argc, char *argv[])
430 const char *plugin_name;
433 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
434 GNUNET_asprintf (&cfg_name,
435 "test_namestore_api_%s.conf",
438 GNUNET_DISK_purge_cfg_dir (cfg_name,
441 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-specific-zone",
448 GNUNET_DISK_purge_cfg_dir (cfg_name,
450 GNUNET_free (cfg_name);
454 /* end of test_namestore_api_zone_iteration_specific_zone.c */