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
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: iterate all zones
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
29 #define ZONE_NICK_1 "nick1"
30 #define ZONE_NICK_2 "nick2"
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
35 static struct GNUNET_NAMESTORE_Handle * nsh;
37 static struct GNUNET_SCHEDULER_Task * endbadly_task;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
43 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
47 static int returned_records;
49 static char * s_name_1;
51 static struct GNUNET_GNSRECORD_Data *s_rd_1;
53 static char * s_name_2;
55 static struct GNUNET_GNSRECORD_Data *s_rd_2;
57 static char * s_name_3;
59 static struct GNUNET_GNSRECORD_Data *s_rd_3;
61 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
65 * Re-establish the connection to the service.
67 * @param cls handle to use to re-connect.
68 * @param tc scheduler context
75 GNUNET_NAMESTORE_zone_iteration_stop (zi);
80 GNUNET_NAMESTORE_disconnect (nsh);
83 GNUNET_free_non_null(s_name_1);
84 GNUNET_free_non_null(s_name_2);
85 GNUNET_free_non_null(s_name_3);
89 GNUNET_free ((void *)s_rd_1->data);
94 GNUNET_free ((void *)s_rd_2->data);
99 GNUNET_free ((void *)s_rd_3->data);
100 GNUNET_free (s_rd_3);
104 GNUNET_free (privkey);
107 if (privkey2 != NULL)
108 GNUNET_free (privkey2);
119 GNUNET_NAMESTORE_zone_iteration_stop (zi);
122 if (endbadly_task != NULL)
124 GNUNET_SCHEDULER_cancel (endbadly_task);
125 endbadly_task = NULL;
129 GNUNET_free (privkey);
132 if (privkey2 != NULL)
133 GNUNET_free (privkey2);
136 GNUNET_free (s_name_1);
137 GNUNET_free (s_name_2);
138 GNUNET_free (s_name_3);
141 GNUNET_free ((void *)s_rd_1->data);
142 GNUNET_free (s_rd_1);
146 GNUNET_free ((void *)s_rd_2->data);
147 GNUNET_free (s_rd_2);
151 GNUNET_free ((void *)s_rd_3->data);
152 GNUNET_free (s_rd_3);
155 GNUNET_NAMESTORE_disconnect (nsh);
161 check_zone_1 (const char *label, unsigned int rd_count,
162 const struct GNUNET_GNSRECORD_Data *rd)
164 int failed = GNUNET_NO;
166 for (c = 0; c< rd_count ; c++)
168 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
169 if (0 != strcmp (rd[c].data, ZONE_NICK_1))
179 check_zone_2 (const char *label,
180 unsigned int rd_count,
181 const struct GNUNET_GNSRECORD_Data *rd)
183 int failed = GNUNET_NO;
186 for (c = 0; c< rd_count ; c++)
188 if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
189 if (0 != strcmp (rd[c].data, ZONE_NICK_2))
201 zone_proc_end (void *cls)
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Received last result, iteration done after receing %u results\n",
208 GNUNET_SCHEDULER_add_now (&end, NULL);
213 zone_proc (void *cls,
214 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
216 unsigned int rd_count,
217 const struct GNUNET_GNSRECORD_Data *rd)
219 int failed = GNUNET_NO;
221 GNUNET_assert (NULL != zone);
222 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
224 failed = check_zone_1 (label, rd_count, rd);
225 if (GNUNET_YES == failed)
228 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
230 failed = check_zone_2 (label, rd_count, rd);
231 if (GNUNET_YES == failed)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Received invalid zone\n");
242 if (failed == GNUNET_NO)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Telling namestore to send the next result\n");
247 GNUNET_NAMESTORE_zone_iterator_next (zi,
254 GNUNET_SCHEDULER_add_now (&end, NULL);
273 if (success == GNUNET_OK)
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
283 if (NULL != endbadly_task)
284 GNUNET_SCHEDULER_cancel (endbadly_task);
285 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
292 returned_records = 0;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
294 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
306 if (NULL != endbadly_task)
307 GNUNET_SCHEDULER_cancel (endbadly_task);
308 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
315 static struct GNUNET_GNSRECORD_Data *
316 create_record (unsigned int count)
318 struct GNUNET_GNSRECORD_Data * rd;
320 rd = GNUNET_new_array (count,
321 struct GNUNET_GNSRECORD_Data);
322 for (unsigned int c = 0; c < count; c++)
324 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
325 rd[c].record_type = 1111;
326 rd[c].data_size = 50;
327 rd[c].data = GNUNET_malloc(50);
329 memset ((char *) rd[c].data, 'a', 50);
336 nick_2_cont (void *cls,
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
346 GNUNET_asprintf(&s_name_1, "dummy1");
347 s_rd_1 = create_record(1);
348 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Created record 2 \n");
354 GNUNET_asprintf(&s_name_2, "dummy2");
355 s_rd_2 = create_record(1);
356 GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
357 1, s_rd_2, &put_cont, NULL);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Created record 3\n");
362 /* name in different zone */
363 GNUNET_asprintf(&s_name_3, "dummy3");
364 s_rd_3 = create_record(1);
365 GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
372 nick_1_cont (void *cls, int32_t success, const char *emsg)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Nick 1 added : %s\n",
376 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
378 nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Namestore cannot store no block\n"));
388 * Callback called from the zone iterator when we iterate over
389 * the empty zone. Check that we got no records and then
390 * start the actual tests by filling the zone.
393 empty_zone_proc (void *cls,
394 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
396 unsigned int rd_count,
397 const struct GNUNET_GNSRECORD_Data *rd)
399 GNUNET_assert (nsh == cls);
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 _("Expected empty zone but received zone private key\n"));
406 if (endbadly_task != NULL)
407 GNUNET_SCHEDULER_cancel (endbadly_task);
408 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
411 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 _("Expected no zone content but received data\n"));
416 if (endbadly_task != NULL)
417 GNUNET_SCHEDULER_cancel (endbadly_task);
418 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
426 empty_zone_end (void *cls)
428 GNUNET_assert (nsh == cls);
430 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
431 GNUNET_assert (privkey != NULL);
432 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
433 GNUNET_assert (privkey2 != NULL);
435 nsqe = GNUNET_NAMESTORE_set_nick (nsh,
442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443 _("Namestore cannot store no block\n"));
451 const struct GNUNET_CONFIGURATION_Handle *cfg,
452 struct GNUNET_TESTING_Peer *peer)
454 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
457 nsh = GNUNET_NAMESTORE_connect (cfg);
458 GNUNET_break (NULL != nsh);
460 /* first, iterate over empty namestore */
461 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 "Failed to create zone iterator\n");
474 GNUNET_SCHEDULER_cancel (endbadly_task);
475 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
481 main (int argc, char *argv[])
483 const char *plugin_name;
486 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
487 GNUNET_asprintf (&cfg_name,
488 "test_namestore_api_%s.conf",
491 GNUNET_DISK_purge_cfg_dir (cfg_name,
494 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
501 GNUNET_DISK_purge_cfg_dir (cfg_name,
503 GNUNET_free (cfg_name);
508 /* end of test_namestore_api_zone_iteration.c */