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_specific_zone.c
22 * @brief testcase for zone iteration functionality: iterate of a specific zone
25 #include "gnunet_common.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_namestore_service.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 GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
40 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
42 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
44 static struct GNUNET_CRYPTO_ShortHashCode zone;
46 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey2;
48 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey2;
50 static struct GNUNET_CRYPTO_ShortHashCode zone2;
52 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
56 static int returned_records;
58 static struct GNUNET_CRYPTO_RsaSignature *sig_1;
60 static char * s_name_1;
62 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
64 static struct GNUNET_CRYPTO_RsaSignature *sig_2;
66 static char * s_name_2;
68 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
70 static struct GNUNET_CRYPTO_RsaSignature *sig_3;
72 static char * s_name_3;
74 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
78 * Re-establish the connection to the service.
80 * @param cls handle to use to re-connect.
81 * @param tc scheduler context
84 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
88 GNUNET_SCHEDULER_cancel (stopiteration_task);
89 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
93 GNUNET_NAMESTORE_disconnect (nsh);
95 GNUNET_free_non_null(sig_1);
96 GNUNET_free_non_null(sig_2);
97 GNUNET_free_non_null(sig_3);
98 GNUNET_free_non_null(s_name_1);
99 GNUNET_free_non_null(s_name_2);
100 GNUNET_free_non_null(s_name_3);
103 GNUNET_free ((void *)s_rd_1->data);
104 GNUNET_free (s_rd_1);
108 GNUNET_free ((void *)s_rd_2->data);
109 GNUNET_free (s_rd_2);
113 GNUNET_free ((void *)s_rd_3->data);
114 GNUNET_free (s_rd_3);
118 GNUNET_CRYPTO_rsa_key_free (privkey);
121 if (privkey2 != NULL)
122 GNUNET_CRYPTO_rsa_key_free (privkey2);
129 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
133 GNUNET_SCHEDULER_cancel (stopiteration_task);
134 stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
136 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
138 GNUNET_SCHEDULER_cancel (endbadly_task);
139 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
142 GNUNET_CRYPTO_rsa_key_free (privkey);
144 if (privkey2 != NULL)
145 GNUNET_CRYPTO_rsa_key_free (privkey2);
151 GNUNET_free (s_name_1);
152 GNUNET_free (s_name_2);
153 GNUNET_free (s_name_3);
156 GNUNET_free ((void *)s_rd_1->data);
157 GNUNET_free (s_rd_1);
161 GNUNET_free ((void *)s_rd_2->data);
162 GNUNET_free (s_rd_2);
166 GNUNET_free ((void *)s_rd_3->data);
167 GNUNET_free (s_rd_3);
170 GNUNET_NAMESTORE_disconnect (nsh);
176 zone_proc (void *cls,
177 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
178 struct GNUNET_TIME_Absolute expire,
180 unsigned int rd_count,
181 const struct GNUNET_NAMESTORE_RecordData *rd,
182 const struct GNUNET_CRYPTO_RsaSignature *signature)
184 int failed = GNUNET_NO;
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for zone `%s'\n", GNUNET_short_h2s (&zone));
186 if ((zone_key == NULL) && (name == NULL))
188 GNUNET_break (2 == returned_records);
189 if (2 == returned_records)
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received last result, iteration done after %u records\n", returned_records);
195 GNUNET_SCHEDULER_add_now (&end, NULL);
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing results name %s \n", name);
200 if (0 == strcmp (name, s_name_1))
204 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
215 if (0 != memcmp (signature, sig_1, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
221 else if (0 == strcmp (name, s_name_2))
225 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
236 if (0 != memcmp (signature, sig_2, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing result failed: got name `%s'\n", name);
247 GNUNET_SCHEDULER_add_now (&end, NULL);
250 if (failed == GNUNET_NO)
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Telling namestore to send the next result\n");
254 GNUNET_NAMESTORE_zone_iterator_next (zi);
259 GNUNET_SCHEDULER_add_now (&end, NULL);
266 put_cont (void *cls, int32_t success, const char *emsg)
270 if (success == GNUNET_OK)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
279 GNUNET_SCHEDULER_cancel (endbadly_task);
280 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
286 returned_records = 0;
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over zone `%s'\n",
288 GNUNET_short_h2s(&zone));
289 zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
291 GNUNET_NAMESTORE_RF_NONE,
292 GNUNET_NAMESTORE_RF_NONE,
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
299 GNUNET_SCHEDULER_cancel (endbadly_task);
300 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
306 static struct GNUNET_NAMESTORE_RecordData *
307 create_record (unsigned int count)
310 struct GNUNET_NAMESTORE_RecordData * rd;
312 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
313 for (c = 0; c < count; c++)
315 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value;
316 rd[c].record_type = 1111;
317 rd[c].data_size = 50;
318 rd[c].data = GNUNET_malloc(50);
319 memset ((char *) rd[c].data, 'a', 50);
327 const struct GNUNET_CONFIGURATION_Handle *cfg,
328 struct GNUNET_TESTING_Peer *peer)
331 struct GNUNET_TIME_Absolute et;
333 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,&endbadly, NULL);
334 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
335 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
337 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
338 GNUNET_free (hostkey_file);
339 GNUNET_assert (privkey != NULL);
340 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
341 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (pubkey), &zone);
343 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
344 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
346 privkey2 = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
347 GNUNET_free (hostkey_file);
348 GNUNET_assert (privkey2 != NULL);
349 GNUNET_CRYPTO_rsa_key_get_public(privkey2, &pubkey2);
350 GNUNET_CRYPTO_short_hash (&pubkey2, sizeof (pubkey), &zone2);
351 nsh = GNUNET_NAMESTORE_connect (cfg);
352 GNUNET_break (NULL != nsh);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
355 GNUNET_asprintf(&s_name_1, "dummy1");
356 s_rd_1 = create_record(1);
357 et.abs_value = s_rd_1[0].expiration_time;
358 sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
359 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_1, s_rd_1, &put_cont, NULL);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
362 GNUNET_asprintf(&s_name_2, "dummy2");
363 s_rd_2 = create_record(1);
364 et.abs_value = s_rd_2[0].expiration_time;
365 sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
366 GNUNET_NAMESTORE_record_create(nsh, privkey, s_name_2, s_rd_2, &put_cont, NULL);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
369 /* name in different zone */
370 GNUNET_asprintf(&s_name_3, "dummy3");
371 s_rd_3 = create_record(1);
372 et.abs_value = s_rd_3[0].expiration_time;
373 sig_3 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_3, s_rd_3, 1);
374 GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, &put_cont, NULL);
379 main (int argc, char *argv[])
383 GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration-specific-zone",
385 "test_namestore_api.conf",
392 /* end of test_namestore_api_zone_iteration_specific_zone.c */