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 "namestore.h"
28 #include "gnunet_signatures.h"
30 #define VERBOSE GNUNET_NO
33 #define TEST_RECORD_TYPE 1234
34 #define TEST_RECORD_DATALEN 123
35 #define TEST_RECORD_DATA 'a'
37 #define TEST_CREATE_RECORD_TYPE 4321
38 #define TEST_CREATE_RECORD_DATALEN 255
39 #define TEST_CREATE_RECORD_DATA 'b'
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43 static struct GNUNET_NAMESTORE_Handle * nsh;
45 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
46 static struct GNUNET_OS_Process *arm;
48 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
49 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
50 struct GNUNET_CRYPTO_RsaSignature *s_signature;
51 struct GNUNET_CRYPTO_RsaSignature *s_signature_updated;
52 static GNUNET_HashCode s_zone;
53 struct GNUNET_NAMESTORE_RecordData *s_first_record;
54 struct GNUNET_NAMESTORE_RecordData *s_second_record;
62 start_arm (const char *cfgname)
64 arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
65 "gnunet-service-arm", "-c", cfgname,
79 if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
80 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
81 GNUNET_OS_process_wait (arm);
82 GNUNET_OS_process_close (arm);
88 * Re-establish the connection to the service.
90 * @param cls handle to use to re-connect.
91 * @param tc scheduler context
94 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
101 GNUNET_CRYPTO_rsa_key_free (privkey);
112 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
116 GNUNET_SCHEDULER_cancel (endbadly_task);
117 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
120 GNUNET_free ((void *) s_first_record->data);
121 GNUNET_free (s_first_record);
122 GNUNET_free_non_null (s_second_record);
125 GNUNET_CRYPTO_rsa_key_free (privkey);
129 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
136 void name_lookup_second_proc (void *cls,
137 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
138 struct GNUNET_TIME_Absolute expire,
140 unsigned int rd_count,
141 const struct GNUNET_NAMESTORE_RecordData *rd,
142 const struct GNUNET_CRYPTO_RsaSignature *signature)
144 static int found = GNUNET_NO;
145 int failed = GNUNET_NO;
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
151 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
157 if (0 != strcmp(n, s_name))
169 for (c = 0; c < rd_count; c++)
171 if ((GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_first_record)) &&
172 (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], s_second_record)))
179 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, n, rd_count, rd, signature))
185 struct GNUNET_NAMESTORE_RecordData rd_new[2];
186 rd_new[0] = *s_first_record;
187 rd_new[1] = *s_second_record;
188 s_signature_updated = GNUNET_NAMESTORE_create_signature(privkey, s_name, rd_new, 2);
190 if (0 != memcmp (s_signature_updated, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
197 if (failed == GNUNET_NO)
204 if (found != GNUNET_YES)
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
211 GNUNET_SCHEDULER_add_now(&end, NULL);
216 create_second_cont (void *cls, int32_t success, const char *emsg)
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create second record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
220 if (success == GNUNET_OK)
223 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_second_proc, name);
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
229 GNUNET_SCHEDULER_add_now(&end, NULL);
234 void name_lookup_initial_proc (void *cls,
235 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
236 struct GNUNET_TIME_Absolute expire,
238 unsigned int rd_count,
239 const struct GNUNET_NAMESTORE_RecordData *rd,
240 const struct GNUNET_CRYPTO_RsaSignature *signature)
243 static int found = GNUNET_NO;
244 int failed = GNUNET_NO;
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
250 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
256 if (0 != strcmp(n, s_name))
262 if (RECORDS != rd_count)
268 for (c = 0; c < RECORDS; c++)
270 if (GNUNET_NO == GNUNET_NAMESTORE_records_cmp(&rd[c], &s_first_record[c]))
277 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, n, rd_count, rd, signature))
283 if (0 != memcmp (s_signature, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
290 if (failed == GNUNET_NO)
295 /* create a second record */
296 s_second_record = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
297 s_second_record->expiration = GNUNET_TIME_absolute_get_forever();
298 s_second_record->record_type = TEST_CREATE_RECORD_TYPE;
299 s_second_record->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
300 s_second_record->data = &s_second_record[1];
301 s_second_record->data_size = TEST_CREATE_RECORD_DATALEN;
302 memset ((char *) s_second_record->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
304 GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_second_record, &create_second_cont, name);
309 if (found != GNUNET_YES)
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
315 GNUNET_SCHEDULER_add_now (&end, NULL);
320 create_first_cont (void *cls, int32_t success, const char *emsg)
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
324 if (success == GNUNET_OK)
327 /* check if record was created correct */
328 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_initial_proc, name);
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
334 GNUNET_SCHEDULER_add_now(&end, NULL);
340 put_cont (void *cls, int32_t success, const char *emsg)
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
344 if (success == GNUNET_OK)
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
352 GNUNET_SCHEDULER_add_now(&end, NULL);
356 static struct GNUNET_NAMESTORE_RecordData *
357 create_record (int count)
360 struct GNUNET_NAMESTORE_RecordData * rd;
361 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
363 for (c = 0; c < count; c++)
365 rd[c].expiration = GNUNET_TIME_absolute_get();
366 rd[c].record_type = TEST_RECORD_TYPE;
367 rd[c].data_size = TEST_RECORD_DATALEN;
368 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
369 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
376 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
381 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
382 "FILENAME", &afsdir))
384 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
385 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
386 if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
388 GNUNET_free (afsdir);
394 run (void *cls, char *const *args, const char *cfgfile,
395 const struct GNUNET_CONFIGURATION_Handle *cfg)
397 delete_existing_db(cfg);
399 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
402 /* load privat key */
403 privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
404 GNUNET_assert (privkey != NULL);
406 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
409 s_name = "dummy.dummy.gnunet";
410 s_first_record = create_record (1);
412 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, s_first_record);
413 char rd_ser[rd_ser_len];
414 GNUNET_NAMESTORE_records_serialize(1, s_first_record, rd_ser_len, rd_ser);
416 s_signature = GNUNET_NAMESTORE_create_signature(privkey, s_name, s_first_record, 1);
418 /* create random zone hash */
419 GNUNET_CRYPTO_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_h2s_full(&s_zone));
424 GNUNET_assert (arm != NULL);
426 nsh = GNUNET_NAMESTORE_connect (cfg);
427 GNUNET_break (NULL != nsh);
429 GNUNET_break (s_first_record != NULL);
430 GNUNET_break (s_name != NULL);
432 /* create initial record */
433 GNUNET_NAMESTORE_record_create (nsh, privkey, s_name, s_first_record, &create_first_cont, s_name);
439 static char *const argv[] = { "test-namestore-api",
441 "test_namestore_api.conf",
447 static struct GNUNET_GETOPT_CommandLineOption options[] = {
448 GNUNET_GETOPT_OPTION_END
452 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
453 "nohelp", options, &run, &res);
458 main (int argc, char *argv[])
463 GNUNET_free (s_signature);
467 /* end of test_namestore_api.c */