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"
28 #define VERBOSE GNUNET_NO
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32 static struct GNUNET_NAMESTORE_Handle * nsh;
34 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
35 static struct GNUNET_OS_Process *arm;
37 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
38 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
39 static GNUNET_HashCode zone;
43 #define TEST_RECORD_TYPE 1234
44 #define TEST_RECORD_DATALEN 123
45 #define TEST_RECORD_DATA 'a'
49 start_arm (const char *cfgname)
51 arm = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
52 "gnunet-service-arm", "-c", cfgname,
66 if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
67 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
68 GNUNET_OS_process_wait (arm);
69 GNUNET_OS_process_close (arm);
75 * Re-establish the connection to the service.
77 * @param cls handle to use to re-connect.
78 * @param tc scheduler context
81 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
88 GNUNET_CRYPTO_rsa_key_free (privkey);
99 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (endbadly_task);
104 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
108 GNUNET_CRYPTO_rsa_key_free (privkey);
112 GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
123 void name_lookup_proc (void *cls,
124 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
125 struct GNUNET_TIME_Absolute expire,
127 unsigned int rd_count,
128 const struct GNUNET_NAMESTORE_RecordData *rd,
129 const struct GNUNET_CRYPTO_RsaSignature *signature)
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore lookup result %p `%s' %i %p %p\n", zone_key, name, rd_count, rd, signature);
133 GNUNET_SCHEDULER_add_now(&end, NULL);
136 void put_cont (void *cls, int32_t success, const char *emsg)
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Name store added record for `%s': %s\n", name, (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
142 GNUNET_NAMESTORE_lookup_record (nsh, &zone, name, 0, &name_lookup_proc, NULL);
146 delete_existing_db (const struct GNUNET_CONFIGURATION_Handle *cfg)
151 GNUNET_CONFIGURATION_get_value_filename (cfg, "namestore-sqlite",
152 "FILENAME", &afsdir))
154 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
155 if (GNUNET_OK == GNUNET_DISK_file_test (afsdir))
156 if (GNUNET_OK == GNUNET_DISK_directory_remove(afsdir))
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleted existing database `%s' \n", afsdir);
158 GNUNET_free (afsdir);
164 run (void *cls, char *const *args, const char *cfgfile,
165 const struct GNUNET_CONFIGURATION_Handle *cfg)
167 delete_existing_db(cfg);
168 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
171 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, "4UCICULTINKC87UO4326KEEDQ9MTEP2AJT88MJFVGTGNK12QNGMQI2S41VI07UUU6EO19BTB06PDL0HE6VP1OM50HOJEI75RHP4JP80.zkey");
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
173 privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
174 GNUNET_free (hostkey_file);
175 GNUNET_assert (privkey != NULL);
176 GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
178 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &zone);
181 struct GNUNET_CRYPTO_RsaSignature signature;
182 memset (&signature, '\0', sizeof (signature));
183 struct GNUNET_NAMESTORE_RecordData rd;
185 rd.expiration = GNUNET_TIME_absolute_get();
186 rd.record_type = TEST_RECORD_TYPE;
187 rd.data_size = TEST_RECORD_DATALEN;
188 rd.data = GNUNET_malloc(TEST_RECORD_DATALEN);
189 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
190 char * name = "dummy.dummy.gnunet";
193 GNUNET_assert (arm != NULL);
195 nsh = GNUNET_NAMESTORE_connect (cfg);
196 GNUNET_break (NULL != nsh);
198 GNUNET_NAMESTORE_record_put (nsh, &pubkey, name,
199 GNUNET_TIME_absolute_get_forever(),
200 1, &rd, &signature, put_cont, name);
202 GNUNET_free ((void *)rd.data);
209 static char *const argv[] = { "test-namestore-api",
211 "test_namestore_api.conf",
217 static struct GNUNET_GETOPT_CommandLineOption options[] = {
218 GNUNET_GETOPT_OPTION_END
222 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
223 "nohelp", options, &run, &res);
228 main (int argc, char *argv[])
237 /* end of test_namestore_api.c */