2 This file is part of GNUnet.
3 Copyright (C) 2009 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: 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.
72 GNUNET_NAMESTORE_zone_iteration_stop (zi);
77 GNUNET_NAMESTORE_disconnect (nsh);
80 GNUNET_free_non_null(s_name_1);
81 GNUNET_free_non_null(s_name_2);
82 GNUNET_free_non_null(s_name_3);
86 GNUNET_free ((void *)s_rd_1->data);
91 GNUNET_free ((void *)s_rd_2->data);
96 GNUNET_free ((void *)s_rd_3->data);
101 GNUNET_free (privkey);
104 if (privkey2 != NULL)
105 GNUNET_free (privkey2);
116 GNUNET_NAMESTORE_zone_iteration_stop (zi);
120 if (endbadly_task != NULL)
122 GNUNET_SCHEDULER_cancel (endbadly_task);
123 endbadly_task = NULL;
127 GNUNET_free (privkey);
130 if (privkey2 != NULL)
131 GNUNET_free (privkey2);
134 GNUNET_free (s_name_1);
135 GNUNET_free (s_name_2);
136 GNUNET_free (s_name_3);
139 GNUNET_free ((void *)s_rd_1->data);
140 GNUNET_free (s_rd_1);
144 GNUNET_free ((void *)s_rd_2->data);
145 GNUNET_free (s_rd_2);
149 GNUNET_free ((void *)s_rd_3->data);
150 GNUNET_free (s_rd_3);
153 GNUNET_NAMESTORE_disconnect (nsh);
159 zone_proc (void *cls,
160 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
162 unsigned int rd_count,
163 const struct GNUNET_GNSRECORD_Data *rd)
165 int failed = GNUNET_NO;
166 if ((zone == NULL) && (label == NULL))
168 GNUNET_break (1 <= returned_records);
169 if (1 >= returned_records)
170 res = 1; /* Last iteraterator callback, we are done */
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Received last result, iteration done after receing %u results\n",
177 GNUNET_SCHEDULER_add_now (&end, NULL);
180 GNUNET_assert (NULL != zone);
181 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
183 if (0 == strcmp (label, s_name_1))
187 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
199 else if (0 == strcmp (label, s_name_2))
203 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Received invalid record count\n");
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Comparing result failed: got name `%s' for first zone\n", label);
225 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
227 if (0 == strcmp (label, s_name_3))
231 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 "Received invalid record count\n");
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Comparing result failed: got name `%s' for first zone\n", label);
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "Received invalid zone\n");
260 if (failed == GNUNET_NO)
262 if (1 == returned_records)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Telling namestore to stop zone iteration\n");
266 GNUNET_NAMESTORE_zone_iteration_stop (zi);
268 if (NULL != endbadly_task)
270 GNUNET_SCHEDULER_cancel (endbadly_task);
271 endbadly_task = NULL;
274 GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Telling namestore to send the next result\n");
280 GNUNET_NAMESTORE_zone_iterator_next (zi);
285 GNUNET_SCHEDULER_add_now (&end, NULL);
291 put_cont (void *cls, int32_t success, const char *emsg)
295 if (success == GNUNET_OK)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
305 if (NULL != endbadly_task)
306 GNUNET_SCHEDULER_cancel (endbadly_task);
307 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
314 returned_records = 0;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
316 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
324 if (NULL != endbadly_task)
325 GNUNET_SCHEDULER_cancel (endbadly_task);
326 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
333 static struct GNUNET_GNSRECORD_Data *
334 create_record (unsigned int count)
337 struct GNUNET_GNSRECORD_Data * rd;
339 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
340 for (c = 0; c < count; c++)
342 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
343 rd[c].record_type = 1111;
344 rd[c].data_size = 50;
345 rd[c].data = GNUNET_malloc(50);
347 memset ((char *) rd[c].data, 'a', 50);
354 * Callback called from the zone iterator when we iterate over
355 * the empty zone. Check that we got no records and then
356 * start the actual tests by filling the zone.
359 empty_zone_proc (void *cls,
360 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
362 unsigned int rd_count,
363 const struct GNUNET_GNSRECORD_Data *rd)
367 GNUNET_assert (nsh == cls);
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 _("Expected empty zone but received zone private key\n"));
373 if (endbadly_task != NULL)
374 GNUNET_SCHEDULER_cancel (endbadly_task);
375 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
378 if ((NULL != label) || (NULL != rd) || (0 != rd))
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 _("Expected no zone content but received data\n"));
383 if (endbadly_task != NULL)
384 GNUNET_SCHEDULER_cancel (endbadly_task);
385 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
391 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
392 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
394 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
395 GNUNET_free (hostkey_file);
396 GNUNET_assert (privkey != NULL);
398 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
399 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
401 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
402 GNUNET_free (hostkey_file);
403 GNUNET_assert (privkey2 != NULL);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
407 GNUNET_asprintf(&s_name_1, "dummy1");
408 s_rd_1 = create_record(1);
409 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
410 1, s_rd_1, &put_cont, NULL);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
413 GNUNET_asprintf(&s_name_2, "dummy2");
414 s_rd_2 = create_record(1);
415 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
416 1, s_rd_2, &put_cont, NULL);
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
420 /* name in different zone */
421 GNUNET_asprintf(&s_name_3, "dummy3");
422 s_rd_3 = create_record(1);
423 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
424 1, s_rd_3, &put_cont, NULL);
430 const struct GNUNET_CONFIGURATION_Handle *cfg,
431 struct GNUNET_TESTING_Peer *peer)
433 GNUNET_assert (GNUNET_OK ==
434 GNUNET_CONFIGURATION_get_value_string (cfg,
438 GNUNET_DISK_directory_remove (directory);
440 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
441 nsh = GNUNET_NAMESTORE_connect (cfg);
442 GNUNET_break (NULL != nsh);
443 /* first, iterate over empty namestore */
444 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
445 NULL, &empty_zone_proc, nsh);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 "Failed to create zone iterator\n");
451 GNUNET_SCHEDULER_cancel (endbadly_task);
452 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
458 main (int argc, char *argv[])
462 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
463 "test_namestore_api.conf",
469 if (NULL != directory)
471 GNUNET_DISK_directory_remove (directory);
472 GNUNET_free (directory);
477 /* end of test_namestore_api_zone_iteration_stop.c */