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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file namestore/test_namestore_api_zone_iteration.c
17 * @brief testcase for zone iteration functionality: iterate all zones
20 #include "gnunet_namestore_service.h"
21 #include "gnunet_testing_lib.h"
22 #include "namestore.h"
23 #include "gnunet_dnsparser_lib.h"
25 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
27 #define ZONE_NICK_1 "nick1"
28 #define ZONE_NICK_2 "nick2"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
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 struct GNUNET_NAMESTORE_QueueEntry *nsqe;
63 * Re-establish the connection to the service.
65 * @param cls handle to use to re-connect.
66 * @param tc scheduler context
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);
117 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 check_zone_1 (const char *label, unsigned int rd_count,
160 const struct GNUNET_GNSRECORD_Data *rd)
162 int failed = GNUNET_NO;
164 for (c = 0; c< rd_count ; c++)
166 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
167 if (0 != strcmp (rd[c].data, ZONE_NICK_1))
177 check_zone_2 (const char *label,
178 unsigned int rd_count,
179 const struct GNUNET_GNSRECORD_Data *rd)
181 int failed = GNUNET_NO;
184 for (c = 0; c< rd_count ; c++)
186 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
187 if (0 != strcmp (rd[c].data, ZONE_NICK_2))
199 zone_proc_end (void *cls)
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Received last result, iteration done after receing %u results\n",
206 GNUNET_SCHEDULER_add_now (&end, NULL);
211 zone_proc (void *cls,
212 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
214 unsigned int rd_count,
215 const struct GNUNET_GNSRECORD_Data *rd)
217 int failed = GNUNET_NO;
219 GNUNET_assert (NULL != zone);
220 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
222 failed = check_zone_1 (label, rd_count, rd);
223 if (GNUNET_YES == failed)
226 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
228 failed = check_zone_2 (label, rd_count, rd);
229 if (GNUNET_YES == failed)
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Received invalid zone\n");
240 if (failed == GNUNET_NO)
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Telling namestore to send the next result\n");
245 GNUNET_NAMESTORE_zone_iterator_next (zi,
252 GNUNET_SCHEDULER_add_now (&end, NULL);
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)
316 struct GNUNET_GNSRECORD_Data * rd;
318 rd = GNUNET_new_array (count,
319 struct GNUNET_GNSRECORD_Data);
320 for (unsigned int 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 = TEST_RECORD_TYPE;
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,
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
344 GNUNET_asprintf(&s_name_1, "dummy1");
345 s_rd_1 = create_record(1);
346 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Created record 2 \n");
352 GNUNET_asprintf(&s_name_2, "dummy2");
353 s_rd_2 = create_record(1);
354 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
355 1, s_rd_2, &put_cont, NULL);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Created record 3\n");
360 /* name in different zone */
361 GNUNET_asprintf(&s_name_3, "dummy3");
362 s_rd_3 = create_record(1);
363 GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
370 nick_1_cont (void *cls, int32_t success, const char *emsg)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Nick 1 added : %s\n",
374 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
376 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 _("Namestore cannot store no block\n"));
386 * Callback called from the zone iterator when we iterate over
387 * the empty zone. Check that we got no records and then
388 * start the actual tests by filling the zone.
391 empty_zone_proc (void *cls,
392 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
394 unsigned int rd_count,
395 const struct GNUNET_GNSRECORD_Data *rd)
397 GNUNET_assert (nsh == cls);
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402 _("Expected empty zone but received zone private key\n"));
404 if (endbadly_task != NULL)
405 GNUNET_SCHEDULER_cancel (endbadly_task);
406 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
409 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412 _("Expected no zone content but received data\n"));
414 if (endbadly_task != NULL)
415 GNUNET_SCHEDULER_cancel (endbadly_task);
416 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
424 empty_zone_end (void *cls)
426 GNUNET_assert (nsh == cls);
428 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
429 GNUNET_assert (privkey != NULL);
430 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
431 GNUNET_assert (privkey2 != NULL);
433 nsqe = GNUNET_NAMESTORE_set_nick (nsh,
440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 _("Namestore cannot store no block\n"));
449 const struct GNUNET_CONFIGURATION_Handle *cfg,
450 struct GNUNET_TESTING_Peer *peer)
452 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
455 nsh = GNUNET_NAMESTORE_connect (cfg);
456 GNUNET_break (NULL != nsh);
458 /* first, iterate over empty namestore */
459 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 "Failed to create zone iterator\n");
472 GNUNET_SCHEDULER_cancel (endbadly_task);
473 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
479 main (int argc, char *argv[])
481 const char *plugin_name;
484 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
485 GNUNET_asprintf (&cfg_name,
486 "test_namestore_api_%s.conf",
489 GNUNET_DISK_purge_cfg_dir (cfg_name,
492 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
499 GNUNET_DISK_purge_cfg_dir (cfg_name,
501 GNUNET_free (cfg_name);
506 /* end of test_namestore_api_zone_iteration.c */