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: iterate all zones
25 #include "gnunet_common.h"
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 GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
38 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
42 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
46 static int returned_records;
48 static char * s_name_1;
50 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
52 static char * s_name_2;
54 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
56 static char * s_name_3;
58 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
62 * Re-establish the connection to the service.
64 * @param cls handle to use to re-connect.
65 * @param tc scheduler context
68 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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);
112 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_NAMESTORE_zone_iteration_stop (zi);
120 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
122 GNUNET_SCHEDULER_cancel (endbadly_task);
123 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
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_EccPrivateKey *zone,
162 unsigned int rd_count,
163 const struct GNUNET_NAMESTORE_RecordData *rd)
165 int failed = GNUNET_NO;
166 if ((zone == NULL) && (label == NULL))
168 GNUNET_break (3 == returned_records);
169 if (3 == returned_records)
171 res = 0; /* Last iteraterator callback, we are done */
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Received last result, iteration done after receing %u results\n",
180 GNUNET_SCHEDULER_add_now (&end, NULL);
185 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
187 if (0 == strcmp (label, s_name_1))
191 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
203 else if (0 == strcmp (label, s_name_2))
207 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 "Received invalid record count\n");
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Comparing result failed: got name `%s' for first zone\n", label);
229 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
231 if (0 == strcmp (label, s_name_3))
235 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 "Received invalid record count\n");
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Comparing result failed: got name `%s' for first zone\n", label);
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
265 if (failed == GNUNET_NO)
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Telling namestore to send the next result\n");
270 GNUNET_NAMESTORE_zone_iterator_next (zi);
275 GNUNET_SCHEDULER_add_now (&end, NULL);
281 put_cont (void *cls, int32_t success, const char *emsg)
285 if (success == GNUNET_OK)
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
295 if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
296 GNUNET_SCHEDULER_cancel (endbadly_task);
297 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
304 returned_records = 0;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
306 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\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 static struct GNUNET_NAMESTORE_RecordData *
324 create_record (unsigned int count)
327 struct GNUNET_NAMESTORE_RecordData * rd;
329 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
330 for (c = 0; c < count; c++)
332 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
333 rd[c].record_type = 1111;
334 rd[c].data_size = 50;
335 rd[c].data = GNUNET_malloc(50);
336 memset ((char *) rd[c].data, 'a', 50);
343 * Callback called from the zone iterator when we iterate over
344 * the empty zone. Check that we got no records and then
345 * start the actual tests by filling the zone.
348 empty_zone_proc (void *cls,
349 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
351 unsigned int rd_count,
352 const struct GNUNET_NAMESTORE_RecordData *rd)
356 GNUNET_assert (nsh == cls);
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Expected empty zone but received zone private key\n"));
362 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
363 GNUNET_SCHEDULER_cancel (endbadly_task);
364 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
367 if ((NULL != label) || (NULL != rd) || (0 != rd))
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Expected no zone content but received data\n"));
372 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
373 GNUNET_SCHEDULER_cancel (endbadly_task);
374 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
380 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
381 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
383 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
384 GNUNET_free (hostkey_file);
385 GNUNET_assert (privkey != NULL);
387 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
388 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
390 privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
391 GNUNET_free (hostkey_file);
392 GNUNET_assert (privkey2 != NULL);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
396 GNUNET_asprintf(&s_name_1, "dummy1");
397 s_rd_1 = create_record(1);
398 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
399 1, s_rd_1, &put_cont, NULL);
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
402 GNUNET_asprintf(&s_name_2, "dummy2");
403 s_rd_2 = create_record(1);
404 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
405 1, s_rd_2, &put_cont, NULL);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
409 /* name in different zone */
410 GNUNET_asprintf(&s_name_3, "dummy3");
411 s_rd_3 = create_record(1);
412 GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
413 1, s_rd_3, &put_cont, NULL);
419 const struct GNUNET_CONFIGURATION_Handle *cfg,
420 struct GNUNET_TESTING_Peer *peer)
422 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
423 nsh = GNUNET_NAMESTORE_connect (cfg);
424 GNUNET_break (NULL != nsh);
425 /* first, iterate over empty namestore */
426 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
427 NULL, &empty_zone_proc, nsh);
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
432 GNUNET_SCHEDULER_cancel (endbadly_task);
433 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
439 main (int argc, char *argv[])
443 GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration",
445 "test_namestore_api.conf",
453 /* end of test_namestore_api_zone_iteration.c */