2 This file is part of GNUnet.
3 (C) 2009 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file namestore/test_namestore_api_zone_iteration.c
22 * @brief testcase for zone iteration functionality: stop iterating of zones
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
31 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
33 static struct GNUNET_NAMESTORE_Handle * nsh;
35 static struct GNUNET_SCHEDULER_Task * endbadly_task;
37 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
41 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
45 static int returned_records;
47 static char * s_name_1;
49 static struct GNUNET_GNSRECORD_Data *s_rd_1;
51 static char * s_name_2;
53 static struct GNUNET_GNSRECORD_Data *s_rd_2;
55 static char * s_name_3;
57 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59 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 (1 <= returned_records);
170 if (1 >= returned_records)
171 res = 1; /* Last iteraterator callback, we are done */
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Received last result, iteration done after receing %u results\n",
178 GNUNET_SCHEDULER_add_now (&end, NULL);
181 GNUNET_assert (NULL != zone);
182 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
184 if (0 == strcmp (label, s_name_1))
188 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
200 else if (0 == strcmp (label, s_name_2))
204 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Received invalid record count\n");
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Comparing result failed: got name `%s' for first zone\n", label);
226 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
228 if (0 == strcmp (label, s_name_3))
232 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "Received invalid record count\n");
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Comparing result failed: got name `%s' for first zone\n", label);
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 "Received invalid zone\n");
261 if (failed == GNUNET_NO)
263 if (1 == returned_records)
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Telling namestore to stop zone iteration\n");
267 GNUNET_NAMESTORE_zone_iteration_stop (zi);
269 if (NULL != endbadly_task)
271 GNUNET_SCHEDULER_cancel (endbadly_task);
272 endbadly_task = NULL;
275 GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Telling namestore to send the next result\n");
281 GNUNET_NAMESTORE_zone_iterator_next (zi);
286 GNUNET_SCHEDULER_add_now (&end, NULL);
292 put_cont (void *cls, int32_t success, const char *emsg)
296 if (success == GNUNET_OK)
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
306 if (NULL != endbadly_task)
307 GNUNET_SCHEDULER_cancel (endbadly_task);
308 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
315 returned_records = 0;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
317 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
325 if (NULL != endbadly_task)
326 GNUNET_SCHEDULER_cancel (endbadly_task);
327 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
334 static struct GNUNET_GNSRECORD_Data *
335 create_record (unsigned int count)
338 struct GNUNET_GNSRECORD_Data * rd;
340 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
341 for (c = 0; c < count; c++)
343 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
344 rd[c].record_type = 1111;
345 rd[c].data_size = 50;
346 rd[c].data = GNUNET_malloc(50);
348 memset ((char *) rd[c].data, 'a', 50);
355 * Callback called from the zone iterator when we iterate over
356 * the empty zone. Check that we got no records and then
357 * start the actual tests by filling the zone.
360 empty_zone_proc (void *cls,
361 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
363 unsigned int rd_count,
364 const struct GNUNET_GNSRECORD_Data *rd)
368 GNUNET_assert (nsh == cls);
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 _("Expected empty zone but received zone private key\n"));
374 if (endbadly_task != NULL)
375 GNUNET_SCHEDULER_cancel (endbadly_task);
376 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
379 if ((NULL != label) || (NULL != rd) || (0 != rd))
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Expected no zone content but received data\n"));
384 if (endbadly_task != NULL)
385 GNUNET_SCHEDULER_cancel (endbadly_task);
386 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
392 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
393 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
395 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
396 GNUNET_free (hostkey_file);
397 GNUNET_assert (privkey != NULL);
399 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
400 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
402 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
403 GNUNET_free (hostkey_file);
404 GNUNET_assert (privkey2 != NULL);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
408 GNUNET_asprintf(&s_name_1, "dummy1");
409 s_rd_1 = create_record(1);
410 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
411 1, s_rd_1, &put_cont, NULL);
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
414 GNUNET_asprintf(&s_name_2, "dummy2");
415 s_rd_2 = create_record(1);
416 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
417 1, s_rd_2, &put_cont, NULL);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
421 /* name in different zone */
422 GNUNET_asprintf(&s_name_3, "dummy3");
423 s_rd_3 = create_record(1);
424 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
425 1, s_rd_3, &put_cont, NULL);
431 const struct GNUNET_CONFIGURATION_Handle *cfg,
432 struct GNUNET_TESTING_Peer *peer)
434 GNUNET_assert (GNUNET_OK ==
435 GNUNET_CONFIGURATION_get_value_string (cfg,
439 GNUNET_DISK_directory_remove (directory);
441 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
442 nsh = GNUNET_NAMESTORE_connect (cfg);
443 GNUNET_break (NULL != nsh);
444 /* first, iterate over empty namestore */
445 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
446 NULL, &empty_zone_proc, nsh);
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 "Failed to create zone iterator\n");
452 GNUNET_SCHEDULER_cancel (endbadly_task);
453 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
459 main (int argc, char *argv[])
463 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
464 "test_namestore_api.conf",
470 if (NULL != directory)
472 GNUNET_DISK_directory_remove (directory);
473 GNUNET_free (directory);
478 /* end of test_namestore_api_zone_iteration_stop.c */