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.c
22 * @brief testcase for zone iteration functionality: iterate all zones
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
29 #define ZONE_NICK_1 "nick1"
30 #define ZONE_NICK_2 "nick2"
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;
61 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
63 static char *directory;
66 * Re-establish the connection to the service.
68 * @param cls handle to use to re-connect.
69 * @param tc scheduler context
76 GNUNET_NAMESTORE_zone_iteration_stop (zi);
81 GNUNET_NAMESTORE_disconnect (nsh);
84 GNUNET_free_non_null(s_name_1);
85 GNUNET_free_non_null(s_name_2);
86 GNUNET_free_non_null(s_name_3);
90 GNUNET_free ((void *)s_rd_1->data);
95 GNUNET_free ((void *)s_rd_2->data);
100 GNUNET_free ((void *)s_rd_3->data);
101 GNUNET_free (s_rd_3);
105 GNUNET_free (privkey);
108 if (privkey2 != NULL)
109 GNUNET_free (privkey2);
120 GNUNET_NAMESTORE_zone_iteration_stop (zi);
123 if (endbadly_task != NULL)
125 GNUNET_SCHEDULER_cancel (endbadly_task);
126 endbadly_task = NULL;
130 GNUNET_free (privkey);
133 if (privkey2 != NULL)
134 GNUNET_free (privkey2);
137 GNUNET_free (s_name_1);
138 GNUNET_free (s_name_2);
139 GNUNET_free (s_name_3);
142 GNUNET_free ((void *)s_rd_1->data);
143 GNUNET_free (s_rd_1);
147 GNUNET_free ((void *)s_rd_2->data);
148 GNUNET_free (s_rd_2);
152 GNUNET_free ((void *)s_rd_3->data);
153 GNUNET_free (s_rd_3);
156 GNUNET_NAMESTORE_disconnect (nsh);
162 check_zone_1 (const char *label, unsigned int rd_count,
163 const struct GNUNET_GNSRECORD_Data *rd)
165 int failed = GNUNET_NO;
167 for (c = 0; c< rd_count ; c++)
169 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
170 if (0 != strcmp (rd[c].data, ZONE_NICK_1))
180 check_zone_2 (const char *label,
181 unsigned int rd_count,
182 const struct GNUNET_GNSRECORD_Data *rd)
184 int failed = GNUNET_NO;
187 for (c = 0; c< rd_count ; c++)
189 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
190 if (0 != strcmp (rd[c].data, ZONE_NICK_2))
202 zone_proc_end (void *cls)
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Received last result, iteration done after receing %u results\n",
209 GNUNET_SCHEDULER_add_now (&end, NULL);
214 zone_proc (void *cls,
215 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
217 unsigned int rd_count,
218 const struct GNUNET_GNSRECORD_Data *rd)
220 int failed = GNUNET_NO;
222 GNUNET_assert (NULL != zone);
223 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
225 failed = check_zone_1 (label, rd_count, rd);
226 if (GNUNET_YES == failed)
229 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
231 failed = check_zone_2 (label, rd_count, rd);
232 if (GNUNET_YES == failed)
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Received invalid zone\n");
243 if (failed == GNUNET_NO)
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Telling namestore to send the next result\n");
248 GNUNET_NAMESTORE_zone_iterator_next (zi);
254 GNUNET_SCHEDULER_add_now (&end, NULL);
267 put_cont (void *cls, int32_t success, const char *emsg)
271 if (success == GNUNET_OK)
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
281 if (NULL != endbadly_task)
282 GNUNET_SCHEDULER_cancel (endbadly_task);
283 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
290 returned_records = 0;
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
292 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
304 if (NULL != endbadly_task)
305 GNUNET_SCHEDULER_cancel (endbadly_task);
306 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
313 static struct GNUNET_GNSRECORD_Data *
314 create_record (unsigned int count)
317 struct GNUNET_GNSRECORD_Data * rd;
319 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
320 for (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 = 1111;
324 rd[c].data_size = 50;
325 rd[c].data = GNUNET_malloc(50);
327 memset ((char *) rd[c].data, 'a', 50);
334 nick_2_cont (void *cls, int32_t success, const char *emsg)
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
342 GNUNET_asprintf(&s_name_1, "dummy1");
343 s_rd_1 = create_record(1);
344 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Created record 2 \n");
350 GNUNET_asprintf(&s_name_2, "dummy2");
351 s_rd_2 = create_record(1);
352 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
353 1, s_rd_2, &put_cont, NULL);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Created record 3\n");
358 /* name in different zone */
359 GNUNET_asprintf(&s_name_3, "dummy3");
360 s_rd_3 = create_record(1);
361 GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
368 nick_1_cont (void *cls, int32_t success, const char *emsg)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Nick 1 added : %s\n",
372 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
374 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 _("Namestore cannot store no block\n"));
384 * Callback called from the zone iterator when we iterate over
385 * the empty zone. Check that we got no records and then
386 * start the actual tests by filling the zone.
389 empty_zone_proc (void *cls,
390 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
392 unsigned int rd_count,
393 const struct GNUNET_GNSRECORD_Data *rd)
395 GNUNET_assert (nsh == cls);
399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400 _("Expected empty zone but received zone private key\n"));
402 if (endbadly_task != NULL)
403 GNUNET_SCHEDULER_cancel (endbadly_task);
404 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
407 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410 _("Expected no zone content but received data\n"));
412 if (endbadly_task != NULL)
413 GNUNET_SCHEDULER_cancel (endbadly_task);
414 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
422 empty_zone_end (void *cls)
425 GNUNET_assert (nsh == cls);
428 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
429 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
431 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
432 GNUNET_free (hostkey_file);
433 GNUNET_assert (privkey != NULL);
435 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
436 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
438 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
439 GNUNET_free (hostkey_file);
440 GNUNET_assert (privkey2 != NULL);
442 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, ZONE_NICK_1, &nick_1_cont, &privkey);
445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
446 _("Namestore cannot store no block\n"));
454 const struct GNUNET_CONFIGURATION_Handle *cfg,
455 struct GNUNET_TESTING_Peer *peer)
458 GNUNET_assert (GNUNET_OK ==
459 GNUNET_CONFIGURATION_get_value_string (cfg,
463 GNUNET_DISK_directory_remove (directory);
465 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
466 nsh = GNUNET_NAMESTORE_connect (cfg);
467 GNUNET_break (NULL != nsh);
469 /* first, iterate over empty namestore */
470 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 "Failed to create zone iterator\n");
483 GNUNET_SCHEDULER_cancel (endbadly_task);
484 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
490 main (int argc, char *argv[])
494 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
495 "test_namestore_api.conf",
501 if (NULL != directory)
503 GNUNET_DISK_directory_remove (directory);
504 GNUNET_free (directory);
510 /* end of test_namestore_api_zone_iteration.c */