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 to: remove record
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
29 #define TEST_RECORD_TYPE 1234
31 #define TEST_RECORD_DATALEN 123
33 #define TEST_RECORD_DATA 'a'
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
38 static struct GNUNET_NAMESTORE_Handle *nsh;
40 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
42 static struct GNUNET_CRYPTO_EccPrivateKey *privkey;
44 static struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
46 static struct GNUNET_HashCode derived_hash;
52 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
60 GNUNET_NAMESTORE_disconnect (nsh);
65 GNUNET_free (privkey);
68 GNUNET_SCHEDULER_shutdown ();
73 * Re-establish the connection to the service.
75 * @param cls handle to use to re-connect.
76 * @param tc scheduler context
79 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 GNUNET_NAMESTORE_cancel (nsqe);
92 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 name_lookup_proc (void *cls, const struct GNUNET_NAMESTORE_Block *block);
103 remove_cont (void *cls,
107 const char *name = cls;
108 if (GNUNET_YES != success)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 _("Records could not be removed: `%s'\n"), emsg);
112 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
113 GNUNET_SCHEDULER_cancel (endbadly_task);
114 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
119 "Records were removed, perform lookup\n");
121 removed = GNUNET_YES;
122 nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
123 &name_lookup_proc, (void *) name);
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 _("Namestore cannot perform lookup for removed record\n"));
129 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
130 GNUNET_SCHEDULER_cancel (endbadly_task);
131 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
138 rd_decrypt_cb (void *cls,
139 unsigned int rd_count,
140 const struct GNUNET_NAMESTORE_RecordData *rd)
142 const char *name = cls;
143 char rd_cmp_data[TEST_RECORD_DATALEN];
145 GNUNET_assert (GNUNET_NO == removed);
146 GNUNET_assert (1 == rd_count);
147 GNUNET_assert (NULL != rd);
148 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
150 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
151 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
152 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155 "Block was decrypted successfully, removing records \n");
157 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
158 0, NULL, &remove_cont, (void *) name);
163 name_lookup_proc (void *cls,
164 const struct GNUNET_NAMESTORE_Block *block)
166 const char *name = cls;
169 if (removed && (NULL == block))
171 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_cancel (endbadly_task);
174 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
176 GNUNET_SCHEDULER_add_now (&end, NULL);
179 GNUNET_assert (NULL != cls);
180 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
182 GNUNET_SCHEDULER_cancel (endbadly_task);
183 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 _("Namestore returned no block\n"));
190 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
191 GNUNET_SCHEDULER_cancel (endbadly_task);
192 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Namestore returned block, decrypting \n");
197 GNUNET_assert (GNUNET_OK ==
198 GNUNET_NAMESTORE_block_decrypt (block,
199 &pubkey, name, &rd_decrypt_cb, (void *) name));
204 put_cont (void *cls, int32_t success,
207 const char *name = cls;
209 GNUNET_assert (NULL != cls);
210 if (GNUNET_SYSERR == success)
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 "Namestore could not store record: `%s'\n",
216 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
217 GNUNET_SCHEDULER_cancel (endbadly_task);
218 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Name store added record for `%s': %s\n",
225 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
227 /* Create derived hash */
228 GNUNET_NAMESTORE_query_from_private_key (privkey,
232 nsqe = GNUNET_NAMESTORE_lookup_block (nsh, &derived_hash,
233 &name_lookup_proc, (void *) name);
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 _("Namestore cannot perform lookup\n"));
239 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
240 GNUNET_SCHEDULER_cancel (endbadly_task);
241 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
249 const struct GNUNET_CONFIGURATION_Handle *cfg,
250 struct GNUNET_TESTING_Peer *peer)
252 struct GNUNET_NAMESTORE_RecordData rd;
254 const char * name = "dummy.dummy.gnunet";
256 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
258 GNUNET_asprintf (&hostkey_file,
261 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
263 privkey = GNUNET_CRYPTO_ecc_key_create_from_file (hostkey_file);
264 GNUNET_free (hostkey_file);
265 GNUNET_assert (privkey != NULL);
266 GNUNET_CRYPTO_ecc_key_get_public_for_signature (privkey, &pubkey);
270 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
271 rd.record_type = TEST_RECORD_TYPE;
272 rd.data_size = TEST_RECORD_DATALEN;
273 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
274 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
276 nsh = GNUNET_NAMESTORE_connect (cfg);
277 GNUNET_break (NULL != nsh);
278 nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
279 1, &rd, &put_cont, (void *) name);
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 _("Namestore cannot store no block\n"));
285 GNUNET_free ((void *)rd.data);
290 main (int argc, char *argv[])
294 GNUNET_TESTING_service_run ("test-namestore-api",
296 "test_namestore_api.conf",
303 /* end of test_namestore_api.c */