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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34 static struct GNUNET_NAMESTORE_Handle * nsh;
36 static struct GNUNET_SCHEDULER_Task * endbadly_task;
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
42 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
46 static int returned_records;
48 static char * s_name_1;
50 static struct GNUNET_GNSRECORD_Data *s_rd_1;
52 static char * s_name_2;
54 static struct GNUNET_GNSRECORD_Data *s_rd_2;
56 static char * s_name_3;
58 static struct GNUNET_GNSRECORD_Data *s_rd_3;
64 * @param cls handle to use to re-connect.
70 GNUNET_SCHEDULER_shutdown ();
80 GNUNET_NAMESTORE_zone_iteration_stop (zi);
83 if (NULL != endbadly_task)
85 GNUNET_SCHEDULER_cancel (endbadly_task);
90 GNUNET_free (privkey);
95 GNUNET_free (privkey2);
98 GNUNET_free (s_name_1);
99 GNUNET_free (s_name_2);
100 GNUNET_free (s_name_3);
103 GNUNET_free ((void *)s_rd_1->data);
104 GNUNET_free (s_rd_1);
108 GNUNET_free ((void *)s_rd_2->data);
109 GNUNET_free (s_rd_2);
113 GNUNET_free ((void *)s_rd_3->data);
114 GNUNET_free (s_rd_3);
118 GNUNET_NAMESTORE_disconnect (nsh);
133 zone_proc (void *cls,
134 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
136 unsigned int rd_count,
137 const struct GNUNET_GNSRECORD_Data *rd)
139 int failed = GNUNET_NO;
141 GNUNET_assert (NULL != zone);
142 if (0 == memcmp (zone,
144 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
146 if (0 == strcmp (label, s_name_1))
150 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
162 else if (0 == strcmp (label, s_name_2))
166 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Received invalid record count\n");
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Comparing result failed: got name `%s' for first zone\n",
189 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 "Received data for not requested zone\n");
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "Received invalid zone\n");
203 if (failed == GNUNET_NO)
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Telling namestore to send the next result\n");
208 GNUNET_NAMESTORE_zone_iterator_next (zi,
215 GNUNET_SCHEDULER_shutdown ();
221 zone_proc_end (void *cls)
224 GNUNET_break (2 == returned_records);
225 if (2 == returned_records)
227 res = 0; /* Last iteraterator callback, we are done */
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Received last result, iteration done after receing %u results\n",
237 GNUNET_SCHEDULER_shutdown ();
248 if (success == GNUNET_OK)
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Created record %u \n", c);
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 "Failed to created records: `%s'\n",
261 GNUNET_SCHEDULER_shutdown ();
268 returned_records = 0;
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "All records created, starting iteration over all zones \n");
271 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 "Failed to create zone iterator\n");
285 GNUNET_SCHEDULER_shutdown ();
292 static struct GNUNET_GNSRECORD_Data *
293 create_record (unsigned int count)
295 struct GNUNET_GNSRECORD_Data *rd;
297 rd = GNUNET_new_array (count,
298 struct GNUNET_GNSRECORD_Data);
299 for (unsigned int c = 0; c < count; c++)
301 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
302 rd[c].record_type = 1111;
303 rd[c].data_size = 50;
304 rd[c].data = GNUNET_malloc(50);
306 memset ((char *) rd[c].data, 'a', 50);
313 * Callback called from the zone iterator when we iterate over
314 * the empty zone. Check that we got no records and then
315 * start the actual tests by filling the zone.
318 empty_zone_proc (void *cls,
319 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
321 unsigned int rd_count,
322 const struct GNUNET_GNSRECORD_Data *rd)
324 GNUNET_assert (nsh == cls);
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 _("Expected empty zone but received zone private key\n"));
331 GNUNET_SCHEDULER_shutdown ();
334 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
337 _("Expected no zone content but received data\n"));
340 GNUNET_SCHEDULER_shutdown ();
348 empty_zone_proc_end (void *cls)
351 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
352 GNUNET_assert (privkey != NULL);
353 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
354 GNUNET_assert (privkey2 != NULL);
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Created record 1\n");
358 GNUNET_asprintf (&s_name_1,
360 s_rd_1 = create_record (1);
361 GNUNET_NAMESTORE_records_store (nsh,
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Created record 2 \n");
371 GNUNET_asprintf (&s_name_2,
373 s_rd_2 = create_record (1);
374 GNUNET_NAMESTORE_records_store (nsh,
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Created record 3\n");
385 /* name in different zone */
386 GNUNET_asprintf (&s_name_3,
388 s_rd_3 = create_record (1);
389 GNUNET_NAMESTORE_records_store (nsh,
400 const struct GNUNET_CONFIGURATION_Handle *cfg,
401 struct GNUNET_TESTING_Peer *peer)
403 GNUNET_SCHEDULER_add_shutdown (&end,
405 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
408 nsh = GNUNET_NAMESTORE_connect (cfg);
409 GNUNET_break (NULL != nsh);
410 /* first, iterate over empty namestore */
411 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
417 &empty_zone_proc_end,
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 "Failed to create zone iterator\n");
424 GNUNET_SCHEDULER_shutdown ();
430 main (int argc, char *argv[])
432 const char *plugin_name;
435 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
436 GNUNET_asprintf (&cfg_name,
437 "test_namestore_api_%s.conf",
440 GNUNET_DISK_purge_cfg_dir (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 */