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 GNUNET_SCHEDULER_TaskIdentifier 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);
109 if (NULL != directory)
111 GNUNET_DISK_directory_remove (directory);
112 GNUNET_free (directory);
119 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 GNUNET_NAMESTORE_zone_iteration_stop (zi);
127 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
129 GNUNET_SCHEDULER_cancel (endbadly_task);
130 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
134 GNUNET_free (privkey);
137 if (privkey2 != NULL)
138 GNUNET_free (privkey2);
141 GNUNET_free (s_name_1);
142 GNUNET_free (s_name_2);
143 GNUNET_free (s_name_3);
146 GNUNET_free ((void *)s_rd_1->data);
147 GNUNET_free (s_rd_1);
151 GNUNET_free ((void *)s_rd_2->data);
152 GNUNET_free (s_rd_2);
156 GNUNET_free ((void *)s_rd_3->data);
157 GNUNET_free (s_rd_3);
160 GNUNET_NAMESTORE_disconnect (nsh);
166 zone_proc (void *cls,
167 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
169 unsigned int rd_count,
170 const struct GNUNET_GNSRECORD_Data *rd)
172 int failed = GNUNET_NO;
173 if ((zone == NULL) && (label == NULL))
175 GNUNET_break (1 <= returned_records);
176 if (1 >= returned_records)
177 res = 1; /* Last iteraterator callback, we are done */
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "Received last result, iteration done after receing %u results\n",
184 GNUNET_SCHEDULER_add_now (&end, NULL);
189 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
191 if (0 == strcmp (label, s_name_1))
195 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
207 else if (0 == strcmp (label, s_name_2))
211 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Received invalid record count\n");
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Comparing result failed: got name `%s' for first zone\n", label);
233 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
235 if (0 == strcmp (label, s_name_3))
239 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Received invalid record count\n");
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Comparing result failed: got name `%s' for first zone\n", label);
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
269 if (failed == GNUNET_NO)
271 if (1 == returned_records)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Telling namestore to stop zone iteration\n");
275 GNUNET_NAMESTORE_zone_iteration_stop (zi);
277 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
279 GNUNET_SCHEDULER_cancel (endbadly_task);
280 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
283 GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Telling namestore to send the next result\n");
289 GNUNET_NAMESTORE_zone_iterator_next (zi);
294 GNUNET_SCHEDULER_add_now (&end, NULL);
300 put_cont (void *cls, int32_t success, const char *emsg)
304 if (success == GNUNET_OK)
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
314 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
315 GNUNET_SCHEDULER_cancel (endbadly_task);
316 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
323 returned_records = 0;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
325 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
333 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
334 GNUNET_SCHEDULER_cancel (endbadly_task);
335 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
342 static struct GNUNET_GNSRECORD_Data *
343 create_record (unsigned int count)
346 struct GNUNET_GNSRECORD_Data * rd;
348 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
349 for (c = 0; c < count; c++)
351 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
352 rd[c].record_type = 1111;
353 rd[c].data_size = 50;
354 rd[c].data = GNUNET_malloc(50);
356 memset ((char *) rd[c].data, 'a', 50);
363 * Callback called from the zone iterator when we iterate over
364 * the empty zone. Check that we got no records and then
365 * start the actual tests by filling the zone.
368 empty_zone_proc (void *cls,
369 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
371 unsigned int rd_count,
372 const struct GNUNET_GNSRECORD_Data *rd)
376 GNUNET_assert (nsh == cls);
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 _("Expected empty zone but received zone private key\n"));
382 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
383 GNUNET_SCHEDULER_cancel (endbadly_task);
384 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
387 if ((NULL != label) || (NULL != rd) || (0 != rd))
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 _("Expected no zone content but received data\n"));
392 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
393 GNUNET_SCHEDULER_cancel (endbadly_task);
394 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
400 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
401 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
403 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
404 GNUNET_free (hostkey_file);
405 GNUNET_assert (privkey != NULL);
407 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
408 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
410 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
411 GNUNET_free (hostkey_file);
412 GNUNET_assert (privkey2 != NULL);
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
416 GNUNET_asprintf(&s_name_1, "dummy1");
417 s_rd_1 = create_record(1);
418 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
419 1, s_rd_1, &put_cont, NULL);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
422 GNUNET_asprintf(&s_name_2, "dummy2");
423 s_rd_2 = create_record(1);
424 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
425 1, s_rd_2, &put_cont, NULL);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
429 /* name in different zone */
430 GNUNET_asprintf(&s_name_3, "dummy3");
431 s_rd_3 = create_record(1);
432 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
433 1, s_rd_3, &put_cont, NULL);
439 const struct GNUNET_CONFIGURATION_Handle *cfg,
440 struct GNUNET_TESTING_Peer *peer)
443 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory);
445 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
446 nsh = GNUNET_NAMESTORE_connect (cfg);
447 GNUNET_break (NULL != nsh);
448 /* first, iterate over empty namestore */
449 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
450 NULL, &empty_zone_proc, nsh);
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
455 GNUNET_SCHEDULER_cancel (endbadly_task);
456 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
462 main (int argc, char *argv[])
464 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namestore/");
467 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
468 "test_namestore_api.conf",
475 /* end of test_namestore_api_zone_iteration_stop.c */