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.c
22 * @brief testcase for namestore_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29 #include "gnunet_signatures.h"
34 #define TEST_RECORD_TYPE 1234
36 #define TEST_RECORD_DATALEN 123
38 #define TEST_RECORD_DATA 'a'
40 #define TEST_CREATE_RECORD_TYPE 4321
42 #define TEST_CREATE_RECORD_DATALEN 255
44 #define TEST_CREATE_RECORD_DATA 'b'
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
49 static struct GNUNET_NAMESTORE_Handle * nsh;
51 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
53 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
55 static struct GNUNET_CRYPTO_EccPublicKey pubkey;
57 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
59 static struct GNUNET_NAMESTORE_RecordData *s_first_record;
61 static struct GNUNET_NAMESTORE_RecordData *s_second_record;
69 * Re-establish the connection to the service.
71 * @param cls handle to use to re-connect.
72 * @param tc scheduler context
75 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 GNUNET_NAMESTORE_disconnect (nsh);
81 GNUNET_free (privkey);
83 GNUNET_free_non_null (s_name);
89 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
93 GNUNET_SCHEDULER_cancel (endbadly_task);
94 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
96 GNUNET_free ((void *) s_first_record->data);
97 GNUNET_free (s_first_record);
98 GNUNET_free_non_null (s_second_record);
99 GNUNET_free_non_null (s_name);
101 GNUNET_free (privkey);
104 GNUNET_NAMESTORE_disconnect (nsh);
110 name_lookup_second_proc (void *cls,
111 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
112 struct GNUNET_TIME_Absolute expire,
114 unsigned int rd_count,
115 const struct GNUNET_NAMESTORE_RecordData *rd,
116 const struct GNUNET_CRYPTO_EccSignature *signature)
118 static int found = GNUNET_NO;
119 int failed = GNUNET_NO;
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
125 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
131 if (0 != strcmp(n, s_name))
143 for (c = 0; c < rd_count; c++)
145 if ((GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_first_record)) &&
146 (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_second_record)))
153 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (zone_key, expire, n, rd_count, rd, signature))
159 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&pubkey, expire, n, rd_count, rd, signature))
166 if (failed == GNUNET_NO)
173 if (found != GNUNET_YES)
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
180 GNUNET_SCHEDULER_add_now(&end, NULL);
185 create_second_cont (void *cls, int32_t success, const char *emsg)
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create second record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
190 if (success == GNUNET_OK)
193 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_second_proc, name);
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
199 GNUNET_SCHEDULER_add_now(&end, NULL);
205 name_lookup_initial_proc (void *cls,
206 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
207 struct GNUNET_TIME_Absolute expire,
209 unsigned int rd_count,
210 const struct GNUNET_NAMESTORE_RecordData *rd,
211 const struct GNUNET_CRYPTO_EccSignature *signature)
213 struct GNUNET_NAMESTORE_RecordData both_records[2];
215 static int found = GNUNET_NO;
216 int failed = GNUNET_NO;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
222 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_EccPublicKey)))
228 if (0 != strcmp(n, s_name))
234 if (RECORDS != rd_count)
240 for (c = 0; c < RECORDS; c++)
242 if (GNUNET_NO == GNUNET_NAMESTORE_records_cmp (&rd[c], &s_first_record[c]))
249 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&pubkey, expire, n, rd_count, rd, signature))
256 if (failed == GNUNET_NO)
261 /* create a second record */
262 s_second_record = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
263 s_second_record->expiration_time = UINT64_MAX;
264 s_second_record->record_type = TEST_CREATE_RECORD_TYPE;
265 s_second_record->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
266 s_second_record->data = &s_second_record[1];
267 s_second_record->data_size = TEST_CREATE_RECORD_DATALEN;
268 memset ((char *) s_second_record->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
270 both_records[0] = *s_first_record;
271 both_records[1] = *s_second_record;
272 GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, name,
274 &create_second_cont, name);
279 if (found != GNUNET_YES)
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
285 GNUNET_SCHEDULER_add_now (&end, NULL);
291 create_first_cont (void *cls, int32_t success, const char *emsg)
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
295 if (success == GNUNET_OK)
298 /* check if record was created correct */
299 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_initial_proc, name);
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
305 GNUNET_SCHEDULER_add_now(&end, NULL);
310 static struct GNUNET_NAMESTORE_RecordData *
311 create_record (unsigned int count)
314 struct GNUNET_NAMESTORE_RecordData * rd;
316 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
317 for (c = 0; c < count; c++)
319 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
320 rd[c].record_type = TEST_RECORD_TYPE;
321 rd[c].data_size = TEST_RECORD_DATALEN;
322 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
323 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
331 const struct GNUNET_CONFIGURATION_Handle *cfg,
332 struct GNUNET_TESTING_Peer *peer)
337 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,endbadly, NULL);
338 /* load privat key */
339 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
340 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
342 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
343 GNUNET_free (hostkey_file);
345 GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
348 s_name = GNUNET_NAMESTORE_normalize_string ("DUMMY.dummy.gnunet");
349 s_first_record = create_record (1);
351 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
352 char rd_ser[rd_ser_len];
353 GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
355 /* create random zone hash */
356 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_EccPublicKey), &s_zone);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_NAMESTORE_short_h2s (&s_zone));
359 nsh = GNUNET_NAMESTORE_connect (cfg);
360 GNUNET_break (NULL != nsh);
362 GNUNET_break (s_first_record != NULL);
363 GNUNET_break (s_name != NULL);
365 /* create initial record */
366 GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name,
368 &create_first_cont, s_name);
373 main (int argc, char *argv[])
377 GNUNET_TESTING_service_run ("test-namestore-api-create",
379 "test_namestore_api.conf",
386 /* end of test_namestore_api_create.c */