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_to_name.c
22 * @brief testcase for zone to name translation
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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 static struct GNUNET_NAMESTORE_Handle * nsh;
41 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
42 static struct GNUNET_OS_Process *arm;
44 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
45 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
47 struct GNUNET_TIME_Absolute expire;
49 static struct GNUNET_CRYPTO_ShortHashCode s_zone;
50 static struct GNUNET_CRYPTO_ShortHashCode s_zone_value;
54 struct GNUNET_NAMESTORE_RecordData *s_rd;
55 struct GNUNET_CRYPTO_RsaSignature *s_signature;
60 start_arm (const char *cfgname)
62 arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
63 "gnunet-service-arm", "-c", cfgname,
77 if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
78 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
79 GNUNET_OS_process_wait (arm);
80 GNUNET_OS_process_close (arm);
86 * Re-establish the connection to the service.
88 * @param cls handle to use to re-connect.
89 * @param tc scheduler context
92 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
99 GNUNET_CRYPTO_rsa_key_free (privkey);
110 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
114 GNUNET_SCHEDULER_cancel (endbadly_task);
115 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
119 GNUNET_CRYPTO_rsa_key_free (privkey);
123 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
130 void zone_to_name_proc (void *cls,
131 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
132 struct GNUNET_TIME_Absolute expire,
134 unsigned int rd_count,
135 const struct GNUNET_NAMESTORE_RecordData *rd,
136 const struct GNUNET_CRYPTO_RsaSignature *signature)
138 int fail = GNUNET_NO;
140 if ((zone_key == NULL) && (n == NULL) && (rd_count == 0) && (rd == NULL) && (signature == NULL))
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No result found\n");
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result found: `%s'\n", n);
148 if ((n == NULL) || (0 != strcmp(n, s_name)))
158 if ((zone_key == NULL) || (0 != memcmp (zone_key, &pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
163 if (fail == GNUNET_NO)
168 GNUNET_SCHEDULER_add_now(&end, NULL);
173 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
178 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
179 "FILENAME", &afsdir))
181 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
182 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
183 if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
185 GNUNET_free (afsdir);
192 put_cont (void *cls, int32_t success, const char *emsg)
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
196 if (success == GNUNET_OK)
200 /* create initial record */
201 GNUNET_NAMESTORE_zone_to_name (nsh, &s_zone, &s_zone_value, zone_to_name_proc, NULL);
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to put records for name `%s'\n", name);
208 GNUNET_SCHEDULER_add_now(&end, NULL);
213 run (void *cls, char *const *args, const char *cfgfile,
214 const struct GNUNET_CONFIGURATION_Handle *cfg)
216 delete_existing_db(cfg);
218 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
219 GNUNET_asprintf(&s_name, "dummy");
222 /* load privat key */
224 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
225 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
227 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
228 GNUNET_free (hostkey_file);
229 GNUNET_assert (privkey != NULL);
231 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
234 GNUNET_CRYPTO_short_hash (&pubkey, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &s_zone);
235 GNUNET_CRYPTO_short_hash (s_name, strlen (s_name) + 1, &s_zone_value);
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using PKEY `%s' \n", GNUNET_short_h2s (&s_zone_value));
238 struct GNUNET_NAMESTORE_RecordData rd;
239 rd.expiration = GNUNET_TIME_absolute_get();
240 rd.record_type = GNUNET_NAMESTORE_TYPE_PKEY;
241 rd.data_size = sizeof (struct GNUNET_CRYPTO_ShortHashCode);
242 rd.data = GNUNET_malloc(sizeof (struct GNUNET_CRYPTO_ShortHashCode));
243 memcpy ((char *) rd.data, &s_zone_value, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
246 GNUNET_assert (arm != NULL);
248 nsh = GNUNET_NAMESTORE_connect (cfg);
249 GNUNET_break (NULL != nsh);
251 expire = GNUNET_TIME_absolute_get ();
252 s_signature = GNUNET_NAMESTORE_create_signature(privkey, rd.expiration, s_name, &rd, 1);
253 GNUNET_NAMESTORE_record_put(nsh, &pubkey, s_name, expire, 1, &rd, s_signature, put_cont, NULL);
255 GNUNET_free ((void *) rd.data);
261 static char *const argv[] = { "test-namestore-api",
263 "test_namestore_api.conf",
269 static struct GNUNET_GETOPT_CommandLineOption options[] = {
270 GNUNET_GETOPT_OPTION_END
274 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
275 "nohelp", options, &run, &res);
280 main (int argc, char *argv[])
288 /* end of test_namestore_api.c */