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 it
6 under the terms of the GNU 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: stop iterating of 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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
28 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
30 static struct GNUNET_NAMESTORE_Handle * nsh;
32 static struct GNUNET_SCHEDULER_Task * endbadly_task;
34 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
36 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
38 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
42 static int returned_records;
44 static char * s_name_1;
46 static struct GNUNET_GNSRECORD_Data *s_rd_1;
48 static char * s_name_2;
50 static struct GNUNET_GNSRECORD_Data *s_rd_2;
52 static char * s_name_3;
54 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59 * Re-establish the connection to the service.
61 * @param cls handle to use to re-connect.
68 GNUNET_NAMESTORE_zone_iteration_stop (zi);
73 GNUNET_NAMESTORE_disconnect (nsh);
76 GNUNET_free_non_null(s_name_1);
77 GNUNET_free_non_null(s_name_2);
78 GNUNET_free_non_null(s_name_3);
82 GNUNET_free ((void *)s_rd_1->data);
87 GNUNET_free ((void *)s_rd_2->data);
92 GNUNET_free ((void *)s_rd_3->data);
97 GNUNET_free (privkey);
100 if (privkey2 != NULL)
101 GNUNET_free (privkey2);
112 GNUNET_NAMESTORE_zone_iteration_stop (zi);
116 if (endbadly_task != NULL)
118 GNUNET_SCHEDULER_cancel (endbadly_task);
119 endbadly_task = NULL;
123 GNUNET_free (privkey);
126 if (privkey2 != NULL)
127 GNUNET_free (privkey2);
130 GNUNET_free (s_name_1);
131 GNUNET_free (s_name_2);
132 GNUNET_free (s_name_3);
135 GNUNET_free ((void *)s_rd_1->data);
136 GNUNET_free (s_rd_1);
140 GNUNET_free ((void *)s_rd_2->data);
141 GNUNET_free (s_rd_2);
145 GNUNET_free ((void *)s_rd_3->data);
146 GNUNET_free (s_rd_3);
149 GNUNET_NAMESTORE_disconnect (nsh);
162 zone_proc (void *cls,
163 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
165 unsigned int rd_count,
166 const struct GNUNET_GNSRECORD_Data *rd)
168 int failed = GNUNET_NO;
170 GNUNET_assert (NULL != zone);
171 if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
173 if (0 == strcmp (label, s_name_1))
177 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
189 else if (0 == strcmp (label, s_name_2))
193 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Received invalid record count\n");
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Comparing result failed: got name `%s' for first zone\n", label);
215 else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
217 if (0 == strcmp (label, s_name_3))
221 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Received invalid record count\n");
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Comparing result failed: got name `%s' for first zone\n", label);
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Received invalid zone\n");
250 if (failed == GNUNET_NO)
252 if (1 == returned_records)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Telling namestore to stop zone iteration\n");
256 GNUNET_NAMESTORE_zone_iteration_stop (zi);
258 if (NULL != endbadly_task)
260 GNUNET_SCHEDULER_cancel (endbadly_task);
261 endbadly_task = NULL;
264 GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Telling namestore to send the next result\n");
270 GNUNET_NAMESTORE_zone_iterator_next (zi,
276 GNUNET_SCHEDULER_add_now (&end, NULL);
282 zone_proc_end (void *cls)
284 GNUNET_break (1 <= returned_records);
285 if (1 >= returned_records)
286 res = 1; /* Last iteraterator callback, we are done */
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Received last result, iteration done after receing %u results\n",
293 GNUNET_SCHEDULER_add_now (&end, NULL);
298 put_cont (void *cls, int32_t success, const char *emsg)
302 if (success == GNUNET_OK)
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
312 if (NULL != endbadly_task)
313 GNUNET_SCHEDULER_cancel (endbadly_task);
314 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
321 returned_records = 0;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "All records created, starting iteration over all zones \n");
324 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 "Failed to create zone iterator\n");
337 if (NULL != endbadly_task)
338 GNUNET_SCHEDULER_cancel (endbadly_task);
339 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
346 static struct GNUNET_GNSRECORD_Data *
347 create_record (unsigned int count)
349 struct GNUNET_GNSRECORD_Data *rd;
351 rd = GNUNET_new_array (count,
352 struct GNUNET_GNSRECORD_Data);
353 for (unsigned int c = 0; c < count; c++)
355 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
356 rd[c].record_type = TEST_RECORD_TYPE;
357 rd[c].data_size = 50;
358 rd[c].data = GNUNET_malloc(50);
360 memset ((char *) rd[c].data, 'a', 50);
367 * Callback called from the zone iterator when we iterate over
368 * the empty zone. Check that we got no records and then
369 * start the actual tests by filling the zone.
372 empty_zone_proc (void *cls,
373 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
375 unsigned int rd_count,
376 const struct GNUNET_GNSRECORD_Data *rd)
379 GNUNET_assert (nsh == cls);
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383 _("Expected empty zone but received zone private key\n"));
385 if (endbadly_task != NULL)
386 GNUNET_SCHEDULER_cancel (endbadly_task);
387 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
390 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 _("Expected no zone content but received data\n"));
395 if (endbadly_task != NULL)
396 GNUNET_SCHEDULER_cancel (endbadly_task);
397 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
405 empty_zone_proc_end (void *cls)
409 GNUNET_assert (nsh == cls);
411 GNUNET_asprintf(&hostkey_file,
414 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Using zonekey file `%s' \n",
418 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
419 GNUNET_free (hostkey_file);
420 GNUNET_assert (privkey != NULL);
422 GNUNET_asprintf (&hostkey_file,
425 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Using zonekey file `%s'\n",
429 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
430 GNUNET_free (hostkey_file);
431 GNUNET_assert (privkey2 != NULL);
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Created record 1\n");
436 GNUNET_asprintf(&s_name_1,
438 s_rd_1 = create_record(1);
439 GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
440 1, s_rd_1, &put_cont, NULL);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Created record 2 \n");
444 GNUNET_asprintf(&s_name_2,
446 s_rd_2 = create_record(1);
447 GNUNET_NAMESTORE_records_store (nsh,
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Created record 3\n");
457 /* name in different zone */
458 GNUNET_asprintf(&s_name_3, "dummy3");
459 s_rd_3 = create_record(1);
460 GNUNET_NAMESTORE_records_store(nsh,
471 const struct GNUNET_CONFIGURATION_Handle *cfg,
472 struct GNUNET_TESTING_Peer *peer)
474 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
477 nsh = GNUNET_NAMESTORE_connect (cfg);
478 GNUNET_break (NULL != nsh);
479 /* first, iterate over empty namestore */
480 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
486 &empty_zone_proc_end,
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "Failed to create zone iterator\n");
493 GNUNET_SCHEDULER_cancel (endbadly_task);
494 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
500 main (int argc, char *argv[])
502 const char *plugin_name;
505 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
506 GNUNET_asprintf (&cfg_name,
507 "test_namestore_api_%s.conf",
509 GNUNET_DISK_purge_cfg_dir (cfg_name,
513 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
520 GNUNET_DISK_purge_cfg_dir (cfg_name,
522 GNUNET_free (cfg_name);
527 /* end of test_namestore_api_zone_iteration_stop.c */