2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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;
60 static char *directory;
63 * Re-establish the connection to the service.
65 * @param cls handle to use to re-connect.
66 * @param tc scheduler context
69 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 GNUNET_NAMESTORE_zone_iteration_stop (zi);
78 GNUNET_NAMESTORE_disconnect (nsh);
81 GNUNET_free_non_null(s_name_1);
82 GNUNET_free_non_null(s_name_2);
83 GNUNET_free_non_null(s_name_3);
87 GNUNET_free ((void *)s_rd_1->data);
92 GNUNET_free ((void *)s_rd_2->data);
97 GNUNET_free ((void *)s_rd_3->data);
102 GNUNET_free (privkey);
105 if (privkey2 != NULL)
106 GNUNET_free (privkey2);
113 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_NAMESTORE_zone_iteration_stop (zi);
121 if (endbadly_task != NULL)
123 GNUNET_SCHEDULER_cancel (endbadly_task);
124 endbadly_task = NULL;
128 GNUNET_free (privkey);
131 if (privkey2 != NULL)
132 GNUNET_free (privkey2);
135 GNUNET_free (s_name_1);
136 GNUNET_free (s_name_2);
137 GNUNET_free (s_name_3);
140 GNUNET_free ((void *)s_rd_1->data);
141 GNUNET_free (s_rd_1);
145 GNUNET_free ((void *)s_rd_2->data);
146 GNUNET_free (s_rd_2);
150 GNUNET_free ((void *)s_rd_3->data);
151 GNUNET_free (s_rd_3);
154 GNUNET_NAMESTORE_disconnect (nsh);
160 zone_proc (void *cls,
161 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
163 unsigned int rd_count,
164 const struct GNUNET_GNSRECORD_Data *rd)
166 int failed = GNUNET_NO;
167 if ((zone == NULL) && (label == NULL))
169 GNUNET_break (2 == returned_records);
170 if (2 == returned_records)
172 res = 0; /* Last iteraterator callback, we are done */
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Received last result, iteration done after receing %u results\n",
181 GNUNET_SCHEDULER_add_now (&end, NULL);
184 GNUNET_assert (NULL != zone);
185 if (0 == memcmp (zone,
187 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
189 if (0 == strcmp (label, s_name_1))
193 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
205 else if (0 == strcmp (label, s_name_2))
209 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Received invalid record count\n");
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Comparing result failed: got name `%s' for first zone\n",
232 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Received data for not requested zone\n");
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Received invalid zone\n");
246 if (failed == GNUNET_NO)
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Telling namestore to send the next result\n");
251 GNUNET_NAMESTORE_zone_iterator_next (zi);
256 GNUNET_SCHEDULER_add_now (&end, NULL);
262 put_cont (void *cls, int32_t success, const char *emsg)
266 if (success == GNUNET_OK)
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Created record %u \n", c);
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 "Failed to created records: `%s'\n",
278 if (NULL != endbadly_task)
279 GNUNET_SCHEDULER_cancel (endbadly_task);
280 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
287 returned_records = 0;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "All records created, starting iteration over all zones \n");
290 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "Failed to create zone iterator\n");
299 if (NULL != endbadly_task)
300 GNUNET_SCHEDULER_cancel (endbadly_task);
301 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
308 static struct GNUNET_GNSRECORD_Data *
309 create_record (unsigned int count)
312 struct GNUNET_GNSRECORD_Data * rd;
314 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
315 for (c = 0; c < count; c++)
317 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
318 rd[c].record_type = 1111;
319 rd[c].data_size = 50;
320 rd[c].data = GNUNET_malloc(50);
322 memset ((char *) rd[c].data, 'a', 50);
329 * Callback called from the zone iterator when we iterate over
330 * the empty zone. Check that we got no records and then
331 * start the actual tests by filling the zone.
334 empty_zone_proc (void *cls,
335 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
337 unsigned int rd_count,
338 const struct GNUNET_GNSRECORD_Data *rd)
342 GNUNET_assert (nsh == cls);
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Expected empty zone but received zone private key\n"));
348 if (endbadly_task != NULL)
349 GNUNET_SCHEDULER_cancel (endbadly_task);
350 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
353 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356 _("Expected no zone content but received data\n"));
358 if (endbadly_task != NULL)
359 GNUNET_SCHEDULER_cancel (endbadly_task);
360 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
366 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
367 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
369 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
370 GNUNET_free (hostkey_file);
371 GNUNET_assert (privkey != NULL);
373 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
374 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
376 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
377 GNUNET_free (hostkey_file);
378 GNUNET_assert (privkey2 != NULL);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Created record 1\n");
383 GNUNET_asprintf(&s_name_1, "dummy1");
384 s_rd_1 = create_record(1);
385 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
386 1, s_rd_1, &put_cont, NULL);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Created record 2 \n");
390 GNUNET_asprintf(&s_name_2, "dummy2");
391 s_rd_2 = create_record(1);
392 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
393 1, s_rd_2, &put_cont, NULL);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Created record 3\n");
398 /* name in different zone */
399 GNUNET_asprintf(&s_name_3, "dummy3");
400 s_rd_3 = create_record(1);
401 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
402 1, s_rd_3, &put_cont, NULL);
408 const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_TESTING_Peer *peer)
412 GNUNET_assert (GNUNET_OK ==
413 GNUNET_CONFIGURATION_get_value_string (cfg,
417 GNUNET_DISK_directory_remove (directory);
419 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
422 nsh = GNUNET_NAMESTORE_connect (cfg);
423 GNUNET_break (NULL != nsh);
424 /* first, iterate over empty namestore */
425 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
427 &empty_zone_proc, nsh);
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431 "Failed to create zone iterator\n");
433 GNUNET_SCHEDULER_cancel (endbadly_task);
434 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
440 main (int argc, char *argv[])
444 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
445 "test_namestore_api.conf",
451 if (NULL != directory)
453 GNUNET_DISK_directory_remove (directory);
454 GNUNET_free (directory);
459 /* end of test_namestore_api_zone_iteration_specific_zone.c */