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_rd;
54 struct GNUNET_NAMESTORE_RecordData *s_create_rd;
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;
121 for (c = 0; c < RECORDS; c++)
122 GNUNET_free_non_null((void *) s_rd[c].data);
124 GNUNET_free (s_create_rd);
127 GNUNET_CRYPTO_rsa_key_free (privkey);
131 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
138 void name_lookup_proc (void *cls,
139 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
140 struct GNUNET_TIME_Absolute expire,
142 unsigned int rd_count,
143 const struct GNUNET_NAMESTORE_RecordData *rd,
144 const struct GNUNET_CRYPTO_RsaSignature *signature)
146 static int found = GNUNET_NO;
147 int failed = GNUNET_NO;
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking returned results\n");
153 if (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
159 if (0 != strcmp(n, s_name))
165 if (RECORDS+1 != rd_count)
171 for (c = 0; c < RECORDS; c++)
173 GNUNET_break (rd[c].expiration.abs_value == s_rd[c].expiration.abs_value);
174 GNUNET_break (rd[c].record_type == TEST_RECORD_TYPE);
175 GNUNET_break (rd[c].data_size == TEST_RECORD_DATALEN);
176 GNUNET_break (0 == memcmp (rd[c].data, s_rd[c].data, TEST_RECORD_DATALEN));
179 if (rd[c].record_type != TEST_CREATE_RECORD_TYPE)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "record_type %u %u\n", rd[c].record_type , TEST_CREATE_RECORD_TYPE);
185 if (rd[c].data_size != TEST_CREATE_RECORD_DATALEN)
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "data_size %u %u\n", rd[c].data_size , TEST_CREATE_RECORD_DATALEN);
193 char *dummy[TEST_CREATE_RECORD_DATALEN];
194 memset (&dummy, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
195 if (0 != memcmp (&dummy, s_create_rd->data, TEST_CREATE_RECORD_DATALEN))
202 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature(&pubkey, n, rd_count, rd, signature))
208 struct GNUNET_NAMESTORE_RecordData rd_new[RECORDS +1];
210 for (c2 = 0; c2 < RECORDS; c2++)
211 rd_new[c2] = s_rd[c2];
212 rd_new[c2] = *s_create_rd;
213 s_signature_updated = GNUNET_NAMESTORE_create_signature(privkey, n, rd_new, RECORDS +1);
215 if (0 != memcmp (s_signature_updated, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature)))
220 GNUNET_free (s_signature_updated);
223 if (failed == GNUNET_NO)
230 if (found != GNUNET_YES)
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to lookup records for name `%s'\n", s_name);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Lookup done for name %s'\n", s_name);
237 GNUNET_SCHEDULER_add_now(&end, NULL);
241 create_cont (void *cls, int32_t success, const char *emsg)
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
245 if (success == GNUNET_OK)
249 GNUNET_NAMESTORE_lookup_record (nsh, &s_zone, name, 0, &name_lookup_proc, name);
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
255 GNUNET_SCHEDULER_add_now(&end, NULL);
261 put_cont (void *cls, int32_t success, const char *emsg)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
265 if (success == GNUNET_OK)
268 s_create_rd = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_RecordData) + TEST_CREATE_RECORD_DATALEN);
269 s_create_rd->expiration = GNUNET_TIME_absolute_get_forever();
270 s_create_rd->record_type = TEST_CREATE_RECORD_TYPE;
271 s_create_rd->flags = GNUNET_NAMESTORE_RF_AUTHORITY;
272 s_create_rd->data = &s_create_rd[1];
273 s_create_rd->data_size = TEST_CREATE_RECORD_DATALEN;
274 memset ((char *) s_create_rd->data, TEST_CREATE_RECORD_DATA, TEST_CREATE_RECORD_DATALEN);
276 GNUNET_NAMESTORE_record_create (nsh, privkey, name, s_create_rd, &create_cont, name);
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
282 GNUNET_SCHEDULER_add_now(&end, NULL);
286 static struct GNUNET_NAMESTORE_RecordData *
287 create_record (int count)
290 struct GNUNET_NAMESTORE_RecordData * rd;
291 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
293 for (c = 0; c < RECORDS; c++)
295 rd[c].expiration = GNUNET_TIME_absolute_get();
296 rd[c].record_type = TEST_RECORD_TYPE;
297 rd[c].data_size = TEST_RECORD_DATALEN;
298 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
299 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
306 run (void *cls, char *const *args, const char *cfgfile,
307 const struct GNUNET_CONFIGURATION_Handle *cfg)
309 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
312 /* load privat key */
313 privkey = GNUNET_CRYPTO_rsa_key_create_from_file("hostkey");
314 GNUNET_assert (privkey != NULL);
316 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
319 s_name = "dummy.dummy.gnunet";
320 s_rd = create_record (RECORDS);
322 rd_ser_len = GNUNET_NAMESTORE_records_get_size(RECORDS, s_rd);
323 char rd_ser[rd_ser_len];
324 GNUNET_NAMESTORE_records_serialize(RECORDS, s_rd, rd_ser_len, rd_ser);
326 s_signature = GNUNET_NAMESTORE_create_signature(privkey, s_name, s_rd, RECORDS);
328 /* create random zone hash */
329 GNUNET_CRYPTO_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name: `%s' Zone: `%s' \n", s_name, GNUNET_h2s_full(&s_zone));
335 GNUNET_assert (arm != NULL);
337 nsh = GNUNET_NAMESTORE_connect (cfg);
338 GNUNET_break (NULL != nsh);
342 GNUNET_break (s_rd != NULL);
343 GNUNET_break (s_name != NULL);
345 GNUNET_NAMESTORE_record_put (nsh, &pubkey, s_name,
346 GNUNET_TIME_absolute_get_forever(),
347 RECORDS, s_rd, s_signature, put_cont, s_name);
356 static char *const argv[] = { "test-namestore-api",
358 "test_namestore_api.conf",
364 static struct GNUNET_GETOPT_CommandLineOption options[] = {
365 GNUNET_GETOPT_OPTION_END
369 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
370 "nohelp", options, &run, &res);
375 main (int argc, char *argv[])
380 GNUNET_free (s_signature);
384 /* end of test_namestore_api.c */